From c2628ac3b9b2eb84f27cdf6dc6a533edeb17ac7d Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Mon, 31 Jul 2023 10:51:27 +0200 Subject: [PATCH] MDL-77035 core: Base js class for a bulk action Implements a base js class that each individual bulk action implementation needs to extend. This class contains all common event listeners, methods (including abstract) that each implementation need to use. Also, it introduces a default mustache template for the bulk action trigger elements. --- lib/amd/build/bulkactions/bulk_action.min.js | 11 +++ .../build/bulkactions/bulk_action.min.js.map | 1 + lib/amd/src/bulkactions/bulk_action.js | 85 +++++++++++++++++++ .../bulkactions/bulk_action_trigger.mustache | 37 ++++++++ 4 files changed, 134 insertions(+) create mode 100644 lib/amd/build/bulkactions/bulk_action.min.js create mode 100644 lib/amd/build/bulkactions/bulk_action.min.js.map create mode 100644 lib/amd/src/bulkactions/bulk_action.js create mode 100644 lib/templates/bulkactions/bulk_action_trigger.mustache diff --git a/lib/amd/build/bulkactions/bulk_action.min.js b/lib/amd/build/bulkactions/bulk_action.min.js new file mode 100644 index 00000000000..c2735e5a80f --- /dev/null +++ b/lib/amd/build/bulkactions/bulk_action.min.js @@ -0,0 +1,11 @@ +define("core/bulkactions/bulk_action",["exports"],(function(_exports){Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.default=void 0;return _exports.default= +/** + * Base class for defining a bulk action. + * + * @module core/bulkactions/bulk_action + * @copyright 2023 Mihail Geshoski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class{constructor(){var obj,key,value;value=[],(key="selectedItems")in(obj=this)?Object.defineProperty(obj,key,{value:value,enumerable:!0,configurable:!0,writable:!0}):obj[key]=value}registerListenerEvents(containerElement){containerElement.addEventListener("click",(e=>{e.target.closest(this.getBulkActionTriggerSelector())&&(e.preventDefault(),this.triggerBulkAction())}))}setSelectedItems(selectedItems){this.selectedItems=selectedItems}getBulkActionTriggerSelector(){throw new Error("getBulkActionTriggerSelector() must be implemented in ".concat(this.constructor.name))}triggerBulkAction(){throw new Error("triggerBulkAction() must be implemented in ".concat(this.constructor.name))}renderBulkActionTrigger(){throw new Error("renderBulkActionTrigger() must be implemented in ".concat(this.constructor.name))}},_exports.default})); + +//# sourceMappingURL=bulk_action.min.js.map \ No newline at end of file diff --git a/lib/amd/build/bulkactions/bulk_action.min.js.map b/lib/amd/build/bulkactions/bulk_action.min.js.map new file mode 100644 index 00000000000..76c593a6b84 --- /dev/null +++ b/lib/amd/build/bulkactions/bulk_action.min.js.map @@ -0,0 +1 @@ +{"version":3,"file":"bulk_action.min.js","sources":["../../src/bulkactions/bulk_action.js"],"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 .\n\n/**\n * Base class for defining a bulk action.\n *\n * @module core/bulkactions/bulk_action\n * @copyright 2023 Mihail Geshoski \n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\nexport default class BulkAction {\n\n /** @property {array} selectedItems The array of selected item elements. */\n selectedItems = [];\n\n /**\n * Registers the listener events for the bulk actions.\n *\n * @method registerListenerEvents\n * @param {HTMLElement} containerElement The container element for the bulk actions.\n * @returns {void}\n */\n registerListenerEvents(containerElement) {\n // Listen for the click event on the bulk action trigger element.\n containerElement.addEventListener('click', (e) => {\n if (e.target.closest(this.getBulkActionTriggerSelector())) {\n e.preventDefault();\n this.triggerBulkAction();\n }\n });\n }\n\n /**\n * Setter method for the selectedItems property.\n *\n * @method setSelectedItems\n * @param {Array} selectedItems The array of selected item elements..\n * @returns {void}\n */\n setSelectedItems(selectedItems) {\n this.selectedItems = selectedItems;\n }\n\n /**\n * Defines the selector of the element that triggers the bulk action.\n *\n * @method getBulkActionTriggerSelector\n * @returns {string}\n */\n getBulkActionTriggerSelector() {\n throw new Error(`getBulkActionTriggerSelector() must be implemented in ${this.constructor.name}`);\n }\n\n /**\n * Defines the behavior once the bulk action is triggered.\n *\n * @method triggerBulkAction\n */\n triggerBulkAction() {\n throw new Error(`triggerBulkAction() must be implemented in ${this.constructor.name}`);\n }\n\n /**\n * Renders the bulk action trigger element.\n *\n * @method renderBulkActionTrigger\n * @returns {Promise}\n */\n renderBulkActionTrigger() {\n throw new Error(`renderBulkActionTrigger() must be implemented in ${this.constructor.name}`);\n }\n}\n"],"names":["registerListenerEvents","containerElement","addEventListener","e","target","closest","this","getBulkActionTriggerSelector","preventDefault","triggerBulkAction","setSelectedItems","selectedItems","Error","constructor","name","renderBulkActionTrigger"],"mappings":";;;;;;;;4CA0BoB,2IAShBA,uBAAuBC,kBAEnBA,iBAAiBC,iBAAiB,SAAUC,IACpCA,EAAEC,OAAOC,QAAQC,KAAKC,kCACtBJ,EAAEK,sBACGC,wBAYjBC,iBAAiBC,oBACRA,cAAgBA,cASzBJ,qCACU,IAAIK,sEAA+DN,KAAKO,YAAYC,OAQ9FL,0BACU,IAAIG,2DAAoDN,KAAKO,YAAYC,OASnFC,gCACU,IAAIH,iEAA0DN,KAAKO,YAAYC"} \ No newline at end of file diff --git a/lib/amd/src/bulkactions/bulk_action.js b/lib/amd/src/bulkactions/bulk_action.js new file mode 100644 index 00000000000..17822623f41 --- /dev/null +++ b/lib/amd/src/bulkactions/bulk_action.js @@ -0,0 +1,85 @@ +// 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 . + +/** + * Base class for defining a bulk action. + * + * @module core/bulkactions/bulk_action + * @copyright 2023 Mihail Geshoski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +export default class BulkAction { + + /** @property {array} selectedItems The array of selected item elements. */ + selectedItems = []; + + /** + * Registers the listener events for the bulk actions. + * + * @method registerListenerEvents + * @param {HTMLElement} containerElement The container element for the bulk actions. + * @returns {void} + */ + registerListenerEvents(containerElement) { + // Listen for the click event on the bulk action trigger element. + containerElement.addEventListener('click', (e) => { + if (e.target.closest(this.getBulkActionTriggerSelector())) { + e.preventDefault(); + this.triggerBulkAction(); + } + }); + } + + /** + * Setter method for the selectedItems property. + * + * @method setSelectedItems + * @param {Array} selectedItems The array of selected item elements.. + * @returns {void} + */ + setSelectedItems(selectedItems) { + this.selectedItems = selectedItems; + } + + /** + * Defines the selector of the element that triggers the bulk action. + * + * @method getBulkActionTriggerSelector + * @returns {string} + */ + getBulkActionTriggerSelector() { + throw new Error(`getBulkActionTriggerSelector() must be implemented in ${this.constructor.name}`); + } + + /** + * Defines the behavior once the bulk action is triggered. + * + * @method triggerBulkAction + */ + triggerBulkAction() { + throw new Error(`triggerBulkAction() must be implemented in ${this.constructor.name}`); + } + + /** + * Renders the bulk action trigger element. + * + * @method renderBulkActionTrigger + * @returns {Promise} + */ + renderBulkActionTrigger() { + throw new Error(`renderBulkActionTrigger() must be implemented in ${this.constructor.name}`); + } +} diff --git a/lib/templates/bulkactions/bulk_action_trigger.mustache b/lib/templates/bulkactions/bulk_action_trigger.mustache new file mode 100644 index 00000000000..b380263f4cb --- /dev/null +++ b/lib/templates/bulkactions/bulk_action_trigger.mustache @@ -0,0 +1,37 @@ +{{! + 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 . +}} +{{! + @template core/bulkactions/bulk_action_trigger + + Renders a trigger element for a given bulk action. + + Context variables required for this template: + * none + + Example context (json): + { + } +}} +