Merge branch 'MDL-70947-master' of git://github.com/HuongNV13/moodle

This commit is contained in:
Jun Pataleta
2021-04-30 10:34:11 +08:00
12 changed files with 198 additions and 54 deletions
+24 -25
View File
@@ -57,8 +57,6 @@ 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
@@ -765,7 +763,10 @@ M.course_dndupload = {
xhr.onreadystatechange = function() {
if (xhr.readyState == 1) {
this.originalUnloadEvent = window.onbeforeunload;
self.reportUploadDirtyState(true);
// Trigger form upload start events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadStarted(section.get('id'));
});
}
if (xhr.readyState == 4) {
if (xhr.status == 200) {
@@ -793,7 +794,10 @@ M.course_dndupload = {
} else {
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
}
self.reportUploadDirtyState(false);
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(section.get('id'));
});
}
};
@@ -1025,7 +1029,10 @@ M.course_dndupload = {
xhr.onreadystatechange = function() {
if (xhr.readyState == 1) {
this.originalUnloadEvent = window.onbeforeunload;
self.reportUploadDirtyState(true);
// Trigger form upload start events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadStarted(section.get('id'));
});
}
if (xhr.readyState == 4) {
if (xhr.status == 200) {
@@ -1043,13 +1050,24 @@ M.course_dndupload = {
} else {
// Error - remove the dummy element
resel.parent.removeChild(resel.li);
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(section.get('id'));
});
new M.core.alert({message: result.error});
}
}
} else {
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(section.get('id'));
});
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
}
self.reportUploadDirtyState(false);
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(section.get('id'));
});
}
};
@@ -1083,24 +1101,5 @@ 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;
};
}
}
};
@@ -358,6 +358,11 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
host.saveSelection();
// Trigger form upload start events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadStarted(self.editor.get('id'));
});
var options = host.get('filepickeroptions').image,
savepath = (options.savepath === undefined) ? '/' : options.savepath,
formData = new FormData(),
@@ -412,6 +417,10 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
if (placeholder) {
placeholder.remove(true);
}
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
throw new M.core.ajaxException(result);
}
@@ -439,12 +448,20 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
}
} else {
Y.use('moodle-core-notification-alert', function() {
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
});
if (placeholder) {
placeholder.remove(true);
}
}
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
}
};
xhr.open("POST", M.cfg.wwwroot + '/repository/repository_ajax.php?action=upload', true);
File diff suppressed because one or more lines are too long
@@ -358,6 +358,11 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
host.saveSelection();
// Trigger form upload start events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadStarted(self.editor.get('id'));
});
var options = host.get('filepickeroptions').image,
savepath = (options.savepath === undefined) ? '/' : options.savepath,
formData = new FormData(),
@@ -412,6 +417,10 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
if (placeholder) {
placeholder.remove(true);
}
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
throw new M.core.ajaxException(result);
}
@@ -439,12 +448,20 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
}
} else {
Y.use('moodle-core-notification-alert', function() {
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
});
if (placeholder) {
placeholder.remove(true);
}
}
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
}
};
xhr.open("POST", M.cfg.wwwroot + '/repository/repository_ajax.php?action=upload', true);
@@ -356,6 +356,11 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
host.saveSelection();
// Trigger form upload start events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadStarted(self.editor.get('id'));
});
var options = host.get('filepickeroptions').image,
savepath = (options.savepath === undefined) ? '/' : options.savepath,
formData = new FormData(),
@@ -410,6 +415,10 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
if (placeholder) {
placeholder.remove(true);
}
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
throw new M.core.ajaxException(result);
}
@@ -437,12 +446,20 @@ Y.namespace('M.atto_image').Button = Y.Base.create('button', Y.M.editor_atto.Edi
}
} else {
Y.use('moodle-core-notification-alert', function() {
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
new M.core.alert({message: M.util.get_string('servererror', 'moodle')});
});
if (placeholder) {
placeholder.remove(true);
}
}
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(self.editor.get('id'));
});
}
};
xhr.open("POST", M.cfg.wwwroot + '/repository/repository_ajax.php?action=upload', true);
+2
View File
@@ -0,0 +1,2 @@
define ("core_form/events",["exports","core/str"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.triggerUploadCompleted=a.triggerUploadStarted=a.types=void 0;var c;(0,b.get_string)("changesmadereallygoaway","moodle").then(function(a){c=a;return a}).catch();var d=function(a){if(a){a.returnValue=c}},e={uploadStarted:"core_form/uploadStarted",uploadCompleted:"core_form/uploadCompleted"};a.types=e;a.triggerUploadStarted=function triggerUploadStarted(a){window.addEventListener("beforeunload",d);var b=new CustomEvent(e.uploadStarted,{bubbles:!0,cancellable:!1}),c=document.getElementById(a);c.dispatchEvent(b);return b};a.triggerUploadCompleted=function triggerUploadCompleted(a){window.removeEventListener("beforeunload",d);var b=new CustomEvent(e.uploadCompleted,{bubbles:!0,cancellable:!1}),c=document.getElementById(a);c.dispatchEvent(b);return b}});
//# sourceMappingURL=events.min.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"sources":["../src/events.js"],"names":["changesMadeString","then","string","catch","changesMadeCheck","e","returnValue","types","uploadStarted","uploadCompleted","triggerUploadStarted","elementId","window","addEventListener","customEvent","CustomEvent","bubbles","cancellable","element","document","getElementById","dispatchEvent","triggerUploadCompleted","removeEventListener"],"mappings":"6LA2BA,GAAIA,CAAAA,CAAJ,CACA,iBAAU,yBAAV,CAAqC,QAArC,EAA+CC,IAA/C,CAAoD,SAAAC,CAAM,CAAI,CAC1DF,CAAiB,CAAGE,CAApB,CACA,MAAOA,CAAAA,CACV,CAHD,EAGGC,KAHH,G,GASMC,CAAAA,CAAgB,CAAG,SAAAC,CAAC,CAAI,CAC1B,GAAIA,CAAJ,CAAO,CACHA,CAAC,CAACC,WAAF,CAAgBN,CACnB,CACJ,C,CAKYO,CAAK,CAAG,CACjBC,aAAa,CAAE,yBADE,CAEjBC,eAAe,CAAE,2BAFA,C,kCAWe,QAAvBC,CAAAA,oBAAuB,CAAAC,CAAS,CAAI,CAE7CC,MAAM,CAACC,gBAAP,CAAwB,cAAxB,CAAwCT,CAAxC,EAF6C,GAGvCU,CAAAA,CAAW,CAAG,GAAIC,CAAAA,WAAJ,CAAgBR,CAAK,CAACC,aAAtB,CAAqC,CACrDQ,OAAO,GAD8C,CAErDC,WAAW,GAF0C,CAArC,CAHyB,CAOvCC,CAAO,CAAGC,QAAQ,CAACC,cAAT,CAAwBT,CAAxB,CAP6B,CAQ7CO,CAAO,CAACG,aAAR,CAAsBP,CAAtB,EAEA,MAAOA,CAAAA,CACV,C,0BAQqC,QAAzBQ,CAAAA,sBAAyB,CAAAX,CAAS,CAAI,CAE/CC,MAAM,CAACW,mBAAP,CAA2B,cAA3B,CAA2CnB,CAA3C,EAF+C,GAGzCU,CAAAA,CAAW,CAAG,GAAIC,CAAAA,WAAJ,CAAgBR,CAAK,CAACE,eAAtB,CAAuC,CACvDO,OAAO,GADgD,CAEvDC,WAAW,GAF4C,CAAvC,CAH2B,CAOzCC,CAAO,CAAGC,QAAQ,CAACC,cAAT,CAAwBT,CAAxB,CAP+B,CAQ/CO,CAAO,CAACG,aAAR,CAAsBP,CAAtB,EAEA,MAAOA,CAAAA,CACV,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see <http://www.gnu.org/licenses/>.\n\n/**\n * Contain the events the form component can trigger.\n *\n * @module core_form/events\n * @package core_form\n * @copyright 2021 Huong Nguyen <[email protected]>\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * @since 3.10\n */\n\nimport {get_string as getString} from 'core/str';\n\nlet changesMadeString;\ngetString('changesmadereallygoaway', 'moodle').then(string => {\n changesMadeString = string;\n return string;\n}).catch();\n\n/**\n * Prevent user navigate away when upload progress still running.\n * @param {Event} e The event\n */\nconst changesMadeCheck = e => {\n if (e) {\n e.returnValue = changesMadeString;\n }\n};\n\n/**\n * List of the events.\n **/\nexport const types = {\n uploadStarted: 'core_form/uploadStarted',\n uploadCompleted: 'core_form/uploadCompleted',\n};\n\n/**\n * Trigger upload start event.\n *\n * @param {String} elementId\n * @returns {CustomEvent<unknown>}\n */\nexport const triggerUploadStarted = elementId => {\n // Add an additional check for changes made.\n window.addEventListener('beforeunload', changesMadeCheck);\n const customEvent = new CustomEvent(types.uploadStarted, {\n bubbles: true,\n cancellable: false\n });\n const element = document.getElementById(elementId);\n element.dispatchEvent(customEvent);\n\n return customEvent;\n};\n\n/**\n * Trigger upload complete event.\n *\n * @param {String} elementId\n * @returns {CustomEvent<unknown>}\n */\nexport const triggerUploadCompleted = elementId => {\n // Remove the additional check for changes made.\n window.removeEventListener('beforeunload', changesMadeCheck);\n const customEvent = new CustomEvent(types.uploadCompleted, {\n bubbles: true,\n cancellable: false\n });\n const element = document.getElementById(elementId);\n element.dispatchEvent(customEvent);\n\n return customEvent;\n};\n"],"file":"events.min.js"}
+1 -1
View File
@@ -1,2 +1,2 @@
define ("core_form/submit",["exports"],function(a){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;var b=0,c=[],d=function(a){c.push(a);if(!b){b=setInterval(function(){var a=document.cookie.split(e()+"=");if(2==a.length){f();clearInterval(b);b=0;c.forEach(function(a){a.disabled=!1})}},500)}},e=function(){return"moodledownload_"+M.cfg.sesskey},f=function(){document.cookie=encodeURIComponent(e())+"=deleted; expires="+new Date(0).toUTCString()};a.init=function init(a){var b=document.getElementById(a);if("off"===b.form.dataset.doubleSubmitProtection){return}b.form.addEventListener("submit",function(a){var c=function(){if(a.defaultPrevented||b.disabled){return}b.disabled=!0;f();d(b)};window.addEventListener("beforeunload",c);setTimeout(function(){window.removeEventListener("beforeunload",c)},0)},!1)}});
define ("core_form/submit",["exports","core_form/events"],function(a,b){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=void 0;var c=0,d=[],e=function(a){d.push(a);if(!c){c=setInterval(function(){var a=document.cookie.split(f()+"=");if(2==a.length){g();clearInterval(c);c=0;d.forEach(function(a){a.disabled=!1})}},500)}},f=function(){return"moodledownload_"+M.cfg.sesskey},g=function(){document.cookie=encodeURIComponent(f())+"=deleted; expires="+new Date(0).toUTCString()};a.init=function init(a){var c=document.getElementById(a);document.addEventListener(b.types.uploadStarted,function(a){window.console.log(a.target);c.disabled=!0});document.addEventListener(b.types.uploadCompleted,function(a){window.console.log(a.target);c.disabled=!1});if("off"===c.form.dataset.doubleSubmitProtection){return}c.form.addEventListener("submit",function(a){var b=function(){if(a.defaultPrevented||c.disabled){return}c.disabled=!0;g();e(c)};window.addEventListener("beforeunload",b);setTimeout(function(){window.removeEventListener("beforeunload",b)},0)},!1)}});
//# sourceMappingURL=submit.min.js.map
File diff suppressed because one or more lines are too long
+88
View File
@@ -0,0 +1,88 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Contain the events the form component can trigger.
*
* @module core_form/events
* @package core_form
* @copyright 2021 Huong Nguyen <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.10
*/
import {get_string as getString} from 'core/str';
let changesMadeString;
getString('changesmadereallygoaway', 'moodle').then(string => {
changesMadeString = string;
return string;
}).catch();
/**
* Prevent user navigate away when upload progress still running.
* @param {Event} e The event
*/
const changesMadeCheck = e => {
if (e) {
e.returnValue = changesMadeString;
}
};
/**
* List of the events.
**/
export const types = {
uploadStarted: 'core_form/uploadStarted',
uploadCompleted: 'core_form/uploadCompleted',
};
/**
* Trigger upload start event.
*
* @param {String} elementId
* @returns {CustomEvent<unknown>}
*/
export const triggerUploadStarted = elementId => {
// Add an additional check for changes made.
window.addEventListener('beforeunload', changesMadeCheck);
const customEvent = new CustomEvent(types.uploadStarted, {
bubbles: true,
cancellable: false
});
const element = document.getElementById(elementId);
element.dispatchEvent(customEvent);
return customEvent;
};
/**
* Trigger upload complete event.
*
* @param {String} elementId
* @returns {CustomEvent<unknown>}
*/
export const triggerUploadCompleted = elementId => {
// Remove the additional check for changes made.
window.removeEventListener('beforeunload', changesMadeCheck);
const customEvent = new CustomEvent(types.uploadCompleted, {
bubbles: true,
cancellable: false
});
const element = document.getElementById(elementId);
element.dispatchEvent(customEvent);
return customEvent;
};
+15
View File
@@ -24,6 +24,8 @@
* @since 3.8
*/
import {types} from 'core_form/events';
/** @type {number} ID for setInterval used when polling for download cookie */
let cookieListener = 0;
@@ -85,6 +87,19 @@ const clearDownloadCookie = () => {
*/
export const init = (elementId) => {
const button = document.getElementById(elementId);
// Add event listener for file upload start.
document.addEventListener(types.uploadStarted, e => {
window.console.log(e.target); // This will be the element/section where the file is uploaded to.
button.disabled = true;
});
// Add event listener for file upload complete.
document.addEventListener(types.uploadCompleted, e => {
window.console.log(e.target); // This will be the element/section where the file is uploaded to.
button.disabled = false;
});
// If the form has double submit protection disabled, do nothing.
if (button.form.dataset.doubleSubmitProtection === 'off') {
return;
+12 -24
View File
@@ -504,8 +504,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,
// The file manager helper.
filemanagerhelper: null,
/**
* Initialise the settings for the dnduploader
@@ -527,6 +527,7 @@ M.form_dndupload.init = function(Y, options) {
this.currentfiles = params.currentfiles;
this.currentfilecount = params.currentfilecount;
this.currentareasize = 0;
this.filemanagerhelper = this.options.filemanager;
// Retrieve the current size of the area.
for (var i = 0; i < this.currentfiles.length; i++) {
@@ -538,8 +539,11 @@ M.form_dndupload.init = function(Y, options) {
this.callbackcancel();
}
}
this.originalUnloadEvent = window.onbeforeunload;
this.reportUploadDirtyState(true);
// Trigger form upload start events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadStarted(this.filemanagerhelper.filemanager.get('id'));
}.bind(this));
},
/**
@@ -852,7 +856,10 @@ M.form_dndupload.init = function(Y, options) {
* Run the callback to the filemanager/filepicker
*/
uploadfinished: function(lastresult) {
this.reportUploadDirtyState(false);
// Trigger form upload complete events.
require(['core_form/events'], function(FormEvent) {
FormEvent.triggerUploadCompleted(this.filemanagerhelper.filemanager.get('id'));
}.bind(this));
this.callback(lastresult);
},
@@ -952,25 +959,6 @@ 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;
};
}
}
});