From b2b0bdd14efdda2c8d379b0db22077fdccd6eea7 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 23 Jun 2021 14:04:05 +0800 Subject: [PATCH 1/2] MDL-51202 groups: Update group edit/delete management --- group/amd/build/managegrouplist.min.js | Bin 0 -> 748 bytes group/amd/build/managegrouplist.min.js.map | Bin 0 -> 4199 bytes group/amd/src/managegrouplist.js | 70 +++++++++++++++++++++ group/classes/output/index_page.php | 21 +++++-- group/module.js | 61 ++---------------- group/templates/index.mustache | 15 +++-- 6 files changed, 100 insertions(+), 67 deletions(-) create mode 100644 group/amd/build/managegrouplist.min.js create mode 100644 group/amd/build/managegrouplist.min.js.map create mode 100644 group/amd/src/managegrouplist.js diff --git a/group/amd/build/managegrouplist.min.js b/group/amd/build/managegrouplist.min.js new file mode 100644 index 0000000000000000000000000000000000000000..e78cb2a071b169aa66474e486226682563e8d53b GIT binary patch literal 748 zcmcJNK}*Ci5QXplind-72wA;adJx=$peTwbk!6}pyOB1jlVouz{qLqLWmOQocnNdJ zyqRy_)7Th`87q_{+_dE4u1J{01)!1uvX@WqR0qE*2-%YctX^qBcTMunoh zt6xzCsSkqp9I?zpq{wEFOq!JPQ@N=kZ%!b|9JT3 zh}B$btXDKrG|7^Lx62_?(`JTY(och*{)WmJi*ong0pTI^g5$!2qt{Ys>x%oo7wm+J#B zQG(X%U2^6o1r9Az3gi}SnsS79&V~1;g!9y K^3}^8hLaDM>AKs=5^bxMNSC6LdAYeDg ztR{UdrK!|0tt2g?X(kf~GGvlBLDGVuNeYFQktyVW^(T>y3Q0`TqMM|)l1Nobeb*jY zvo%VR?gf%3M@c0OIW~`$%T&#A1zHaWq6~(@3kCk@nFsXjdt)KOkeUbdIOpbDYNoFH zJ@-5~@CG8>>+**Khm4(U1O-l2CagbBQ^5RU^EdBU3WkW>o> zJoerQF<2vaTmfP?bGc7q2akwizapU<{tkpm3{WD>kP6OxQ-MT6OsH7~jCH|W9)=Og z{K)!Cwmp1BlXt0o_@R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YkL1iRNwOs%v2`s8<@6lZPZF6mV7G;H`MQ}1Kwc&wAs*y9&mPXZXtQQeEhIH3} z%Z22$NlCD#SuZ}F6}(DRd{+crr4Q4!I}rI8k|bv zPJMx>BiXO65&jzC+KE?9NPn?6@T}vT$Sr5o7nJ{tFKWJ9T(LFyws;TaHp8#Esu8l= zv+|Fn#&V;I8@#@ulbQrn-{1aSs=LmZi2a#tb1hP9Y*znz$iIDsY~PI+k($f99<. + +/** + * Manage the group list. + * + * @module core_group/managegrouplis + * @copyright 2021 Andrew Nicols + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +const checkSelectedGroups = (groupList, deleteGroupSelector, modifyMembersSelector) => { + const isAnySelected = Array.from(groupList.options).some(element => element.selected); + + const setDeleteButtonState = state => { + document.querySelector(deleteGroupSelector).disabled = !state; + }; + + const setModifyButtonState = state => { + document.querySelector(modifyMembersSelector).disabled = !state; + }; + + + if (!isAnySelected) { + // If no group is selected, then disable both buttons. + setDeleteButtonState(false); + setModifyButtonState(false); + + return; + } + + // Check whether any selected element is deletable. + const isDeletable = !Array.from(groupList.options).some(element => { + if (!element.selected) { + return false; + } + return !parseInt(element.dataset.isDeletable); + }); + + // Check whether any selected element is editable. + const isEditable = !Array.from(groupList.options).some(element => { + if (!element.selected) { + return false; + } + return !parseInt(element.dataset.isEditable); + }); + + setDeleteButtonState(isDeletable); + setModifyButtonState(isEditable); +}; + +export const init = (groupListSelector, deleteGroupSelector, modifyMembersSelector) => { + const groupList = document.querySelector(groupListSelector); + + groupList.addEventListener('change', () => { + checkSelectedGroups(groupList, deleteGroupSelector, modifyMembersSelector); + }); +}; diff --git a/group/classes/output/index_page.php b/group/classes/output/index_page.php index a0069a6b5d..0b3d51ab6e 100644 --- a/group/classes/output/index_page.php +++ b/group/classes/output/index_page.php @@ -86,6 +86,20 @@ class index_page implements renderable, templatable { $this->uneditablemembersgroups = $uneditablemembersgroups; } + protected function get_group_data(): array { + return array_map(function($groupdata) { + $groupdata->deletable = '1'; + $groupdata->editable = '1'; + if (array_key_exists($groupdata->value, $this->undeletablegroups)) { + $groupdata->deletable = '0'; + } + if (array_key_exists($groupdata->value, $this->uneditablemembersgroups)) { + $groupdata->editable = '0'; + } + return $groupdata; + }, $this->groups); + } + /** * Export the data. * @@ -100,17 +114,12 @@ class index_page implements renderable, templatable { // Variables that will be passed to the JS helper. $data->courseid = $this->courseid; $data->wwwroot = $CFG->wwwroot; - // To be passed to the JS init script in the template. Encode as a JSON string. - $data->undeletablegroups = json_encode($this->undeletablegroups); - - // To be passed to the JS init script in the template. Encode as a JSON string. - $data->uneditablemembersgroups = json_encode($this->uneditablemembersgroups); // Some buttons are enabled if single group selected. $data->addmembersdisabled = $this->disableaddedit; $data->editgroupsettingsdisabled = $this->disableaddedit; $data->deletegroupdisabled = $this->disabledelete; - $data->groups = $this->groups; + $data->groups = $this->get_group_data(); $data->members = $this->selectedgroupmembers; $data->selectedgroup = $this->selectedgroupname; diff --git a/group/module.js b/group/module.js index cacaa78280..2bc72c4140 100644 --- a/group/module.js +++ b/group/module.js @@ -3,61 +3,8 @@ * and open the template in the editor. */ M.core_group = { - hoveroverlay : null -}; - -M.core_group.init_index = function(Y, wwwroot, courseid) { - M.core_group.groupsCombo = new UpdatableGroupsCombo(wwwroot, courseid); - M.core_group.membersCombo = new UpdatableMembersCombo(wwwroot, courseid); -}; - -M.core_group.groupslist = function(Y, preventgroupremoval, preventgroupedit) { - var actions = { - init : function() { - // We need to add check_deletable both on change for the groups, and then call it the first time the page loads - Y.one('#groups').on('change', this.check_deletable, this); - Y.one('#groups').on('change', this.checkMemberseditable, this); - this.check_deletable(); - this.checkMemberseditable(); - }, - check_deletable : function() { - // Ensure that if the 'preventremoval' attribute is set, the delete button is greyed out - var candelete = true; - var optionselected = false; - Y.one('#groups').get('options').each(function(option) { - if (option.get('selected')) { - optionselected = true; - if (option.getAttribute('value') in preventgroupremoval) { - candelete = false; - } - } - }, this); - var deletebutton = Y.one('#deletegroup'); - if (candelete && optionselected) { - deletebutton.removeAttribute('disabled'); - } else { - deletebutton.setAttribute('disabled', 'disabled'); - } - }, - checkMemberseditable: function() { - // Ensure that if the 'preventremoval' attribute is set, the edit button is greyed out - var canedit = true; - var optionselected = false; - Y.one('#groups').get('options').each(function(option) { - if (option.get('selected')) { - optionselected = true; - if (option.getAttribute('value') in preventgroupedit) { - canedit = false; - } - } - }, this); - var addmembersbutton = Y.one('#showaddmembersform'); - if (canedit && optionselected) { - addmembersbutton.removeAttribute('disabled'); - } else { - addmembersbutton.setAttribute('disabled', 'disabled'); - } - } - } - actions.init(); + init_index: function(Y, wwwroot, courseid) { + M.core_group.groupsCombo = new UpdatableGroupsCombo(wwwroot, courseid); + M.core_group.membersCombo = new UpdatableMembersCombo(wwwroot, courseid); + }, }; diff --git a/group/templates/index.mustache b/group/templates/index.mustache index f9d4fddfb9..5483dd5c79 100644 --- a/group/templates/index.mustache +++ b/group/templates/index.mustache @@ -87,7 +87,13 @@ @@ -134,13 +140,14 @@ {{#js}} +require(['core_group/managegrouplist'], function(manageGroups) { + manageGroups.init('#groups', '#deletegroup', '#showaddmembersform'); +}); + require(['jquery', 'core/yui'], function($) { $("#groups").change(function() { M.core_group.membersCombo.refreshMembers(); }); M.core_group.init_index(Y, "{{wwwroot}}", {{courseid}}); - var undeletableGroups = JSON.parse('{{{undeletablegroups}}}'); - var uneditableGroups = JSON.parse('{{{uneditablemembersgroups}}}'); - M.core_group.groupslist(Y, undeletableGroups, uneditableGroups); }); {{/js}} -- 2.30.0