From 27e40683aac814d7bde6ff60437f24eb38ffe283 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 18 Aug 2023 12:21:42 +0800 Subject: [PATCH] MDL-78324 report_insights: Convert actions to esm This module is converted to ESM in preparation for simplification of Modal instantiation. --- report/insights/amd/build/actions.min.js | 12 +- report/insights/amd/build/actions.min.js.map | 2 +- report/insights/amd/src/actions.js | 203 +++++++++---------- 3 files changed, 106 insertions(+), 111 deletions(-) diff --git a/report/insights/amd/build/actions.min.js b/report/insights/amd/build/actions.min.js index c56f85f5481..f5efc70ce24 100644 --- a/report/insights/amd/build/actions.min.js +++ b/report/insights/amd/build/actions.min.js @@ -1,9 +1,9 @@ +define("report_insights/actions",["exports","core/str","core/ajax","core/notification","core/url","core/modal_factory","core/modal_events"],(function(_exports,_str,_ajax,_notification,_url,_modal_factory,_modal_events){function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}} /** - * Module to manage report insights actions that are executed using AJAX. - * - * @copyright 2017 David Monllao {@link http://www.davidmonllao.com} - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -define("report_insights/actions",["jquery","core/str","core/ajax","core/notification","core/url","core/modal_factory","core/modal_events"],(function($,Str,Ajax,Notification,Url,ModalFactory,ModalEvents){return{initBulk:function(rootNode){$(rootNode+" [data-bulk-actionname]").on("click",(function(e){e.preventDefault();var action=$(e.currentTarget),actionName=action.data("bulk-actionname"),actionVisibleName=action.text().trim(),predictionIds=[],predictionContainers=[];if($('.insights-list input[data-togglegroup^="insight-bulk-action-"][data-toggle="slave"]:checked').each((function(){var container=$(this).closest("tr[data-prediction-id]");predictionContainers.push(container),predictionIds.push(container.data("prediction-id"))})),0===predictionIds.length)return this;var strings=[];return Str.get_strings([{key:"confirmbulkaction",component:"report_insights",param:{action:actionVisibleName,nitems:predictionIds.length}},{key:"confirm",component:"moodle"}]).then((function(strs){return strings=strs,ModalFactory.create({type:ModalFactory.types.SAVE_CANCEL,title:actionVisibleName,body:strings[0]})})).then((function(modal){return modal.setSaveButtonText(strings[1]),modal.show(),modal.getRoot().on(ModalEvents.save,(function(){return function(predictionIds,predictionContainers,actionName){return Ajax.call([{methodname:"report_insights_action_executed",args:{predictionids:predictionIds,actionname:actionName}}])[0].then((function(){var tableNode=!1;if(predictionContainers.forEach((function(el){!1===tableNode&&(tableNode=el.closest("table")),el.remove()})),0===tableNode.find("tbody > tr").length){let params={contextid:tableNode.closest("div.insight-container").data("context-id"),modelid:tableNode.closest("div.insight-container").data("model-id")};window.location.assign(Url.relativeUrl("report/insights/insights.php",params,!1))}})).catch(Notification.exception)}(predictionIds,predictionContainers,actionName)})),modal})).catch(Notification.exception),this}))}}})); + * Module to manage report insights actions that are executed using AJAX. + * + * @copyright 2017 David Monllao {@link http://www.davidmonllao.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */Object.defineProperty(_exports,"__esModule",{value:!0}),_exports.initBulk=void 0,_ajax=_interopRequireDefault(_ajax),_notification=_interopRequireDefault(_notification),_url=_interopRequireDefault(_url),_modal_factory=_interopRequireDefault(_modal_factory),_modal_events=_interopRequireDefault(_modal_events);const executeAction=(predictionIds,predictionContainers,actionName)=>{var predictionids,actionname;(predictionids=predictionIds,actionname=actionName,_ajax.default.call([{methodname:"report_insights_action_executed",args:{actionname:actionname,predictionids:predictionids}}])[0]).then((()=>{const tableNode=(predictionContainers=>{for(const el of predictionContainers)if(el.closest("table"))return el.closest("table");return null})(predictionContainers);if(predictionContainers.forEach((el=>el.remove())),!tableNode.querySelector("tbody > tr")){const params={contextid:tableNode.closest("div.insight-container").dataset.contextId,modelid:tableNode.closest("div.insight-container").dataset.modelId};window.location.assign(_url.default.relativeUrl("report/insights/insights.php",params,!1))}})).catch(_notification.default.exception)};_exports.initBulk=rootNode=>{document.addEventListener("click",(e=>{const action=e.target.closest("".concat(rootNode," [data-bulk-actionname]"));if(!action)return;e.preventDefault();const actionName=action.dataset.bulkActionname,actionVisibleName=action.textContent.trim(),predictionContainers=Array.from(document.querySelectorAll('.insights-list input[data-togglegroup^="insight-bulk-action-"][data-toggle="slave"]:checked')).map((checkbox=>checkbox.closest("tr[data-prediction-id]"))),predictionIds=predictionContainers.map((el=>el.dataset.predictionId));if(0===predictionIds.length)return;const stringParams={action:actionVisibleName,nitems:predictionIds.length};_modal_factory.default.create({type:_modal_factory.default.types.SAVE_CANCEL,title:actionVisibleName,body:(0,_str.get_strings)("confirmbulkaction","report_insights",stringParams),buttons:{save:(0,_str.get_strings)("confirm")},show:!0}).then((modal=>(modal.getRoot().on(_modal_events.default.save,(function(){return executeAction(predictionIds,predictionContainers,actionName)})),modal))).catch(_notification.default.exception)}))}})); //# sourceMappingURL=actions.min.js.map \ No newline at end of file diff --git a/report/insights/amd/build/actions.min.js.map b/report/insights/amd/build/actions.min.js.map index 2dcd494c4b2..d8fe33a4320 100644 --- a/report/insights/amd/build/actions.min.js.map +++ b/report/insights/amd/build/actions.min.js.map @@ -1 +1 @@ -{"version":3,"file":"actions.min.js","sources":["../src/actions.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 * Module to manage report insights actions that are executed using AJAX.\n *\n * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\n/**\n * This module manages prediction actions that require AJAX requests.\n *\n * @module report_insights/actions\n */\ndefine(['jquery', 'core/str', 'core/ajax', 'core/notification', 'core/url', 'core/modal_factory', 'core/modal_events'],\n function($, Str, Ajax, Notification, Url, ModalFactory, ModalEvents) {\n\n return {\n\n /**\n * Attach on click handlers for bulk actions.\n *\n * @param {String} rootNode\n * @access public\n */\n initBulk: function(rootNode) {\n\n /**\n * Executes the provided action.\n *\n * @param {Array} predictionIds\n * @param {Array} predictionContainers\n * @param {String} actionName\n * @return {Promise}\n */\n var executeAction = function(predictionIds, predictionContainers, actionName) {\n\n return Ajax.call([\n {\n methodname: 'report_insights_action_executed',\n args: {\n predictionids: predictionIds,\n actionname: actionName\n }\n }\n ])[0].then(function() {\n // Remove the selected elements from the list.\n\n var tableNode = false;\n predictionContainers.forEach(function(el) {\n if (tableNode === false) {\n tableNode = el.closest('table');\n }\n el.remove();\n });\n\n if (tableNode.find('tbody > tr').length === 0) {\n let params = {\n contextid: tableNode.closest('div.insight-container').data('context-id'),\n modelid: tableNode.closest('div.insight-container').data('model-id')\n };\n window.location.assign(Url.relativeUrl(\"report/insights/insights.php\", params, false));\n }\n return;\n }).catch(Notification.exception);\n };\n\n $(rootNode + ' [data-bulk-actionname]').on('click', function(e) {\n e.preventDefault();\n var action = $(e.currentTarget);\n var actionName = action.data('bulk-actionname');\n var actionVisibleName = action.text().trim();\n\n var predictionIds = [];\n var predictionContainers = [];\n\n $('.insights-list input[data-togglegroup^=\"insight-bulk-action-\"][data-toggle=\"slave\"]:checked').each(function() {\n var container = $(this).closest('tr[data-prediction-id]');\n predictionContainers.push(container);\n predictionIds.push(container.data('prediction-id'));\n });\n\n if (predictionIds.length === 0) {\n // No items selected message.\n return this;\n }\n\n var strings = [];\n Str.get_strings([{\n key: 'confirmbulkaction',\n component: 'report_insights',\n param: {\n action: actionVisibleName,\n nitems: predictionIds.length\n }\n }, {\n key: 'confirm',\n component: 'moodle'\n }]\n ).then(function(strs) {\n strings = strs;\n return ModalFactory.create({\n type: ModalFactory.types.SAVE_CANCEL,\n title: actionVisibleName,\n body: strings[0],\n });\n }).then(function(modal) {\n modal.setSaveButtonText(strings[1]);\n modal.show();\n modal.getRoot().on(ModalEvents.save, function() {\n // The action is now confirmed, sending an action for it.\n return executeAction(predictionIds, predictionContainers, actionName);\n });\n\n return modal;\n }).catch(Notification.exception);\n\n return this;\n });\n },\n };\n});\n"],"names":["define","$","Str","Ajax","Notification","Url","ModalFactory","ModalEvents","initBulk","rootNode","on","e","preventDefault","action","currentTarget","actionName","data","actionVisibleName","text","trim","predictionIds","predictionContainers","each","container","this","closest","push","length","strings","get_strings","key","component","param","nitems","then","strs","create","type","types","SAVE_CANCEL","title","body","modal","setSaveButtonText","show","getRoot","save","call","methodname","args","predictionids","actionname","tableNode","forEach","el","remove","find","params","contextid","modelid","window","location","assign","relativeUrl","catch","exception","executeAction"],"mappings":";;;;;;AA2BAA,iCAAO,CAAC,SAAU,WAAY,YAAa,oBAAqB,WAAY,qBAAsB,sBAC1F,SAASC,EAAGC,IAAKC,KAAMC,aAAcC,IAAKC,aAAcC,mBAErD,CAQHC,SAAU,SAASC,UA0CfR,EAAEQ,SAAW,2BAA2BC,GAAG,SAAS,SAASC,GACzDA,EAAEC,qBACEC,OAASZ,EAAEU,EAAEG,eACbC,WAAaF,OAAOG,KAAK,mBACzBC,kBAAoBJ,OAAOK,OAAOC,OAElCC,cAAgB,GAChBC,qBAAuB,MAE3BpB,EAAE,+FAA+FqB,MAAK,eAC9FC,UAAYtB,EAAEuB,MAAMC,QAAQ,0BAChCJ,qBAAqBK,KAAKH,WAC1BH,cAAcM,KAAKH,UAAUP,KAAK,qBAGT,IAAzBI,cAAcO,cAEPH,SAGPI,QAAU,UACd1B,IAAI2B,YAAY,CAAC,CACbC,IAAK,oBACLC,UAAW,kBACXC,MAAO,CACHnB,OAAQI,kBACRgB,OAAQb,cAAcO,SAE3B,CACCG,IAAK,UACLC,UAAW,YAEbG,MAAK,SAASC,aACZP,QAAUO,KACH7B,aAAa8B,OAAO,CACvBC,KAAM/B,aAAagC,MAAMC,YACzBC,MAAOvB,kBACPwB,KAAMb,QAAQ,QAEnBM,MAAK,SAASQ,cACbA,MAAMC,kBAAkBf,QAAQ,IAChCc,MAAME,OACNF,MAAMG,UAAUnC,GAAGH,YAAYuC,MAAM,kBA1EzB,SAAS1B,cAAeC,qBAAsBN,mBAEvDZ,KAAK4C,KAAK,CACb,CACIC,WAAY,kCACZC,KAAM,CACFC,cAAe9B,cACf+B,WAAYpC,eAGrB,GAAGmB,MAAK,eAGHkB,WAAY,KAChB/B,qBAAqBgC,SAAQ,SAASC,KAChB,IAAdF,YACAA,UAAYE,GAAG7B,QAAQ,UAE3B6B,GAAGC,YAGqC,IAAxCH,UAAUI,KAAK,cAAc7B,OAAc,KACvC8B,OAAS,CACTC,UAAWN,UAAU3B,QAAQ,yBAAyBT,KAAK,cAC3D2C,QAASP,UAAU3B,QAAQ,yBAAyBT,KAAK,aAE7D4C,OAAOC,SAASC,OAAOzD,IAAI0D,YAAY,+BAAgCN,QAAQ,QAGpFO,MAAM5D,aAAa6D,WA+CPC,CAAc9C,cAAeC,qBAAsBN,eAGvD2B,SACRsB,MAAM5D,aAAa6D,WAEfzC"} \ No newline at end of file +{"version":3,"file":"actions.min.js","sources":["../src/actions.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 * Module to manage report insights actions that are executed using AJAX.\n *\n * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\n\n/**\n * This module manages prediction actions that require AJAX requests.\n *\n * @module report_insights/actions\n */\n\nimport {get_strings as getString} from 'core/str';\nimport Ajax from 'core/ajax';\nimport Notification from 'core/notification';\nimport Url from 'core/url';\nimport ModalFactory from 'core/modal_factory';\nimport ModalEvents from 'core/modal_events';\n\n\n/**\n * Executes the provided action.\n *\n * @param {Array} predictionids\n * @param {String} actionname\n * @return {Promise}\n */\nconst markActionExecuted = (predictionids, actionname) => Ajax.call([\n {\n methodname: 'report_insights_action_executed',\n args: {\n actionname,\n predictionids,\n },\n }\n])[0];\n\nconst getPredictionTable = (predictionContainers) => {\n for (const el of predictionContainers) {\n if (el.closest('table')) {\n return el.closest('table');\n }\n }\n\n return null;\n};\n\nconst executeAction = (predictionIds, predictionContainers, actionName) => {\n markActionExecuted(predictionIds, actionName).then(() => {\n // Remove the selected elements from the list.\n const tableNode = getPredictionTable(predictionContainers);\n predictionContainers.forEach((el) => el.remove());\n\n if (!tableNode.querySelector('tbody > tr')) {\n const params = {\n contextid: tableNode.closest('div.insight-container').dataset.contextId,\n modelid: tableNode.closest('div.insight-container').dataset.modelId,\n };\n window.location.assign(Url.relativeUrl(\"report/insights/insights.php\", params, false));\n }\n return;\n }).catch(Notification.exception);\n};\n\n/**\n * Attach on click handlers for bulk actions.\n *\n * @param {String} rootNode\n * @access public\n */\nexport const initBulk = (rootNode) => {\n document.addEventListener('click', (e) => {\n const action = e.target.closest(`${rootNode} [data-bulk-actionname]`);\n if (!action) {\n return;\n }\n\n e.preventDefault();\n const actionName = action.dataset.bulkActionname;\n const actionVisibleName = action.textContent.trim();\n\n const predictionContainers = Array.from(document.querySelectorAll(\n '.insights-list input[data-togglegroup^=\"insight-bulk-action-\"][data-toggle=\"slave\"]:checked',\n )).map((checkbox) => checkbox.closest('tr[data-prediction-id]'));\n const predictionIds = predictionContainers.map((el) => el.dataset.predictionId);\n\n if (predictionIds.length === 0) {\n // No items selected message.\n return;\n }\n\n const stringParams = {\n action: actionVisibleName,\n nitems: predictionIds.length,\n };\n\n ModalFactory.create({\n type: ModalFactory.types.SAVE_CANCEL,\n title: actionVisibleName,\n body: getString('confirmbulkaction', 'report_insights', stringParams),\n buttons: {\n save: getString('confirm'),\n },\n show: true,\n }).then((modal) => {\n modal.getRoot().on(ModalEvents.save, function() {\n // The action is now confirmed, sending an action for it.\n return executeAction(predictionIds, predictionContainers, actionName);\n });\n\n return modal;\n }).catch(Notification.exception);\n });\n};\n"],"names":["executeAction","predictionIds","predictionContainers","actionName","predictionids","actionname","Ajax","call","methodname","args","then","tableNode","el","closest","getPredictionTable","forEach","remove","querySelector","params","contextid","dataset","contextId","modelid","modelId","window","location","assign","Url","relativeUrl","catch","Notification","exception","rootNode","document","addEventListener","e","action","target","preventDefault","bulkActionname","actionVisibleName","textContent","trim","Array","from","querySelectorAll","map","checkbox","predictionId","length","stringParams","nitems","create","type","ModalFactory","types","SAVE_CANCEL","title","body","buttons","save","show","modal","getRoot","on","ModalEvents"],"mappings":";;;;;;gUA+DMA,cAAgB,CAACC,cAAeC,qBAAsBC,cApBjC,IAACC,cAAeC,YAAfD,cAqBLH,cArBoBI,WAqBLF,WArBoBG,cAAKC,KAAK,CAChE,CACIC,WAAY,kCACZC,KAAM,CACFJ,WAAAA,WACAD,cAAAA,kBAGT,IAa+CM,MAAK,WAEzCC,UAbcT,CAAAA,2BACnB,MAAMU,MAAMV,wBACTU,GAAGC,QAAQ,gBACJD,GAAGC,QAAQ,gBAInB,MAMeC,CAAmBZ,yBACrCA,qBAAqBa,SAASH,IAAOA,GAAGI,YAEnCL,UAAUM,cAAc,cAAe,OAClCC,OAAS,CACXC,UAAWR,UAAUE,QAAQ,yBAAyBO,QAAQC,UAC9DC,QAASX,UAAUE,QAAQ,yBAAyBO,QAAQG,SAEhEC,OAAOC,SAASC,OAAOC,aAAIC,YAAY,+BAAgCV,QAAQ,QAGpFW,MAAMC,sBAAaC,8BASDC,WACrBC,SAASC,iBAAiB,SAAUC,UAC1BC,OAASD,EAAEE,OAAOxB,kBAAWmB,yCAC9BI,cAILD,EAAEG,uBACInC,WAAaiC,OAAOhB,QAAQmB,eAC5BC,kBAAoBJ,OAAOK,YAAYC,OAEvCxC,qBAAuByC,MAAMC,KAAKX,SAASY,iBAC7C,gGACDC,KAAKC,UAAaA,SAASlC,QAAQ,4BAChCZ,cAAgBC,qBAAqB4C,KAAKlC,IAAOA,GAAGQ,QAAQ4B,kBAErC,IAAzB/C,cAAcgD,oBAKZC,aAAe,CACjBd,OAAQI,kBACRW,OAAQlD,cAAcgD,+BAGbG,OAAO,CAChBC,KAAMC,uBAAaC,MAAMC,YACzBC,MAAOjB,kBACPkB,MAAM,oBAAU,oBAAqB,kBAAmBR,cACxDS,QAAS,CACLC,MAAM,oBAAU,YAEpBC,MAAM,IACPnD,MAAMoD,QACLA,MAAMC,UAAUC,GAAGC,sBAAYL,MAAM,kBAE1B5D,cAAcC,cAAeC,qBAAsBC,eAGvD2D,SACRjC,MAAMC,sBAAaC"} \ No newline at end of file diff --git a/report/insights/amd/src/actions.js b/report/insights/amd/src/actions.js index 206b1203beb..ef426e8f6de 100644 --- a/report/insights/amd/src/actions.js +++ b/report/insights/amd/src/actions.js @@ -25,111 +25,106 @@ * * @module report_insights/actions */ -define(['jquery', 'core/str', 'core/ajax', 'core/notification', 'core/url', 'core/modal_factory', 'core/modal_events'], - function($, Str, Ajax, Notification, Url, ModalFactory, ModalEvents) { - return { +import {get_strings as getString} from 'core/str'; +import Ajax from 'core/ajax'; +import Notification from 'core/notification'; +import Url from 'core/url'; +import ModalFactory from 'core/modal_factory'; +import ModalEvents from 'core/modal_events'; - /** - * Attach on click handlers for bulk actions. - * - * @param {String} rootNode - * @access public - */ - initBulk: function(rootNode) { - /** - * Executes the provided action. - * - * @param {Array} predictionIds - * @param {Array} predictionContainers - * @param {String} actionName - * @return {Promise} - */ - var executeAction = function(predictionIds, predictionContainers, actionName) { - - return Ajax.call([ - { - methodname: 'report_insights_action_executed', - args: { - predictionids: predictionIds, - actionname: actionName - } - } - ])[0].then(function() { - // Remove the selected elements from the list. - - var tableNode = false; - predictionContainers.forEach(function(el) { - if (tableNode === false) { - tableNode = el.closest('table'); - } - el.remove(); - }); - - if (tableNode.find('tbody > tr').length === 0) { - let params = { - contextid: tableNode.closest('div.insight-container').data('context-id'), - modelid: tableNode.closest('div.insight-container').data('model-id') - }; - window.location.assign(Url.relativeUrl("report/insights/insights.php", params, false)); - } - return; - }).catch(Notification.exception); - }; - - $(rootNode + ' [data-bulk-actionname]').on('click', function(e) { - e.preventDefault(); - var action = $(e.currentTarget); - var actionName = action.data('bulk-actionname'); - var actionVisibleName = action.text().trim(); - - var predictionIds = []; - var predictionContainers = []; - - $('.insights-list input[data-togglegroup^="insight-bulk-action-"][data-toggle="slave"]:checked').each(function() { - var container = $(this).closest('tr[data-prediction-id]'); - predictionContainers.push(container); - predictionIds.push(container.data('prediction-id')); - }); - - if (predictionIds.length === 0) { - // No items selected message. - return this; - } - - var strings = []; - Str.get_strings([{ - key: 'confirmbulkaction', - component: 'report_insights', - param: { - action: actionVisibleName, - nitems: predictionIds.length - } - }, { - key: 'confirm', - component: 'moodle' - }] - ).then(function(strs) { - strings = strs; - return ModalFactory.create({ - type: ModalFactory.types.SAVE_CANCEL, - title: actionVisibleName, - body: strings[0], - }); - }).then(function(modal) { - modal.setSaveButtonText(strings[1]); - modal.show(); - modal.getRoot().on(ModalEvents.save, function() { - // The action is now confirmed, sending an action for it. - return executeAction(predictionIds, predictionContainers, actionName); - }); - - return modal; - }).catch(Notification.exception); - - return this; - }); +/** + * Executes the provided action. + * + * @param {Array} predictionids + * @param {String} actionname + * @return {Promise} + */ +const markActionExecuted = (predictionids, actionname) => Ajax.call([ + { + methodname: 'report_insights_action_executed', + args: { + actionname, + predictionids, }, - }; -}); + } +])[0]; + +const getPredictionTable = (predictionContainers) => { + for (const el of predictionContainers) { + if (el.closest('table')) { + return el.closest('table'); + } + } + + return null; +}; + +const executeAction = (predictionIds, predictionContainers, actionName) => { + markActionExecuted(predictionIds, actionName).then(() => { + // Remove the selected elements from the list. + const tableNode = getPredictionTable(predictionContainers); + predictionContainers.forEach((el) => el.remove()); + + if (!tableNode.querySelector('tbody > tr')) { + const params = { + contextid: tableNode.closest('div.insight-container').dataset.contextId, + modelid: tableNode.closest('div.insight-container').dataset.modelId, + }; + window.location.assign(Url.relativeUrl("report/insights/insights.php", params, false)); + } + return; + }).catch(Notification.exception); +}; + +/** + * Attach on click handlers for bulk actions. + * + * @param {String} rootNode + * @access public + */ +export const initBulk = (rootNode) => { + document.addEventListener('click', (e) => { + const action = e.target.closest(`${rootNode} [data-bulk-actionname]`); + if (!action) { + return; + } + + e.preventDefault(); + const actionName = action.dataset.bulkActionname; + const actionVisibleName = action.textContent.trim(); + + const predictionContainers = Array.from(document.querySelectorAll( + '.insights-list input[data-togglegroup^="insight-bulk-action-"][data-toggle="slave"]:checked', + )).map((checkbox) => checkbox.closest('tr[data-prediction-id]')); + const predictionIds = predictionContainers.map((el) => el.dataset.predictionId); + + if (predictionIds.length === 0) { + // No items selected message. + return; + } + + const stringParams = { + action: actionVisibleName, + nitems: predictionIds.length, + }; + + ModalFactory.create({ + type: ModalFactory.types.SAVE_CANCEL, + title: actionVisibleName, + body: getString('confirmbulkaction', 'report_insights', stringParams), + buttons: { + save: getString('confirm'), + }, + show: true, + }).then((modal) => { + modal.getRoot().on(ModalEvents.save, function() { + // The action is now confirmed, sending an action for it. + return executeAction(predictionIds, predictionContainers, actionName); + }); + + return modal; + }).catch(Notification.exception); + }); +};