index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. const Plugin = require('../../core/Plugin')
  2. const Translator = require('../../core/Translator')
  3. const dragDrop = require('drag-drop')
  4. const DashboardUI = require('./Dashboard')
  5. const StatusBar = require('../StatusBar')
  6. const Informer = require('../Informer')
  7. const { findAllDOMElements, toArray } = require('../../core/Utils')
  8. const prettyBytes = require('prettier-bytes')
  9. const { defaultTabIcon } = require('./icons')
  10. // some code for managing focus was adopted from https://github.com/ghosh/micromodal
  11. // MIT licence, https://github.com/ghosh/micromodal/blob/master/LICENSE.md
  12. // Copyright (c) 2017 Indrashish Ghosh
  13. const FOCUSABLE_ELEMENTS = [
  14. 'a[href]',
  15. 'area[href]',
  16. 'input:not([disabled]):not([type="hidden"]):not([hidden])',
  17. 'select:not([disabled])',
  18. 'textarea:not([disabled])',
  19. 'button:not([disabled])',
  20. 'iframe',
  21. 'object',
  22. 'embed',
  23. '[contenteditable]',
  24. '[tabindex]:not([tabindex^="-"])'
  25. ]
  26. /**
  27. * Dashboard UI with previews, metadata editing, tabs for various services and more
  28. */
  29. module.exports = class Dashboard extends Plugin {
  30. constructor (uppy, opts) {
  31. super(uppy, opts)
  32. this.id = this.opts.id || 'Dashboard'
  33. this.title = 'Dashboard'
  34. this.type = 'orchestrator'
  35. const defaultLocale = {
  36. strings: {
  37. selectToUpload: 'Select files to upload',
  38. closeModal: 'Close Modal',
  39. upload: 'Upload',
  40. importFrom: 'Import files from',
  41. dashboardWindowTitle: 'Uppy Dashboard Window (Press escape to close)',
  42. dashboardTitle: 'Uppy Dashboard',
  43. copyLinkToClipboardSuccess: 'Link copied to clipboard.',
  44. copyLinkToClipboardFallback: 'Copy the URL below',
  45. fileSource: 'File source',
  46. done: 'Done',
  47. localDisk: 'Local Disk',
  48. myDevice: 'My Device',
  49. dropPasteImport: 'Drop files here, paste, import from one of the locations above or',
  50. dropPaste: 'Drop files here, paste or',
  51. browse: 'browse',
  52. fileProgress: 'File progress: upload speed and ETA',
  53. numberOfSelectedFiles: 'Number of selected files',
  54. uploadAllNewFiles: 'Upload all new files',
  55. emptyFolderAdded: 'No files were added from empty folder',
  56. folderAdded: {
  57. 0: 'Added %{smart_count} file from %{folder}',
  58. 1: 'Added %{smart_count} files from %{folder}'
  59. }
  60. }
  61. }
  62. // set default options
  63. const defaultOptions = {
  64. target: 'body',
  65. metaFields: [],
  66. trigger: '#uppy-select-files',
  67. inline: false,
  68. width: 750,
  69. height: 550,
  70. semiTransparent: false,
  71. defaultTabIcon: defaultTabIcon,
  72. showProgressDetails: false,
  73. hideUploadButton: false,
  74. hideProgressAfterFinish: false,
  75. note: null,
  76. closeModalOnClickOutside: false,
  77. locale: defaultLocale,
  78. onRequestCloseModal: () => this.closeModal()
  79. }
  80. // merge default options with the ones set by user
  81. this.opts = Object.assign({}, defaultOptions, opts)
  82. this.locale = Object.assign({}, defaultLocale, this.opts.locale)
  83. this.locale.strings = Object.assign({}, defaultLocale.strings, this.opts.locale.strings)
  84. this.translator = new Translator({locale: this.locale})
  85. this.i18n = this.translator.translate.bind(this.translator)
  86. this.closeModal = this.closeModal.bind(this)
  87. this.requestCloseModal = this.requestCloseModal.bind(this)
  88. this.openModal = this.openModal.bind(this)
  89. this.isModalOpen = this.isModalOpen.bind(this)
  90. this.addTarget = this.addTarget.bind(this)
  91. this.hideAllPanels = this.hideAllPanels.bind(this)
  92. this.showPanel = this.showPanel.bind(this)
  93. this.getFocusableNodes = this.getFocusableNodes.bind(this)
  94. this.setFocusToFirstNode = this.setFocusToFirstNode.bind(this)
  95. this.maintainFocus = this.maintainFocus.bind(this)
  96. this.initEvents = this.initEvents.bind(this)
  97. this.onKeydown = this.onKeydown.bind(this)
  98. this.handleClickOutside = this.handleClickOutside.bind(this)
  99. this.handleFileCard = this.handleFileCard.bind(this)
  100. this.handleDrop = this.handleDrop.bind(this)
  101. this.handlePaste = this.handlePaste.bind(this)
  102. this.handleInputChange = this.handleInputChange.bind(this)
  103. this.updateDashboardElWidth = this.updateDashboardElWidth.bind(this)
  104. this.render = this.render.bind(this)
  105. this.install = this.install.bind(this)
  106. }
  107. addTarget (plugin) {
  108. const callerPluginId = plugin.id || plugin.constructor.name
  109. const callerPluginName = plugin.title || callerPluginId
  110. const callerPluginType = plugin.type
  111. if (callerPluginType !== 'acquirer' &&
  112. callerPluginType !== 'progressindicator' &&
  113. callerPluginType !== 'presenter') {
  114. let msg = 'Dashboard: Modal can only be used by plugins of types: acquirer, progressindicator, presenter'
  115. this.uppy.log(msg)
  116. return
  117. }
  118. const target = {
  119. id: callerPluginId,
  120. name: callerPluginName,
  121. type: callerPluginType
  122. }
  123. const state = this.getPluginState()
  124. const newTargets = state.targets.slice()
  125. newTargets.push(target)
  126. this.setPluginState({
  127. targets: newTargets
  128. })
  129. return this.el
  130. }
  131. hideAllPanels () {
  132. this.setPluginState({
  133. activePanel: false
  134. })
  135. }
  136. showPanel (id) {
  137. const { targets } = this.getPluginState()
  138. const activePanel = targets.filter((target) => {
  139. return target.type === 'acquirer' && target.id === id
  140. })[0]
  141. this.setPluginState({
  142. activePanel: activePanel
  143. })
  144. }
  145. requestCloseModal () {
  146. if (this.opts.onRequestCloseModal) {
  147. return this.opts.onRequestCloseModal()
  148. } else {
  149. this.closeModal()
  150. }
  151. }
  152. getFocusableNodes () {
  153. const nodes = this.el.querySelectorAll(FOCUSABLE_ELEMENTS)
  154. return Object.keys(nodes).map((key) => nodes[key])
  155. }
  156. setFocusToFirstNode () {
  157. const focusableNodes = this.getFocusableNodes()
  158. if (focusableNodes.length) focusableNodes[0].focus()
  159. }
  160. maintainFocus (event) {
  161. var focusableNodes = this.getFocusableNodes()
  162. var focusedItemIndex = focusableNodes.indexOf(document.activeElement)
  163. if (event.shiftKey && focusedItemIndex === 0) {
  164. focusableNodes[focusableNodes.length - 1].focus()
  165. event.preventDefault()
  166. }
  167. if (!event.shiftKey && focusedItemIndex === focusableNodes.length - 1) {
  168. focusableNodes[0].focus()
  169. event.preventDefault()
  170. }
  171. }
  172. openModal () {
  173. this.setPluginState({
  174. isHidden: false
  175. })
  176. // save scroll position
  177. this.savedDocumentScrollPosition = window.scrollY
  178. // add class to body that sets position fixed, move everything back
  179. // to scroll position
  180. document.body.classList.add('uppy-Dashboard-isOpen')
  181. document.body.style.top = `-${this.savedDocumentScrollPosition}px`
  182. this.updateDashboardElWidth()
  183. this.setFocusToFirstNode()
  184. }
  185. closeModal () {
  186. this.setPluginState({
  187. isHidden: true
  188. })
  189. document.body.classList.remove('uppy-Dashboard-isOpen')
  190. window.scrollTo(0, this.savedDocumentScrollPosition)
  191. }
  192. isModalOpen () {
  193. return !this.getPluginState().isHidden || false
  194. }
  195. onKeydown (event) {
  196. // close modal on esc key press
  197. if (event.keyCode === 27) this.requestCloseModal(event)
  198. // maintainFocus on tab key press
  199. if (event.keyCode === 9) this.maintainFocus(event)
  200. }
  201. handleClickOutside () {
  202. if (this.opts.closeModalOnClickOutside) this.requestCloseModal()
  203. }
  204. handlePaste (ev) {
  205. const files = toArray(ev.clipboardData.items)
  206. files.forEach((file) => {
  207. if (file.kind !== 'file') return
  208. const blob = file.getAsFile()
  209. if (!blob) {
  210. this.uppy.log('[Dashboard] File pasted, but the file blob is empty')
  211. this.uppy.info('Error pasting file', 'error')
  212. return
  213. }
  214. this.uppy.log('[Dashboard] File pasted')
  215. this.uppy.addFile({
  216. source: this.id,
  217. name: file.name,
  218. type: file.type,
  219. data: blob
  220. })
  221. })
  222. }
  223. handleInputChange (ev) {
  224. ev.preventDefault()
  225. const files = toArray(ev.target.files)
  226. files.forEach((file) => {
  227. this.uppy.addFile({
  228. source: this.id,
  229. name: file.name,
  230. type: file.type,
  231. data: file
  232. })
  233. })
  234. }
  235. initEvents () {
  236. // Modal open button
  237. const showModalTrigger = findAllDOMElements(this.opts.trigger)
  238. if (!this.opts.inline && showModalTrigger) {
  239. showModalTrigger.forEach(trigger => trigger.addEventListener('click', this.openModal))
  240. }
  241. if (!this.opts.inline && !showModalTrigger) {
  242. this.uppy.log('Dashboard modal trigger not found, you won’t be able to select files. Make sure `trigger` is set correctly in Dashboard options', 'error')
  243. }
  244. if (!this.opts.inline) {
  245. document.addEventListener('keydown', this.onKeydown)
  246. }
  247. // Drag Drop
  248. this.removeDragDropListener = dragDrop(this.el, (files) => {
  249. this.handleDrop(files)
  250. })
  251. this.uppy.on('dashboard:file-card', this.handleFileCard)
  252. this.updateDashboardElWidth()
  253. window.addEventListener('resize', this.updateDashboardElWidth)
  254. }
  255. removeEvents () {
  256. const showModalTrigger = findAllDOMElements(this.opts.trigger)
  257. if (!this.opts.inline && showModalTrigger) {
  258. showModalTrigger.forEach(trigger => trigger.removeEventListener('click', this.openModal))
  259. }
  260. if (!this.opts.inline) {
  261. document.removeEventListener('keydown', this.onKeydown)
  262. }
  263. this.removeDragDropListener()
  264. this.uppy.off('dashboard:file-card', this.handleFileCard)
  265. window.removeEventListener('resize', this.updateDashboardElWidth)
  266. }
  267. updateDashboardElWidth () {
  268. const dashboardEl = this.el.querySelector('.uppy-Dashboard-inner')
  269. this.uppy.log(`Dashboard width: ${dashboardEl.offsetWidth}`)
  270. this.setPluginState({
  271. containerWidth: dashboardEl.offsetWidth
  272. })
  273. }
  274. handleFileCard (fileId) {
  275. this.setPluginState({
  276. fileCardFor: fileId || false
  277. })
  278. }
  279. handleDrop (files) {
  280. this.uppy.log('[Dashboard] Files were dropped')
  281. files.forEach((file) => {
  282. this.uppy.addFile({
  283. source: this.id,
  284. name: file.name,
  285. type: file.type,
  286. data: file
  287. })
  288. })
  289. }
  290. render (state) {
  291. const pluginState = this.getPluginState()
  292. const files = state.files
  293. const newFiles = Object.keys(files).filter((file) => {
  294. return !files[file].progress.uploadStarted
  295. })
  296. const inProgressFiles = Object.keys(files).filter((file) => {
  297. return !files[file].progress.uploadComplete &&
  298. files[file].progress.uploadStarted &&
  299. !files[file].isPaused
  300. })
  301. let inProgressFilesArray = []
  302. inProgressFiles.forEach((file) => {
  303. inProgressFilesArray.push(files[file])
  304. })
  305. let totalSize = 0
  306. let totalUploadedSize = 0
  307. inProgressFilesArray.forEach((file) => {
  308. totalSize = totalSize + (file.progress.bytesTotal || 0)
  309. totalUploadedSize = totalUploadedSize + (file.progress.bytesUploaded || 0)
  310. })
  311. totalSize = prettyBytes(totalSize)
  312. totalUploadedSize = prettyBytes(totalUploadedSize)
  313. const attachRenderFunctionToTarget = (target) => {
  314. const plugin = this.uppy.getPlugin(target.id)
  315. return Object.assign({}, target, {
  316. icon: plugin.icon || this.opts.defaultTabIcon,
  317. render: plugin.render
  318. })
  319. }
  320. const isSupported = (target) => {
  321. const plugin = this.uppy.getPlugin(target.id)
  322. // If the plugin does not provide a `supported` check, assume the plugin works everywhere.
  323. if (typeof plugin.isSupported !== 'function') {
  324. return true
  325. }
  326. return plugin.isSupported()
  327. }
  328. const acquirers = pluginState.targets
  329. .filter(target => target.type === 'acquirer' && isSupported(target))
  330. .map(attachRenderFunctionToTarget)
  331. const progressindicators = pluginState.targets
  332. .filter(target => target.type === 'progressindicator')
  333. .map(attachRenderFunctionToTarget)
  334. const startUpload = (ev) => {
  335. this.uppy.upload().catch((err) => {
  336. // Log error.
  337. this.uppy.log(err.stack || err.message || err)
  338. })
  339. }
  340. const cancelUpload = (fileID) => {
  341. this.uppy.emit('upload-cancel', fileID)
  342. this.uppy.removeFile(fileID)
  343. }
  344. const showFileCard = (fileID) => {
  345. this.uppy.emit('dashboard:file-card', fileID)
  346. }
  347. const fileCardDone = (meta, fileID) => {
  348. this.uppy.setFileMeta(fileID, meta)
  349. this.uppy.emit('dashboard:file-card')
  350. }
  351. return DashboardUI({
  352. state: state,
  353. modal: pluginState,
  354. newFiles: newFiles,
  355. files: files,
  356. totalFileCount: Object.keys(files).length,
  357. totalProgress: state.totalProgress,
  358. acquirers: acquirers,
  359. activePanel: pluginState.activePanel,
  360. getPlugin: this.uppy.getPlugin,
  361. progressindicators: progressindicators,
  362. autoProceed: this.uppy.opts.autoProceed,
  363. hideUploadButton: this.opts.hideUploadButton,
  364. id: this.id,
  365. closeModal: this.requestCloseModal,
  366. handleClickOutside: this.handleClickOutside,
  367. handleInputChange: this.handleInputChange,
  368. handlePaste: this.handlePaste,
  369. showProgressDetails: this.opts.showProgressDetails,
  370. inline: this.opts.inline,
  371. showPanel: this.showPanel,
  372. hideAllPanels: this.hideAllPanels,
  373. log: this.uppy.log,
  374. i18n: this.i18n,
  375. addFile: this.uppy.addFile,
  376. removeFile: this.uppy.removeFile,
  377. info: this.uppy.info,
  378. note: this.opts.note,
  379. metaFields: this.getPluginState().metaFields,
  380. resumableUploads: this.uppy.state.capabilities.resumableUploads || false,
  381. startUpload: startUpload,
  382. pauseUpload: this.uppy.pauseResume,
  383. retryUpload: this.uppy.retryUpload,
  384. cancelUpload: cancelUpload,
  385. fileCardFor: pluginState.fileCardFor,
  386. showFileCard: showFileCard,
  387. fileCardDone: fileCardDone,
  388. updateDashboardElWidth: this.updateDashboardElWidth,
  389. maxWidth: this.opts.maxWidth,
  390. maxHeight: this.opts.maxHeight,
  391. currentWidth: pluginState.containerWidth,
  392. isWide: pluginState.containerWidth > 400
  393. })
  394. }
  395. discoverProviderPlugins () {
  396. this.uppy.iteratePlugins((plugin) => {
  397. if (plugin && !plugin.target && plugin.opts && plugin.opts.target === this.constructor) {
  398. this.addTarget(plugin)
  399. }
  400. })
  401. }
  402. install () {
  403. // Set default state for Modal
  404. this.setPluginState({
  405. isHidden: true,
  406. showFileCard: false,
  407. activePanel: false,
  408. metaFields: this.opts.metaFields,
  409. targets: []
  410. })
  411. const target = this.opts.target
  412. if (target) {
  413. this.mount(target, this)
  414. }
  415. const plugins = this.opts.plugins || []
  416. plugins.forEach((pluginID) => {
  417. const plugin = this.uppy.getPlugin(pluginID)
  418. if (plugin) plugin.mount(this, plugin)
  419. })
  420. if (!this.opts.disableStatusBar) {
  421. this.uppy.use(StatusBar, {
  422. target: this,
  423. hideUploadButton: this.opts.hideUploadButton,
  424. hideAfterFinish: this.opts.hideProgressAfterFinish
  425. })
  426. }
  427. if (!this.opts.disableInformer) {
  428. this.uppy.use(Informer, {
  429. target: this
  430. })
  431. }
  432. this.discoverProviderPlugins()
  433. this.initEvents()
  434. }
  435. uninstall () {
  436. if (!this.opts.disableInformer) {
  437. const informer = this.uppy.getPlugin('Informer')
  438. if (informer) this.uppy.removePlugin(informer)
  439. }
  440. if (!this.opts.disableStatusBar) {
  441. const statusBar = this.uppy.getPlugin('StatusBar')
  442. // Checking if this plugin exists, in case it was removed by uppy-core
  443. // before the Dashboard was.
  444. if (statusBar) this.uppy.removePlugin(statusBar)
  445. }
  446. const plugins = this.opts.plugins || []
  447. plugins.forEach((pluginID) => {
  448. const plugin = this.uppy.getPlugin(pluginID)
  449. if (plugin) plugin.unmount()
  450. })
  451. this.unmount()
  452. this.removeEvents()
  453. }
  454. }