Преглед изворни кода

Merge branch 'master' into status-bar/add-spinner

Artur Paikin пре 6 година
родитељ
комит
aa9c2496ad

+ 33 - 10
packages/@uppy/dashboard/src/index.js

@@ -122,6 +122,7 @@ module.exports = class Dashboard extends Plugin {
       hideProgressAfterFinish: false,
       hideProgressAfterFinish: false,
       note: null,
       note: null,
       closeModalOnClickOutside: false,
       closeModalOnClickOutside: false,
+      closeAfterFinish: false,
       disableStatusBar: false,
       disableStatusBar: false,
       disableInformer: false,
       disableInformer: false,
       disableThumbnailGenerator: false,
       disableThumbnailGenerator: false,
@@ -159,7 +160,9 @@ module.exports = class Dashboard extends Plugin {
     this.maintainFocus = this.maintainFocus.bind(this)
     this.maintainFocus = this.maintainFocus.bind(this)
 
 
     this.initEvents = this.initEvents.bind(this)
     this.initEvents = this.initEvents.bind(this)
-    this.onKeydown = this.onKeydown.bind(this)
+    this.handleKeyDown = this.handleKeyDown.bind(this)
+    this.handleFileAdded = this.handleFileAdded.bind(this)
+    this.handleComplete = this.handleComplete.bind(this)
     this.handleClickOutside = this.handleClickOutside.bind(this)
     this.handleClickOutside = this.handleClickOutside.bind(this)
     this.toggleFileCard = this.toggleFileCard.bind(this)
     this.toggleFileCard = this.toggleFileCard.bind(this)
     this.toggleAddFilesPanel = this.toggleAddFilesPanel.bind(this)
     this.toggleAddFilesPanel = this.toggleAddFilesPanel.bind(this)
@@ -321,7 +324,7 @@ module.exports = class Dashboard extends Plugin {
     }
     }
 
 
     // handle ESC and TAB keys in modal dialog
     // handle ESC and TAB keys in modal dialog
-    document.addEventListener('keydown', this.onKeydown)
+    document.addEventListener('keydown', this.handleKeyDown)
 
 
     // this.rerender(this.uppy.getState())
     // this.rerender(this.uppy.getState())
     this.setFocusToBrowse()
     this.setFocusToBrowse()
@@ -355,7 +358,7 @@ module.exports = class Dashboard extends Plugin {
     }
     }
 
 
     // handle ESC and TAB keys in modal dialog
     // handle ESC and TAB keys in modal dialog
-    document.removeEventListener('keydown', this.onKeydown)
+    document.removeEventListener('keydown', this.handleKeyDown)
 
 
     this.savedActiveElement.focus()
     this.savedActiveElement.focus()
 
 
@@ -374,7 +377,7 @@ module.exports = class Dashboard extends Plugin {
     return !this.getPluginState().isHidden || false
     return !this.getPluginState().isHidden || false
   }
   }
 
 
-  onKeydown (event) {
+  handleKeyDown (event) {
     // close modal on esc key press
     // close modal on esc key press
     if (event.keyCode === ESC_KEY) this.requestCloseModal(event)
     if (event.keyCode === ESC_KEY) this.requestCloseModal(event)
     // maintainFocus on tab key press
     // maintainFocus on tab key press
@@ -461,10 +464,19 @@ module.exports = class Dashboard extends Plugin {
     this.ro.observe(this.el.querySelector('.uppy-Dashboard-inner'))
     this.ro.observe(this.el.querySelector('.uppy-Dashboard-inner'))
 
 
     this.uppy.on('plugin-remove', this.removeTarget)
     this.uppy.on('plugin-remove', this.removeTarget)
-    this.uppy.on('file-added', (ev) => {
-      this.toggleAddFilesPanel(false)
-      this.hideAllPanels()
-    })
+    this.uppy.on('file-added', this.handleFileAdded)
+    this.uppy.on('complete', this.handleComplete)
+  }
+
+  handleFileAdded () {
+    this.toggleAddFilesPanel(false)
+  }
+
+  handleComplete ({ failed, uploadID }) {
+    if (this.opts.closeAfterFinish && failed.length === 0) {
+      // All uploads are done
+      this.requestCloseModal()
+    }
   }
   }
 
 
   removeEvents () {
   removeEvents () {
@@ -479,7 +491,8 @@ module.exports = class Dashboard extends Plugin {
     // window.removeEventListener('resize', this.throttledUpdateDashboardElWidth)
     // window.removeEventListener('resize', this.throttledUpdateDashboardElWidth)
     window.removeEventListener('popstate', this.handlePopState, false)
     window.removeEventListener('popstate', this.handlePopState, false)
     this.uppy.off('plugin-remove', this.removeTarget)
     this.uppy.off('plugin-remove', this.removeTarget)
-    this.uppy.off('file-added', (ev) => this.toggleAddFilesPanel(false))
+    this.uppy.off('file-added', this.handleFileAdded)
+    this.uppy.off('complete', this.handleComplete)
   }
   }
 
 
   toggleFileCard (fileId) {
   toggleFileCard (fileId) {
@@ -695,7 +708,17 @@ module.exports = class Dashboard extends Plugin {
       targets: []
       targets: []
     })
     })
 
 
-    const target = this.opts.target
+    const { inline, closeAfterFinish } = this.opts
+    if (inline && closeAfterFinish) {
+      throw new Error('[Dashboard] `closeAfterFinish: true` cannot be used on an inline Dashboard, because an inline Dashboard cannot be closed at all. Either set `inline: false`, or disable the `closeAfterFinish` option.')
+    }
+
+    const { allowMultipleUploads } = this.uppy.opts
+    if (allowMultipleUploads && closeAfterFinish) {
+      this.uppy.log('[Dashboard] When using `closeAfterFinish`, we recommended setting the `allowMultipleUploads` option to `false` in the Uppy constructor. See https://uppy.io/docs/uppy/#allowMultipleUploads-true', 'warning')
+    }
+
+    const { target } = this.opts
     if (target) {
     if (target) {
       this.mount(target, this)
       this.mount(target, this)
     }
     }

+ 9 - 3
packages/@uppy/status-bar/src/StatusBar.js

@@ -51,6 +51,7 @@ module.exports = (props) => {
   props = props || {}
   props = props || {}
 
 
   const { newFiles,
   const { newFiles,
+    allowNewUpload,
     isUploadStarted,
     isUploadStarted,
     resumableUploads,
     resumableUploads,
     error,
     error,
@@ -85,8 +86,13 @@ module.exports = (props) => {
   const width = typeof progressValue === 'number' ? progressValue : 100
   const width = typeof progressValue === 'number' ? progressValue : 100
   const isHidden = (uploadState === statusBarStates.STATE_WAITING && props.hideUploadButton) ||
   const isHidden = (uploadState === statusBarStates.STATE_WAITING && props.hideUploadButton) ||
     (uploadState === statusBarStates.STATE_WAITING && !props.newFiles > 0) ||
     (uploadState === statusBarStates.STATE_WAITING && !props.newFiles > 0) ||
-    (uploadState === statusBarStates.STATE_COMPLETE && props.hideAfterFinish) ||
-    !props.allowNewUpload
+    (uploadState === statusBarStates.STATE_COMPLETE && props.hideAfterFinish)
+
+  const showUploadButton = props.newFiles && !props.hideUploadButton && props.allowNewUpload
+  const showRetryButton = props.error && !props.hideRetryButton
+  const showCancelButton = !props.hidePauseResumeCancelButtons &&
+    uploadState !== statusBarStates.STATE_WAITING &&
+    uploadState !== statusBarStates.STATE_COMPLETE
 
 
   const progressClassNames = `uppy-StatusBar-progress
   const progressClassNames = `uppy-StatusBar-progress
                            ${progressMode ? 'is-' + progressMode : ''}`
                            ${progressMode ? 'is-' + progressMode : ''}`
@@ -97,7 +103,7 @@ module.exports = (props) => {
     `is-${uploadState}`
     `is-${uploadState}`
   )
   )
 
 
-  const showUploadBtn = !error && newFiles && !isUploadStarted && !hideUploadButton
+  const showUploadBtn = !error && newFiles && !isUploadStarted && allowNewUpload && !hideUploadButton
   const showCancelBtn = !hideCancelButton && uploadState !== statusBarStates.STATE_WAITING && uploadState !== statusBarStates.STATE_COMPLETE
   const showCancelBtn = !hideCancelButton && uploadState !== statusBarStates.STATE_WAITING && uploadState !== statusBarStates.STATE_COMPLETE
   const showPauseResumeBtn = resumableUploads && !hidePauseResumeButton && uploadState !== statusBarStates.STATE_WAITING && uploadState !== statusBarStates.STATE_COMPLETE
   const showPauseResumeBtn = resumableUploads && !hidePauseResumeButton && uploadState !== statusBarStates.STATE_WAITING && uploadState !== statusBarStates.STATE_COMPLETE
   const showRetryBtn = error && !hideRetryButton
   const showRetryBtn = error && !hideRetryButton

+ 9 - 0
website/src/docs/dashboard.md

@@ -71,6 +71,7 @@ uppy.use(Dashboard, {
   hideProgressAfterFinish: false,
   hideProgressAfterFinish: false,
   note: null,
   note: null,
   closeModalOnClickOutside: false,
   closeModalOnClickOutside: false,
+  closeAfterFinish: false,
   disableStatusBar: false,
   disableStatusBar: false,
   disableInformer: false,
   disableInformer: false,
   disableThumbnailGenerator: false,
   disableThumbnailGenerator: false,
@@ -184,6 +185,14 @@ Note that this metadata will only be set on a file object if it is entered by th
 
 
 Set to true to automatically close the modal when the user clicks outside of it.
 Set to true to automatically close the modal when the user clicks outside of it.
 
 
+### `closeAfterFinish: false`
+
+Set to true to automatically close the modal when all current uploads are complete. You can use this together with the [`allowMultipleUploads: false`](/docs/uppy#allowMultipleUploads-true) option in Uppy Core to create a smooth experience when uploading a single (batch of) file(s).
+
+With this option, the modal is only automatically closed when uploads are complete _and successful_. If some uploads failed, the modal stays open so the user can retry failed uploads or cancel the current batch and upload an entirely different set of files instead.
+
+> Setting [`allowMultipleUploads: false`](/docs/uppy#allowMultipleUploads-true) is **strongly** recommended when using this option. With multiple upload batches, the auto-closing behavior can be very confusing for users.
+
 ### `disablePageScrollWhenModalOpen: true`
 ### `disablePageScrollWhenModalOpen: true`
 
 
 Page scrolling is disabled by default when the Dashboard modal is open, so when you scroll a list of files in Uppy, the website in the background stays still. Set to false to override this behaviour and leave page scrolling intact.
 Page scrolling is disabled by default when the Dashboard modal is open, so when you scroll a list of files in Uppy, the website in the background stays still. Set to false to override this behaviour and leave page scrolling intact.