diff --git a/lib/javascript-static.js b/lib/javascript-static.js index 92150820e54..d55e0c27ed6 100644 --- a/lib/javascript-static.js +++ b/lib/javascript-static.js @@ -1,3 +1,4 @@ +/* eslint-disable camelcase */ // Miscellaneous core Javascript functions for Moodle // Global M object is initilised in inline javascript @@ -236,91 +237,78 @@ M.util.set_user_preference = function(name, value) { * @param {String} [args.cancellabel] The label to use on the cancel button. * @param {String} [args.continuelabel] The label to use on the continue button. */ -M.util.show_confirm_dialog = function(e, args) { - var target = e.target; +M.util.show_confirm_dialog = (e, { + message, + continuelabel, + callback = null, + scope = null, + callbackargs = [], +} = {}) => { if (e.preventDefault) { e.preventDefault(); } - require(['core_form/changechecker'], function(FormChangeChecker) { - // eslint-disable-next-line no-undef, new-cap - YUI().use('moodle-core-notification-confirm', function(Y) { - var confirmationDialogue = new M.core.confirm({ - width: '300px', - center: true, - modal: true, - visible: false, - draggable: false, - title: M.util.get_string('confirmation', 'admin'), - noLabel: M.util.get_string('cancel', 'moodle'), - question: args.message - }); + require( + ['core/notification', 'core/str', 'core_form/changechecker', 'core/normalise'], + function(Notification, Str, FormChangeChecker, Normalise) { - // The dialogue was submitted with a positive value indication. - confirmationDialogue.on('complete-yes', function(e) { - // Handle any callbacks. - if (args.callback) { - if (!Y.Lang.isFunction(args.callback)) { - Y.log('Callbacks to show_confirm_dialog must now be functions. ' + - 'Please update your code to pass in a function instead.', - 'warn', 'M.util.show_confirm_dialog'); - return; - } + if (scope === null && e.target) { + // Fall back to the event target if no scope provided. + scope = e.target; + } - var scope = e.target; - if (Y.Lang.isObject(args.scope)) { - scope = args.scope; - } - - var callbackargs = args.callbackargs || []; - args.callback.apply(scope, callbackargs); + Notification.saveCancelPromise( + Str.get_string('confirmation', 'admin'), + message, + continuelabel || Str.get_string('yes', 'moodle'), + ) + .then(() => { + if (callback) { + callback.apply(scope, callbackargs); return; } - var targetancestor = null, - targetform = null; - - if (target.test('a')) { - window.location = target.get('href'); - - } else if ((targetancestor = target.ancestor('a')) !== null) { - window.location = targetancestor.get('href'); - - } else if (target.test('input') || target.test('button')) { - targetform = target.ancestor('form', true); - if (!targetform) { - return; - } - if (target.get('name') && target.get('value')) { - targetform.append(''); - FormChangeChecker.markFormAsDirty(targetform.getDOMNode()); - } - targetform.submit(); - - } else if (target.test('form')) { - FormChangeChecker.markFormAsDirty(target.getDOMNode()); - target.submit(); - - } else { - Y.log("Element of type " + target.get('tagName') + - " is not supported by the M.util.show_confirm_dialog function. Use A, INPUT, BUTTON or FORM", - 'warn', 'javascript-static'); + if (!e.target) { + window.console.error( + `M.util.show_confirm_dialog: No target found for event`, + e + ); + return; } - }, this); - if (args.cancellabel) { - confirmationDialogue.set('noLabel', args.cancellabel); - } + const target = Normalise.getElement(e.target); - if (args.continuelabel) { - confirmationDialogue.set('yesLabel', args.continuelabel); - } + if (target.closest('a')) { + window.location = target.closest('a').getAttribute('href'); + return; + } else if (target.closest('input') || target.closest('button')) { + const form = target.closest('form'); + const hiddenValue = document.createElement('input'); + hiddenValue.setAttribute('type', 'hidden'); + hiddenValue.setAttribute('name', target.getAttribute('name')); + hiddenValue.setAttribute('value', target.getAttribute('value')); + form.appendChild(hiddenValue); + FormChangeChecker.markFormAsDirty(form); + form.submit(); + return; + } else if (target.closest('form')) { + const form = target.closest('form'); + FormChangeChecker.markFormAsDirty(form); + form.submit(); + return; + } + window.console.error( + `Element of type ${target.tagName} is not supported by M.util.show_confirm_dialog.` + ); - confirmationDialogue.render() - .show(); - }); - }); + return; + }) + .catch(() => { + // User cancelled. + return; + }); + } + ); }; /** Useful for full embedding of various stuff */ diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 7df51950287..cbf4bab2854 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -54,6 +54,8 @@ information provided here is intended especially for developers. * New method moodleform::filter_shown_headers() is created to show some expanded headers only and hide the rest. * count_words() and count_letters() have a new optional parameter called $format to format the text before doing the counting. * New core_renderer::sr_text method to generate screen reader only inline texts without using html_writter. +* The M.util.show_confirm_dialog function has been rewritten as an ESM to use the core/notification module instead. + See MDL-77174 for further information. === 4.2 ===