index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const html = require('yo-yo')
  2. require('whatwg-fetch')
  3. const Plugin = require('../Plugin')
  4. const Provider = require('../../uppy-base/src/plugins/Provider')
  5. const View = require('../../generic-provider-views/index')
  6. module.exports = class Google extends Plugin {
  7. constructor (core, opts) {
  8. super(core, opts)
  9. this.type = 'acquirer'
  10. this.id = 'GoogleDrive'
  11. this.title = 'Google Drive'
  12. this.stateId = 'googleDrive'
  13. this.icon = html`
  14. <svg class="UppyIcon UppyModalTab-icon" width="28" height="28" viewBox="0 0 16 16">
  15. <path d="M2.955 14.93l2.667-4.62H16l-2.667 4.62H2.955zm2.378-4.62l-2.666 4.62L0 10.31l5.19-8.99 2.666 4.62-2.523 4.37zm10.523-.25h-5.333l-5.19-8.99h5.334l5.19 8.99z"/>
  16. </svg>
  17. `
  18. // writing out the key explicitly for readability the key used to store
  19. // the provider instance must be equal to this.id.
  20. this.GoogleDrive = new Provider({
  21. host: this.opts.host,
  22. provider: 'drive',
  23. authProvider: 'google'
  24. })
  25. this.files = []
  26. // Visual
  27. this.render = this.render.bind(this)
  28. // set default options
  29. const defaultOptions = {}
  30. // merge default options with the ones set by user
  31. this.opts = Object.assign({}, defaultOptions, opts)
  32. }
  33. install () {
  34. this.view = new View(this)
  35. // Set default state for Google Drive
  36. this.core.setState({
  37. // writing out the key explicitly for readability the key used to store
  38. // the plugin state must be equal to this.stateId.
  39. googleDrive: {
  40. authenticated: false,
  41. files: [],
  42. folders: [],
  43. directories: [],
  44. activeRow: -1,
  45. filterInput: ''
  46. }
  47. })
  48. const target = this.opts.target
  49. const plugin = this
  50. this.target = this.mount(target, plugin)
  51. this[this.id].auth()
  52. .then((authenticated) => {
  53. this.view.updateState({authenticated})
  54. if (authenticated) {
  55. this.view.getFolder('root')
  56. }
  57. })
  58. return
  59. }
  60. isFolder (item) {
  61. return item.mimeType === 'application/vnd.google-apps.folder'
  62. }
  63. getItemData (item) {
  64. return item
  65. }
  66. getItemIcon (item) {
  67. return html`<img src=${item.iconLink}/>`
  68. }
  69. getItemSubList (item) {
  70. return item.items
  71. }
  72. getItemName (item) {
  73. return item.title
  74. }
  75. getMimeType (item) {
  76. return item.mimeType
  77. }
  78. getItemId (item) {
  79. return item.id
  80. }
  81. getItemRequestPath (item) {
  82. return this.getItemId(item)
  83. }
  84. getItemModifiedDate (item) {
  85. return item.modifiedByMeDate
  86. }
  87. render (state) {
  88. return this.view.render(state)
  89. }
  90. }