MDL-21400 converting JS set_user_preference to M namespace and YUI3

This commit is contained in:
Petr Skoda
2010-02-06 15:20:24 +00:00
parent 3b044ba347
commit 41912a261f
3 changed files with 38 additions and 41 deletions

View File

@@ -128,7 +128,7 @@ M.util.CollapsibleRegion = function(Y, id, userpref, strtooltip) {
animation.set('reverse', this.div.hasClass('collapsed'));
// Update the user preference.
if (this.userpref) {
set_user_preference(this.userpref, !this.div.hasClass('collapsed'));
M.util.set_user_preference(this.userpref, !this.div.hasClass('collapsed'));
}
animation.run();
}, this, animation);
@@ -215,6 +215,39 @@ M.util.init_help_icons = function(Y) {
});
};
/**
* Makes a best effort to connect back to Moodle to update a user preference,
* however, there is no mechanism for finding out if the update succeeded.
*
* Before you can use this function in your JavsScript, you must have called
* user_preference_allow_ajax_update from moodlelib.php to tell Moodle that
* the udpate is allowed, and how to safely clean and submitted values.
*
* @param String name the name of the setting to udpate.
* @param String the value to set it to.
*/
M.util.set_user_preference = function(name, value) {
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: {},
};
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: ");
}
}
// Make the request.
Y.io(url, cfg);
});
};
//=== old legacy JS code, hopefully to be replaced soon by M.xx.yy and YUI3 code ===
function popupchecker(msg) {
@@ -1086,42 +1119,6 @@ emoticons_help = {
}
}
/**
* Makes a best effort to connect back to Moodle to update a user preference,
* however, there is no mechanism for finding out if the update succeeded.
*
* Before you can use this function in your JavsScript, you must have called
* user_preference_allow_ajax_update from moodlelib.php to tell Moodle that
* the udpate is allowed, and how to safely clean and submitted values.
*
* @param String name the name of the setting to udpate.
* @param String the value to set it to.
*/
function set_user_preference(name, value) {
// Don't generate a script error if the library has not been loaded,
// unless we are a Developer, in which case we want the error.
if (YAHOO && YAHOO.util && YAHOO.util.Connect || M.cfg.developerdebug) {
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 callback = {};
if (M.cfg.developerdebug) {
callback.failure = function() {
var a = document.createElement('a');
a.href = url;
a.classname = 'error';
a.appendChild(document.createTextNode("Error updating user preference '" + name + "' using ajax. Clicking this link will repeat the Ajax call that failed so you can see the error."));
document.body.insertBefore(a, document.body.firstChild);
}
}
// Make the request.
YAHOO.util.Connect.asyncRequest('GET', url, callback);
}
}
/**
* Oject to handle expanding and collapsing blocks when an icon is clicked on.
* @constructor
@@ -1165,7 +1162,7 @@ block_hider.prototype.handle_click = function(e) {
YAHOO.util.Event.stopEvent(e);
this.ishidden = !this.ishidden;
this.update_state();
set_user_preference(this.userpref, this.ishidden);
M.util.set_user_preference(this.userpref, this.ishidden);
}
/** Set the state of the block show/hide icon to this.ishidden. */