Skip to content

Commit

Permalink
MDL-62712 Filepicker: Improve upload process
Browse files Browse the repository at this point in the history
Lost the uploading file when user press Submit before upload process has done
  • Loading branch information
HuongNV13 committed Jul 10, 2018
1 parent 5ebb6eb commit d49d4db
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
31 changes: 31 additions & 0 deletions course/dndupload.js
Expand Up @@ -57,6 +57,8 @@ M.course_dndupload = {
// The selector identifying the list of modules within a section (note changing this may require
// changes to the get_mods_element function)
modslistselector: 'ul.section',
// Original onbeforeunload event.
originalUnloadEvent: null,

/**
* Initalise the drag and drop upload interface
Expand Down Expand Up @@ -761,6 +763,10 @@ M.course_dndupload = {
// Wait for the AJAX call to complete, then update the
// dummy element with the returned details
xhr.onreadystatechange = function() {
if (xhr.readyState == 1) {
this.originalUnloadEvent = window.onbeforeunload;
self.reportUploadDirtyState(true);
}
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var result = JSON.parse(xhr.responseText);
Expand All @@ -787,6 +793,7 @@ M.course_dndupload = {
} else {
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
}
self.reportUploadDirtyState(false);
}
};

Expand Down Expand Up @@ -990,6 +997,10 @@ M.course_dndupload = {
// Wait for the AJAX call to complete, then update the
// dummy element with the returned details
xhr.onreadystatechange = function() {
if (xhr.readyState == 1) {
this.originalUnloadEvent = window.onbeforeunload;
self.reportUploadDirtyState(true);
}
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var result = JSON.parse(xhr.responseText);
Expand All @@ -1012,6 +1023,7 @@ M.course_dndupload = {
} else {
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
}
self.reportUploadDirtyState(false);
}
};

Expand Down Expand Up @@ -1045,5 +1057,24 @@ M.course_dndupload = {
if (M.core.actionmenu && M.core.actionmenu.newDOMNode) {
M.core.actionmenu.newDOMNode(node);
}
},

/**
* Set the event to prevent user navigate away when upload progress still running.
*
* @param {bool} enable true if upload progress is running, false otherwise
*/
reportUploadDirtyState: function(enable) {
if (!enable) {
window.onbeforeunload = this.originalUnloadEvent;
} else {
window.onbeforeunload = function(e) {
var warningMessage = M.util.get_string('changesmadereallygoaway', 'moodle');
if (e) {
e.returnValue = warningMessage;
}
return warningMessage;
};
}
}
};
3 changes: 2 additions & 1 deletion course/dnduploadlib.php
Expand Up @@ -65,7 +65,8 @@ function dndupload_add_to_course($course, $modnames) {
array('actionchoice', 'moodle'),
array('servererror', 'moodle'),
array('upload', 'moodle'),
array('cancel', 'moodle')
array('cancel', 'moodle'),
array('changesmadereallygoaway', 'moodle')
),
'requires' => array('node', 'event', 'json', 'anim')
);
Expand Down
24 changes: 24 additions & 0 deletions lib/form/dndupload.js
Expand Up @@ -495,6 +495,8 @@ M.form_dndupload.init = function(Y, options) {
overwriteall: false,
// Set to true if the user has clicked on 'rename all'.
renameall: false,
// Original onbeforeunload event.
originalUnloadEvent: null,

/**
* Initialise the settings for the dnduploader
Expand Down Expand Up @@ -527,6 +529,8 @@ M.form_dndupload.init = function(Y, options) {
this.callbackcancel();
}
}
this.originalUnloadEvent = window.onbeforeunload;
this.reportUploadDirtyState(true);
},

/**
Expand Down Expand Up @@ -839,6 +843,7 @@ M.form_dndupload.init = function(Y, options) {
* Run the callback to the filemanager/filepicker
*/
uploadfinished: function(lastresult) {
this.reportUploadDirtyState(false);
this.callback(lastresult);
},

Expand Down Expand Up @@ -938,6 +943,25 @@ M.form_dndupload.init = function(Y, options) {
xhr.open("POST", uploadUrl, true);
xhr.send(formdata);
return true;
},

/**
* Set the event to prevent user navigate away when upload progress still running.
*
* @param {bool} enable true if upload progress is running, false otherwise
*/
reportUploadDirtyState: function(enable) {
if (!enable) {
window.onbeforeunload = this.originalUnloadEvent;
} else {
window.onbeforeunload = function(e) {
var warningMessage = M.util.get_string('changesmadereallygoaway', 'moodle');
if (e) {
e.returnValue = warningMessage;
}
return warningMessage;
};
}
}
});

Expand Down
1 change: 1 addition & 0 deletions lib/outputrequirementslib.php
Expand Up @@ -795,6 +795,7 @@ protected function find_module($component) {
array('dndenabled_inbox', 'moodle'), array('fileexists', 'moodle'), array('maxbytesfile', 'error'),
array('sizegb', 'moodle'), array('sizemb', 'moodle'), array('sizekb', 'moodle'), array('sizeb', 'moodle'),
array('maxareabytesreached', 'moodle'), array('serverconnection', 'error'),
array('changesmadereallygoaway', 'moodle')
));
break;
}
Expand Down

0 comments on commit d49d4db

Please sign in to comment.