r52446 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52445‎ | r52446 | r52447 >
Date:10:57, 26 June 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
Add nice language and group selectors. Thanks to Splarka who helped with JavaScript
Modified paths:
  • /trunk/extensions/Translate/Stats.php (modified) (history)
  • /trunk/extensions/Translate/Translate.i18n.php (modified) (history)
  • /trunk/extensions/Translate/utils/JsSelectToInput.js (added) (history)
  • /trunk/extensions/Translate/utils/JsSelectToInput.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/Stats.php
@@ -92,8 +92,8 @@
9393 $this->eRadio( 'scale', $opts, array( 'days', 'hours' ) ) .
9494 $this->eRadio( 'count', $opts, array( 'edits', 'users' ) ) .
9595 '<tr><td colspan="2"><hr /></td></tr>' .
96 - $this->eInput( 'language', $opts ) .
97 - $this->eInput( 'group', $opts ) .
 96+ $this->eLanguage( 'language', $opts ) .
 97+ $this->eGroup( 'group', $opts ) .
9898 '<tr><td colspan="2"><hr /></td></tr>' .
9999 '<tr><td colspan="2">' . $submit . '</td></tr>'
100100 );
@@ -162,6 +162,75 @@
163163 return $s;
164164 }
165165
 166+ protected function eLanguage( $name, FormOptions $opts ) {
 167+ global $wgLang;
 168+ $value = $opts[$name];
 169+
 170+ $select = $this->languageSelector();
 171+ $select->setTargetId( 'language' );
 172+
 173+ return
 174+ '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
 175+ Xml::input( $name, 20, $value, array( 'id' => $name ) ) .
 176+ $select->getHtmlAndPrepareJs() .
 177+ '</td></tr>' . "\n";
 178+ }
 179+
 180+ protected function languageSelector() {
 181+ global $wgLang;
 182+ if ( is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
 183+ $languages = LanguageNames::getNames( $wgLang->getCode(),
 184+ LanguageNames::FALLBACK_NORMAL,
 185+ LanguageNames::LIST_MW_AND_CLDR
 186+ );
 187+ } else {
 188+ $languages = Language::getLanguageNames( false );
 189+ }
 190+
 191+ ksort( $languages );
 192+
 193+ $selector = new XmlSelect( 'mw-language-selector', 'mw-language-selector' );
 194+ foreach ( $languages as $code => $name ) {
 195+ $selector->addOption( "$code - $name", $code );
 196+ }
 197+
 198+ $jsSelect = new JsSelectToInput( $selector );
 199+ $jsSelect->setSourceId( 'mw-language-selector' );
 200+ return $jsSelect;
 201+ }
 202+
 203+ protected function eGroup( $name, FormOptions $opts ) {
 204+ global $wgLang;
 205+ $value = $opts[$name];
 206+
 207+ $select = $this->groupSelector();
 208+ $select->setTargetId( 'group' );
 209+
 210+ return
 211+ '<tr><td>' . $this->eLabel( $name ) . '</td><td>' .
 212+ Xml::input( $name, 20, $value, array( 'id' => $name ) ) .
 213+ $select->getHtmlAndPrepareJs() .
 214+ '</td></tr>' . "\n";
 215+ }
 216+
 217+ protected function groupSelector() {
 218+ $groups = MessageGroups::singleton()->getGroups();
 219+ foreach ( $groups as $key => $group ) {
 220+ if ( !$group->exists() ) unset($groups[$key]);
 221+ }
 222+
 223+ ksort( $groups );
 224+
 225+ $selector = new XmlSelect( 'mw-group-selector', 'mw-group-selector' );
 226+ foreach ( $groups as $code => $name ) {
 227+ $selector->addOption( $name->getLabel(), $code );
 228+ }
 229+
 230+ $jsSelect = new JsSelectToInput( $selector );
 231+ $jsSelect->setSourceId( 'mw-group-selector' );
 232+ return $jsSelect;
 233+ }
 234+
166235 protected function image( $opts ) {
167236 $title = $this->getTitle();
168237 $cgiparams = wfArrayToCgi( array( 'graphit' => true ), $opts->getAllValues() );
Index: trunk/extensions/Translate/utils/JsSelectToInput.php
@@ -0,0 +1,74 @@
 2+<?php
 3+
 4+
 5+class JsSelectToInput {
 6+ protected $targetId, $sourceId;
 7+ protected $select;
 8+ protected $buttonId;
 9+ protected $msg = 'translate-jssti-add';
 10+
 11+ public function __construct( XmlSelect $select = null ) {
 12+ $this->select = $select;
 13+ }
 14+
 15+ public function setSourceId( $id ) {
 16+ $this->sourceId = $id;
 17+ }
 18+
 19+ public function getSourceId() {
 20+ return $this->sourceId;
 21+ }
 22+
 23+ public function setTargetId( $id ) {
 24+ $this->targetId = $id;
 25+ }
 26+
 27+ public function getTargetId() {
 28+ return $this->targetId;
 29+ }
 30+
 31+ public function setMessage( $message ) {
 32+ $this->msg = $message;
 33+ }
 34+
 35+ public function getMessage() {
 36+ return $this->msg;
 37+ }
 38+
 39+ public function getHtmlAndPrepareJS() {
 40+ if ( $this->sourceId === false ) {
 41+ if ( is_callable( array( $select, 'getAttribute' ) ) ) {
 42+ $this->sourceId = $select->getAttribute['id'];
 43+ }
 44+ if ( !$this->sourceId ) {
 45+ throw new MWException( "ID needs to be specified for the selector" );
 46+ }
 47+ }
 48+
 49+
 50+ self::injectJs();
 51+ $html = $this->getButton( $this->msg, $this->sourceId, $this->targetId );
 52+ $html .= $this->select->getHtml();
 53+ return $html;
 54+ }
 55+
 56+ protected function getButton( $msg, $source, $target ) {
 57+ $source = Xml::escapeJsString( $source );
 58+ $target = Xml::escapeJsString( $target );
 59+ $html = Xml::element( 'input', array(
 60+ 'type' => 'button',
 61+ 'value' => wfMsg( $msg ),
 62+ 'onclick' => "appendFromSelect( '$source', '$target' );"
 63+ ) );
 64+ return $html;
 65+ }
 66+
 67+ public static function injectJs() {
 68+ static $done = false;
 69+ if ( $done ) return;
 70+
 71+ global $wgOut, $wgScriptPath;
 72+ $wgOut->addScriptFile( "$wgScriptPath/extensions/Translate/utils/JsSelectToInput.js" );
 73+ }
 74+
 75+}
\ No newline at end of file
Index: trunk/extensions/Translate/utils/JsSelectToInput.js
@@ -0,0 +1,9 @@
 2+function appendFromSelect(selectid,targetid) {
 3+ var select = document.getElementById(selectid);
 4+ var target = document.getElementById(targetid);
 5+ if(!target || !select) return
 6+ var atxt = select.options[select.selectedIndex].value;
 7+ if(!atxt) return
 8+ if(target.value.replace(/[\s\t\n]/ig,'') != '') atxt = ', ' + atxt;
 9+ target.value += atxt;
 10+}
\ No newline at end of file
Index: trunk/extensions/Translate/Translate.i18n.php
@@ -201,6 +201,8 @@
202202
203203 'translate-save' => 'Save ($1)',
204204
 205+ 'translate-jssti-add' => 'Add to list',
 206+
205207 # Meta message group descriptions
206208 'translate-group-desc-mediawikicore' => 'This message group contains the messages used in the current alpha version of MediaWiki ({{CURRENTVERSION}})',
207209 'translate-group-desc-mediawikimostused' => 'This message group contains the [http://translatewiki.net/wiki/Most_often_used_messages_in_MediaWiki 500 most often used messages] in MediaWiki',

Follow-up revisions

RevisionCommit summaryAuthorDate
r52447Forgot to commit this one in r52446...nikerabbit10:59, 26 June 2009
r52448Core change for r52446, allow querying params like idnikerabbit11:17, 26 June 2009

Status & tagging log