diff --git a/amd/build/condition_form.min.js b/amd/build/condition_form.min.js index f8a34b0..ad9d054 100644 --- a/amd/build/condition_form.min.js +++ b/amd/build/condition_form.min.js @@ -1,10 +1,10 @@ -define("tool_dynamic_cohorts/condition_form",["exports","core/ajax","core/templates","core/fragment","core/modal_events","core/modal_factory","core/notification","core/str"],(function(_exports,_ajax,_templates,_fragment,_modal_events,_modal_factory,_notification,_str){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}} +define("tool_dynamic_cohorts/condition_form",["exports","core/ajax","core/templates","core/fragment","core/modal_events","core/modal_save_cancel","core/notification","core/str"],(function(_exports,_ajax,_templates,_fragment,_modal_events,_modal_save_cancel,_notification,_str){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}} /** * Condition modal form. * * @module tool_dynamic_cohorts/condition_form * @copyright 2024 Catalyst IT * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - * */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_ajax=_interopRequireDefault(_ajax),_templates=_interopRequireDefault(_templates),_fragment=_interopRequireDefault(_fragment),_modal_events=_interopRequireDefault(_modal_events),_modal_factory=_interopRequireDefault(_modal_factory),_notification=_interopRequireDefault(_notification);const SELECTORS_ADD_CONDITION_BUTTON="#id_conditionmodalbutton",SELECTORS_SELECT_CONDITION="#id_condition",SELECTORS_CONDITIONS_LIST="#conditions",SELECTORS_RULE_FORM_CONDITIONS_JSON="#id_conditionjson",SELECTORS_RULE_FORM_IS_CONDITIONS_CHANGED="#id_isconditionschanged",SELECTORS_CONDITIONS_NOT_SAVED_WARNING="#tool-dynamic-cohorts-not-saved",SELECTORS_CONDITION_EDIT_ACTION="tool-dynamic-cohorts-condition-edit",SELECTORS_CONDITION_DELETE_ACTION="tool-dynamic-cohorts-condition-delete",SELECTORS_CONDITIONS="tool-dynamic-cohorts-conditions",getModalFormBody=(className,submittedData,defaults)=>{void 0===defaults&&(defaults="");const params={classname:className,jsonformdata:JSON.stringify(submittedData),defaults:JSON.stringify(defaults)};return _fragment.default.loadFragment("tool_dynamic_cohorts","condition_form",1,params)},displayModalForm=(className,defaults)=>{void 0===defaults&&(defaults=""),_modal_factory.default.create({type:_modal_factory.default.types.SAVE_CANCEL,title:(0,_str.get_string)("conditionformtitle","tool_dynamic_cohorts"),body:getModalFormBody(className,"",defaults),large:!0}).then((function(modal){modal.getRoot().on(_modal_events.default.save,(function(e){e.preventDefault(),modal.getRoot().find("form").submit()})),modal.getRoot().on(_modal_events.default.hidden,(function(){modal.destroy()})),modal.getRoot().on("submit","form",(function(e){e.preventDefault(),submitModalFormAjax(className,modal)})),modal.show()}))},submitModalFormAjax=(className,modal)=>{const changeEvent=document.createEvent("HTMLEvents");changeEvent.initEvent("change",!0,!0),modal.getRoot().find(":input").each((function(index,element){element.dispatchEvent(changeEvent)}));const invalid=modal.getRoot().find('[aria-invalid="true"]');if(invalid.length)invalid.first().focus();else{const submittedData=modal.getRoot().find("form").serialize();_ajax.default.call([{methodname:"tool_dynamic_cohorts_submit_condition_form",args:{classname:className,jsonformdata:JSON.stringify(submittedData)},done:function(response){updateCondition(response),renderConditions(getConditions()),modal.destroy()},fail:function(){modal.setBody(getModalFormBody(className,submittedData,""))}}])}},updateCondition=data=>{let condition={...data},conditions=getConditions();condition.sortorder>=0?conditions[condition.sortorder]=condition:(conditions.push(condition),condition.sortorder=conditions.length-1),saveConditionsToRuleForm(conditions)},getConditions=()=>{let conditions=[];const conditionsjson=document.querySelector(SELECTORS_RULE_FORM_CONDITIONS_JSON).value;return""!==conditionsjson&&(conditions=JSON.parse(conditionsjson)),conditions},saveConditionsToRuleForm=conditions=>{document.querySelector(SELECTORS_RULE_FORM_CONDITIONS_JSON).setAttribute("value",JSON.stringify(conditions)),document.querySelector(SELECTORS_RULE_FORM_IS_CONDITIONS_CHANGED).setAttribute("value",1)},renderConditions=conditions=>{_templates.default.render("tool_dynamic_cohorts/conditions",{conditions:conditions}).then((function(html){document.querySelector(SELECTORS_CONDITIONS_LIST).innerHTML=html,applyConditionActions(),document.querySelector(SELECTORS_CONDITIONS_NOT_SAVED_WARNING).classList.remove("hidden")})).fail((function(){_notification.default.exception({message:"Error updating conditions"})}))},applyConditionActions=()=>{document.getElementsByClassName(SELECTORS_CONDITIONS)[0].addEventListener("click",(event=>{let element="SPAN"===event.target.tagName?event.target:event.target.parentNode;if(element.className===SELECTORS_CONDITION_DELETE_ACTION&&_notification.default.confirm((0,_str.get_string)("confirm","moodle"),(0,_str.get_string)("delete_confirm_condition","tool_dynamic_cohorts"),(0,_str.get_string)("yes","moodle"),(0,_str.get_string)("no","moodle"),(function(){let sortorder=element.dataset.sortorder,conditions=getConditions().filter((c=>c.sortorder!=sortorder)).map(((condition,index)=>({...condition,sortorder:index})));saveConditionsToRuleForm(conditions),renderConditions(conditions)})),element.className===SELECTORS_CONDITION_EDIT_ACTION){let sortorder=element.dataset.sortorder,condition=getConditions()[sortorder];displayModalForm(condition.classname,condition)}}))};_exports.init=()=>{const addButton=document.querySelector(SELECTORS_ADD_CONDITION_BUTTON),conditionSelect=document.querySelector(SELECTORS_SELECT_CONDITION);addButton.addEventListener("click",(e=>{e.preventDefault();const className=conditionSelect.value;""!==className&&displayModalForm(className,"")})),applyConditionActions()}})); + * */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_ajax=_interopRequireDefault(_ajax),_templates=_interopRequireDefault(_templates),_fragment=_interopRequireDefault(_fragment),_modal_events=_interopRequireDefault(_modal_events),_modal_save_cancel=_interopRequireDefault(_modal_save_cancel),_notification=_interopRequireDefault(_notification);const SELECTORS_ADD_CONDITION_BUTTON="#id_conditionmodalbutton",SELECTORS_SELECT_CONDITION="#id_condition",SELECTORS_CONDITIONS_LIST="#conditions",SELECTORS_RULE_FORM_CONDITIONS_JSON="#id_conditionjson",SELECTORS_RULE_FORM_IS_CONDITIONS_CHANGED="#id_isconditionschanged",SELECTORS_CONDITIONS_NOT_SAVED_WARNING="#tool-dynamic-cohorts-not-saved",SELECTORS_CONDITION_EDIT_ACTION="tool-dynamic-cohorts-condition-edit",SELECTORS_CONDITION_DELETE_ACTION="tool-dynamic-cohorts-condition-delete",SELECTORS_CONDITIONS="tool-dynamic-cohorts-conditions",getModalFormBody=(className,submittedData,defaults)=>{void 0===defaults&&(defaults="");const params={classname:className,jsonformdata:JSON.stringify(submittedData),defaults:JSON.stringify(defaults)};return _fragment.default.loadFragment("tool_dynamic_cohorts","condition_form",1,params)},displayModalForm=(className,defaults)=>{void 0===defaults&&(defaults=""),_modal_save_cancel.default.create({title:(0,_str.get_string)("conditionformtitle","tool_dynamic_cohorts"),body:getModalFormBody(className,"",defaults),large:!0}).then((function(modal){modal.getRoot().on(_modal_events.default.save,(function(e){e.preventDefault(),modal.getRoot().find("form").submit()})),modal.getRoot().on(_modal_events.default.hidden,(function(){modal.destroy()})),modal.getRoot().on("submit","form",(function(e){e.preventDefault(),submitModalFormAjax(className,modal)})),modal.show()}))},submitModalFormAjax=(className,modal)=>{const changeEvent=document.createEvent("HTMLEvents");changeEvent.initEvent("change",!0,!0),modal.getRoot().find(":input").each((function(index,element){element.dispatchEvent(changeEvent)}));const invalid=modal.getRoot().find('[aria-invalid="true"]');if(invalid.length)invalid.first().focus();else{const submittedData=modal.getRoot().find("form").serialize();_ajax.default.call([{methodname:"tool_dynamic_cohorts_submit_condition_form",args:{classname:className,jsonformdata:JSON.stringify(submittedData)},done:function(response){updateCondition(response),renderConditions(getConditions()),modal.destroy()},fail:function(){modal.setBody(getModalFormBody(className,submittedData,""))}}])}},updateCondition=data=>{let condition={...data},conditions=getConditions();condition.sortorder>=0?conditions[condition.sortorder]=condition:(conditions.push(condition),condition.sortorder=conditions.length-1),saveConditionsToRuleForm(conditions)},getConditions=()=>{let conditions=[];const conditionsjson=document.querySelector(SELECTORS_RULE_FORM_CONDITIONS_JSON).value;return""!==conditionsjson&&(conditions=JSON.parse(conditionsjson)),conditions},saveConditionsToRuleForm=conditions=>{document.querySelector(SELECTORS_RULE_FORM_CONDITIONS_JSON).setAttribute("value",JSON.stringify(conditions)),document.querySelector(SELECTORS_RULE_FORM_IS_CONDITIONS_CHANGED).setAttribute("value",1)},renderConditions=conditions=>{_templates.default.render("tool_dynamic_cohorts/conditions",{conditions:conditions}).then((function(html){document.querySelector(SELECTORS_CONDITIONS_LIST).innerHTML=html,applyConditionActions(),document.querySelector(SELECTORS_CONDITIONS_NOT_SAVED_WARNING).classList.remove("hidden")})).fail((function(){_notification.default.exception({message:"Error updating conditions"})}))},applyConditionActions=()=>{document.getElementsByClassName(SELECTORS_CONDITIONS)[0].addEventListener("click",(event=>{let element="SPAN"===event.target.tagName?event.target:event.target.parentNode;if(element.className===SELECTORS_CONDITION_DELETE_ACTION&&_notification.default.confirm((0,_str.get_string)("confirm","moodle"),(0,_str.get_string)("delete_confirm_condition","tool_dynamic_cohorts"),(0,_str.get_string)("yes","moodle"),(0,_str.get_string)("no","moodle"),(function(){let sortorder=element.dataset.sortorder,conditions=getConditions().filter((c=>c.sortorder!=sortorder)).map(((condition,index)=>({...condition,sortorder:index})));saveConditionsToRuleForm(conditions),renderConditions(conditions)})),element.className===SELECTORS_CONDITION_EDIT_ACTION){let sortorder=element.dataset.sortorder,condition=getConditions()[sortorder];displayModalForm(condition.classname,condition)}}))};_exports.init=()=>{const addButton=document.querySelector(SELECTORS_ADD_CONDITION_BUTTON),conditionSelect=document.querySelector(SELECTORS_SELECT_CONDITION);addButton.addEventListener("click",(e=>{e.preventDefault();const className=conditionSelect.value;""!==className&&displayModalForm(className,"")})),applyConditionActions()}})); //# sourceMappingURL=condition_form.min.js.map \ No newline at end of file diff --git a/amd/build/condition_form.min.js.map b/amd/build/condition_form.min.js.map index 33fc086..124443e 100644 --- a/amd/build/condition_form.min.js.map +++ b/amd/build/condition_form.min.js.map @@ -1 +1 @@ -{"version":3,"file":"condition_form.min.js","sources":["../src/condition_form.js"],"sourcesContent":["// This file is part of Moodle - https://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 .\n\n/**\n * Condition modal form.\n *\n * @module tool_dynamic_cohorts/condition_form\n * @copyright 2024 Catalyst IT\n * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * */\n\n\nimport Ajax from 'core/ajax';\nimport Templates from 'core/templates';\nimport Fragment from 'core/fragment';\nimport ModalEvents from 'core/modal_events';\nimport ModalFactory from 'core/modal_factory';\nimport Notification from 'core/notification';\nimport {get_string as getString} from 'core/str';\n\n/**\n * A list of used selectors.\n */\nconst SELECTORS = {\n ADD_CONDITION_BUTTON: '#id_conditionmodalbutton',\n SELECT_CONDITION: '#id_condition',\n CONDITIONS_LIST: '#conditions',\n RULE_FORM_CONDITIONS_JSON: '#id_conditionjson',\n RULE_FORM_IS_CONDITIONS_CHANGED: '#id_isconditionschanged',\n CONDITIONS_NOT_SAVED_WARNING: '#tool-dynamic-cohorts-not-saved',\n CONDITION_EDIT_ACTION: 'tool-dynamic-cohorts-condition-edit',\n CONDITION_DELETE_ACTION: 'tool-dynamic-cohorts-condition-delete',\n CONDITIONS: 'tool-dynamic-cohorts-conditions'\n};\n\n\n/**\n * Get modal form html body using fragment API.\n *\n * @param {string} className\n * @param {string} submittedData Submitted form data.\n * @param {any} defaults Default values for the form\n * @returns {Promise}\n */\nconst getModalFormBody = (className, submittedData, defaults) => {\n if (defaults === undefined) {\n defaults = '';\n }\n\n const params = {\n classname: className,\n jsonformdata: JSON.stringify(submittedData),\n defaults: JSON.stringify(defaults),\n };\n\n return Fragment.loadFragment('tool_dynamic_cohorts', 'condition_form', 1, params);\n};\n\n/**\n * Display Modal form.\n *\n * @param {string} className\n * @param {any} defaults Default values for the form\n */\nconst displayModalForm = (className, defaults) => {\n\n if (defaults === undefined) {\n defaults = '';\n }\n\n ModalFactory.create({\n type: ModalFactory.types.SAVE_CANCEL,\n title: getString('conditionformtitle', 'tool_dynamic_cohorts'),\n body: getModalFormBody(className, '', defaults),\n large: true,\n }).then(function (modal) {\n\n modal.getRoot().on(ModalEvents.save, function(e) {\n e.preventDefault();\n modal.getRoot().find('form').submit();\n });\n\n modal.getRoot().on(ModalEvents.hidden, function() {\n modal.destroy();\n });\n\n modal.getRoot().on('submit', 'form', function(e) {\n e.preventDefault();\n submitModalFormAjax(className, modal);\n });\n\n modal.show();\n });\n};\n\n/**\n * Submit modal form via ajax.\n *\n * @param {string} className Condition class name.\n * @param {object} modal Modal object.\n */\nconst submitModalFormAjax = (className, modal) => {\n const changeEvent = document.createEvent('HTMLEvents');\n changeEvent.initEvent('change', true, true);\n\n // Prompt all inputs to run their validation functions.\n // Normally this would happen when the form is submitted, but\n // since we aren't submitting the form normally we need to run client side\n // validation.\n modal.getRoot().find(':input').each(function(index, element) {\n element.dispatchEvent(changeEvent);\n });\n\n const invalid = modal.getRoot().find('[aria-invalid=\"true\"]');\n\n // If we found invalid fields, focus on the first one and do not submit via ajax.\n if (invalid.length) {\n invalid.first().focus();\n } else {\n const submittedData = modal.getRoot().find('form').serialize();\n\n Ajax.call([{\n methodname: 'tool_dynamic_cohorts_submit_condition_form',\n args: {classname: className, jsonformdata: JSON.stringify(submittedData)},\n done: function (response) {\n updateCondition(response);\n renderConditions(getConditions());\n modal.destroy();\n },\n fail: function () {\n modal.setBody(getModalFormBody(className, submittedData, ''));\n }\n }]);\n }\n};\n\n/**\n * Update condition with provided data.\n *\n * @param {object} data Updated condition data.\n */\nconst updateCondition = (data) => {\n let condition = {...data};\n\n let conditions = getConditions();\n\n if (condition.sortorder >= 0) {\n conditions[condition.sortorder] = condition;\n } else {\n conditions.push(condition);\n condition.sortorder = conditions.length - 1;\n }\n\n saveConditionsToRuleForm(conditions);\n};\n\n/**\n * Get a list of all conditions.\n *\n * @returns {*[]}\n */\nconst getConditions = () => {\n let conditions = [];\n const conditionsjson = document.querySelector(SELECTORS.RULE_FORM_CONDITIONS_JSON).value;\n if (conditionsjson !== '') {\n conditions = JSON.parse(conditionsjson);\n }\n return conditions;\n\n};\n\n/**\n * Save a list of conditions to a rule form element.\n *\n * @param {array} conditions A list of conditions to save\n */\nconst saveConditionsToRuleForm = (conditions) => {\n document.querySelector(SELECTORS.RULE_FORM_CONDITIONS_JSON).setAttribute('value', JSON.stringify(conditions));\n document.querySelector(SELECTORS.RULE_FORM_IS_CONDITIONS_CHANGED).setAttribute('value', 1);\n};\n\n/**\n * Display a warning that conditions are not saved.\n */\nconst displayNotSavedWarning = () => {\n document.querySelector(SELECTORS.CONDITIONS_NOT_SAVED_WARNING).classList.remove('hidden');\n};\n\n/**\n * Render conditions.\n *\n * @param {array} conditions A list of conditions to render.\n */\nconst renderConditions = (conditions) => {\n Templates.render(\n 'tool_dynamic_cohorts/conditions',\n {'conditions' : conditions}\n ).then(function(html) {\n document.querySelector(SELECTORS.CONDITIONS_LIST).innerHTML = html;\n applyConditionActions();\n displayNotSavedWarning();\n }).fail(function() {\n Notification.exception({message: 'Error updating conditions'});\n });\n};\n\n/**\n * Apply actions to conditions.\n */\nconst applyConditionActions = () => {\n document.getElementsByClassName(SELECTORS.CONDITIONS)[0].addEventListener('click', event => {\n let element = event.target.tagName === 'SPAN' ? event.target : event.target.parentNode;\n\n // On a click to a delete icon, grab the position of the selected for deleting condition\n // and remove an element of that position from the list of all existing conditions.\n // Then save updated list of conditions to the rule form and render new list on a screen.\n if (element.className === SELECTORS.CONDITION_DELETE_ACTION) {\n Notification.confirm(\n getString('confirm', 'moodle'),\n getString('delete_confirm_condition', 'tool_dynamic_cohorts'),\n getString('yes', 'moodle'),\n getString('no', 'moodle'),\n function () {\n let sortorder = element.dataset.sortorder;\n let conditions = getConditions()\n .filter(c => c.sortorder != sortorder)\n .map((condition, index) => ({...condition, sortorder: index}));\n saveConditionsToRuleForm(conditions);\n renderConditions(conditions);\n });\n }\n\n // On a click to an edit icon for a selected condition, grab condition data from the list of\n // all conditions by its position and then render modal form using the condition class.\n if (element.className === SELECTORS.CONDITION_EDIT_ACTION) {\n let sortorder = element.dataset.sortorder;\n let conditions = getConditions();\n let condition = conditions[sortorder];\n\n displayModalForm(condition.classname, condition);\n }\n });\n};\n\n/**\n * Init of the module.\n */\nexport const init = () => {\n const addButton = document.querySelector(SELECTORS.ADD_CONDITION_BUTTON);\n const conditionSelect = document.querySelector(SELECTORS.SELECT_CONDITION);\n\n addButton.addEventListener('click', (e) => {\n e.preventDefault();\n const className = conditionSelect.value;\n if (className !== '') {\n displayModalForm(className, '');\n }\n });\n applyConditionActions();\n};\n"],"names":["SELECTORS","getModalFormBody","className","submittedData","defaults","undefined","params","classname","jsonformdata","JSON","stringify","Fragment","loadFragment","displayModalForm","create","type","ModalFactory","types","SAVE_CANCEL","title","body","large","then","modal","getRoot","on","ModalEvents","save","e","preventDefault","find","submit","hidden","destroy","submitModalFormAjax","show","changeEvent","document","createEvent","initEvent","each","index","element","dispatchEvent","invalid","length","first","focus","serialize","call","methodname","args","done","response","updateCondition","renderConditions","getConditions","fail","setBody","data","condition","conditions","sortorder","push","saveConditionsToRuleForm","conditionsjson","querySelector","value","parse","setAttribute","render","html","innerHTML","applyConditionActions","classList","remove","exception","message","getElementsByClassName","addEventListener","event","target","tagName","parentNode","confirm","dataset","filter","c","map","addButton","conditionSelect"],"mappings":";;;;;;;sXAmCMA,+BACoB,2BADpBA,2BAEgB,gBAFhBA,0BAGe,cAHfA,oCAIyB,oBAJzBA,0CAK+B,0BAL/BA,uCAM4B,kCAN5BA,gCAOqB,sCAPrBA,kCAQuB,wCARvBA,qBASU,kCAYVC,iBAAmB,CAACC,UAAWC,cAAeC,iBAC/BC,IAAbD,WACAA,SAAW,UAGTE,OAAS,CACXC,UAAWL,UACXM,aAAcC,KAAKC,UAAUP,eAC7BC,SAAUK,KAAK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OAGpCuE"} \ No newline at end of file +{"version":3,"file":"condition_form.min.js","sources":["../src/condition_form.js"],"sourcesContent":["// This file is part of Moodle - https://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 .\n\n/**\n * Condition modal form.\n *\n * @module tool_dynamic_cohorts/condition_form\n * @copyright 2024 Catalyst IT\n * @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n * */\n\n\nimport Ajax from 'core/ajax';\nimport Templates from 'core/templates';\nimport Fragment from 'core/fragment';\nimport ModalEvents from 'core/modal_events';\nimport ModalSaveCancel from 'core/modal_save_cancel';\nimport Notification from 'core/notification';\nimport {get_string as getString} from 'core/str';\n\n/**\n * A list of used selectors.\n */\nconst SELECTORS = {\n ADD_CONDITION_BUTTON: '#id_conditionmodalbutton',\n SELECT_CONDITION: '#id_condition',\n CONDITIONS_LIST: '#conditions',\n RULE_FORM_CONDITIONS_JSON: '#id_conditionjson',\n RULE_FORM_IS_CONDITIONS_CHANGED: '#id_isconditionschanged',\n CONDITIONS_NOT_SAVED_WARNING: '#tool-dynamic-cohorts-not-saved',\n CONDITION_EDIT_ACTION: 'tool-dynamic-cohorts-condition-edit',\n CONDITION_DELETE_ACTION: 'tool-dynamic-cohorts-condition-delete',\n CONDITIONS: 'tool-dynamic-cohorts-conditions'\n};\n\n\n/**\n * Get modal form html body using fragment API.\n *\n * @param {string} className\n * @param {string} submittedData Submitted form data.\n * @param {any} defaults Default values for the form\n * @returns {Promise}\n */\nconst getModalFormBody = (className, submittedData, defaults) => {\n if (defaults === undefined) {\n defaults = '';\n }\n\n const params = {\n classname: className,\n jsonformdata: JSON.stringify(submittedData),\n defaults: JSON.stringify(defaults),\n };\n\n return Fragment.loadFragment('tool_dynamic_cohorts', 'condition_form', 1, params);\n};\n\n/**\n * Display Modal form.\n *\n * @param {string} className\n * @param {any} defaults Default values for the form\n */\nconst displayModalForm = (className, defaults) => {\n\n if (defaults === undefined) {\n defaults = '';\n }\n\n ModalSaveCancel.create({\n title: getString('conditionformtitle', 'tool_dynamic_cohorts'),\n body: getModalFormBody(className, '', defaults),\n large: true,\n }).then(function (modal) {\n\n modal.getRoot().on(ModalEvents.save, function(e) {\n e.preventDefault();\n modal.getRoot().find('form').submit();\n });\n\n modal.getRoot().on(ModalEvents.hidden, function() {\n modal.destroy();\n });\n\n modal.getRoot().on('submit', 'form', function(e) {\n e.preventDefault();\n submitModalFormAjax(className, modal);\n });\n\n modal.show();\n });\n};\n\n/**\n * Submit modal form via ajax.\n *\n * @param {string} className Condition class name.\n * @param {object} modal Modal object.\n */\nconst submitModalFormAjax = (className, modal) => {\n const changeEvent = document.createEvent('HTMLEvents');\n changeEvent.initEvent('change', true, true);\n\n // Prompt all inputs to run their validation functions.\n // Normally this would happen when the form is submitted, but\n // since we aren't submitting the form normally we need to run client side\n // validation.\n modal.getRoot().find(':input').each(function(index, element) {\n element.dispatchEvent(changeEvent);\n });\n\n const invalid = modal.getRoot().find('[aria-invalid=\"true\"]');\n\n // If we found invalid fields, focus on the first one and do not submit via ajax.\n if (invalid.length) {\n invalid.first().focus();\n } else {\n const submittedData = modal.getRoot().find('form').serialize();\n\n Ajax.call([{\n methodname: 'tool_dynamic_cohorts_submit_condition_form',\n args: {classname: className, jsonformdata: JSON.stringify(submittedData)},\n done: function (response) {\n updateCondition(response);\n renderConditions(getConditions());\n modal.destroy();\n },\n fail: function () {\n modal.setBody(getModalFormBody(className, submittedData, ''));\n }\n }]);\n }\n};\n\n/**\n * Update condition with provided data.\n *\n * @param {object} data Updated condition data.\n */\nconst updateCondition = (data) => {\n let condition = {...data};\n\n let conditions = getConditions();\n\n if (condition.sortorder >= 0) {\n conditions[condition.sortorder] = condition;\n } else {\n conditions.push(condition);\n condition.sortorder = conditions.length - 1;\n }\n\n saveConditionsToRuleForm(conditions);\n};\n\n/**\n * Get a list of all conditions.\n *\n * @returns {*[]}\n */\nconst getConditions = () => {\n let conditions = [];\n const conditionsjson = document.querySelector(SELECTORS.RULE_FORM_CONDITIONS_JSON).value;\n if (conditionsjson !== '') {\n conditions = JSON.parse(conditionsjson);\n }\n return conditions;\n\n};\n\n/**\n * Save a list of conditions to a rule form element.\n *\n * @param {array} conditions A list of conditions to save\n */\nconst saveConditionsToRuleForm = (conditions) => {\n document.querySelector(SELECTORS.RULE_FORM_CONDITIONS_JSON).setAttribute('value', JSON.stringify(conditions));\n document.querySelector(SELECTORS.RULE_FORM_IS_CONDITIONS_CHANGED).setAttribute('value', 1);\n};\n\n/**\n * Display a warning that conditions are not saved.\n */\nconst displayNotSavedWarning = () => {\n document.querySelector(SELECTORS.CONDITIONS_NOT_SAVED_WARNING).classList.remove('hidden');\n};\n\n/**\n * Render conditions.\n *\n * @param {array} conditions A list of conditions to render.\n */\nconst renderConditions = (conditions) => {\n Templates.render(\n 'tool_dynamic_cohorts/conditions',\n {'conditions' : conditions}\n ).then(function(html) {\n document.querySelector(SELECTORS.CONDITIONS_LIST).innerHTML = html;\n applyConditionActions();\n displayNotSavedWarning();\n }).fail(function() {\n Notification.exception({message: 'Error updating conditions'});\n });\n};\n\n/**\n * Apply actions to conditions.\n */\nconst applyConditionActions = () => {\n document.getElementsByClassName(SELECTORS.CONDITIONS)[0].addEventListener('click', event => {\n let element = event.target.tagName === 'SPAN' ? event.target : event.target.parentNode;\n\n // On a click to a delete icon, grab the position of the selected for deleting condition\n // and remove an element of that position from the list of all existing conditions.\n // Then save updated list of conditions to the rule form and render new list on a screen.\n if (element.className === SELECTORS.CONDITION_DELETE_ACTION) {\n Notification.confirm(\n getString('confirm', 'moodle'),\n getString('delete_confirm_condition', 'tool_dynamic_cohorts'),\n getString('yes', 'moodle'),\n getString('no', 'moodle'),\n function () {\n let sortorder = element.dataset.sortorder;\n let conditions = getConditions()\n .filter(c => c.sortorder != sortorder)\n .map((condition, index) => ({...condition, sortorder: index}));\n saveConditionsToRuleForm(conditions);\n renderConditions(conditions);\n });\n }\n\n // On a click to an edit icon for a selected condition, grab condition data from the list of\n // all conditions by its position and then render modal form using the condition class.\n if (element.className === SELECTORS.CONDITION_EDIT_ACTION) {\n let sortorder = element.dataset.sortorder;\n let conditions = getConditions();\n let condition = conditions[sortorder];\n\n displayModalForm(condition.classname, condition);\n }\n });\n};\n\n/**\n * Init of the module.\n */\nexport const init = () => {\n const addButton = document.querySelector(SELECTORS.ADD_CONDITION_BUTTON);\n const conditionSelect = document.querySelector(SELECTORS.SELECT_CONDITION);\n\n addButton.addEventListener('click', (e) => {\n e.preventDefault();\n const className = conditionSelect.value;\n if (className !== '') {\n displayModalForm(className, '');\n }\n });\n applyConditionActions();\n};\n"],"names":["SELECTORS","getModalFormBody","className","submittedData","defaults","undefined","params","classname","jsonformdata","JSON","stringify","Fragment","loadFragment","displayModalForm","create","title","body","large","then","modal","getRoot","on","ModalEvents","save","e","preventDefault","find","submit","hidden","destroy","submitModalFormAjax","show","changeEvent","document","createEvent","initEvent","each","index","element","dispatchEvent","invalid","length","first","focus","serialize","call","methodname","args","done","response","updateCondition","renderConditions","getConditions","fail","setBody","data","condition","conditions","sortorder","push","saveConditionsToRuleForm","conditionsjson","querySelector","value","parse","setAttribute","render","html","innerHTML","applyConditionActions","classList","remove","exception","message","getElementsByClassName","addEventListener","event","target","tagName","parentNode","confirm","dataset","filter","c","map","addButton","conditionSelect"],"mappings":";;;;;;;8XAmCMA,+BACoB,2BADpBA,2BAEgB,gBAFhBA,0BAGe,cAHfA,oCAIyB,oBAJzBA,0CAK+B,0BAL/BA,uCAM4B,kCAN5BA,gCAOqB,sCAPrBA,kCAQuB,wCARvBA,qBASU,kCAYVC,iBAAmB,CAACC,UAAWC,cAAeC,iBAC/BC,IA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EC,uBACIvB,UAAYoF,gBAAgBvB,MAChB,KAAd7D,WACAW,iBAAiBX,UAAW,OAGpCmE"} \ No newline at end of file diff --git a/amd/build/manage_rules.min.js b/amd/build/manage_rules.min.js index 81bf25e..434ab5a 100644 --- a/amd/build/manage_rules.min.js +++ b/amd/build/manage_rules.min.js @@ -1,10 +1,10 @@ -define("tool_dynamic_cohorts/manage_rules",["exports","core/ajax","core/notification","core/templates","core/modal_events","core/modal_factory","core/str","core_table/dynamic"],(function(_exports,_ajax,_notification,_templates,_modal_events,_modal_factory,_str,DynamicTable){function _getRequireWildcardCache(nodeInterop){if("function"!=typeof WeakMap)return null;var cacheBabelInterop=new WeakMap,cacheNodeInterop=new WeakMap;return(_getRequireWildcardCache=function(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop})(nodeInterop)}function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}} +define("tool_dynamic_cohorts/manage_rules",["exports","core/ajax","core/notification","core/templates","core/modal_events","core/local/modal/alert","core/str","core_table/dynamic"],(function(_exports,_ajax,_notification,_templates,_modal_events,_alert,_str,DynamicTable){function _getRequireWildcardCache(nodeInterop){if("function"!=typeof WeakMap)return null;var cacheBabelInterop=new WeakMap,cacheNodeInterop=new WeakMap;return(_getRequireWildcardCache=function(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop})(nodeInterop)}function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}} /** * Manage rules JS module. * * @module tool_dynamic_cohorts/manage_rules * @copyright 2024 Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_ajax=_interopRequireDefault(_ajax),_notification=_interopRequireDefault(_notification),_templates=_interopRequireDefault(_templates),_modal_events=_interopRequireDefault(_modal_events),_modal_factory=_interopRequireDefault(_modal_factory),DynamicTable=function(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule)return obj;if(null===obj||"object"!=typeof obj&&"function"!=typeof obj)return{default:obj};var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj))return cache.get(obj);var newObj={},hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj)if("default"!==key&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;desc&&(desc.get||desc.set)?Object.defineProperty(newObj,key,desc):newObj[key]=obj[key]}newObj.default=obj,cache&&cache.set(obj,newObj);return newObj}(DynamicTable);const SELECTORS_RULE_MATCHING_USERS="tool-dynamic-cohorts-matching-users",SELECTORS_RULE_CONDITIONS=".tool-dynamic-cohorts-condition-view";_exports.init=()=>{loadMatchingUsers(),initRuleConditionsModals(),document.addEventListener(DynamicTable.Events.tableContentRefreshed,(()=>loadMatchingUsers())),document.addEventListener(DynamicTable.Events.tableContentRefreshed,(()=>initRuleConditionsModals()))};const loadMatchingUsers=()=>{Array.from(document.getElementsByClassName(SELECTORS_RULE_MATCHING_USERS)).forEach((collection=>{const ruleid=collection.dataset.ruleid,loader=collection.children[0],link=collection.children[1];_ajax.default.call([{methodname:"tool_dynamic_cohorts_get_total_matching_users_for_rule",args:{ruleid:ruleid},done:function(number){link.children[0].append(number.toLocaleString().replace(/,/g," ")),loader.classList.add("hidden"),link.classList.remove("hidden")},fail:function(response){_notification.default.exception(response)}}])}))},initRuleConditionsModals=()=>{document.querySelectorAll(SELECTORS_RULE_CONDITIONS).forEach((link=>{let ruleid=link.dataset.ruleid;link.addEventListener("click",(function(){_ajax.default.call([{methodname:"tool_dynamic_cohorts_get_conditions",args:{ruleid:ruleid},done:function(conditions){_templates.default.render("tool_dynamic_cohorts/conditions",{conditions:conditions,hidecontrols:!0}).then((function(html){_modal_factory.default.create({type:_modal_factory.default.types.ALERT,title:(0,_str.get_string)("conditionsformtitle","tool_dynamic_cohorts"),body:html,large:!0}).then((function(modal){modal.getRoot().on(_modal_events.default.hidden,(function(){modal.destroy()})),modal.show()}))})).fail((function(response){_notification.default.exception(response)}))},fail:function(response){_notification.default.exception(response)}}])}))}))}})); + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.init=void 0,_ajax=_interopRequireDefault(_ajax),_notification=_interopRequireDefault(_notification),_templates=_interopRequireDefault(_templates),_modal_events=_interopRequireDefault(_modal_events),_alert=_interopRequireDefault(_alert),DynamicTable=function(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule)return obj;if(null===obj||"object"!=typeof obj&&"function"!=typeof obj)return{default:obj};var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj))return cache.get(obj);var newObj={},hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj)if("default"!==key&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;desc&&(desc.get||desc.set)?Object.defineProperty(newObj,key,desc):newObj[key]=obj[key]}newObj.default=obj,cache&&cache.set(obj,newObj);return newObj}(DynamicTable);const SELECTORS_RULE_MATCHING_USERS="tool-dynamic-cohorts-matching-users",SELECTORS_RULE_CONDITIONS=".tool-dynamic-cohorts-condition-view";_exports.init=()=>{loadMatchingUsers(),initRuleConditionsModals(),document.addEventListener(DynamicTable.Events.tableContentRefreshed,(()=>loadMatchingUsers())),document.addEventListener(DynamicTable.Events.tableContentRefreshed,(()=>initRuleConditionsModals()))};const loadMatchingUsers=()=>{Array.from(document.getElementsByClassName(SELECTORS_RULE_MATCHING_USERS)).forEach((collection=>{const ruleid=collection.dataset.ruleid,loader=collection.children[0],link=collection.children[1];_ajax.default.call([{methodname:"tool_dynamic_cohorts_get_total_matching_users_for_rule",args:{ruleid:ruleid},done:function(number){link.children[0].append(number.toLocaleString().replace(/,/g," ")),loader.classList.add("hidden"),link.classList.remove("hidden")},fail:function(response){_notification.default.exception(response)}}])}))},initRuleConditionsModals=()=>{document.querySelectorAll(SELECTORS_RULE_CONDITIONS).forEach((link=>{let ruleid=link.dataset.ruleid;link.addEventListener("click",(function(){_ajax.default.call([{methodname:"tool_dynamic_cohorts_get_conditions",args:{ruleid:ruleid},done:function(conditions){_templates.default.render("tool_dynamic_cohorts/conditions",{conditions:conditions,hidecontrols:!0}).then((function(html){_alert.default.create({title:(0,_str.get_string)("conditionsformtitle","tool_dynamic_cohorts"),body:html,large:!0}).then((function(modal){modal.getRoot().on(_modal_events.default.hidden,(function(){modal.destroy()})),modal.show()}))})).fail((function(response){_notification.default.exception(response)}))},fail:function(response){_notification.default.exception(response)}}])}))}))}})); //# sourceMappingURL=manage_rules.min.js.map \ No newline at end of file diff --git a/amd/build/manage_rules.min.js.map b/amd/build/manage_rules.min.js.map index 1dc8f5b..07c1076 100644 --- a/amd/build/manage_rules.min.js.map +++ b/amd/build/manage_rules.min.js.map @@ -1 +1 @@ -{"version":3,"file":"manage_rules.min.js","sources":["../src/manage_rules.js"],"sourcesContent":["// This file is part of Moodle - https://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 .\n\n/**\n * Manage rules JS module.\n *\n * @module tool_dynamic_cohorts/manage_rules\n * @copyright 2024 Catalyst IT\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Ajax from 'core/ajax';\nimport Notification from 'core/notification';\nimport Templates from 'core/templates';\nimport ModalEvents from 'core/modal_events';\nimport ModalFactory from 'core/modal_factory';\nimport {get_string as getString} from 'core/str';\nimport * as DynamicTable from 'core_table/dynamic';\n\n/**\n * A list of used selectors.\n */\nconst SELECTORS = {\n RULE_MATCHING_USERS: 'tool-dynamic-cohorts-matching-users',\n RULE_CONDITIONS: '.tool-dynamic-cohorts-condition-view',\n};\n\n/**\n * Init of the module.\n */\nexport const init = () => {\n loadMatchingUsers();\n initRuleConditionsModals();\n\n document.addEventListener(DynamicTable.Events.tableContentRefreshed, () => loadMatchingUsers());\n document.addEventListener(DynamicTable.Events.tableContentRefreshed, () => initRuleConditionsModals());\n};\n\n/**\n * Load matching users for each rule.\n */\nconst loadMatchingUsers = () => {\n Array.from(document.getElementsByClassName(SELECTORS.RULE_MATCHING_USERS)).forEach((collection) => {\n const ruleid = collection.dataset.ruleid;\n const loader = collection.children[0];\n const link = collection.children[1];\n\n Ajax.call([{\n methodname: 'tool_dynamic_cohorts_get_total_matching_users_for_rule',\n args: {ruleid: ruleid},\n done: function (number) {\n link.children[0].append(number.toLocaleString().replace(/,/g, \" \"));\n loader.classList.add('hidden');\n link.classList.remove('hidden');\n },\n fail: function (response) {\n Notification.exception(response);\n }\n }]);\n });\n};\n\n/**\n * Initialise displaying each rule conditions in a modal.\n */\nconst initRuleConditionsModals = () => {\n document.querySelectorAll(SELECTORS.RULE_CONDITIONS).forEach(link => {\n let ruleid = link.dataset.ruleid;\n link.addEventListener('click', function() {\n Ajax.call([{\n methodname: 'tool_dynamic_cohorts_get_conditions',\n args: {ruleid: ruleid},\n done: function (conditions) {\n Templates.render(\n 'tool_dynamic_cohorts/conditions',\n {'conditions' : conditions, 'hidecontrols': true}\n ).then(function(html) {\n ModalFactory.create({\n type: ModalFactory.types.ALERT,\n title: getString('conditionsformtitle', 'tool_dynamic_cohorts'),\n body: html,\n large: true,\n }).then(function (modal) {\n modal.getRoot().on(ModalEvents.hidden, function() {\n modal.destroy();\n });\n modal.show();\n });\n }).fail(function(response) {\n Notification.exception(response);\n });\n },\n fail: function (response) {\n Notification.exception(response);\n }\n }]);\n });\n });\n};\n"],"names":["SELECTORS","loadMatchingUsers","initRuleConditionsModals","document","addEventListener","DynamicTable","Events","tableContentRefreshed","Array","from","getElementsByClassName","forEach","collection","ruleid","dataset","loader","children","link","call","methodname","args","done","number","append","toLocaleString","replace","classList","add","remove","fail","response","exception","querySelectorAll","conditions","render","then","html","create","type","ModalFactory","types","ALERT","title","body","large","modal","getRoot","on","ModalEvents","hidden","destroy","show"],"mappings":";;;;;;;g/BAkCMA,8BACmB,sCADnBA,0BAEe,qDAMD,KAChBC,oBACAC,2BAEAC,SAASC,iBAAiBC,aAAaC,OAAOC,uBAAuB,IAAMN,sBAC3EE,SAASC,iBAAiBC,aAAaC,OAAOC,uBAAuB,IAAML,oCAMzED,kBAAoB,KACtBO,MAAMC,KAAKN,SAASO,uBAAuBV,gCAAgCW,SAASC,mBAC1EC,OAASD,WAAWE,QAAQD,OAC5BE,OAASH,WAAWI,SAAS,GAC7BC,KAAOL,WAAWI,SAAS,iBAE5BE,KAAK,CAAC,CACPC,WAAY,yDACZC,KAAM,CAACP,OAAQA,QACfQ,KAAM,SAAUC,QACZL,KAAKD,SAAS,GAAGO,OAAOD,OAAOE,iBAAiBC,QAAQ,KAAM,MAC9DV,OAAOW,UAAUC,IAAI,UACrBV,KAAKS,UAAUE,OAAO,WAE1BC,KAAM,SAAUC,gCACCC,UAAUD,kBASjC5B,yBAA2B,KAC7BC,SAAS6B,iBAAiBhC,2BAA2BW,SAAQM,WACrDJ,OAASI,KAAKH,QAAQD,OAC1BI,KAAKb,iBAAiB,SAAS,yBACtBc,KAAK,CAAC,CACPC,WAAY,sCACZC,KAAM,CAACP,OAAQA,QACfQ,KAAM,SAAUY,+BACFC,OACN,kCACA,YAAgBD,yBAA4B,IAC9CE,MAAK,SAASC,6BACCC,OAAO,CAChBC,KAAMC,uBAAaC,MAAMC,MACzBC,OAAO,mBAAU,sBAAuB,wBACxCC,KAAMP,KACNQ,OAAO,IACRT,MAAK,SAAUU,OACdA,MAAMC,UAAUC,GAAGC,sBAAYC,QAAQ,WACnCJ,MAAMK,aAEVL,MAAMM,aAEXtB,MAAK,SAASC,gCACAC,UAAUD,cAG/BD,KAAM,SAAUC,gCACCC,UAAUD"} \ No newline at end of file +{"version":3,"file":"manage_rules.min.js","sources":["../src/manage_rules.js"],"sourcesContent":["// This file is part of Moodle - https://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 .\n\n/**\n * Manage rules JS module.\n *\n * @module tool_dynamic_cohorts/manage_rules\n * @copyright 2024 Catalyst IT\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nimport Ajax from 'core/ajax';\nimport Notification from 'core/notification';\nimport Templates from 'core/templates';\nimport ModalEvents from 'core/modal_events';\nimport ModalAlert from \"core/local/modal/alert\";\nimport {get_string as getString} from 'core/str';\nimport * as DynamicTable from 'core_table/dynamic';\n\n/**\n * A list of used selectors.\n */\nconst SELECTORS = {\n RULE_MATCHING_USERS: 'tool-dynamic-cohorts-matching-users',\n RULE_CONDITIONS: '.tool-dynamic-cohorts-condition-view',\n};\n\n/**\n * Init of the module.\n */\nexport const init = () => {\n loadMatchingUsers();\n initRuleConditionsModals();\n\n document.addEventListener(DynamicTable.Events.tableContentRefreshed, () => loadMatchingUsers());\n document.addEventListener(DynamicTable.Events.tableContentRefreshed, () => initRuleConditionsModals());\n};\n\n/**\n * Load matching users for each rule.\n */\nconst loadMatchingUsers = () => {\n Array.from(document.getElementsByClassName(SELECTORS.RULE_MATCHING_USERS)).forEach((collection) => {\n const ruleid = collection.dataset.ruleid;\n const loader = collection.children[0];\n const link = collection.children[1];\n\n Ajax.call([{\n methodname: 'tool_dynamic_cohorts_get_total_matching_users_for_rule',\n args: {ruleid: ruleid},\n done: function (number) {\n link.children[0].append(number.toLocaleString().replace(/,/g, \" \"));\n loader.classList.add('hidden');\n link.classList.remove('hidden');\n },\n fail: function (response) {\n Notification.exception(response);\n }\n }]);\n });\n};\n\n/**\n * Initialise displaying each rule conditions in a modal.\n */\nconst initRuleConditionsModals = () => {\n document.querySelectorAll(SELECTORS.RULE_CONDITIONS).forEach(link => {\n let ruleid = link.dataset.ruleid;\n link.addEventListener('click', function() {\n Ajax.call([{\n methodname: 'tool_dynamic_cohorts_get_conditions',\n args: {ruleid: ruleid},\n done: function (conditions) {\n Templates.render(\n 'tool_dynamic_cohorts/conditions',\n {'conditions' : conditions, 'hidecontrols': true}\n ).then(function(html) {\n ModalAlert.create({\n title: getString('conditionsformtitle', 'tool_dynamic_cohorts'),\n body: html,\n large: true,\n }).then(function (modal) {\n modal.getRoot().on(ModalEvents.hidden, function() {\n modal.destroy();\n });\n modal.show();\n });\n }).fail(function(response) {\n Notification.exception(response);\n });\n },\n fail: function (response) {\n Notification.exception(response);\n }\n }]);\n });\n });\n};\n"],"names":["SELECTORS","loadMatchingUsers","initRuleConditionsModals","document","addEventListener","DynamicTable","Events","tableContentRefreshed","Array","from","getElementsByClassName","forEach","collection","ruleid","dataset","loader","children","link","call","methodname","args","done","number","append","toLocaleString","replace","classList","add","remove","fail","response","exception","querySelectorAll","conditions","render","then","html","create","title","body","large","modal","getRoot","on","ModalEvents","hidden","destroy","show"],"mappings":";;;;;;;g+BAkCMA,8BACmB,sCADnBA,0BAEe,qDAMD,KAChBC,oBACAC,2BAEAC,SAASC,iBAAiBC,aAAaC,OAAOC,uBAAuB,IAAMN,sBAC3EE,SAASC,iBAAiBC,aAAaC,OAAOC,uBAAuB,IAAML,oCAMzED,kBAAoB,KACtBO,MAAMC,KAAKN,SAASO,uBAAuBV,gCAAgCW,SAASC,mBAC1EC,OAASD,WAAWE,QAAQD,OAC5BE,OAASH,WAAWI,SAAS,GAC7BC,KAAOL,WAAWI,SAAS,iBAE5BE,KAAK,CAAC,CACPC,WAAY,yDACZC,KAAM,CAACP,OAAQA,QACfQ,KAAM,SAAUC,QACZL,KAAKD,SAAS,GAAGO,OAAOD,OAAOE,iBAAiBC,QAAQ,KAAM,MAC9DV,OAAOW,UAAUC,IAAI,UACrBV,KAAKS,UAAUE,OAAO,WAE1BC,KAAM,SAAUC,gCACCC,UAAUD,kBASjC5B,yBAA2B,KAC7BC,SAAS6B,iBAAiBhC,2BAA2BW,SAAQM,WACrDJ,OAASI,KAAKH,QAAQD,OAC1BI,KAAKb,iBAAiB,SAAS,yBACtBc,KAAK,CAAC,CACPC,WAAY,sCACZC,KAAM,CAACP,OAAQA,QACfQ,KAAM,SAAUY,+BACFC,OACN,kCACA,YAAgBD,yBAA4B,IAC9CE,MAAK,SAASC,qBACDC,OAAO,CACdC,OAAO,mBAAU,sBAAuB,wBACxCC,KAAMH,KACNI,OAAO,IACRL,MAAK,SAAUM,OACdA,MAAMC,UAAUC,GAAGC,sBAAYC,QAAQ,WACnCJ,MAAMK,aAEVL,MAAMM,aAEXlB,MAAK,SAASC,gCACAC,UAAUD,cAG/BD,KAAM,SAAUC,gCACCC,UAAUD"} \ No newline at end of file diff --git a/amd/src/condition_form.js b/amd/src/condition_form.js index 668591e..4a2b956 100644 --- a/amd/src/condition_form.js +++ b/amd/src/condition_form.js @@ -26,7 +26,7 @@ import Ajax from 'core/ajax'; import Templates from 'core/templates'; import Fragment from 'core/fragment'; import ModalEvents from 'core/modal_events'; -import ModalFactory from 'core/modal_factory'; +import ModalSaveCancel from 'core/modal_save_cancel'; import Notification from 'core/notification'; import {get_string as getString} from 'core/str'; @@ -80,8 +80,7 @@ const displayModalForm = (className, defaults) => { defaults = ''; } - ModalFactory.create({ - type: ModalFactory.types.SAVE_CANCEL, + ModalSaveCancel.create({ title: getString('conditionformtitle', 'tool_dynamic_cohorts'), body: getModalFormBody(className, '', defaults), large: true, diff --git a/amd/src/manage_rules.js b/amd/src/manage_rules.js index 91890e8..93cfe8a 100644 --- a/amd/src/manage_rules.js +++ b/amd/src/manage_rules.js @@ -25,7 +25,7 @@ import Ajax from 'core/ajax'; import Notification from 'core/notification'; import Templates from 'core/templates'; import ModalEvents from 'core/modal_events'; -import ModalFactory from 'core/modal_factory'; +import ModalAlert from "core/local/modal/alert"; import {get_string as getString} from 'core/str'; import * as DynamicTable from 'core_table/dynamic'; @@ -87,8 +87,7 @@ const initRuleConditionsModals = () => { 'tool_dynamic_cohorts/conditions', {'conditions' : conditions, 'hidecontrols': true} ).then(function(html) { - ModalFactory.create({ - type: ModalFactory.types.ALERT, + ModalAlert.create({ title: getString('conditionsformtitle', 'tool_dynamic_cohorts'), body: html, large: true,