mirror of
https://github.com/moodle/moodle.git
synced 2025-01-29 11:46:19 +01:00
MDL-77174 js: Update M.util.show_confirm_dialog to use ESM/AMD
This commit updates the M.util.show_confirm_dialog function to modify its underlying behaviour to use the core/notification::saveCancelPromise features. This will allow us to work towards deprecating moodle-core-notification-confirm
This commit is contained in:
parent
42d3e7de14
commit
bee509fb81
@ -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('<input type="hidden" name="' + target.get('name') +
|
||||
'" value="' + target.get('value') + '">');
|
||||
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 */
|
||||
|
@ -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 ===
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user