MDL-21400 JS confirm dialog converted to YUI3/2

This commit is contained in:
Petr Skoda
2010-02-06 17:35:11 +00:00
parent 26114d526f
commit 20fb563e97
3 changed files with 93 additions and 87 deletions

View File

@@ -228,18 +228,18 @@ M.util.init_help_icons = function(Y) {
* @param String the value to set it to.
*/
M.util.set_user_preference = function(name, value) {
YUI(M.yui.loader).use('io', function(Y) {
YUI(M.yui.loader).use('io', function(Y) {
var url = M.cfg.wwwroot + '/lib/ajax/setuserpref.php?sesskey=' +
M.cfg.sesskey + '&pref=' + encodeURI(name) + '&value=' + encodeURI(value);
// If we are a developer, ensure that failures are reported.
var cfg = {
method: 'get',
on: {},
method: 'get',
on: {},
};
if (M.cfg.developerdebug) {
cfg.on.failure = function(id, o, args) {
alert("Error updating user preference '" + name + "' using ajax. Clicking this link will repeat the Ajax call that failed so you can see the error: ");
alert("Error updating user preference '" + name + "' using ajax. Clicking this link will repeat the Ajax call that failed so you can see the error: ");
}
}
@@ -248,6 +248,88 @@ 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.
*/
M.util.show_confirm_dialog = function(e, args) {
var target = e.target;
if (e.preventDefault) {
e.preventDefault();
}
YUI(M.yui.loader).use('yui2-container', 'yui2-event', function(Y) {
var simpledialog = new YAHOO.widget.SimpleDialog('confirmdialog',
{ width: '300px',
fixedcenter: true,
modal: true,
visible: false,
draggable: false
}
);
simpledialog.setHeader(mstr.admin.confirmation);
simpledialog.setBody(args.message);
simpledialog.cfg.setProperty('icon', YAHOO.widget.SimpleDialog.ICON_WARN);
var handle_cancel = function() {
simpledialog.hide();
};
var handle_yes = function() {
simpledialog.hide();
if (args.callback) {
// args comes from PHP, so callback will be a string, needs to be evaluated by JS
var callback = null;
if (Y.Lang.isFunction(args.callback)) {
callback = args.callback;
} else {
callback = eval('('+args.callback+')');
}
if (Y.Lang.isObject(args.scope)) {
var sc = args.scope;
} else {
var sc = e.target;
}
if (args.callbackargs) {
callback.apply(sc, args.callbackargs);
} else {
callback.apply(sc);
}
return;
}
if (target.get('tagName').toLowerCase() == 'a') {
window.location = target.get('href');
} else if (target.get('tagName').toLowerCase() == 'input') {
var parentelement = target.get('parentNode');
while (parentelement.get('tagName').toLowerCase() != 'form' && parentelement.get('tagName').toLowerCase() != 'body') {
parentelement = parentelement.get('parentNode');
}
if (parentelement.get('tagName').toLowerCase() == 'form') {
parentelement.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 or INPUT");
}
};
var buttons = [ { text: mstr.moodle.cancel, handler: handle_cancel, isDefault: true },
{ text: mstr.moodle.yes, handler: handle_yes } ];
simpledialog.cfg.queueProperty('buttons', buttons);
simpledialog.render(document.body);
simpledialog.show();
});
}
//=== old legacy JS code, hopefully to be replaced soon by M.xx.yy and YUI3 code ===
function popupchecker(msg) {
@@ -1260,83 +1342,6 @@ function stripHTML(str) {
return ret;
}
/**
* Prints a confirmation dialog in the style of DOM.confirm().
* @param object event A DOM event
* @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.
*/
function confirm_dialog(event, args) {
var message = args.message;
var target = this;
target.args = args;
YAHOO.util.Event.preventDefault(event);
var simpledialog = new YAHOO.widget.SimpleDialog('confirmdialog',
{ width: '300px',
fixedcenter: true,
modal: true,
visible: false,
draggable: false
}
);
simpledialog.setHeader(mstr.admin.confirmation);
simpledialog.setBody(message);
simpledialog.cfg.setProperty('icon', YAHOO.widget.SimpleDialog.ICON_WARN);
this.handle_cancel = function() {
this.hide();
};
this.handle_yes = function() {
this.hide();
if (target.args.callback) {
// args comes from PHP, so callback will be a string, needs to be evaluated by JS
var callback = null;
if (Y.Lang.isFunction(target.args.callback)) {
callback = target.args.callback;
} else {
callback = eval('('+target.args.callback+')');
}
if (Y.Lang.isObject(target.args.scope)) {
callback.apply(target.args.scope);
} else {
callback();
}
}
if (target.tagName.toLowerCase() == 'a') {
window.location = target.href;
} else if (target.tagName.toLowerCase() == 'input') {
var parentelement = target.parentNode;
while (parentelement.tagName.toLowerCase() != 'form' && parentelement.tagName.toLowerCase() != 'body') {
parentelement = parentelement.parentNode;
}
if (parentelement.tagName.toLowerCase() == 'form') {
parentelement.submit();
}
} else if(M.cfg.developerdebug) {
alert("Element of type " + target.tagName + " is not supported by the confirm_dialog function. Use A or INPUT");
}
};
var buttons = [ { text: mstr.moodle.cancel, handler: this.handle_cancel, isDefault: true },
{ text: mstr.moodle.yes, handler: this.handle_yes } ];
simpledialog.cfg.queueProperty('buttons', buttons);
simpledialog.render(document.body);
simpledialog.show();
return simpledialog;
}
function dialog_callback() {
console.debug(this);
console.debug(this.args);
}
Number.prototype.fixed=function(n){
with(Math)
return round(Number(this)*pow(10,n))/pow(10,n);