(bug 26265) Many semicolons are missing from JavaScript files
[lhc/web/wiklou.git] / skins / common / upload.js
1 window.licenseSelectorCheck = function() {
2 var selector = document.getElementById( "wpLicense" );
3 var selection = selector.options[selector.selectedIndex].value;
4 if( selector.selectedIndex > 0 ) {
5 if( selection == "" ) {
6 // Option disabled, but browser is broken and doesn't respect this
7 selector.selectedIndex = 0;
8 }
9 }
10 // We might show a preview
11 wgUploadLicenseObj.fetchPreview( selection );
12 };
13
14 window.wgUploadSetup = function() {
15 // Disable URL box if the URL copy upload source type is not selected
16 var e = document.getElementById( 'wpSourceTypeurl' );
17 if( e ) {
18 if( !e.checked ) {
19 var ein = document.getElementById( 'wpUploadFileURL' );
20 if(ein)
21 ein.setAttribute( 'disabled', 'disabled' );
22 }
23 }
24
25 // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
26 // But for some reason, setting the text to itself works
27 var selector = document.getElementById("wpLicense");
28 if (selector) {
29 var ua = navigator.userAgent;
30 var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
31 if (isMacIe) {
32 for (var i = 0; i < selector.options.length; i++) {
33 selector.options[i].text = selector.options[i].text;
34 }
35 }
36 }
37
38 // Toggle source type
39 var sourceTypeCheckboxes = document.getElementsByName( 'wpSourceType' );
40 for ( var i = 0; i < sourceTypeCheckboxes.length; i++ ) {
41 sourceTypeCheckboxes[i].onchange = toggleUploadInputs;
42 }
43
44 // AJAX wpDestFile warnings
45 if ( wgAjaxUploadDestCheck ) {
46 // Insert an event handler that fetches upload warnings when wpDestFile
47 // has been changed
48 document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
49 wgUploadWarningObj.checkNow(this.value);
50 };
51 // Insert a row where the warnings will be displayed just below the
52 // wpDestFile row
53 var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
54 var row = optionsTable.insertRow( 1 );
55 var td = document.createElement( 'td' );
56 td.id = 'wpDestFile-warning';
57 td.colSpan = 2;
58
59 row.appendChild( td );
60 }
61
62 if ( wgAjaxLicensePreview ) {
63 // License selector check
64 document.getElementById( 'wpLicense' ).onchange = licenseSelectorCheck;
65
66 // License selector table row
67 var wpLicense = document.getElementById( 'wpLicense' );
68 var wpLicenseRow = wpLicense.parentNode.parentNode;
69 var wpLicenseTbody = wpLicenseRow.parentNode;
70
71 var row = document.createElement( 'tr' );
72 var td = document.createElement( 'td' );
73 row.appendChild( td );
74 td = document.createElement( 'td' );
75 td.id = 'mw-license-preview';
76 row.appendChild( td );
77
78 wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
79 }
80
81
82 // fillDestFile setup
83 for ( var i = 0; i < wgUploadSourceIds.length; i++ )
84 document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
85 fillDestFilename( this.id );
86 };
87 };
88
89 /**
90 * Iterate over all upload source fields and disable all except the selected one.
91 *
92 * @return emptiness
93 */
94 window.toggleUploadInputs = function() {
95 // Iterate over all rows with UploadSourceField
96 var rows;
97 if ( document.getElementsByClassName ) {
98 rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
99 } else {
100 // Older browsers don't support getElementsByClassName
101 rows = new Array();
102
103 var allRows = document.getElementsByTagName( 'tr' );
104 for ( var i = 0; i < allRows.length; i++ ) {
105 if ( allRows[i].className == 'mw-htmlform-field-UploadSourceField' )
106 rows.push( allRows[i] );
107 }
108 }
109
110 for ( var i = 0; i < rows.length; i++ ) {
111 var inputs = rows[i].getElementsByTagName( 'input' );
112
113 // Check if this row is selected
114 var isChecked = true; // Default true in case wpSourceType is not found
115 for ( var j = 0; j < inputs.length; j++ ) {
116 if ( inputs[j].name == 'wpSourceType' )
117 isChecked = inputs[j].checked;
118 }
119
120 // Disable all unselected rows
121 for ( var j = 0; j < inputs.length; j++ ) {
122 if ( inputs[j].type != 'radio')
123 inputs[j].disabled = !isChecked;
124 }
125 }
126 };
127
128 window.wgUploadWarningObj = {
129 'responseCache' : { '' : '&nbsp;' },
130 'nameToCheck' : '',
131 'typing': false,
132 'delay': 500, // ms
133 'timeoutID': false,
134
135 'keypress': function () {
136 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
137
138 // Find file to upload
139 var destFile = document.getElementById('wpDestFile');
140 var warningElt = document.getElementById( 'wpDestFile-warning' );
141 if ( !destFile || !warningElt ) return ;
142
143 this.nameToCheck = destFile.value ;
144
145 // Clear timer
146 if ( this.timeoutID ) {
147 window.clearTimeout( this.timeoutID );
148 }
149 // Check response cache
150 for (cached in this.responseCache) {
151 if (this.nameToCheck == cached) {
152 this.setWarning(this.responseCache[this.nameToCheck]);
153 return;
154 }
155 }
156
157 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
158 },
159
160 'checkNow': function (fname) {
161 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
162 if ( this.timeoutID ) {
163 window.clearTimeout( this.timeoutID );
164 }
165 this.nameToCheck = fname;
166 this.timeout();
167 },
168
169 'timeout' : function() {
170 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
171 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
172
173 // Get variables into local scope so that they will be preserved for the
174 // anonymous callback. fileName is copied so that multiple overlapping
175 // ajax requests can be supported.
176 var obj = this;
177 var fileName = this.nameToCheck;
178 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
179 function (result) {
180 obj.processResult(result, fileName)
181 }
182 );
183 },
184
185 'processResult' : function (result, fileName) {
186 removeSpinner( 'destcheck' );
187 this.setWarning(result.responseText);
188 this.responseCache[fileName] = result.responseText;
189 },
190
191 'setWarning' : function (warning) {
192 var warningElt = document.getElementById( 'wpDestFile-warning' );
193 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
194
195 this.setInnerHTML(warningElt, warning);
196
197 // Set a value in the form indicating that the warning is acknowledged and
198 // doesn't need to be redisplayed post-upload
199 if ( warning == '' || warning == '&nbsp;' ) {
200 ackElt[0].value = '';
201 } else {
202 ackElt[0].value = '1';
203 }
204
205 },
206 'setInnerHTML' : function (element, text) {
207 // Check for no change to avoid flicker in IE 7
208 if (element.innerHTML != text) {
209 element.innerHTML = text;
210 }
211 }
212 };
213
214 window.fillDestFilename = function(id) {
215 if (!wgUploadAutoFill) {
216 return;
217 }
218 if (!document.getElementById) {
219 return;
220 }
221 // Remove any previously flagged errors
222 var e = document.getElementById( 'mw-upload-permitted' );
223 if( e ) e.className = '';
224
225 var e = document.getElementById( 'mw-upload-prohibited' );
226 if( e ) e.className = '';
227
228 var path = document.getElementById(id).value;
229 // Find trailing part
230 var slash = path.lastIndexOf('/');
231 var backslash = path.lastIndexOf('\\');
232 var fname;
233 if (slash == -1 && backslash == -1) {
234 fname = path;
235 } else if (slash > backslash) {
236 fname = path.substring(slash+1, 10000);
237 } else {
238 fname = path.substring(backslash+1, 10000);
239 }
240
241 // Clear the filename if it does not have a valid extension.
242 // URLs are less likely to have a useful extension, so don't include them in the
243 // extension check.
244 if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
245 var found = false;
246 if( fname.lastIndexOf( '.' ) != -1 ) {
247 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
248 for( var i = 0; i < wgFileExtensions.length; i++ ) {
249 if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
250 found = true;
251 break;
252 }
253 }
254 }
255 if( !found ) {
256 // Not a valid extension
257 // Clear the upload and set mw-upload-permitted to error
258 document.getElementById(id).value = '';
259 var e = document.getElementById( 'mw-upload-permitted' );
260 if( e ) e.className = 'error';
261
262 var e = document.getElementById( 'mw-upload-prohibited' );
263 if( e ) e.className = 'error';
264
265 // Clear wpDestFile as well
266 var e = document.getElementById( 'wpDestFile' );
267 if( e ) e.value = '';
268
269 return false;
270 }
271 }
272
273 // Replace spaces by underscores
274 fname = fname.replace( / /g, '_' );
275 // Capitalise first letter if needed
276 if ( wgCapitalizeUploads ) {
277 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) );
278 }
279
280 // Output result
281 var destFile = document.getElementById('wpDestFile');
282 if (destFile) {
283 destFile.value = fname;
284 wgUploadWarningObj.checkNow(fname) ;
285 }
286 };
287
288 window.toggleFilenameFiller = function() {
289 if(!document.getElementById) return;
290 var upfield = document.getElementById('wpUploadFile');
291 var destName = document.getElementById('wpDestFile').value;
292 if (destName=='' || destName==' ') {
293 wgUploadAutoFill = true;
294 } else {
295 wgUploadAutoFill = false;
296 }
297 };
298
299 window.wgUploadLicenseObj = {
300
301 'responseCache' : { '' : '' },
302
303 'fetchPreview': function( license ) {
304 if( !wgAjaxLicensePreview ) return;
305 for (cached in this.responseCache) {
306 if (cached == license) {
307 this.showPreview( this.responseCache[license] );
308 return;
309 }
310 }
311 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
312
313 var title = document.getElementById('wpDestFile').value;
314 if ( !title ) title = 'File:Sample.jpg';
315
316 var url = wgScriptPath + '/api' + wgScriptExtension
317 + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
318 + '&title=' + encodeURIComponent( title )
319 + '&prop=text&pst&format=json';
320
321 var req = sajax_init_object();
322 req.onreadystatechange = function() {
323 if ( req.readyState == 4 && req.status == 200 )
324 wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
325 };
326 req.open( 'GET', url, true );
327 req.send( '' );
328 },
329
330 'processResult' : function( result, license ) {
331 removeSpinner( 'license' );
332 this.responseCache[license] = result['parse']['text']['*'];
333 this.showPreview( this.responseCache[license] );
334
335 },
336
337 'showPreview' : function( preview ) {
338 var previewPanel = document.getElementById( 'mw-license-preview' );
339 if( previewPanel.innerHTML != preview )
340 previewPanel.innerHTML = preview;
341 }
342
343 };
344
345 addOnloadHook( wgUploadSetup );