From 9b0f29239b308f9f4be143605c1fbac1b99473e2 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Wed, 1 Nov 2017 18:28:47 +0800 Subject: [PATCH] MDL-60590 enrol_manual: button title depends on cohort list existence Also added a SELECTORS object to tidy the code by keeping the selectors in a central location. --- enrol/manual/amd/build/quickenrolment.min.js | 2 +- enrol/manual/amd/src/quickenrolment.js | 25 +++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/enrol/manual/amd/build/quickenrolment.min.js b/enrol/manual/amd/build/quickenrolment.min.js index 8a0e6ec4fa5..552a0888890 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=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"),f=c.find(':input[value="_qf__force_multiselect_submission"]');f.each(function(){this.remove()});var g=c.serialize();this.modal.hide();var h={type:"GET",processData:!1,contentType:"application/json"},i=d.wwwroot+"/enrol/manual/ajax.php?"+g;b.ajax(i,h).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={COHORTSELECT:"#id_cohortlist",TRIGGERBUTTONS:".enrolusersbutton.enrol_manual_plugin [type='submit']",UNWANTEDHIDDENFIELDS:":input[value='_qf__force_multiselect_submission']"},j=function(a){this.contextid=a.contextid,this.initModal()};return j.prototype.courseid=0,j.prototype.modal=null,j.prototype.initModal=function(){var a=b(i.TRIGGERBUTTONS),d=c.get_strings([{key:"enroluserscohorts",component:"enrol_manual"},{key:"enrolusers",component:"enrol_manual"}]),h=d.then(function(a){return a[1]});return f.create({type:f.types.SAVE_CANCEL,large:!0,title:h,body:this.getBody()},a).then(function(a){this.modal=a;var b=1;0!==this.modal.getRoot().find("form").find(i.COHORTSELECT).length&&(b=0);var c=d.then(function(a){return a[b]});return 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)),a}.bind(this)).fail(e.exception)},j.prototype.submitForm=function(a){a.preventDefault(),this.modal.getRoot().find("form").submit()},j.prototype.submitFormAjax=function(a){a.preventDefault();var c=this.modal.getRoot().find("form"),f=c.find(i.UNWANTEDHIDDENFIELDS);f.each(function(){this.remove()});var g=c.serialize();this.modal.hide();var h={type:"GET",processData:!1,contentType:"application/json"},j=d.wwwroot+"/enrol/manual/ajax.php?"+g;b.ajax(j,h).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)},j.prototype.getBody=function(){return h.loadFragment("enrol_manual","enrol_users_form",this.contextid,{}).fail(e.exception)},j.prototype.getFooter=function(){return a.render("enrol_manual/enrol_modal_footer",{})},{init:function(a){new j(a)}}}); \ No newline at end of file diff --git a/enrol/manual/amd/src/quickenrolment.js b/enrol/manual/amd/src/quickenrolment.js index c2f7a3b7a58..102e1746224 100644 --- a/enrol/manual/amd/src/quickenrolment.js +++ b/enrol/manual/amd/src/quickenrolment.js @@ -31,6 +31,13 @@ define(['core/templates', ], function(Template, $, Str, Config, Notification, ModalFactory, ModalEvents, Fragment) { + /** @type {Object} The list of selectors for the quick enrolment modal. */ + var SELECTORS = { + COHORTSELECT: "#id_cohortlist", + TRIGGERBUTTONS: ".enrolusersbutton.enrol_manual_plugin [type='submit']", + UNWANTEDHIDDENFIELDS: ":input[value='_qf__force_multiselect_submission']" + }; + /** * Constructor * @@ -57,7 +64,7 @@ define(['core/templates', * @private */ QuickEnrolment.prototype.initModal = function() { - var triggerButtons = $('.enrolusersbutton.enrol_manual_plugin [type="submit"]'); + var triggerButtons = $(SELECTORS.TRIGGERBUTTONS); var stringsPromise = Str.get_strings([ {key: 'enroluserscohorts', component: 'enrol_manual'}, @@ -68,10 +75,6 @@ define(['core/templates', return strings[1]; }); - var buttonPromise = stringsPromise.then(function(strings) { - return strings[0]; - }); - return ModalFactory.create({ type: ModalFactory.types.SAVE_CANCEL, large: true, @@ -81,6 +84,16 @@ define(['core/templates', .then(function(modal) { this.modal = modal; + // The save button text depends on whether or not cohorts exist. + var stringindex = 1; + if (this.modal.getRoot().find('form').find(SELECTORS.COHORTSELECT).length !== 0) { + stringindex = 0; + } + + var buttonPromise = stringsPromise.then(function(strings) { + return strings[stringindex]; + }); + this.modal.setSaveButtonText(buttonPromise); // We want the reset the form every time it is opened. @@ -123,7 +136,7 @@ define(['core/templates', // Before send the data through AJAX, we need to parse and remove some unwanted hidden fields. // This hidden fields are added automatically by mforms and when it reaches the AJAX we get an error. - var hidden = form.find(':input[value="_qf__force_multiselect_submission"]'); + var hidden = form.find(SELECTORS.UNWANTEDHIDDENFIELDS); hidden.each(function () { this.remove(); });