Index: branches/salvatoreingala/Gadgets/modules/jquery.formBuilder.js |
— | — | @@ -8,9 +8,15 @@ |
9 | 9 | |
10 | 10 | var idPrefix = "mw-gadgets-dialog-"; |
11 | 11 | |
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] !== '@' ) { |
14 | 18 | return str; |
| 19 | + } else if ( str.substr( 0, 2 ) == '@@' ) { |
| 20 | + return str.substr( 1 ); |
15 | 21 | } else { |
16 | 22 | //TODO: better validation |
17 | 23 | return mw.msg( str.substring( 1 ) ); |
— | — | @@ -101,7 +107,7 @@ |
102 | 108 | EmptyField.call( this, name, desc ); |
103 | 109 | |
104 | 110 | var $label = $( '<label/>' ) |
105 | | - .text( $s( this.desc.label ) ) |
| 111 | + .text( preproc( this.desc.label ) ) |
106 | 112 | .attr('for', idPrefix + this.name ); |
107 | 113 | |
108 | 114 | this.$p.append( $label ); |
— | — | @@ -256,7 +262,7 @@ |
257 | 263 | $.each( desc.options, function( optName, optVal ) { |
258 | 264 | var i = values.length; |
259 | 265 | $( '<option/>' ) |
260 | | - .text( $s( optName ) ) |
| 266 | + .text( preproc( optName ) ) |
261 | 267 | .val( i ) |
262 | 268 | .appendTo( $select ); |
263 | 269 | values.push( optVal ); |
— | — | @@ -287,6 +293,14 @@ |
288 | 294 | "select" : SelectField |
289 | 295 | }; |
290 | 296 | |
| 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 | + */ |
291 | 305 | function buildFormBody() { |
292 | 306 | var description = this.get( 0 ); |
293 | 307 | if ( typeof description != 'object' ) { |
— | — | @@ -299,7 +313,7 @@ |
300 | 314 | //If there is an "intro", adds it to the form as a label |
301 | 315 | if ( typeof description.intro == 'string' ) { |
302 | 316 | $( '<p/>' ) |
303 | | - .text( $s( description.intro ) ) |
| 317 | + .text( preproc( description.intro ) ) |
304 | 318 | .addClass( 'mw-gadgets-prefsDialog-intro' ) |
305 | 319 | .appendTo( $form ); |
306 | 320 | } |
— | — | @@ -357,6 +371,13 @@ |
358 | 372 | } |
359 | 373 | |
360 | 374 | 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 | + */ |
361 | 382 | getValues: function() { |
362 | 383 | var data = this.data( 'formBuilder' ), |
363 | 384 | result = {}; |
— | — | @@ -368,6 +389,12 @@ |
369 | 390 | |
370 | 391 | return result; |
371 | 392 | }, |
| 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 | + */ |
372 | 399 | validate: function() { |
373 | 400 | var data = this.data( 'formBuilder' ); |
374 | 401 | return data.validator.form(); |