index.js 15 KB

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