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.
This commit is contained in:
Mihail Geshoski
2023-09-20 23:53:43 +02:00
parent 662a2fca9c
commit c2628ac3b9
4 changed files with 134 additions and 0 deletions
+11
View File
@@ -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 <mihail@moodle.com>
* @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
@@ -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 <http://www.gnu.org/licenses/>.\n\n/**\n * Base class for defining a bulk action.\n *\n * @module core/bulkactions/bulk_action\n * @copyright 2023 Mihail Geshoski <[email protected]>\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"}
+85
View File
@@ -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 <http://www.gnu.org/licenses/>.
/**
* Base class for defining a bulk action.
*
* @module core/bulkactions/bulk_action
* @copyright 2023 Mihail Geshoski <[email protected]>
* @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}`);
}
}
@@ -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 <http://www.gnu.org/licenses/>.
}}
{{!
@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):
{
}
}}
<button
type="button"
class="btn py-0 d-flex flex-column"
data-action="{{$action}}{{/action}}"
title="{{$title}}{{/title}}"
>
<span class="w-100 pl-2">{{$icon}}{{/icon}}</span>
<span>{{$title}}{{/title}}</span>
</button>