From 17137d471d704f38cb60a0083fd9f0fca4dd4221 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 18 Aug 2017 10:53:28 +0800 Subject: [PATCH 1/2] MDL-59859 core: Accept Promise for save button Accept either a promise, or a string for the content of the save button in the save / cancel modal. --- lib/amd/build/modal_save_cancel.min.js | 2 +- lib/amd/src/modal_save_cancel.js | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/amd/build/modal_save_cancel.min.js b/lib/amd/build/modal_save_cancel.min.js index 96737e88ad3..e2d2ab77118 100644 --- a/lib/amd/build/modal_save_cancel.min.js +++ b/lib/amd/build/modal_save_cancel.min.js @@ -1 +1 @@ -define(["jquery","core/notification","core/custom_interaction_events","core/modal","core/modal_events"],function(a,b,c,d,e){var f={SAVE_BUTTON:'[data-action="save"]',CANCEL_BUTTON:'[data-action="cancel"]'},g=function(a){d.call(this,a),this.getFooter().find(f.SAVE_BUTTON).length||b.exception({message:"No save button found"}),this.getFooter().find(f.CANCEL_BUTTON).length||b.exception({message:"No cancel button found"})};return g.prototype=Object.create(d.prototype),g.prototype.constructor=g,g.prototype.setFooter=function(){b.exception({message:"Can not change the footer of a save cancel modal"})},g.prototype.registerEventListeners=function(){d.prototype.registerEventListeners.call(this),this.getModal().on(c.events.activate,f.SAVE_BUTTON,function(b,c){var d=a.Event(e.save);this.getRoot().trigger(d,this),d.isDefaultPrevented()||(this.hide(),c.originalEvent.preventDefault())}.bind(this)),this.getModal().on(c.events.activate,f.CANCEL_BUTTON,function(b,c){var d=a.Event(e.cancel);this.getRoot().trigger(d,this),d.isDefaultPrevented()||(this.hide(),c.originalEvent.preventDefault())}.bind(this))},g.prototype.setSaveButtonText=function(a){this.getFooter().find(f.SAVE_BUTTON).text(a)},g}); \ No newline at end of file +define(["jquery","core/notification","core/custom_interaction_events","core/modal","core/modal_events"],function(a,b,c,d,e){var f={SAVE_BUTTON:'[data-action="save"]',CANCEL_BUTTON:'[data-action="cancel"]'},g=function(a){d.call(this,a),this.getFooter().find(f.SAVE_BUTTON).length||b.exception({message:"No save button found"}),this.getFooter().find(f.CANCEL_BUTTON).length||b.exception({message:"No cancel button found"})};return g.prototype=Object.create(d.prototype),g.prototype.constructor=g,g.prototype.setFooter=function(){b.exception({message:"Can not change the footer of a save cancel modal"})},g.prototype.registerEventListeners=function(){d.prototype.registerEventListeners.call(this),this.getModal().on(c.events.activate,f.SAVE_BUTTON,function(b,c){var d=a.Event(e.save);this.getRoot().trigger(d,this),d.isDefaultPrevented()||(this.hide(),c.originalEvent.preventDefault())}.bind(this)),this.getModal().on(c.events.activate,f.CANCEL_BUTTON,function(b,c){var d=a.Event(e.cancel);this.getRoot().trigger(d,this),d.isDefaultPrevented()||(this.hide(),c.originalEvent.preventDefault())}.bind(this))},g.prototype.setSaveButtonText=function(a){var b=this.getFooter().find(f.SAVE_BUTTON);this.asyncSet(a,b.text.bind(b))},g}); \ No newline at end of file diff --git a/lib/amd/src/modal_save_cancel.js b/lib/amd/src/modal_save_cancel.js index 5949bb6ab02..f62751c48db 100644 --- a/lib/amd/src/modal_save_cancel.js +++ b/lib/amd/src/modal_save_cancel.js @@ -91,10 +91,15 @@ define(['jquery', 'core/notification', 'core/custom_interaction_events', 'core/m /** * Allows to overwrite the text of "Save changes" button. * - * @param {String} text + * This method is overloaded to take either a string value for the button title or a jQuery promise that is resolved with + * text most commonly from a Str.get_string call. + * + * @param {(String|object)} value The button text, or a jQuery promise which will resolve it */ - ModalSaveCancel.prototype.setSaveButtonText = function(text) { - this.getFooter().find(SELECTORS.SAVE_BUTTON).text(text); + ModalSaveCancel.prototype.setSaveButtonText = function(value) { + var button = this.getFooter().find(SELECTORS.SAVE_BUTTON); + + this.asyncSet(value, button.text.bind(button)); }; return ModalSaveCancel; From b05f2cd7945d1b42cf4bddce31163e698d7181f3 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 18 Aug 2017 10:02:00 +0800 Subject: [PATCH 2/2] MDL-59859 enrol_manual: Improve modal creation ordering --- enrol/manual/amd/build/quickenrolment.min.js | 2 +- enrol/manual/amd/src/quickenrolment.js | 50 ++++++++++++-------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/enrol/manual/amd/build/quickenrolment.min.js b/enrol/manual/amd/build/quickenrolment.min.js index 583e1361bd2..698752c13b2 100644 --- a/enrol/manual/amd/build/quickenrolment.min.js +++ b/enrol/manual/amd/build/quickenrolment.min.js @@ -1 +1 @@ -define(["core/templates","jquery","core/str","core/config","core/notification","core/modal_factory","core/modal_events","core/fragment"],function(a,b,c,d,e,f,g,h){var i=function(a){this.contextid=a.contextid,this.initModal()};return i.prototype.courseid=0,i.prototype.modal=null,i.prototype.initModal=function(){var a=b('.enrolusersbutton.enrol_manual_plugin [type="submit"]'),d=[{key:"enroluserscohorts",component:"enrol_manual"},{key:"enrolusers",component:"enrol_manual"}];b.when(c.get_strings(d)).then(function(b){var c=b[0],d=b[1];return f.create({type:f.types.SAVE_CANCEL,title:d,body:this.getBody()},a).then(function(a){this.modal=a,this.modal.setLarge(),this.modal.setSaveButtonText(c),this.modal.getRoot().on(g.hidden,function(){this.modal.setBody(this.getBody())}.bind(this)),this.modal.getRoot().on(g.save,this.submitForm.bind(this)),this.modal.getRoot().on("submit","form",this.submitFormAjax.bind(this))}.bind(this))}.bind(this)).fail(e.exception)},i.prototype.submitForm=function(a){a.preventDefault(),this.modal.getRoot().find("form").submit()},i.prototype.submitFormAjax=function(a){a.preventDefault();var c=this.modal.getRoot().find("form").serialize();this.modal.hide();var f={type:"GET",processData:!1,contentType:"application/json"},g=d.wwwroot+"/enrol/manual/ajax.php?"+c;b.ajax(g,f).then(function(a){a.error?e.addNotification({message:a.error,type:"error"}):("undefined"!=typeof window.M.core_formchangechecker&&window.M.core_formchangechecker.reset_form_dirty_state(),window.location.reload())}).fail(e.exception)},i.prototype.getBody=function(){return h.loadFragment("enrol_manual","enrol_users_form",this.contextid,{}).fail(e.exception)},i.prototype.getFooter=function(){return a.render("enrol_manual/enrol_modal_footer",{})},{init:function(a){new i(a)}}}); \ No newline at end of file +define(["core/templates","jquery","core/str","core/config","core/notification","core/modal_factory","core/modal_events","core/fragment"],function(a,b,c,d,e,f,g,h){var i=function(a){this.contextid=a.contextid,this.initModal()};return i.prototype.courseid=0,i.prototype.modal=null,i.prototype.initModal=function(){var a=b('.enrolusersbutton.enrol_manual_plugin [type="submit"]'),d=c.get_strings([{key:"enroluserscohorts",component:"enrol_manual"},{key:"enrolusers",component:"enrol_manual"}]),h=d.then(function(a){return a[1]}),i=d.then(function(a){return a[0]});return f.create({type:f.types.SAVE_CANCEL,large:!0,title:h,body:this.getBody()},a).then(function(a){return this.modal=a,this.modal.setSaveButtonText(i),this.modal.getRoot().on(g.hidden,function(){this.modal.setBody(this.getBody())}.bind(this)),this.modal.getRoot().on(g.save,this.submitForm.bind(this)),this.modal.getRoot().on("submit","form",this.submitFormAjax.bind(this)),a}.bind(this)).fail(e.exception)},i.prototype.submitForm=function(a){a.preventDefault(),this.modal.getRoot().find("form").submit()},i.prototype.submitFormAjax=function(a){a.preventDefault();var c=this.modal.getRoot().find("form").serialize();this.modal.hide();var f={type:"GET",processData:!1,contentType:"application/json"},g=d.wwwroot+"/enrol/manual/ajax.php?"+c;b.ajax(g,f).then(function(a){a.error?e.addNotification({message:a.error,type:"error"}):("undefined"!=typeof window.M.core_formchangechecker&&window.M.core_formchangechecker.reset_form_dirty_state(),window.location.reload())}).fail(e.exception)},i.prototype.getBody=function(){return h.loadFragment("enrol_manual","enrol_users_form",this.contextid,{}).fail(e.exception)},i.prototype.getFooter=function(){return a.render("enrol_manual/enrol_modal_footer",{})},{init:function(a){new i(a)}}}); \ No newline at end of file diff --git a/enrol/manual/amd/src/quickenrolment.js b/enrol/manual/amd/src/quickenrolment.js index eedda356f4e..600a89406f8 100644 --- a/enrol/manual/amd/src/quickenrolment.js +++ b/enrol/manual/amd/src/quickenrolment.js @@ -59,33 +59,41 @@ define(['core/templates', QuickEnrolment.prototype.initModal = function() { var triggerButtons = $('.enrolusersbutton.enrol_manual_plugin [type="submit"]'); - var strparams = [ + var stringsPromise = Str.get_strings([ {key: 'enroluserscohorts', component: 'enrol_manual'}, - {key: 'enrolusers', component: 'enrol_manual'} - ]; + {key: 'enrolusers', component: 'enrol_manual'}, + ]); - $.when(Str.get_strings(strparams)).then(function(strlist) { - var modalSaveChanges = strlist[0], - modalTitle = strlist[1]; + var titlePromise = stringsPromise.then(function(strings) { + return strings[1]; + }); - return ModalFactory.create({ - type: ModalFactory.types.SAVE_CANCEL, - title: modalTitle, - body: this.getBody() - }, triggerButtons).then(function(modal) { - this.modal = modal; - this.modal.setLarge(); - this.modal.setSaveButtonText(modalSaveChanges); + var buttonPromise = stringsPromise.then(function(strings) { + return strings[0]; + }); - // We want the reset the form every time it is opened. - this.modal.getRoot().on(ModalEvents.hidden, function() { - this.modal.setBody(this.getBody()); - }.bind(this)); + return ModalFactory.create({ + type: ModalFactory.types.SAVE_CANCEL, + large: true, + title: titlePromise, + body: this.getBody() + }, triggerButtons) + .then(function(modal) { + this.modal = modal; - this.modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this)); - this.modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this)); + this.modal.setSaveButtonText(buttonPromise); + + // We want the reset the form every time it is opened. + this.modal.getRoot().on(ModalEvents.hidden, function() { + this.modal.setBody(this.getBody()); }.bind(this)); - }.bind(this)).fail(Notification.exception); + + this.modal.getRoot().on(ModalEvents.save, this.submitForm.bind(this)); + this.modal.getRoot().on('submit', 'form', this.submitFormAjax.bind(this)); + + return modal; + }.bind(this)) + .fail(Notification.exception); }; /**