r112712 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112711‎ | r112712 | r112713 >
Date:20:09, 29 February 2012
Author:foxtrott
Status:deferred
Tags:
Comment:
enable handling of modules of input types; use associative array for input type parameters to simplify subclassing
Modified paths:
  • /trunk/extensions/SemanticForms/includes/forminputs/SF_FormInput.php (modified) (history)
  • /trunk/extensions/SemanticForms/specials/SF_CreateForm.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/specials/SF_CreateForm.php
@@ -360,7 +360,8 @@
361361
362362 $params = method_exists( $inputTypeClass, 'getParameters' ) ? call_user_func( array( $inputTypeClass, 'getParameters' ) ) : array();
363363
364 - foreach ( $params as $i => $param ) {
 364+ $i = 0;
 365+ foreach ( $params as $param ) {
365366 $paramName = $param['name'];
366367 $type = $param['type'];
367368 $desc = $wgParser->parse( $param['description'], new Title(), new ParserOptions() )->getText();
@@ -387,6 +388,7 @@
388389 if ( $i % 3 == 2 || $i == count( $params ) - 1 ) {
389390 $text .= "<div style=\"clear: both\";></div></div>\n";
390391 }
 392+ ++$i;
391393 }
392394 return $text;
393395 }
Index: trunk/extensions/SemanticForms/includes/forminputs/SF_FormInput.php
@@ -87,27 +87,27 @@
8888 */
8989 public static function getParameters() {
9090 $params = array();
91 - $params[] = array(
 91+ $params['mandatory'] = array(
9292 'name' => 'mandatory',
9393 'type' => 'boolean',
9494 'description' => wfMsg( 'sf_forminputs_mandatory' )
9595 );
96 - $params[] = array(
 96+ $params['restricted'] = array(
9797 'name' => 'restricted',
9898 'type' => 'boolean',
9999 'description' => wfMsg( 'sf_forminputs_restricted' )
100100 );
101 - $params[] = array(
 101+ $params['class'] = array(
102102 'name' => 'class',
103103 'type' => 'string',
104104 'description' => wfMsg( 'sf_forminputs_class' )
105105 );
106 - $params[] = array(
 106+ $params['property'] = array(
107107 'name' => 'property',
108108 'type' => 'string',
109109 'description' => wfMsg( 'sf_forminputs_property' )
110110 );
111 - $params[] = array(
 111+ $params['default'] = array(
112112 'name' => 'default',
113113 'type' => 'string',
114114 'description' => wfMsg( 'sf_forminputs_default' )
@@ -165,6 +165,19 @@
166166 public function getJsValidationFunctionData() {
167167 return $this->mJsValidationFunctionData;
168168 }
 169+
 170+
 171+ /**
 172+ * Returns the names of the resource modules this input type uses.
 173+ *
 174+ * Returns the names of the modules as an array or - if there is only one
 175+ * module - as a string.
 176+ *
 177+ * @return null|string|array
 178+ */
 179+ public function getResourceModuleNames() {
 180+ return null;
 181+ }
169182
170183 /**
171184 * For each input type one or more JavaScript initialization functions may
@@ -294,27 +307,38 @@
295308
296309 $input = new $calledClass ( $sfgFieldNum, $cur_value, $input_name, $is_disabled, $other_args );
297310
 311+ $modules = $input->getResourceModuleNames();
 312+
 313+ // register modules for the input
 314+ if ( $modules !== null ) {
 315+ $wgOut->addModuleStyles( $modules );
 316+ $wgOut->addModuleScripts( $modules );
 317+ }
 318+
298319 // create calls to JS initialization and validation
299320 // TODO: This data should be transferred as a JSON blob and then be evaluated from a dedicated JS file
300 - $jstext = '';
 321+ if ( $input->getJsInitFunctionData() || $input->getJsValidationFunctionData() ) {
301322
302 - foreach ( $input->getJsInitFunctionData() as $jsInitFunctionData ) {
 323+ $jstext = '';
303324
304 - $jstext .= <<<JAVASCRIPT
305 -jQuery(function(){ jQuery('#input_$sfgFieldNum').SemanticForms_registerInputInit({$jsInitFunctionData['name']}, {$jsInitFunctionData['param']} ); });
306 -JAVASCRIPT;
307 - }
 325+ foreach ( $input->getJsInitFunctionData() as $jsInitFunctionData ) {
 326+ $jstext .= "jQuery('#input_$sfgFieldNum').SemanticForms_registerInputInit({$jsInitFunctionData['name']}, {$jsInitFunctionData['param']} );";
 327+ }
308328
309 - foreach ( $input->getJsValidationFunctionData() as $jsValidationFunctionData ) {
 329+ foreach ( $input->getJsValidationFunctionData() as $jsValidationFunctionData ) {
 330+ $jstext .= "jQuery('#input_$sfgFieldNum').SemanticForms_registerInputValidation( {$jsValidationFunctionData['name']}, {$jsValidationFunctionData['param']});";
 331+ }
310332
311 - $jstext .= <<<JAVASCRIPT
312 -jQuery(function(){ jQuery('#input_$sfgFieldNum').SemanticForms_registerInputValidation( {$jsValidationFunctionData['name']}, {$jsValidationFunctionData['param']}); });
313 -JAVASCRIPT;
 333+ if ( $modules !== null ) {
 334+ $jstext = 'mw.loader.using(' . json_encode( $modules ) . ', function(){' . $jstext . '});';
 335+ }
 336+
 337+ $jstext = 'jQuery(function(){' . $jstext . '});';
 338+
 339+ // write JS code directly to the page's code
 340+ $wgOut->addScript( Html::inlineScript( $jstext ) );
314341 }
315342
316 - // write JS code directly to the page's code
317 - $wgOut->addScript( Html::inlineScript( $jstext ) );
318 -
319343 return $input->getHtmlText();
320344 }
321345