r62547 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62546‎ | r62547 | r62548 >
Date:21:12, 15 February 2010
Author:kipcool
Status:deferred
Tags:
Comment:
Add new rows when clicking on the "+" button
Modified paths:
  • /trunk/extensions/Wikidata/OmegaWiki/Editor.php (modified) (history)
  • /trunk/extensions/Wikidata/OmegaWiki/suggest.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/OmegaWiki/suggest.js
@@ -133,6 +133,7 @@
134134
135135 function suggestLinkClicked(event, suggestLink) {
136136 var suggestLinkId = suggestLink.id;
 137+ // removing the "link" at the end of the Id
137138 var suggestPrefix = suggestLinkId.substr(0, suggestLinkId.length - 4);
138139
139140 var suggestDiv = document.getElementById(suggestPrefix + "div");
@@ -922,3 +923,59 @@
923924 }
924925 return "";
925926 }
 927+
 928+
 929+// add a new row for translation or definition
 930+function addEmptyRow(elementId) {
 931+ var element = document.getElementById( elementId );
 932+
 933+ var container = element.parentNode ;
 934+
 935+ // create a clone to work on
 936+ var new_element = element.cloneNode(true);
 937+
 938+ // removes the green button for the old row
 939+ element.firstChild.removeChild ( element.firstChild.firstChild ) ;
 940+
 941+ // all new textareas field should be set empty
 942+ var textAreaList = new_element.getElementsByTagName('textarea');
 943+ for (i=0; i<textAreaList.length ; i++)
 944+ {
 945+ if ( textAreaList[i].type == 'text' ) {
 946+ textAreaList[i].value = '' ;
 947+ }
 948+ }
 949+
 950+// for the spelling, it is not a textarea but an input type=text.
 951+// (some other input fields, hidden, are needed, so we should not clear all <input> )
 952+ var inputList = new_element.getElementsByTagName('input');
 953+ for (i=0; i<inputList.length ; i++)
 954+ {
 955+ if ( inputList[i].type == 'text' ) {
 956+ inputList[i].value = '' ;
 957+ }
 958+ if ( inputList[i].name == 'onUpdate' ) {
 959+ inputList[i].value = inputList[i].value.replace("add-", "add-X-") ;
 960+ }
 961+ }
 962+
 963+ recursiveChangeId( new_element ) ;
 964+
 965+ // add the element as the last one (null)
 966+ container.appendChild(new_element );
 967+}
 968+
 969+function recursiveChangeId(element) {
 970+ if (element == null) return;
 971+ if ( element.hasChildNodes() ) {
 972+ var children = element.childNodes ;
 973+ for (var i=0; i<children.length ; i++)
 974+ {
 975+ recursiveChangeId ( children[i] ) ;
 976+ }
 977+ }
 978+
 979+ if ( element.id ) element.id = element.id.replace("add-", "add-X-") ;
 980+ if ( element.name ) element.name = element.name.replace("add-", "add-X-") ;
 981+ return ;
 982+}
Index: trunk/extensions/Wikidata/OmegaWiki/Editor.php
@@ -209,7 +209,7 @@
210210 public function save( IdStack $idPath, $value );
211211
212212 public function getUpdateValue( IdStack $idPath );
213 - public function getAddValue( IdStack $idPath );
 213+ public function getAddValues( IdStack $idPath );
214214
215215 public function getEditors();
216216 public function getAttributeEditorMap();
@@ -316,7 +316,7 @@
317317 return null;
318318 }
319319
320 - public function getAddValue( IdStack $idPath ) {
 320+ public function getAddValues( IdStack $idPath ) {
321321 return null;
322322 }
323323
@@ -344,28 +344,42 @@
345345 $this->controller = $controller;
346346 }
347347
348 - public function getAddValue( IdStack $idPath ) {
 348+ public function getAddValues( IdStack $idPath ) {
349349 $addStructure = $this->getAddStructure();
350350
351351 if ( count( $addStructure->getAttributes() ) > 0 ) {
352 - $relation = new ArrayRecordSet( $addStructure, $addStructure ); // TODO Determine real key
353 - $values = array();
 352+ $relations = array();
354353
355 - foreach ( $this->getEditors() as $editor )
 354+ $value_array_array = array(array());
 355+
 356+ foreach ( $this->getEditors() as $editor ) {
356357 if ( $attribute = $editor->getAddAttribute() ) {
357358 $idPath->pushAttribute( $attribute );
358 - $values[] = $editor->getAddValue( $idPath );
 359+
 360+ $addValues = $editor->getAddValues( $idPath );
 361+ $i = 0;
 362+ foreach ( $addValues as $value ) {
 363+ $value_array_array[$i][] = $value ;
 364+ $i++;
 365+ }
 366+
359367 $idPath->popAttribute();
360368 }
 369+ }
361370
362 - $relation->addRecord( $values );
 371+ foreach ( $value_array_array as $value_array ) {
 372+ $relation = new ArrayRecordSet( $addStructure, $addStructure ) ; // TODO Determine real key
 373+ $relation->addRecord( $value_array );
 374+ $relations[] = $relation ;
 375+ }
363376
364 - return $relation;
 377+ return $relations ;
365378 }
366379 else
367380 return null;
368381 }
369382
 383+
370384 protected function saveRecord( IdStack $idPath, Record $record ) {
371385 foreach ( $this->getEditors() as $editor ) {
372386 $attribute = $editor->getAttribute();
@@ -452,16 +466,25 @@
453467 }
454468
455469 public function getAddRecord( IdStack $idPath, Structure $structure, $editors ) {
456 - $result = new ArrayRecord( $structure );
 470+ $results = array();
457471
458472 foreach ( $editors as $editor )
459473 if ( $attribute = $editor->getAddAttribute() ) {
460474 $idPath->pushAttribute( $attribute );
461 - $result->setAttributeValue( $attribute, $editor->getAddValue( $idPath ) );
 475+ $addValues = $editor->getAddValues( $idPath );
 476+ $i = 0 ;
 477+ foreach ( $addValues as $value ) {
 478+ if ( ! $results[$i] ) {
 479+ $results[$i] = new ArrayRecord( $structure );
 480+ }
 481+ $results[$i]->setAttributeValue( $attribute, $value );
 482+ $i++ ;
 483+ }
 484+
462485 $idPath->popAttribute();
463486 }
464487
465 - return $result;
 488+ return $results;
466489 }
467490
468491 public function getUpdateRecord( IdStack $idPath, Structure $structure, $editors ) {
@@ -484,8 +507,12 @@
485508
486509 if ( count( $addStructure->getAttributes() ) > 0 ) {
487510 $addEditors = $this->getAddEditors(); // array of editors
488 - $record = $this->getAddRecord( $idPath, $addStructure, $addEditors ); // array of records
489 - $this->controller->add( $idPath, $record );
 511+
 512+ $records = array();
 513+ $records = $this->getAddRecord( $idPath, $addStructure, $addEditors );
 514+ foreach ( $records as $record ) {
 515+ $this->controller->add( $idPath, $record );
 516+ }
490517 }
491518 }
492519
@@ -779,7 +806,7 @@
780807
781808 # + is add new Fo o(but grep this file for Add.png for more)
782809 if ( $allowRemove ) {
783 - $result .= '<td class="add"><img src="' . $wgScriptPath . '/extensions/Wikidata/Images/Add.png" title="' . wfMsgSc( "AddHint" ) . '" alt="Add"/></td>' . EOL;
 810+ $result .= '<td class="add"><img src="' . $wgScriptPath . '/extensions/Wikidata/Images/Add.png" title="' . wfMsgSc( "AddHint" ) . '" alt="Add" onclick="addEmptyRow(this.parentNode.parentNode.id);"/></td>' . EOL;
784811 }
785812
786813 $result .= $this->getStructureAsAddCells( $idPath, $this );
@@ -829,17 +856,25 @@
830857 return $result;
831858 }
832859
833 - public function getAddValue( IdStack $idPath ) {
834 - $result = new ArrayRecord( $this->getAddStructure() );
 860+ public function getAddValues( IdStack $idPath ) {
 861+ $results = array();
835862
836863 foreach ( $this->getEditors() as $editor )
837864 if ( $attribute = $editor->getAddAttribute() ) {
838865 $idPath->pushAttribute( $attribute );
839 - $result->setAttributeValue( $attribute, $editor->getAddValue( $idPath ) );
 866+ $addValues = array();
 867+ $addValues = $editor->getAddValues( $idPath );
 868+ $i = 0 ;
 869+ foreach ( $addValues as $value ) {
 870+ if ( ! $results[$i] ) {
 871+ $results[$i] = new ArrayRecord( $this->getAddStructure() );
 872+ }
 873+ $results[$i]->setAttributeValue( $attribute, $value );
 874+ $i++ ;
 875+ }
840876 $idPath->popAttribute();
841877 }
842 -
843 - return $result;
 878+ return $results ;
844879 }
845880
846881 public function getUpdateAttribute() {
@@ -947,8 +982,18 @@
948983 return $this->getInputValue( "update-" . $idPath->getId() );
949984 }
950985
951 - public function getAddValue( IdStack $idPath ) {
952 - return $this->getInputValue( "add-" . $idPath->getId() );
 986+ // tries to get multiple "add" values e.g. adding multiple translations at once
 987+ // the "X-" corresponds to what is in suggest.js, function recursiveChangeId
 988+ public function getAddValues( IdStack $idPath ) {
 989+ $addValues = array();
 990+ $prefix = "add-" ;
 991+
 992+ while ( ( $value = $this->getInputValue( $prefix . $idPath->getId() ) ) != '' ) {
 993+ $addValues[] = $value ;
 994+ $prefix = $prefix . "X-" ;
 995+ }
 996+
 997+ return $addValues ;
953998 }
954999
9551000 public function view( IdStack $idPath, $value ) {
@@ -1751,8 +1796,8 @@
17521797 return $this->wrappedEditor->getUpdateValue( $idPath );
17531798 }
17541799
1755 - public function getAddValue( IdStack $idPath ) {
1756 - return $this->wrappedEditor->getAddValue( $idPath );
 1800+ public function getAddValues( IdStack $idPath ) {
 1801+ return $this->wrappedEditor->getAddValues( $idPath );
17571802 }
17581803
17591804 public function getEditors() {
@@ -1906,7 +1951,7 @@
19071952
19081953 # For which class is this add?
19091954 $result .= '<li>' .
1910 - '<h' . $this->headerLevel . '><span id="collapse-' . $recordId . '" class="toggle ' . addCollapsablePrefixToClass( $class ) . '" onclick="toggle(this, event);">' . $this->getExpansionPrefix( $idPath->getClass(), $idPath->getId() ) . ' <img src="' . $wgScriptPath . '/extensions/Wikidata/Images/Add.png" title="Enter new list item to add" alt="Add"/>' . $this->captionEditor->add( $idPath ) . '</span></h' . $this->headerLevel . '>' . EOL;
 1955+ '<h' . $this->headerLevel . '><span id="collapse-' . $recordId . '" class="toggle ' . addCollapsablePrefixToClass( $class ) . '" onclick="toggle(this, event);">' . $this->getExpansionPrefix( $idPath->getClass(), $idPath->getId() ) . ' <img src="' . $wgScriptPath . '/extensions/Wikidata/Images/Add.png" title="Enter new list item to add" alt="Add" onclick="addEmptyRow(this.parentNode.parentNode.id);"/>' . $this->captionEditor->add( $idPath ) . '</span></h' . $this->headerLevel . '>' . EOL;
19111956 $idPath->popAttribute();
19121957
19131958 $idPath->pushAttribute( $valueAttribute );
@@ -2255,8 +2300,13 @@
22562301 $this->recordEditor->save( $idPath, $record );
22572302 $idPath->popKey();
22582303 }
2259 - else
2260 - $this->controller->add( $idPath, $this->recordEditor->getAddValue( $idPath ) );
 2304+ else {
 2305+ $addValues = array() ;
 2306+ $addValues = $this->recordEditor->getAddValues( $idPath ) ;
 2307+ foreach ( $addValues as $addValue ) {
 2308+ $this->controller->add( $idPath, $addValue );
 2309+ }
 2310+ }
22612311 }
22622312
22632313 public function setRecordEditor( Editor $recordEditor ) {

Status & tagging log