index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const html = require('yo-yo')
  2. const Plugin = require('../Plugin')
  3. const Provider = require('../../Provider')
  4. const View = require('../../generic-provider-views')
  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 = this.opts.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(core, {
  23. host: this.opts.host,
  24. provider: 'dropbox'
  25. })
  26. this.files = []
  27. this.onAuth = this.onAuth.bind(this)
  28. // Visual
  29. this.render = this.render.bind(this)
  30. // set default options
  31. const defaultOptions = {}
  32. // merge default options with the ones set by user
  33. this.opts = Object.assign({}, defaultOptions, opts)
  34. }
  35. install () {
  36. this.view = new View(this)
  37. // Set default state
  38. this.core.setState({
  39. // writing out the key explicitly for readability the key used to store
  40. // the plugin state must be equal to this.stateId.
  41. dropbox: {
  42. authenticated: false,
  43. files: [],
  44. folders: [],
  45. directories: [],
  46. activeRow: -1,
  47. filterInput: '',
  48. isSearchVisible: false
  49. }
  50. })
  51. const target = this.opts.target
  52. if (target) {
  53. this.mount(target, this)
  54. }
  55. }
  56. uninstall () {
  57. this.unmount()
  58. }
  59. onAuth (authenticated) {
  60. this.view.updateState({authenticated})
  61. if (authenticated) {
  62. this.view.getFolder()
  63. }
  64. }
  65. isFolder (item) {
  66. return item['.tag'] === 'folder'
  67. }
  68. getItemData (item) {
  69. return item
  70. }
  71. getItemIcon (item) {
  72. return icons[item['.tag']]()
  73. }
  74. getItemSubList (item) {
  75. return item.entries
  76. }
  77. getItemName (item) {
  78. return item.name || ''
  79. }
  80. getMimeType (item) {
  81. // mime types aren't supported.
  82. return null
  83. }
  84. getItemId (item) {
  85. return item.id
  86. }
  87. getItemRequestPath (item) {
  88. return encodeURIComponent(item.path_lower)
  89. }
  90. getItemModifiedDate (item) {
  91. return item.server_modified
  92. }
  93. getItemThumbnailUrl (item) {
  94. return `${this.opts.host}/${this.Dropbox.id}/thumbnail/${this.getItemRequestPath(item)}`
  95. }
  96. render (state) {
  97. return this.view.render(state)
  98. }
  99. }