index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const html = require('yo-yo')
  2. const Plugin = require('../Plugin')
  3. const Provider = require('../../uppy-base/src/plugins/Provider')
  4. const View = require('../../generic-provider-views/index')
  5. const icons = require('./icons')
  6. module.exports = class Dropbox extends Plugin {
  7. constructor (core, opts) {
  8. super(core, opts)
  9. this.type = 'acquirer'
  10. this.id = 'Dropbox'
  11. this.title = 'Dropbox'
  12. this.stateId = 'dropbox'
  13. this.icon = html`
  14. <svg class="UppyIcon" width="128" height="118" viewBox="0 0 128 118">
  15. <path d="M38.145.777L1.108 24.96l25.608 20.507 37.344-23.06z"/>
  16. <path d="M1.108 65.975l37.037 24.183L64.06 68.525l-37.343-23.06zM64.06 68.525l25.917 21.633 37.036-24.183-25.61-20.51z"/>
  17. <path d="M127.014 24.96L89.977.776 64.06 22.407l37.345 23.06zM64.136 73.18l-25.99 21.567-11.122-7.262v8.142l37.112 22.256 37.114-22.256v-8.142l-11.12 7.262z"/>
  18. </svg>
  19. `
  20. // writing out the key explicitly for readability the key used to store
  21. // the provider instance must be equal to this.id.
  22. this.Dropbox = new Provider({
  23. host: this.opts.host,
  24. provider: 'dropbox'
  25. })
  26. this.files = []
  27. // Visual
  28. this.render = this.render.bind(this)
  29. // set default options
  30. const defaultOptions = {}
  31. // merge default options with the ones set by user
  32. this.opts = Object.assign({}, defaultOptions, opts)
  33. }
  34. install () {
  35. this.view = new View(this)
  36. // Set default state
  37. this.core.setState({
  38. // writing out the key explicitly for readability the key used to store
  39. // the plugin state must be equal to this.stateId.
  40. dropbox: {
  41. authenticated: false,
  42. files: [],
  43. folders: [],
  44. directories: [],
  45. activeRow: -1,
  46. filterInput: ''
  47. }
  48. })
  49. const target = this.opts.target
  50. const plugin = this
  51. this.target = this.mount(target, plugin)
  52. this[this.id].auth()
  53. .then((authenticated) => {
  54. this.view.updateState({authenticated})
  55. if (authenticated) {
  56. this.view.getFolder()
  57. }
  58. })
  59. return
  60. }
  61. isFolder (item) {
  62. return item.is_dir
  63. }
  64. getItemData (item) {
  65. return Object.assign({}, item, {size: item.bytes})
  66. }
  67. getItemIcon (item) {
  68. var icon = icons[item.icon]
  69. if (!icon) {
  70. if (item.icon.startsWith('folder')) {
  71. icon = icons['folder']
  72. } else {
  73. icon = icons['page_white']
  74. }
  75. }
  76. return icon()
  77. }
  78. getItemSubList (item) {
  79. return item.contents
  80. }
  81. getItemName (item) {
  82. return item.path.length > 1 ? item.path.substring(1) : item.path
  83. }
  84. getMimeType (item) {
  85. return item.mime_type
  86. }
  87. getItemId (item) {
  88. return item.rev
  89. }
  90. getItemRequestPath (item) {
  91. return encodeURIComponent(this.getItemName(item))
  92. }
  93. getItemModifiedDate (item) {
  94. return item.modified
  95. }
  96. render (state) {
  97. return this.view.render(state)
  98. }
  99. }