Plugin.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Boilerplate that all Plugins share - and should not be used
  3. * directly. It also shows which methods final plugins should implement/override,
  4. * this deciding on structure.
  5. *
  6. * @param {object} main Uppy core object
  7. * @param {object} object with plugin options
  8. * @return {array | string} files or success/fail message
  9. */
  10. export default class Plugin {
  11. constructor (core, opts) {
  12. this.core = core
  13. this.opts = opts
  14. this.type = 'none'
  15. this.name = this.constructor.name
  16. }
  17. // setProgress (percentage, current, total) {
  18. // var finalPercentage = percentage
  19. //
  20. // // if (current !== undefined && total !== undefined) {
  21. // // var percentageOfTotal = (percentage / total);
  22. // // // finalPercentage = percentageOfTotal;
  23. // // if (current > 1) {
  24. // // finalPercentage = percentage + (100 / (total * current));
  25. // // } else {
  26. // // finalPercentage = percentage;
  27. // // }
  28. // // }
  29. //
  30. // this.core.setProgress(this, finalPercentage)
  31. // }
  32. /**
  33. * Check if supplied `target` is a `string` or an `object`.
  34. * If object (that means its a plugin), search `plugins` for
  35. * a plugin with same name and return its target.
  36. *
  37. * @param {String|Object} target
  38. *
  39. */
  40. getTarget (target, callerPlugin) {
  41. if (typeof target === 'string') {
  42. this.core.log('string is a target')
  43. return target
  44. } else {
  45. this.core.log('plugin is a target')
  46. let targetPlugin = this.core.getPlugin(target.name)
  47. return targetPlugin.prepareTarget(callerPlugin)
  48. }
  49. }
  50. extractFiles (results) {
  51. console.log({
  52. class: 'Plugin',
  53. method: 'extractFiles',
  54. results: results
  55. })
  56. // check if the results array is empty
  57. // if (!results || !results.count) {
  58. // return results
  59. // }
  60. const files = []
  61. results.forEach((result) => {
  62. try {
  63. Array.from(result.files).forEach((file) => files.push(file))
  64. } catch (e) {
  65. console.log(e)
  66. }
  67. })
  68. // const files = [];
  69. // for (let i in results) {
  70. // for (let j in results[i].files) {
  71. // files.push(results[i].files.item(j));
  72. // for (let j in results[i].files) {
  73. // // files.push(results[i].files.item(j));
  74. // }
  75. // }
  76. // return Array.from(fileList);
  77. return files
  78. }
  79. focus () {
  80. console.log('focus pocus!')
  81. return
  82. }
  83. install () {
  84. return
  85. }
  86. run (results) {
  87. return results
  88. }
  89. }