r107229 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107228‎ | r107229 | r107230 >
Date:22:34, 24 December 2011
Author:jarry1250
Status:resolved (Comments)
Tags:
Comment:
(JavaScript) implement validation and rendering of language codes via AJAX method; also implement nice JS remove links for translation groups
Modified paths:
  • /trunk/extensions/TranslateSvg/ext.translateSvg.core.js (modified) (history)

Diff [purge]

Index: trunk/extensions/TranslateSvg/ext.translateSvg.core.js
@@ -1,30 +1,94 @@
2 -var paragraph = $('<p></p>').text( mw.msg('translatesvg-add') );
3 -var match = paragraph.html().match( /\[\[#addlanguage\|(.*?)\]\]/ );
4 -addtext = paragraph.html().replace( /\[\[#addlanguage\|.*?\]\]/, '<a id="newtrans" href="#"></a>' );
5 -paragraph.html( addtext );
6 -$('form#specialtranslatesvg').prepend(paragraph);
7 -$('#newtrans')
8 - .text( match[1] )
9 - .click(function() {
10 - var langcode = prompt( mw.msg('translatesvg-specify') );
11 - if( langcode !== null ){
12 - var newfieldset = $('fieldset#fallback').clone();
13 - newfieldset.find('input').each( function (){
14 - $(this).attr( 'id', $(this).attr( 'id' ).toString().replace( 'fallback', langcode ) );
15 - $(this).attr( 'name', $(this).attr( 'name' ).toString().replace( 'fallback', langcode ) );
16 - $(this).attr( 'value', getExisting( $(this).attr( 'value' ) ) );
17 - });
18 - newfieldset.find('label').each( function (){
19 - $(this).attr( 'for', $(this).attr( 'for' ).toString().replace( 'fallback', langcode ) );
20 - });
21 - newfieldset.attr( 'id', langcode );
22 - newfieldset = $('a#newtrans').after( newfieldset );
23 - $('fieldset#' + langcode + ' legend').remove();
24 - $('fieldset#' + langcode ).prepend( '<legend>' + codeToText( langcode ) + '</legend>' );
25 - }
26 - return false;
27 -}); //TODO: validate input
 2+jQuery( function( $ ) {
 3+ var languages = { isset: false };
 4+
 5+ //Replace fake "add language" link with real one and add it
 6+ var paragraph = $( '<p id="newtrans"></p>' ).text( mw.msg( 'translatesvg-add' ) );
 7+ var match = paragraph.html().match( /\[\[#addlanguage\|(.*?)\]\]/ );
 8+ paragraph.html( paragraph.html().replace( match[0], '<a id="newtranslink" href="#">' + match[1] + '</a>' ) );
 9+ $( 'div.mw-specialpage-summary' ).append( paragraph );
 10+
 11+ //Add in "remove" links
 12+ $( 'legend' ).each( function(){
 13+ var langcode = $(this).parent().attr('id');
 14+ if( langcode !== 'qqq' && langcode !== 'fallback' ){
 15+ $(this).append( '&#160;' ).append( getRemoveLink() );
 16+ }
 17+ } );
 18+
 19+ $( '#newtranslink' )
 20+ .click( function() {
 21+ var langcode = prompt( mw.msg( 'translatesvg-specify' ) );
 22+ if( langcode !== null ){
 23+ if( !langcode.match( /^[a-z]+( ?:-[a-z]+ )?/ ) ){
 24+ //Not valid language code
 25+ return false; //TODO: more helpful?
 26+ }
 27+ if( $( 'fieldset#'+langcode ).length !== 0 ){
 28+ //Already exists
 29+ return false; //TODO: more helpful?
 30+ }
 31+ if( languages.isset ){
 32+ if( !( langcode in languages ) ){
 33+ //Have already loaded valid languages and this ain't one of them
 34+ return false; //TODO: more helpful?
 35+ }
 36+ $( 'form#specialtranslatesvg' ).prepend( getNewFieldset( langcode, languages[langcode] ) );
 37+ } else {
 38+ var temp = $( '<fieldset id="temp"><br /></fieldset>' ).append( $.createSpinner() );
 39+ $( 'form#specialtranslatesvg' ).prepend( temp );
 40+ jQuery.getJSON(
 41+ mw.util.wikiScript( 'api' ), {
 42+ 'format': 'json',
 43+ 'action': 'query',
 44+ 'meta': 'siteinfo',
 45+ 'siprop': 'languages'
 46+ }, function( data ) {
 47+ var length = data.query.languages.length;
 48+ for( var i = 0; i < length; i++ ){
 49+ languages[ data.query.languages[i]['code'] ] = data.query.languages[i]['*'];
 50+ }
 51+ if( ( langcode in languages ) ){
 52+ $( 'form#specialtranslatesvg' ).prepend( getNewFieldset( langcode, languages[langcode] ) );
 53+ } else {
 54+ //Have now loaded valid languages and this ain't one of them
 55+ //TODO: more helpful?
 56+ }
 57+ $( 'fieldset#temp' ).remove();
 58+ languages.isset = true;
 59+ }
 60+ );
 61+ }
 62+ }
 63+ return false;
 64+ } );
 65+} );
2866
 67+function getNewFieldset( langcode, langname ){
 68+ var newfieldset = $( 'fieldset#fallback' ).clone();
 69+ newfieldset.find( 'input' ).each( function (){
 70+ $( this ).attr( 'id', $( this ).attr( 'id' ).toString().replace( 'fallback', langcode ) );
 71+ $( this ).attr( 'name', $( this ).attr( 'name' ).toString().replace( 'fallback', langcode ) );
 72+ $( this ).attr( 'value', getExisting( $( this ).attr( 'value' ) ) );
 73+ } );
 74+ newfieldset.find( 'label' ).each( function (){
 75+ $( this ).attr( 'for', $( this ).attr( 'for' ).toString().replace( 'fallback', langcode ) );
 76+ } );
 77+ newfieldset.attr( 'id', langcode );
 78+ newfieldset.find( 'legend' ).each( function (){
 79+ $( this ).text( langname );
 80+ $( this ).append( '&#160;' ).append( getRemoveLink() );
 81+ } );
 82+ return newfieldset;
 83+}
 84+
 85+function getRemoveLink(){
 86+ var removelink = $( '<a href="#"><abbr title="'
 87+ + mw.msg( 'translatesvg-remove' )
 88+ + '"><small>[x]</small></abbr></a>' );
 89+ removelink.click( function(){ $(this).parent().parent().remove(); } );
 90+ return removelink;
 91+}
 92+
2993 function getExisting( fallback ){
3094 //No need to check for an existing translation since we're creating a new language box
3195 if( fallback.match( /^[0-9 .,]+$/ ) ){
@@ -32,8 +96,4 @@
3397 } else {
3498 return '';
3599 }
36 -}
37 -function codeToText( langcode ){
38 - //TODO
39 - return langcode;
40100 }
\ No newline at end of file

Follow-up revisions

RevisionCommit summaryAuthorDate
r107239Fix per SPQRobin: Internationalise thumb alignment; Fix very poor step checki...jarry125023:19, 24 December 2011

Comments

#Comment by Reedy (talk | contribs)   22:56, 24 December 2011

Minor addition/note

Some of your code in SpecialTranslatesvg is getting a bit long

				if( $language !== 'qqq' ){
					$grouphtml .= Html::element( 'br' ) .
							"   " . Xml::inputLabel( wfMsg( 'translatesvg-xcoordinate-pre' ), $language.'-'.$i.'-x', $language.'-'.$i.'-x', 5, $existing['x'] ) . 
							"   " . Xml::inputLabel( wfMsg( 'translatesvg-ycoordinate-pre' ), $language.'-'.$i.'-y', $language.'-'.$i.'-y', 5, $existing['y'] );
				}

Might want to break up onto a few more lines

#Comment by Jarry1250 (talk | contribs)   23:10, 24 December 2011

Yes, it'll get longer in the next commit and then shorter again (I'm psychic). I'll check again when I've merged fully.

#Comment by Nikerabbit (talk | contribs)   11:05, 25 December 2011
+	var removelink = $( '<a href="#"><abbr title="'
+				+ mw.msg( 'translatesvg-remove' )

The message should be escaped properly.

#Comment by Jarry1250 (talk | contribs)   16:30, 26 December 2011

(Fixed in r107239 as something of an aside.)

Status & tagging log