r112829 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r112828‎ | r112829 | r112830 >
Date:19:26, 1 March 2012
Author:foxtrott
Status:deferred
Tags:
Comment:
fix issues with wikieditor for textarea; rework textarea and textarea with autocompletion; include missing dependencies for ext...main module
Modified paths:
  • /trunk/extensions/SemanticForms/SemanticForms.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/forminputs/SF_FormInput.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/forminputs/SF_TextAreaInput.php (modified) (history)
  • /trunk/extensions/SemanticForms/includes/forminputs/SF_TextAreaWithAutocompleteInput.php (modified) (history)
  • /trunk/extensions/SemanticForms/libs/SF_wikieditor.js (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/SemanticForms.php
@@ -208,6 +208,8 @@
209209 'jquery.ui.button',
210210 'jquery.ui.sortable',
211211 'jquery.ui.widget',
 212+ 'ext.semanticforms.fancybox',
 213+ 'ext.semanticforms.autogrow',
212214 ),
213215 ),
214216 'ext.semanticforms.fancybox' => $sfgResourceTemplate + array(
@@ -240,7 +242,11 @@
241243 'ext.semanticforms.wikieditor' => $sfgResourceTemplate + array(
242244 'scripts' => 'libs/SF_wikieditor.js',
243245 'styles' => 'skins/SF_wikieditor.css',
 246+ 'dependencies' => array(
 247+ 'ext.semanticforms.main',
 248+ 'jquery.wikiEditor',
244249 ),
 250+ ),
245251 'ext.semanticforms.imagepreview' => $sfgResourceTemplate + array(
246252 'scripts' => 'libs/SF_imagePreview.js',
247253 ),
Index: trunk/extensions/SemanticForms/includes/forminputs/SF_TextAreaWithAutocompleteInput.php
@@ -16,167 +16,32 @@
1717 return 'textarea with autocomplete';
1818 }
1919
20 - public static function getDefaultPropTypes() {
21 - return array();
 20+ public static function getParameters() {
 21+ $params = parent::getParameters();
 22+ $params = array_merge( $params, SFTextWithAutocompleteInput::getAutocompletionParameters() );
 23+ return $params;
2224 }
2325
24 - public static function getOtherPropTypesHandled() {
25 - return array( '_wpg', '_str' );
26 - }
 26+ protected function getTextAreaAttributes() {
2727
28 - public static function getOtherPropTypeListsHandled() {
29 - return array( '_wpg', '_str' );
30 - }
 28+ $textarea_attrs = parent::getTextAreaAttributes();
3129
32 - public static function getHTML( $cur_value, $input_name, $is_mandatory, $is_disabled, $other_args ) {
33 -
34 - // TODO: Lots of duplication of code in the parent class. Needs refactoring!
35 -
36 - global $wgOut;
37 -
3830 // If 'no autocomplete' was specified, print a regular
3931 // textarea instead.
40 - if ( array_key_exists( 'no autocomplete', $other_args ) &&
41 - $other_args['no autocomplete'] == true ) {
42 - unset( $other_args['autocompletion source'] );
43 - return SFTextAreaInput::getHTML( $cur_value, $input_name, $is_mandatory, $is_disabled, $other_args );
44 - }
 32+ if ( !array_key_exists( 'no autocomplete', $this->mOtherArgs ) ||
 33+ $this->mOtherArgs['no autocomplete'] == false ) {
4534
46 - global $sfgTabIndex, $sfgFieldNum;
 35+ list( $autocompleteSettings, $remoteDataType, $delimiter ) = SFTextWithAutocompleteInput::setAutocompleteValues( $this->mOtherArgs );
4736
48 - list( $autocompleteSettings, $remoteDataType, $delimiter ) = SFTextWithAutocompleteInput::setAutocompleteValues( $other_args );
49 -
50 - $input_id = 'input_' . $sfgFieldNum;
51 -
52 - if ( array_key_exists( 'editor', $other_args ) &&
53 - $other_args['editor'] == 'wikieditor' &&
54 -
55 - method_exists( $wgOut, 'getResourceLoader' ) &&
56 - in_array( 'jquery.wikiEditor', $wgOut->getResourceLoader()->getModuleNames() ) &&
57 -
58 - class_exists( 'WikiEditorHooks' ) ) {
59 -
60 - // load modules for all enabled features
61 - WikiEditorHooks::editPageShowEditFormInitial( $this );
62 -
63 - $wgOut->addModules( 'ext.semanticforms.wikieditor' );
64 -
65 - $jstext = <<<JAVASCRIPT
66 - jQuery( jQuery('#$input_id').SemanticForms_registerInputInit( ext.wikieditor.init, null ) );
67 -JAVASCRIPT;
68 -
69 - // write JS code directly to the page's code
70 - $wgOut->addScript( Html::inlineScript( $jstext ) );
71 -
72 - $className = "wikieditor ";
73 - } else {
74 - $className = "";
75 - }
76 -
77 - $className .= ( $is_mandatory ) ? 'autocompleteInput mandatoryField' : 'autocompleteInput createboxInput';
78 - if ( array_key_exists( 'class', $other_args ) ) {
79 - $className .= ' ' . $other_args['class'];
80 - }
81 -
82 - if ( array_key_exists( 'rows', $other_args ) ) {
83 - $rows = $other_args['rows'];
84 - } else {
85 - $rows = 5;
86 - }
87 - $text = '';
88 - if ( array_key_exists( 'autogrow', $other_args ) ) {
89 - $className .= ' autoGrow';
90 - }
91 -
92 - $textarea_attrs = array(
93 - 'tabindex' => $sfgTabIndex,
94 - 'id' => $input_id,
95 - 'name' => $input_name,
96 - 'rows' => $rows,
97 - 'class' => $className,
98 - 'autocompletesettings' => $autocompleteSettings,
99 - );
100 -
101 - if ( array_key_exists( 'cols', $other_args ) ) {
102 - $textarea_attrs['cols'] = $other_args['cols'];
103 - // Needed to prevent CSS from overriding the manually-
104 - // set width.
105 - $textarea_attrs['style'] = 'width: auto';
106 - } elseif ( array_key_exists( 'autogrow', $other_args ) ) {
107 - // If 'autogrow' has been set, automatically set
108 - // the number of columns - otherwise, the Javascript
109 - // won't be able to know how many characters there
110 - // are per line, and thus won't work.
111 - $textarea_attrs['cols'] = 90;
112 - $textarea_attrs['style'] = 'width: auto';
113 - } else {
114 - $textarea_attrs['cols'] = 90;
115 - $textarea_attrs['style'] = 'width: 100%';
116 - }
117 -
118 - if ( array_key_exists( 'origName', $other_args ) ) {
119 - $inputAttrs['origName'] = $other_args['origName'];
120 - }
12137 if ( !is_null( $remoteDataType ) ) {
12238 $textarea_attrs['autocompletedatatype'] = $remoteDataType;
12339 }
124 - if ( $is_disabled ) {
125 - $textarea_attrs['disabled'] = 'disabled';
126 - }
127 - if ( array_key_exists( 'maxlength', $other_args ) ) {
128 - $maxlength = $other_args['maxlength'];
129 - // For every actual character pressed (i.e., excluding
130 - // things like the Shift key), reduce the string to
131 - // its allowed length if it's exceeded that.
132 - // This JS code is complicated so that it'll work
133 - // correctly in IE - IE moves the cursor to the end
134 - // whenever this.value is reset, so we'll make sure
135 - // to do that only when we need to.
136 - $maxLengthJSCheck = "if (window.event && window.event.keyCode < 48 && window.event.keyCode != 13) return; if (this.value.length > $maxlength) { this.value = this.value.substring(0, $maxlength); }";
137 - $textarea_attrs['onKeyDown'] = $maxLengthJSCheck;
138 - $textarea_attrs['onKeyUp'] = $maxLengthJSCheck;
139 - }
140 - if ( array_key_exists( 'placeholder', $other_args ) ) {
141 - $textarea_attrs = $other_args['placeholder'];
142 - }
14340
144 - $textarea_input = Html::element( 'textarea', $textarea_attrs, $cur_value );
145 - $text .= $textarea_input;
 41+ $textarea_attrs['autocompletesettings'] = $autocompleteSettings;
14642
147 - if ( array_key_exists( 'uploadable', $other_args ) && $other_args['uploadable'] == true ) {
148 - if ( array_key_exists( 'default filename', $other_args ) ) {
149 - $default_filename = $other_args['default filename'];
150 - } else {
151 - $default_filename = '';
 43+ $textarea_attrs['class'] .= ' autocompleteInput';
15244 }
153 - $text .= self::uploadableHTML( $input_id, $delimiter, $default_filename, $cur_value, $other_args );
154 - }
15545
156 - $spanClass = 'inputSpan';
157 - if ( $is_mandatory ) {
158 - $spanClass .= ' mandatoryFieldSpan';
 46+ return $textarea_attrs;
15947 }
160 - $text = "\n" . Html::rawElement( 'span', array( 'class' => $spanClass ), $text );
161 -
162 - return $text;
16348 }
164 -
165 - public static function getParameters() {
166 - $params = parent::getParameters();
167 - $params = array_merge( $params, SFTextWithAutocompleteInput::getAutocompletionParameters() );
168 - return $params;
169 - }
170 -
171 - /**
172 - * Returns the HTML code to be included in the output page for this input.
173 - */
174 - public function getHtmlText() {
175 - return self::getHTML(
176 - $this->mCurrentValue,
177 - $this->mInputName,
178 - $this->mIsMandatory,
179 - $this->mIsDisabled,
180 - $this->mOtherArgs
181 - );
182 - }
183 -}
Index: trunk/extensions/SemanticForms/includes/forminputs/SF_FormInput.php
@@ -320,17 +320,20 @@
321321 if ( $input->getJsInitFunctionData() || $input->getJsValidationFunctionData() ) {
322322
323323 $jstext = '';
 324+ $input_id = $input_name == 'sf_free_text' ? 'sf_free_text' : "input_$sfgFieldNum";
324325
325326 foreach ( $input->getJsInitFunctionData() as $jsInitFunctionData ) {
326 - $jstext .= "jQuery('#input_$sfgFieldNum').SemanticForms_registerInputInit({$jsInitFunctionData['name']}, {$jsInitFunctionData['param']} );";
 327+ $jstext .= "jQuery('#$input_id').SemanticForms_registerInputInit({$jsInitFunctionData['name']}, {$jsInitFunctionData['param']} );";
327328 }
328329
329330 foreach ( $input->getJsValidationFunctionData() as $jsValidationFunctionData ) {
330 - $jstext .= "jQuery('#input_$sfgFieldNum').SemanticForms_registerInputValidation( {$jsValidationFunctionData['name']}, {$jsValidationFunctionData['param']});";
 331+ $jstext .= "jQuery('#$input_id').SemanticForms_registerInputValidation( {$jsValidationFunctionData['name']}, {$jsValidationFunctionData['param']});";
331332 }
332333
333334 if ( $modules !== null ) {
334 - $jstext = 'mw.loader.using(' . json_encode( $modules ) . ', function(){' . $jstext . '});';
 335+ $jstext = 'mw.loader.using(' . json_encode( $modules )
 336+ . ',function(){' . $jstext
 337+ . '},function(e,module){alert(module+": "+e);});';
335338 }
336339
337340 $jstext = 'jQuery(function(){' . $jstext . '});';
Index: trunk/extensions/SemanticForms/includes/forminputs/SF_TextAreaInput.php
@@ -14,6 +14,45 @@
1515 */
1616 class SFTextAreaInput extends SFFormInput {
1717
 18+ protected $mUseWikieditor = false;
 19+
 20+ /**
 21+ * Constructor for the SFTextAreaInput class.
 22+ *
 23+ * @param String $input_number
 24+ * The number of the input in the form. For a simple HTML input element
 25+ * this should end up in the id attribute in the format 'input_<number>'.
 26+ * @param String $cur_value
 27+ * The current value of the input field. For a simple HTML input
 28+ * element this should end up in the value attribute.
 29+ * @param String $input_name
 30+ * The name of the input. For a simple HTML input element this should
 31+ * end up in the name attribute.
 32+ * @param Array $other_args
 33+ * An associative array of other parameters that were present in the
 34+ * input definition.
 35+ */
 36+ public function __construct( $input_number, $cur_value, $input_name, $disabled, $other_args ) {
 37+
 38+ global $wgOut;
 39+
 40+ parent::__construct( $input_number, $cur_value, $input_name, $disabled, $other_args );
 41+
 42+ if (
 43+ array_key_exists( 'editor', $this->mOtherArgs ) &&
 44+ $this->mOtherArgs['editor'] == 'wikieditor' &&
 45+
 46+ method_exists( $wgOut, 'getResourceLoader' ) &&
 47+ in_array( 'jquery.wikiEditor', $wgOut->getResourceLoader()->getModuleNames() ) &&
 48+
 49+ class_exists( 'WikiEditorHooks' )
 50+ ) {
 51+ $this->mUseWikieditor = true;
 52+ $this->addJsInitFunctionData( 'window.ext.wikieditor.init' );
 53+ }
 54+ }
 55+
 56+
1857 public static function getName() {
1958 return 'textarea';
2059 }
@@ -30,68 +69,100 @@
3170 return array( '_wpg', '_str' );
3271 }
3372
34 - public static function getHTML( $cur_value, $input_name, $is_mandatory, $is_disabled, $other_args ) {
 73+ public static function getParameters() {
 74+ $params = parent::getParameters();
3575
36 - global $wgOut;
 76+ $params['preload'] = array(
 77+ 'name' => 'preload',
 78+ 'type' => 'string',
 79+ 'description' => wfMsg( 'sf_forminputs_preload' )
 80+ );
 81+ $params['rows'] = array(
 82+ 'name' => 'rows',
 83+ 'type' => 'int',
 84+ 'description' => wfMsg( 'sf_forminputs_rows' )
 85+ );
 86+ $params['cols'] = array(
 87+ 'name' => 'cols',
 88+ 'type' => 'int',
 89+ 'description' => wfMsg( 'sf_forminputs_cols' )
 90+ );
 91+ $params['maxlength'] = array(
 92+ 'name' => 'maxlength',
 93+ 'type' => 'int',
 94+ 'description' => wfMsg( 'sf_forminputs_maxlength' )
 95+ );
 96+ $params['placeholder'] = array(
 97+ 'name' => 'placeholder',
 98+ 'type' => 'string',
 99+ 'description' => wfMsg( 'sf_forminputs_placeholder' )
 100+ );
 101+ $params['autogrow'] = array(
 102+ 'name' => 'autogrow',
 103+ 'type' => 'boolean',
 104+ 'description' => wfMsg( 'sf_forminputs_autogrow' )
 105+ );
 106+ return $params;
 107+ }
 108+
 109+ /**
 110+ * Returns the names of the resource modules this input type uses.
 111+ *
 112+ * Returns the names of the modules as an array or - if there is only one
 113+ * module - as a string.
 114+ *
 115+ * @return null|string|array
 116+ */
 117+ public function getResourceModuleNames() {
 118+ return $this->mUseWikieditor?'ext.semanticforms.wikieditor':null;
 119+ }
 120+
 121+ protected function getTextAreaAttributes() {
 122+
37123 global $sfgTabIndex, $sfgFieldNum;
38124
39125 // Use a special ID for the free text field, for FCK's needs.
40 - $input_id = $input_name == 'sf_free_text' ? 'sf_free_text' : "input_$sfgFieldNum";
 126+ $input_id = $this->mInputName == 'sf_free_text' ? 'sf_free_text' : "input_$sfgFieldNum";
41127
42 - if ( array_key_exists( 'editor', $other_args ) &&
43 - $other_args['editor'] == 'wikieditor' &&
 128+ if ( $this->mUseWikieditor ) {
44129
45 - method_exists( $wgOut, 'getResourceLoader' ) &&
46 - in_array( 'jquery.wikiEditor', $wgOut->getResourceLoader()->getModuleNames() ) &&
47 -
48 - class_exists( 'WikiEditorHooks' ) ) {
49 -
50130 // load modules for all enabled features
51131 WikiEditorHooks::editPageShowEditFormInitial( $this );
 132+ $className = 'wikieditor ';
 133+ } else {
 134+ $className = '';
 135+ }
52136
53 - $wgOut->addModules( 'ext.semanticforms.wikieditor' );
 137+ $className .= ( $this->mIsMandatory ) ? 'mandatoryField' : 'createboxInput';
54138
55 - $jstext = <<<JAVASCRIPT
56 - jQuery( jQuery('#$input_id').SemanticForms_registerInputInit( ext.wikieditor.init, null ) );
57 -JAVASCRIPT;
58 -
59 - // write JS code directly to the page's code
60 - $wgOut->addScript( Html::inlineScript( $jstext ) );
61 -
62 - $className = "wikieditor ";
63 - } else {
64 - $className = "";
 139+ if ( array_key_exists( 'class', $this->mOtherArgs ) ) {
 140+ $className .= ' ' . $this->mOtherArgs['class'];
65141 }
66142
67 - $className .= ( $is_mandatory ) ? 'mandatoryField' : 'createboxInput';
68 - if ( array_key_exists( 'class', $other_args ) ) {
69 - $className .= " " . $other_args['class'];
 143+ if ( array_key_exists( 'autogrow', $this->mOtherArgs ) ) {
 144+ $className .= ' autoGrow';
70145 }
71146
72 - if ( array_key_exists( 'rows', $other_args ) ) {
73 - $rows = $other_args['rows'];
 147+ if ( array_key_exists( 'rows', $this->mOtherArgs ) ) {
 148+ $rows = $this->mOtherArgs['rows'];
74149 } else {
75150 $rows = 5;
76151 }
77152
78 - if ( array_key_exists( 'autogrow', $other_args ) ) {
79 - $className .= ' autoGrow';
80 - }
81 -
82153 $textarea_attrs = array(
83154 'tabindex' => $sfgTabIndex,
 155+ 'name' => $this->mInputName,
84156 'id' => $input_id,
85 - 'name' => $input_name,
 157+ 'class' => $className,
86158 'rows' => $rows,
87 - 'class' => $className,
88159 );
89160
90 - if ( array_key_exists( 'cols', $other_args ) ) {
91 - $textarea_attrs['cols'] = $other_args['cols'];
 161+ if ( array_key_exists( 'cols', $this->mOtherArgs ) ) {
 162+ $textarea_attrs['cols'] = $this->mOtherArgs['cols'];
92163 // Needed to prevent CSS from overriding the manually-
93164 // set width.
94165 $textarea_attrs['style'] = 'width: auto';
95 - } elseif ( array_key_exists( 'autogrow', $other_args ) ) {
 166+ } elseif ( array_key_exists( 'autogrow', $this->mOtherArgs ) ) {
96167 // If 'autogrow' has been set, automatically set
97168 // the number of columns - otherwise, the Javascript
98169 // won't be able to know how many characters there
@@ -103,11 +174,12 @@
104175 $textarea_attrs['style'] = 'width: 100%';
105176 }
106177
107 - if ( $is_disabled ) {
 178+ if ( $this->mIsDisabled ) {
108179 $textarea_attrs['disabled'] = 'disabled';
109180 }
110 - if ( array_key_exists( 'maxlength', $other_args ) ) {
111 - $maxlength = $other_args['maxlength'];
 181+
 182+ if ( array_key_exists( 'maxlength', $this->mOtherArgs ) ) {
 183+ $maxlength = $this->mOtherArgs['maxlength'];
112184 // For every actual character pressed (i.e., excluding
113185 // things like the Shift key), reduce the string to its
114186 // allowed length if it's exceeded that.
@@ -119,63 +191,29 @@
120192 $textarea_attrs['onKeyDown'] = $maxLengthJSCheck;
121193 $textarea_attrs['onKeyUp'] = $maxLengthJSCheck;
122194 }
123 - if ( array_key_exists( 'placeholder', $other_args ) ) {
124 - $textarea_attrs['placeholder'] = $other_args['placeholder'];
125 - }
126195
127 - $text = Html::element( 'textarea', $textarea_attrs, $cur_value );
128 - $spanClass = 'inputSpan';
129 - if ( $is_mandatory ) {
130 - $spanClass .= ' mandatoryFieldSpan';
 196+ if ( array_key_exists( 'placeholder', $this->mOtherArgs ) ) {
 197+ $textarea_attrs['placeholder'] = $this->mOtherArgs['placeholder'];
131198 }
132 - $text = Html::rawElement( 'span', array( 'class' => $spanClass ), $text );
133199
134 - return $text;
 200+ return $textarea_attrs;
135201 }
136202
137 - public static function getParameters() {
138 - $params = parent::getParameters();
139 - $params[] = array(
140 - 'name' => 'preload',
141 - 'type' => 'string',
142 - 'description' => wfMsg( 'sf_forminputs_preload' )
143 - );
144 - $params[] = array(
145 - 'name' => 'rows',
146 - 'type' => 'int',
147 - 'description' => wfMsg( 'sf_forminputs_rows' )
148 - );
149 - $params[] = array(
150 - 'name' => 'cols',
151 - 'type' => 'int',
152 - 'description' => wfMsg( 'sf_forminputs_cols' )
153 - );
154 - $params[] = array(
155 - 'name' => 'maxlength',
156 - 'type' => 'int',
157 - 'description' => wfMsg( 'sf_forminputs_maxlength' )
158 - );
159 - $params[] = array(
160 - 'name' => 'placeholder',
161 - 'type' => 'string',
162 - 'description' => wfMsg( 'sf_forminputs_placeholder' )
163 - );
164 - $params[] = array(
165 - 'name' => 'autogrow',
166 - 'type' => 'boolean',
167 - 'description' => wfMsg( 'sf_forminputs_autogrow' )
168 - );
169 - return $params;
170 - }
171 -
172203 /**
173204 * Returns the HTML code to be included in the output page for this input.
174205 */
175206 public function getHtmlText() {
176207
177 - return self::getHTML(
178 - $this->mCurrentValue, $this->mInputName, $this->mIsMandatory, $this->mIsDisabled, $this->mOtherArgs
179 - );
 208+ $textarea_attrs = $this->getTextAreaAttributes();
 209+
 210+ $text = Html::element( 'textarea', $textarea_attrs, $this->mCurrentValue );
 211+ $spanClass = 'inputSpan';
 212+ if ( $this->mIsMandatory ) {
 213+ $spanClass .= ' mandatoryFieldSpan';
180214 }
 215+ $text = Html::rawElement( 'span', array( 'class' => $spanClass ), $text );
181216
 217+ return $text;
182218 }
 219+
 220+}
Index: trunk/extensions/SemanticForms/libs/SF_wikieditor.js
@@ -3,13 +3,13 @@
44 window.ext = {};
55 }
66
7 -window.ext.wikieditor = new function(){
 7+window.ext.wikieditor = {
88
99 // initialize the wikieditor on the specified element
10 - this.init = function init ( input_id, params ) {
 10+ init : function init ( input_id, params ) {
1111
 12+ jQuery( function() {
1213 if ( window.mediaWiki ) {
13 - mediaWiki.loader.using( 'ext.semanticforms.wikieditor', function(){
1414
1515 var input = jQuery( '#' + input_id );
1616
@@ -41,20 +41,8 @@
4242 }
4343 });
4444
45 - // load toc
46 - // TODO: Can this be enabled? Should it?
47 -// mediaWiki.loader.using( ['jquery.wikiEditor.toc' ] , function(){
48 -// if ( jQuery.wikiEditor.isSupported( jQuery.wikiEditor.modules.toc ) ) {
49 -//
50 -// input.wikiEditor( 'addModule', 'toc' );
51 -//
52 -// }
53 -// });
54 -
55 -
56 -
57 - } );
5845 }
59 - }
6046
 47+ });
 48+ }
6149 };

Follow-up revisions

RevisionCommit summaryAuthorDate
r112906Fix for r112829 - re-added getDefaultPropTypes() declaration, so that 'textar...yaron17:53, 2 March 2012