MediaWiki:JSconfig.js

Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/**
 * JSconfig
 *
 * Global configuration options to enable/disable and configure
 * specific script features from [[MediaWiki:Common.js]] and [[MediaWiki:Monobook.js]]
 * This framework adds config options (saved as cookies) to [[Special:Preferences]]
 * For a more permanent change you can override the default settings in your
 * [[Special:Mypage/monobook.js]]
 * for Example: JSconfig.keys[loadAutoInformationTemplate] = false;
 *
 * Maintainer: [[User:Dschwen]]
 */
mediaWiki.loader.using('mediawiki.cookie', function() 
{
var JSconfig = {
 prefix: 'jsconfig_',
 keys: {},
 meta: {},
 // Register a new configuration item
 //  * name      : String, internal name
 //  * default_value : String or Boolean (type determines configuration widget)
 //  * description  : String, text appearing next to the widget in the preferences, or an hash-object
 //    containing translations of the description indexed by the language code
 //  * prefpage    : Integer (optional), section in the preferences to insert the widget:
 //    0 : User profile     User profile
 //    1 : Skin  Appearance
 //    2 : Math  Date and Time
 //    3 : Files  Editing
 //    4 : Date and time     Recent Changes
 //    5 : Editing Watchlist
 //    6 : Recent changes    Search Options
 //    7 : Watchlist       Misc
 //    8 : Search Gadgets
 //    9 : Misc
 //
 // Access keys through JSconfig.keys[name]
 registerKey: function (name, default_value, description, prefpage) {
  if (typeof JSconfig.keys[name] == 'undefined') {
   JSconfig.keys[name] = default_value;
  } else {
   // all cookies are read as strings,
   // convert to the type of the default value
   switch (typeof default_value) {
   case 'boolean':
    JSconfig.keys[name] = (JSconfig.keys[name] == 'true');
    break;
   case 'number':
    JSconfig.keys[name] = JSconfig.keys[name] / 1;
    break;
   }
  }
  JSconfig.meta[name] = {
   'description': description[wgUserLanguage] || description.en || (typeof description == 'string' && description) || '<i>en</i> translation missing',
   'page': prefpage || 0,
   'default_value': default_value
  };
  // if called after setUpForm(), we'll have to add an extra input field
  if (JSconfig.prefsTabs) {
   JSconfig.addPrefsInput(name);
  }
 },
 readCookies: function () {
  var cookies = document.cookie.split('; ');
  var p = JSconfig.prefix.length;
  var i;
  for (var key = 0; cookies && key < cookies.length; key++) {
   if (cookies[key].substring(0, p) == JSconfig.prefix) {
    i = cookies[key].indexOf('=');
    //alert( cookies[key] + ',' + key + ',' + cookies[key].substring(p,i) );
    JSconfig.keys[cookies[key].substring(p, i)] = cookies[key].substring(i + 1);
   }
  }
 },
 writeCookies: function () {
  var expdate = new Date();
  expdate.setTime(expdate.getTime() + 1000 * 60 * 60 * 24 * 3650); // expires in 3560 days
  for (var key in JSconfig.keys) {
   document.cookie = JSconfig.prefix + key + '=' + JSconfig.keys[key] + '; path=/; expires=' + expdate.toUTCString();
  }
 },
 evaluateForm: function () {
  var w_ctrl, wt;
  //alert('about to save JSconfig');
  for (var key in JSconfig.meta) {
   w_ctrl = document.getElementById(JSconfig.prefix + key);
   if (w_ctrl) {
    wt = typeof JSconfig.meta[key].default_value;
    switch (wt) {
    case 'boolean':
     JSconfig.keys[key] = w_ctrl.checked;
     break;
    case 'string':
     JSconfig.keys[key] = w_ctrl.value;
     break;
    }
   }
  }
  JSconfig.writeCookies();
  return true;
 },
 prefsTabs: false,
 setUpForm: function () {
  var prefChild = document.getElementById('preferences');
  if (!prefChild) {
   return;
  }
  prefChild = prefChild.childNodes;
  //
  // make a list of all preferences sections
  //
  var tabs = [];
  var len = prefChild.length;
  for (var key = 0; key < len; key++) {
   if (prefChild[key].tagName && prefChild[key].tagName.toLowerCase() == 'fieldset') {
    tabs.push(prefChild[key]);
   }
  }
  JSconfig.prefsTabs = tabs;
  //
  // Create Widgets for all registered config keys
  //
  for (var wkey in JSconfig.meta) {
   JSconfig.addPrefsInput(wkey);
  }
  addEvent(document.getElementById('preferences').parentNode, 'submit', JSconfig.evaluateForm);
 },
 addPrefsInput: function (key) {
  var w_div = document.createElement('DIV');
  var w_label = document.createElement('LABEL');
  var wt = typeof JSconfig.meta[key].default_value;
  switch (wt) {
  case 'boolean':
   JSconfig.meta[key].description = ' ' + JSconfig.meta[key].description;
   break;
  default:
   //case 'string':    
   JSconfig.meta[key].description += ': ';
   break;
  }
  w_label.appendChild(document.createTextNode(JSconfig.meta[key].description));
  w_label.htmlFor = JSconfig.prefix + key;
  var w_ctrl = document.createElement('INPUT');
  w_ctrl.id = JSconfig.prefix + key;
  // before insertion into the DOM tree
  switch (wt) {
  case 'boolean':
   w_ctrl.type = 'checkbox';
   w_div.appendChild(w_ctrl);
   w_div.appendChild(w_label);
   break;
  default:
   //case 'string':
   w_ctrl.type = 'text';
   w_div.appendChild(w_label);
   w_div.appendChild(w_ctrl);
   break;
  }
  JSconfig.prefsTabs[JSconfig.meta[key].page].appendChild(w_div);
  // after insertion into the DOM tree
  switch (wt) {
  case 'boolean':
   w_ctrl.defaultChecked = w_ctrl.checked = JSconfig.keys[key];
   break;
  case 'string':
   w_ctrl.defaultValue = w_ctrl.value = JSconfig.keys[key];
   break;
  }
 }
};
JSconfig.readCookies();
 
if (wgCanonicalSpecialPageName == 'Preferences') {
 $(document).ready(JSconfig.setUpForm);
}
});