Core.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import Utils from '../core/Utils'
  2. import Translator from '../core/Translator'
  3. import ee from 'event-emitter'
  4. /**
  5. * Main Uppy core
  6. *
  7. * @param {object} opts general options, like locale, to show modal or not to show
  8. */
  9. export default class Core {
  10. constructor (opts) {
  11. // set default options
  12. const defaultOptions = {
  13. // load English as the default locale
  14. locale: require('../locale/en_US.js'),
  15. autoProceed: false,
  16. debug: false
  17. }
  18. // Merge default options with the ones set by user
  19. this.opts = Object.assign({}, defaultOptions, opts)
  20. // Dictates in what order different plugin types are ran:
  21. this.types = [ 'presetter', 'selecter', 'uploader' ]
  22. this.type = 'core'
  23. // Container for different types of plugins
  24. this.plugins = {}
  25. this.translator = new Translator({locale: this.opts.locale})
  26. this.i18n = this.translator.translate.bind(this.translator)
  27. // console.log(this.i18n('filesChosen', {smart_count: 3}))
  28. // Set up an event EventEmitter
  29. this.emitter = ee()
  30. this.emitter.on('progress', data => {
  31. console.log('до чего дошел прогресс: ' + data.percentage)
  32. })
  33. }
  34. /**
  35. * Registers a plugin with Core
  36. *
  37. * @param {Class} Plugin object
  38. * @param {Object} options object that will be passed to Plugin later
  39. * @return {Object} self for chaining
  40. */
  41. use (Plugin, opts) {
  42. // Instantiate
  43. const plugin = new Plugin(this, opts)
  44. this.plugins[plugin.type] = this.plugins[plugin.type] || []
  45. this.plugins[plugin.type].push(plugin)
  46. return this
  47. }
  48. /**
  49. * Sets plugin’s progress, like for uploads
  50. *
  51. * @param {object} plugin that wants to set progress
  52. * @param {integer} percentage
  53. * @return {object} self for chaining
  54. */
  55. setProgress (plugin, percentage) {
  56. // Any plugin can call this via `this.core.setProgress(this, precentage)`
  57. console.log(plugin.type + ' plugin ' + plugin.name + ' set the progress to ' + percentage)
  58. return this
  59. }
  60. /**
  61. * Logs stuff to console, only if `debug` is set to true. Silent in production.
  62. *
  63. * @return {String|Object} to log
  64. */
  65. log (msg) {
  66. if (this.opts.debug) {
  67. console.log(`Debug log: ${msg}`)
  68. }
  69. }
  70. /**
  71. * Runs all plugins of the same type in parallel
  72. *
  73. * @param {string} type that wants to set progress
  74. * @param {array} files
  75. * @return {Promise} of all methods
  76. */
  77. runType (type, files) {
  78. const methods = this.plugins[type].map(
  79. plugin => plugin.run(files)
  80. )
  81. return Promise.all(methods)
  82. .catch((error) => console.error(error))
  83. }
  84. /**
  85. * Runs a waterfall of runType plugin packs, like so:
  86. * All preseters(data) --> All selecters(data) --> All uploaders(data) --> done
  87. */
  88. run () {
  89. console.log({
  90. class: 'Core',
  91. method: 'run'
  92. })
  93. // Forse `autoProceed` option to false if there are multiple selector Plugins active
  94. if (this.plugins.selecter && this.plugins.selecter.length > 1) {
  95. this.opts.autoProceed = false
  96. }
  97. // First we select only plugins of current type,
  98. // then create an array of runType methods of this plugins
  99. let typeMethods = this.types.filter(type => {
  100. return this.plugins[type]
  101. }).map(type => this.runType.bind(this, type))
  102. Utils.promiseWaterfall(typeMethods)
  103. .then((result) => console.log(result))
  104. .catch((error) => console.error(error))
  105. }
  106. }