From 067b17b01386eb5b19886acff758c778bc358fda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E9=A1=BA=E5=BC=BA?= Date: Fri, 2 Aug 2019 20:53:39 +0800 Subject: [PATCH] improve upload interactive (#3941) * improve upload interactive * improve code --- .../src/components/cur-dir-path/dir-path.js | 17 ++++++-- .../components/file-uploader/file-uploader.js | 14 +++++++ frontend/src/components/main-side-nav.js | 41 +++++++++++-------- frontend/src/utils/utils.js | 7 ++++ 4 files changed, 59 insertions(+), 20 deletions(-) diff --git a/frontend/src/components/cur-dir-path/dir-path.js b/frontend/src/components/cur-dir-path/dir-path.js index fbb7648c5f..a0030e73a7 100644 --- a/frontend/src/components/cur-dir-path/dir-path.js +++ b/frontend/src/components/cur-dir-path/dir-path.js @@ -24,7 +24,16 @@ class DirPath extends React.Component { this.props.onPathClick(path); } - onTabNavClick = (tabName, id) => { + onTabNavClick = (e, tabName, id) => { + if (window.uploader && + window.uploader.isUploadProgressDialogShow && + window.uploader.totalProgress !== 100) { + if (!window.confirm(gettext('A file is being uploaded. Are you sure you want to leave this page?'))) { + e.preventDefault(); + return false; + } + window.uploader.isUploadProgressDialogShow = false; + } this.props.onTabNavClick(tabName, id); } @@ -72,20 +81,20 @@ class DirPath extends React.Component { {this.props.pathPrefix && this.props.pathPrefix.map((item, index) => { return ( - this.onTabNavClick(item.name, item.id)}>{gettext(item.showName)} + this.onTabNavClick(e, item.name, item.id)}>{gettext(item.showName)} / ); })} {this.props.pathPrefix && this.props.pathPrefix.length === 0 && ( - this.onTabNavClick('my-libs')}>{gettext('Libraries')} + this.onTabNavClick(e, 'my-libs')}>{gettext('Libraries')} / )} {!this.props.pathPrefix && ( - this.onTabNavClick('my-libs')}>{gettext('Libraries')} + this.onTabNavClick(e, 'my-libs')}>{gettext('Libraries')} / )} diff --git a/frontend/src/components/file-uploader/file-uploader.js b/frontend/src/components/file-uploader/file-uploader.js index 3d1d83b743..372547bb39 100644 --- a/frontend/src/components/file-uploader/file-uploader.js +++ b/frontend/src/components/file-uploader/file-uploader.js @@ -53,6 +53,7 @@ class FileUploader extends React.Component { this.timestamp = null; this.loaded = 0; this.bitrateInterval = 500; // Interval in milliseconds to calculate the bitrate + window.onbeforeunload = this.onbeforeunload; } componentDidMount() { @@ -86,11 +87,20 @@ class FileUploader extends React.Component { } componentWillUnmount = () => { + window.onbeforeunload = null; if (this.props.dragAndDrop === true) { this.resumable.disableDropOnDocument(); } } + onbeforeunload = () => { + if (window.uploader && + window.uploader.isUploadProgressDialogShow && + window.uploader.totalProgress !== 100) { + return ''; + } + } + bindCallbackHandler = () => { let {maxFilesErrorCallback, minFileSizeErrorCallback, maxFileSizeErrorCallback, fileTypeErrorCallback } = this.props; @@ -207,6 +217,7 @@ class FileUploader extends React.Component { isUploadProgressDialogShow: true, uploadFileList: uploadFileList, }); + Utils.registerGlobalVariable('uploader', 'isUploadProgressDialogShow', true); } buildCustomFileObj = (resumableFile) => { @@ -261,6 +272,7 @@ class FileUploader extends React.Component { totalProgress: progress, uploadBitrate: uploadBitrate }); + Utils.registerGlobalVariable('uploader', 'totalProgress', progress); } onFileUploadSuccess = (resumableFile, message) => { @@ -466,6 +478,7 @@ class FileUploader extends React.Component { onCloseUploadDialog = () => { this.resumable.files = []; this.setState({isUploadProgressDialogShow: false, uploadFileList: []}); + Utils.registerGlobalVariable('uploader', 'isUploadProgressDialogShow', false); } onUploadCancel = (uploadingItem) => { @@ -529,6 +542,7 @@ class FileUploader extends React.Component { }, () => { this.resumable.upload(); }); + Utils.registerGlobalVariable('uploader', 'isUploadProgressDialogShow', true); } cancelFileUpload = () => { diff --git a/frontend/src/components/main-side-nav.js b/frontend/src/components/main-side-nav.js index 20e3c2547a..f3e229eccd 100644 --- a/frontend/src/components/main-side-nav.js +++ b/frontend/src/components/main-side-nav.js @@ -62,7 +62,16 @@ class MainSideNav extends React.Component { }); } - tabItemClick = (param, id) => { + tabItemClick = (e, param, id) => { + if (window.uploader && + window.uploader.isUploadProgressDialogShow && + window.uploader.totalProgress !== 100) { + if (!window.confirm(gettext('A file is being uploaded. Are you sure you want to leave this page?'))) { + e.preventDefault(); + return false; + } + window.uploader.isUploadProgressDialogShow = false; + } this.props.tabItemClick(param, id); } @@ -78,7 +87,7 @@ class MainSideNav extends React.Component { return (