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( ' ' ).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 | +} ); |
28 | 66 | |
| 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( ' ' ).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 | + |
29 | 93 | function getExisting( fallback ){ |
30 | 94 | //No need to check for an existing translation since we're creating a new language box |
31 | 95 | if( fallback.match( /^[0-9 .,]+$/ ) ){ |
— | — | @@ -32,8 +96,4 @@ |
33 | 97 | } else { |
34 | 98 | return ''; |
35 | 99 | } |
36 | | -} |
37 | | -function codeToText( langcode ){ |
38 | | - //TODO |
39 | | - return langcode; |
40 | 100 | } |
\ No newline at end of file |