index.js 17 KB

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