r90466 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90465‎ | r90466 | r90467 >
Date:15:25, 20 June 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
- Renamed $s() function to preproc(), and extended its function slightly
- Added some documentation
Modified paths:
  • /branches/salvatoreingala/Gadgets/modules/jquery.formBuilder.js (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/modules/jquery.formBuilder.js
@@ -8,9 +8,15 @@
99
1010 var idPrefix = "mw-gadgets-dialog-";
1111
12 - function $s( str ) {
13 - if ( str[0] !== '@' ) {
 12+ //Preprocesses strings end possibly replaces them with messages.
 13+ //If str starts with "@" the rest of the string is assumed to be
 14+ //a message, and the result of mw.msg is returned.
 15+ //Two "@@" at the beginning escape for a single "@".
 16+ function preproc( str ) {
 17+ if ( str.length <= 1 || str[0] !== '@' ) {
1418 return str;
 19+ } else if ( str.substr( 0, 2 ) == '@@' ) {
 20+ return str.substr( 1 );
1521 } else {
1622 //TODO: better validation
1723 return mw.msg( str.substring( 1 ) );
@@ -101,7 +107,7 @@
102108 EmptyField.call( this, name, desc );
103109
104110 var $label = $( '<label/>' )
105 - .text( $s( this.desc.label ) )
 111+ .text( preproc( this.desc.label ) )
106112 .attr('for', idPrefix + this.name );
107113
108114 this.$p.append( $label );
@@ -256,7 +262,7 @@
257263 $.each( desc.options, function( optName, optVal ) {
258264 var i = values.length;
259265 $( '<option/>' )
260 - .text( $s( optName ) )
 266+ .text( preproc( optName ) )
261267 .val( i )
262268 .appendTo( $select );
263269 values.push( optVal );
@@ -287,6 +293,14 @@
288294 "select" : SelectField
289295 };
290296
 297+ /* Public methods */
 298+
 299+ /**
 300+ * Main method; takes the given preferences description object and builds
 301+ * the body of the form with the requested fields.
 302+ *
 303+ * @return {Element} the object with the requested form body.
 304+ */
291305 function buildFormBody() {
292306 var description = this.get( 0 );
293307 if ( typeof description != 'object' ) {
@@ -299,7 +313,7 @@
300314 //If there is an "intro", adds it to the form as a label
301315 if ( typeof description.intro == 'string' ) {
302316 $( '<p/>' )
303 - .text( $s( description.intro ) )
 317+ .text( preproc( description.intro ) )
304318 .addClass( 'mw-gadgets-prefsDialog-intro' )
305319 .appendTo( $form );
306320 }
@@ -357,6 +371,13 @@
358372 }
359373
360374 var methods = {
 375+
 376+ /**
 377+ * Returns a dictionary of field names and field values.
 378+ * Returned values are not warranted to pass field validation.
 379+ *
 380+ * @return {Object}
 381+ */
361382 getValues: function() {
362383 var data = this.data( 'formBuilder' ),
363384 result = {};
@@ -368,6 +389,12 @@
369390
370391 return result;
371392 },
 393+
 394+ /**
 395+ * Do validation of form fields and warn the user about wrong values, if any.
 396+ *
 397+ * @return {Boolean} true if all fields pass validation, false otherwise.
 398+ */
372399 validate: function() {
373400 var data = this.data( 'formBuilder' );
374401 return data.validator.form();

Status & tagging log