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