MDL-78324 report_insights: Convert actions to esm
This module is converted to ESM in preparation for simplification of Modal instantiation.
This commit is contained in:
+6
-6
@@ -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
|
||||
File diff suppressed because one or more lines are too long
@@ -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);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user