r84181 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84180‎ | r84181 | r84182 >
Date:17:12, 17 March 2011
Author:yaron
Status:deferred
Tags:
Comment:
Greatly simplified the Javascript that adds more rows - it now just copies over existing HTML, instead of creating its own
Modified paths:
  • /trunk/extensions/SemanticForms/specials/SF_CreateClass.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/specials/SF_CreateClass.php
@@ -19,6 +19,28 @@
2020 SFUtils::loadMessages();
2121 }
2222
 23+ static function addJavascript( $numStartingRows ) {
 24+ global $wgOut;
 25+
 26+ SFUtils::addJavascriptAndCSS();
 27+
 28+ $jsText =<<<END
 29+<script>
 30+var rowNum = $numStartingRows;
 31+function createClassAddRow() {
 32+ rowNum++;
 33+ newRow = jQuery('#starterRow').clone().css('display', '');
 34+ newHTML = newRow.html().replace(/starter/g, rowNum);
 35+ newRow.html(newHTML);
 36+ jQuery('#mainTable').append(newRow);
 37+}
 38+
 39+</script>
 40+
 41+END;
 42+ $wgOut->addScript( $jsText );
 43+ }
 44+
2345 function execute( $query ) {
2446 global $wgOut, $wgRequest, $wgUser, $sfgScriptPath;
2547 global $wgLang, $smwgContLang;
@@ -31,8 +53,8 @@
3254
3355 $this->setHeaders();
3456 $wgOut->addExtensionStyle( $sfgScriptPath . "/skins/SemanticForms.css" );
35 -
3657 $numStartingRows = 10;
 58+ self::addJavascript( $numStartingRows );
3759
3860 $create_button_text = wfMsg( 'create' );
3961
@@ -130,57 +152,14 @@
131153 $list_of_values_label = wfMsg( 'sf_createclass_listofvalues' ) . '?';
132154
133155 $text = <<<END
134 - <script>
135 -
136 -var NumOfRow = $numStartingRows;
137 -function addRowDynamic(options) {
138 - NumOfRow++;
139 - // get the reference of the main div
140 - var mainDiv = document.getElementById('addrows');
141 -
142 - // create new div that will work as a container
143 - var newDiv = document.createElement('div');
144 - newDiv.setAttribute('id','innerDiv' + NumOfRow);
145 -
146 - //create span to contain the text
147 - var newtable = document.createElement('table');
148 - var newRow = document.createElement('tr');
149 - var newCol1 = document.createElement('td');
150 - newCol1.innerHTML="<input type=\"text\" size=\"25\" name=\"property_name_" + NumOfRow + "\" />";
151 - var newCol2 = document.createElement('td');
152 - newCol2.innerHTML="<input type=\"text\" size=\"25\" name=\"field_name_" + NumOfRow + "\" />";
153 - var newCol3 = document.createElement('td');
154 - newCol3.innerHTML = "<input type=\"text\" size=\"25\" name=\"allowed_values_" + NumOfRow + "\" />";
155 - var newCol4 = document.createElement('td');
156 - newCol4.innerHTML = "<input type=\"checkbox\" name=\"is_list_" + NumOfRow + "\" />";
157 -
158 - var selectCol = document.createElement('td');
159 - var s = "<select id=\"property_dropdown_" + NumOfRow + "\" name=\"property_type_" + NumOfRow + "\">" + options + "</select>";
160 - var optionString = "";
161 - var myArray = options.split(',');
162 -
163 - for (i=0; i<myArray.length-1; i++) {
164 - optionString += "<option>" + myArray[i] + "</option>";
165 - }
166 - selectCol.innerHTML = "<select id=\"property_dropdown_" + NumOfRow + "\" name=\"property_type_" + NumOfRow + "\">" + optionString + "</select>";
167 -
168 - newRow.innerHTML = newCol1.innerHTML+newCol2.innerHTML + newCol3.innerHTML;
169 - newtable.innerHTML = "<tr>" + "<td>" + NumOfRow + ". " + newCol1.innerHTML + "</td>" + "<td>" + newCol2.innerHTML + "</td>" + "<td>" + selectCol.innerHTML + "</td>" + "<td>" + newCol3.innerHTML + "</td>" + "<td>" + newCol4.innerHTML + "</td></tr>";
170 - newDiv.appendChild(newtable);
171 -
172 - // finally append the new div to the main div
173 - mainDiv.appendChild(newDiv);
174 -}
175 -</script>
176 -
177156 <form action="" method="post">
178157 <p>$create_class_docu</p>
179158 <p>$leave_field_blank</p>
180159 <p>$template_name_label <input type="text" size="30" name="template_name"></p>
181160 <p>$form_name_label: <input type="text" size="30" name="form_name"></p>
182161 <p>$category_name_label <input type="text" size="30" name="category_name"></p>
183 - <div id="addrows">
184 - <table>
 162+ <div>
 163+ <table id="mainTable">
185164 <tr>
186165 <th colspan="2">$property_name_label</th>
187166 <th>$field_name_label</th>
@@ -190,14 +169,24 @@
191170 </tr>
192171
193172 END;
194 - for ( $i = 1; $i <= $numStartingRows; $i++ ) {
 173+ // Make one more row than what we're displaying - use the
 174+ // last row as a "starter row", to be cloned when the
 175+ // "Add another" button is pressed.
 176+ for ( $i = 1; $i <= $numStartingRows + 1; $i++ ) {
 177+ if ( $i == $numStartingRows + 1 ) {
 178+ $rowString = 'id="starterRow" style="display: none"';
 179+ $n = 'starter';
 180+ } else {
 181+ $rowString = '';
 182+ $n = $i;
 183+ }
195184 $text .= <<<END
196 - <tr>
197 - <td>$i.</td>
198 - <td><input type="text" size="25" name="property_name_$i" /></td>
199 - <td><input type="text" size="25" name="field_name_$i" /></td>
 185+ <tr $rowString>
 186+ <td>$n.</td>
 187+ <td><input type="text" size="25" name="property_name_$n" /></td>
 188+ <td><input type="text" size="25" name="field_name_$n" /></td>
200189 <td>
201 - <select id="property_dropdown_$i" name="property_type_$i">
 190+ <select name="property_type_$n">
202191
203192 END;
204193 $optionsStr ="";
@@ -208,8 +197,8 @@
209198 $text .= <<<END
210199 </select>
211200 </td>
212 - <td><input type="text" size="25" name="allowed_values_$i" /></td>
213 - <td><input type="checkbox" name="is_list_$i" /></td>
 201+ <td><input type="text" size="25" name="allowed_values_$n" /></td>
 202+ <td><input type="checkbox" name="is_list_$n" /></td>
214203
215204 END;
216205 }
@@ -223,7 +212,7 @@
224213 array(
225214 'type' => 'button',
226215 'value' => wfMsg( 'sf_formedit_addanother' ),
227 - 'onclick' => "addRowDynamic('$optionsStr')"
 216+ 'onclick' => "createClassAddRow()"
228217 )
229218 );
230219 $text .= Xml::tags( 'p', null, $add_another_button ) . "\n";