More mediawiki.js cleanup

- Convert prototype object modifications into object literal. Saves bandwidth (less characters) and speeds up execution (no need to access 2 level deep object member repetitively). Local testing (Chrome Web Inspector) shows 14.80KB to 14.67KB (non-cached, debug=false), and execution time on cached request 32ms to 25ms.
- Un-indent 1 tab for d.setTime (Follows up r92964)
- Whitespace consistency
- Move var statements to top of loader's addScript

Follows up: r92933
This commit is contained in:
Krinkle 2011-07-24 20:22:36 +00:00
parent c6f33bf433
commit 5819c37b3c

View file

@ -29,6 +29,7 @@ window.mw = window.mediaWiki = new ( function( $ ) {
return this; return this;
} }
Map.prototype = {
/** /**
* Get the value of one or multiple a keys. * Get the value of one or multiple a keys.
* *
@ -42,7 +43,7 @@ window.mw = window.mediaWiki = new ( function( $ ) {
* objects are always passed by reference in JavaScript!). * objects are always passed by reference in JavaScript!).
* @return Values as a string or object, null if invalid/inexistant. * @return Values as a string or object, null if invalid/inexistant.
*/ */
Map.prototype.get = function( selection, fallback ) { get: function( selection, fallback ) {
if ( $.isArray( selection ) ) { if ( $.isArray( selection ) ) {
selection = $.makeArray( selection ); selection = $.makeArray( selection );
var results = {}; var results = {};
@ -64,7 +65,7 @@ window.mw = window.mediaWiki = new ( function( $ ) {
} else { } else {
return null; // invalid selection key return null; // invalid selection key
} }
}; },
/** /**
* Sets one or multiple key/value pairs. * Sets one or multiple key/value pairs.
@ -73,7 +74,7 @@ window.mw = window.mediaWiki = new ( function( $ ) {
* @param value mixed Value to set (optional, only in use when key is a string) * @param value mixed Value to set (optional, only in use when key is a string)
* @return bool This returns true on success, false on failure. * @return bool This returns true on success, false on failure.
*/ */
Map.prototype.set = function( selection, value ) { set: function( selection, value ) {
if ( $.isPlainObject( selection ) ) { if ( $.isPlainObject( selection ) ) {
for ( var s in selection ) { for ( var s in selection ) {
this.values[s] = selection[s]; this.values[s] = selection[s];
@ -84,7 +85,7 @@ window.mw = window.mediaWiki = new ( function( $ ) {
return true; return true;
} }
return false; return false;
}; },
/** /**
* Checks if one or multiple keys exist. * Checks if one or multiple keys exist.
@ -92,7 +93,7 @@ window.mw = window.mediaWiki = new ( function( $ ) {
* @param selection mixed String key or array of keys to check * @param selection mixed String key or array of keys to check
* @return boolean Existence of key(s) * @return boolean Existence of key(s)
*/ */
Map.prototype.exists = function( selection ) { exists: function( selection ) {
if ( typeof selection === 'object' ) { if ( typeof selection === 'object' ) {
for ( var s = 0; s < selection.length; s++ ) { for ( var s = 0; s < selection.length; s++ ) {
if ( !( selection[s] in this.values ) ) { if ( !( selection[s] in this.values ) ) {
@ -103,6 +104,7 @@ window.mw = window.mediaWiki = new ( function( $ ) {
} else { } else {
return selection in this.values; return selection in this.values;
} }
}
}; };
/** /**
@ -124,25 +126,26 @@ window.mw = window.mediaWiki = new ( function( $ ) {
return this; return this;
} }
Message.prototype = {
/** /**
* Appends (does not replace) parameters for replacement to the .parameters property. * Appends (does not replace) parameters for replacement to the .parameters property.
* *
* @param parameters Array * @param parameters Array
* @return Message * @return Message
*/ */
Message.prototype.params = function( parameters ) { params: function( parameters ) {
for ( var i = 0; i < parameters.length; i++ ) { for ( var i = 0; i < parameters.length; i++ ) {
this.parameters.push( parameters[i] ); this.parameters.push( parameters[i] );
} }
return this; return this;
}; },
/** /**
* Converts message object to it's string form based on the state of format. * Converts message object to it's string form based on the state of format.
* *
* @return string Message as a string in the current form or <key> if key does not exist. * @return string Message as a string in the current form or <key> if key does not exist.
*/ */
Message.prototype.toString = function() { toString: function() {
if ( !this.map.exists( this.key ) ) { if ( !this.map.exists( this.key ) ) {
// Return <key> if key does not exist // Return <key> if key does not exist
return '<' + this.key + '>'; return '<' + this.key + '>';
@ -170,45 +173,46 @@ window.mw = window.mediaWiki = new ( function( $ ) {
} }
*/ */
return text; return text;
}; },
/** /**
* Changes format to parse and converts message to string * Changes format to parse and converts message to string
* *
* @return {string} String form of parsed message * @return {string} String form of parsed message
*/ */
Message.prototype.parse = function() { parse: function() {
this.format = 'parse'; this.format = 'parse';
return this.toString(); return this.toString();
}; },
/** /**
* Changes format to plain and converts message to string * Changes format to plain and converts message to string
* *
* @return {string} String form of plain message * @return {string} String form of plain message
*/ */
Message.prototype.plain = function() { plain: function() {
this.format = 'plain'; this.format = 'plain';
return this.toString(); return this.toString();
}; },
/** /**
* Changes the format to html escaped and converts message to string * Changes the format to html escaped and converts message to string
* *
* @return {string} String form of html escaped message * @return {string} String form of html escaped message
*/ */
Message.prototype.escaped = function() { escaped: function() {
this.format = 'escaped'; this.format = 'escaped';
return this.toString(); return this.toString();
}; },
/** /**
* Checks if message exists * Checks if message exists
* *
* @return {string} String form of parsed message * @return {string} String form of parsed message
*/ */
Message.prototype.exists = function() { exists: function() {
return this.map.exists( this.key ); return this.map.exists( this.key );
}
}; };
/* Public Members */ /* Public Members */
@ -501,11 +505,10 @@ window.mw = window.mediaWiki = new ( function( $ ) {
} ) ); } ) );
} }
} else if ( typeof style === 'string' ) { } else if ( typeof style === 'string' ) {
getMarker().before( mw.html.element( getMarker().before( mw.html.element( 'style', {
'style', 'type': 'text/css',
{ 'type': 'text/css', 'media': media }, 'media': media
new mw.html.Cdata( style ) }, new mw.html.Cdata( style ) ) );
) );
} }
} }
} }
@ -673,11 +676,11 @@ window.mw = window.mediaWiki = new ( function( $ ) {
* @param callback Function: Optional callback which will be run when the script is done * @param callback Function: Optional callback which will be run when the script is done
*/ */
function addScript( src, callback ) { function addScript( src, callback ) {
var done = false, script;
if ( ready ) { if ( ready ) {
// jQuery's getScript method is NOT better than doing this the old-fassioned way // jQuery's getScript method is NOT better than doing this the old-fassioned way
// because jQuery will eval the script's code, and errors will not have sane // because jQuery will eval the script's code, and errors will not have sane
// line numbers. // line numbers.
var done = false,
script = document.createElement( 'script' ); script = document.createElement( 'script' );
script.setAttribute( 'src', src ); script.setAttribute( 'src', src );
script.setAttribute( 'type', 'text/javascript' ); script.setAttribute( 'type', 'text/javascript' );
@ -1163,8 +1166,7 @@ window.mw = window.mediaWiki = new ( function( $ ) {
// Alias $j to jQuery for backwards compatibility // Alias $j to jQuery for backwards compatibility
window.$j = jQuery; window.$j = jQuery;
/* Auto-register from pre-loaded startup scripts */ // Auto-register from pre-loaded startup scripts
if ( jQuery.isFunction( startUp ) ) { if ( jQuery.isFunction( startUp ) ) {
startUp(); startUp();
delete startUp; delete startUp;