Index: trunk/extensions/LiveTranslate/includes/ext.livetranslate.js |
— | — | @@ -9,6 +9,8 @@ |
10 | 10 | |
11 | 11 | var currentLang = 'en'; // TODO |
12 | 12 | |
| 13 | + var runningJobs = 0; |
| 14 | + |
13 | 15 | // Compatibility with pre-RL code. |
14 | 16 | // Messages will have been loaded into wgPushMessages. |
15 | 17 | if ( typeof mediaWiki === 'undefined' ) { |
— | — | @@ -81,16 +83,23 @@ |
82 | 84 | } |
83 | 85 | |
84 | 86 | function translateElement( element, sourceLang, targetLang ) { |
| 87 | + runningJobs++; |
| 88 | + |
85 | 89 | element.contents().each( function() { |
| 90 | + // If it's a text node, then translate it. |
86 | 91 | if ( this.nodeType == 3 ) { |
| 92 | + runningJobs++; |
87 | 93 | translateChunk( this.wholeText, [], 500, sourceLang, targetLang, this ); |
88 | 94 | } |
| 95 | + // If it's an html element, check to see if it should be ignored, and if not, apply function again. |
89 | 96 | else if ( $.inArray( $( this ).attr( 'id' ), [ 'livetranslatediv', 'siteSub', 'jump-to-nav' ] ) == -1 |
90 | 97 | && $.inArray( $( this ).attr( 'class' ), [ 'notranslate', 'printfooter' ] ) == -1 |
91 | 98 | && $( this ).text().trim().length > 0 ) { |
92 | 99 | translateElement( $( this ), sourceLang, targetLang ); |
93 | 100 | } |
94 | 101 | } ); |
| 102 | + |
| 103 | + runningJobs--; |
95 | 104 | } |
96 | 105 | |
97 | 106 | function translateChunk( untranslatedText, chunks, currentMaxSize, sourceLang, targetLang, element ) { |
— | — | @@ -105,6 +114,8 @@ |
106 | 115 | |
107 | 116 | if ( chunkSize < currentMaxSize ) { |
108 | 117 | element.replaceWholeText( chunks.join() ); |
| 118 | + runningJobs--; |
| 119 | + handleTranslationCompletion( targetLang ); |
109 | 120 | } |
110 | 121 | else { |
111 | 122 | translateChunk( untranslatedText.substr( chunkSize ), chunks, currentMaxSize, sourceLang, targetLang, element ); |
— | — | @@ -113,13 +124,11 @@ |
114 | 125 | ); |
115 | 126 | } |
116 | 127 | |
117 | | - function handleTranslationCompletion( translation, targetLang ) { |
118 | | - alert(translation); |
119 | | - //$( '#bodyContent' ).innerHTML = result.translation; |
120 | | - |
121 | | - currentLang = targetLang; |
122 | | - |
123 | | - $( '#livetranslatebutton' ).attr( "disabled", false ); |
| 128 | + function handleTranslationCompletion( targetLang ) { |
| 129 | + if ( !--runningJobs ) { |
| 130 | + currentLang = targetLang; |
| 131 | + $( '#livetranslatebutton' ).attr( "disabled", false ); |
| 132 | + } |
124 | 133 | } |
125 | 134 | |
126 | 135 | } ); })(jQuery); |
\ No newline at end of file |