MyCustomProvider.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const { Plugin } = require('@uppy/core')
  2. const { Provider } = require('@uppy/companion-client')
  3. const ProviderViews = require('@uppy/provider-views')
  4. const { h } = require('preact')
  5. module.exports = class MyCustomProvider extends Plugin {
  6. constructor (uppy, opts) {
  7. super(uppy, opts)
  8. this.type = 'acquirer'
  9. this.id = this.opts.id || 'MyCustomProvider'
  10. Provider.initPlugin(this, opts)
  11. this.title = 'MyCustomProvider'
  12. this.icon = () => (
  13. <img src="https://uppy.io/images/logos/uppy-dog-head-arrow.svg" width="23" />
  14. )
  15. // writing out the key explicitly for readability the key used to store
  16. // the provider instance must be equal to this.id.
  17. this[this.id] = new Provider(uppy, {
  18. companionUrl: this.opts.companionUrl,
  19. provider: 'mycustomprovider'
  20. })
  21. this.files = []
  22. this.onAuth = this.onAuth.bind(this)
  23. this.render = this.render.bind(this)
  24. // merge default options with the ones set by user
  25. this.opts = Object.assign({}, opts)
  26. }
  27. install () {
  28. this.view = new ProviderViews(this)
  29. // Set default state
  30. this.setPluginState({
  31. authenticated: false,
  32. files: [],
  33. folders: [],
  34. directories: [],
  35. activeRow: -1,
  36. filterInput: '',
  37. isSearchVisible: false
  38. })
  39. const target = this.opts.target
  40. if (target) {
  41. this.mount(target, this)
  42. }
  43. }
  44. uninstall () {
  45. this.view.tearDown()
  46. this.unmount()
  47. }
  48. onAuth (authenticated) {
  49. this.setPluginState({ authenticated })
  50. if (authenticated) {
  51. this.view.getFolder()
  52. }
  53. }
  54. isFolder (item) {
  55. return false
  56. }
  57. getItemData (item) {
  58. return item
  59. }
  60. getItemIcon (item) {
  61. return 'https://uppy.io/images/logos/uppy-dog-head-arrow.svg'
  62. }
  63. getItemSubList (item) {
  64. return item.entries
  65. }
  66. getItemName (item) {
  67. return item.name
  68. }
  69. getMimeType (item) {
  70. // mime types aren't supported.
  71. return null
  72. }
  73. getItemId (item) {
  74. return item.name
  75. }
  76. getItemRequestPath (item) {
  77. return encodeURIComponent(item.name)
  78. }
  79. getItemModifiedDate (item) {
  80. return Date.now()
  81. }
  82. getItemThumbnailUrl (item) {
  83. return 'https://uppy.io/images/logos/uppy-dog-head-arrow.svg'
  84. }
  85. getUsername () {
  86. return 'Cool Dog'
  87. }
  88. render (state) {
  89. return this.view.render(state)
  90. }
  91. }