MDL-42428 Javascript: Switch M.util.show_confirm_dialog to use moodle-core-notification

This commit is contained in:
Andrew Nicols 2014-01-28 14:59:21 +08:00
parent 5c876ed21f
commit ab20c84aff

View File

@ -223,10 +223,16 @@ M.util.set_user_preference = function(name, value) {
/**
* Prints a confirmation dialog in the style of DOM.confirm().
* @param object event A YUI DOM event or null if launched manually
* @param string message The message to show in the dialog
* @param string url The URL to forward to if YES is clicked. Disabled if fn is given
* @param function fn A JS function to run if YES is clicked.
*
* @method show_confirm_dialog
* @param {EventFacade} e
* @param {Object} args
* @param {String} args.message The question to ask the user
* @param {Function} [args.callback] A callback to apply on confirmation.
* @param {Object} [args.scope] The scope to use when calling the callback.
* @param {Object} [args.callbackargs] Any arguments to pass to the callback.
* @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;
@ -234,47 +240,35 @@ M.util.show_confirm_dialog = function(e, args) {
e.preventDefault();
}
YUI().use('yui2-container', 'yui2-event', function(Y) {
var simpledialog = new Y.YUI2.widget.SimpleDialog('confirmdialog',
{width: '300px',
fixedcenter: true,
modal: true,
visible: false,
draggable: false
}
);
simpledialog.setHeader(M.str.admin.confirmation);
simpledialog.setBody(args.message);
simpledialog.cfg.setProperty('icon', Y.YUI2.widget.SimpleDialog.ICON_WARN);
var handle_cancel = function() {
simpledialog.hide();
};
var handle_yes = function() {
simpledialog.hide();
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
});
// The dialogue was submitted with a positive value indication.
confirmationDialogue.on('complete-yes', function(e) {
// Handle any callbacks.
if (args.callback) {
var callback = null;
if (Y.Lang.isFunction(args.callback)) {
callback = args.callback;
} else {
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;
}
var scope = e.target;
if (Y.Lang.isObject(args.scope)) {
var sc = args.scope;
} else {
var sc = e.target;
scope = args.scope;
}
if (args.callbackargs) {
callback.apply(sc, args.callbackargs);
} else {
callback.apply(sc);
}
var callbackargs = args.callbackargs || [];
args.callback.apply(scope, callbackargs);
return;
}
@ -288,9 +282,7 @@ M.util.show_confirm_dialog = function(e, args) {
window.location = targetancestor.get('href');
} else if (target.test('input')) {
targetform = target.ancestor(function(node) { return node.get('tagName').toLowerCase() == 'form'; });
// We cannot use target.ancestor('form') on the previous line
// because of http://yuilibrary.com/projects/yui3/ticket/2531561
targetform = target.ancestor('form', true);
if (!targetform) {
return;
}
@ -300,32 +292,26 @@ M.util.show_confirm_dialog = function(e, args) {
}
targetform.submit();
} else if (target.get('tagName').toLowerCase() == 'form') {
// We cannot use target.test('form') on the previous line because of
// http://yuilibrary.com/projects/yui3/ticket/2531561
} else if (target.test('form')) {
target.submit();
} else if (M.cfg.developerdebug) {
alert("Element of type " + target.get('tagName') + " is not supported by the M.util.show_confirm_dialog function. Use A, INPUT, or FORM");
} else {
Y.log("Element of type " + target.get('tagName') +
" is not supported by the M.util.show_confirm_dialog function. Use A, INPUT, or FORM",
'warn', 'javascript-static');
}
};
}, this);
if (!args.cancellabel) {
args.cancellabel = M.str.moodle.cancel;
}
if (!args.continuelabel) {
args.continuelabel = M.str.moodle.yes;
if (args.cancellabel) {
confirmationDialogue.set('noLabel', args.cancellabel);
}
var buttons = [
{text: args.cancellabel, handler: handle_cancel, isDefault: true},
{text: args.continuelabel, handler: handle_yes}
];
if (args.continuelabel) {
confirmationDialogue.set('yesLabel', args.continuelabel);
}
simpledialog.cfg.queueProperty('buttons', buttons);
simpledialog.render(document.body);
simpledialog.show();
confirmationDialogue.render()
.show();
});
};