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;
10 // We might show a preview
11 wgUploadLicenseObj.fetchPreview( selection );
14 window.wgUploadSetup = function() {
15 // Disable URL box if the URL copy upload source type is not selected
16 var e = document.getElementById( 'wpSourceTypeurl' );
19 var ein = document.getElementById( 'wpUploadFileURL' );
21 ein.setAttribute( 'disabled', 'disabled' );
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");
29 var ua = navigator.userAgent;
30 var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
32 for (var i = 0; i < selector.options.length; i++) {
33 selector.options[i].text = selector.options[i].text;
39 var sourceTypeCheckboxes = document.getElementsByName( 'wpSourceType' );
40 for ( var i = 0; i < sourceTypeCheckboxes.length; i++ ) {
41 sourceTypeCheckboxes[i].onchange = toggleUploadInputs;
44 // AJAX wpDestFile warnings
45 if ( wgAjaxUploadDestCheck ) {
46 // Insert an event handler that fetches upload warnings when wpDestFile
48 document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
49 wgUploadWarningObj.checkNow(this.value);
51 // Insert a row where the warnings will be displayed just below the
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';
59 row.appendChild( td );
62 var wpLicense = document.getElementById( 'wpLicense' );
63 if ( wgAjaxLicensePreview && wpLicense ) {
64 // License selector check
65 wpLicense.onchange = licenseSelectorCheck;
67 // License selector table row
68 var wpLicenseRow = wpLicense.parentNode.parentNode;
69 var wpLicenseTbody = wpLicenseRow.parentNode;
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 );
78 wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
83 for ( var i = 0; i < wgUploadSourceIds.length; i++ )
84 document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
85 fillDestFilename( this.id );
90 * Iterate over all upload source fields and disable all except the selected one.
94 window.toggleUploadInputs = function() {
95 // Iterate over all rows with UploadSourceField
97 if ( document.getElementsByClassName ) {
98 rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
100 // Older browsers don't support getElementsByClassName
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] );
110 for ( var i = 0; i < rows.length; i++ ) {
111 var inputs = rows[i].getElementsByTagName( 'input' );
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;
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;
128 window.wgUploadWarningObj = {
129 'responseCache' : { '' : ' ' },
135 'keypress': function () {
136 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
138 // Find file to upload
139 var destFile = document.getElementById('wpDestFile');
140 var warningElt = document.getElementById( 'wpDestFile-warning' );
141 if ( !destFile || !warningElt ) return ;
143 this.nameToCheck = destFile.value ;
146 if ( this.timeoutID ) {
147 window.clearTimeout( this.timeoutID );
149 // Check response cache
150 for (cached in this.responseCache) {
151 if (this.nameToCheck == cached) {
152 this.setWarning(this.responseCache[this.nameToCheck]);
157 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
160 'checkNow': function (fname) {
161 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
162 if ( this.timeoutID ) {
163 window.clearTimeout( this.timeoutID );
165 this.nameToCheck = fname;
169 'timeout' : function() {
170 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
171 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
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.
177 var fileName = this.nameToCheck;
178 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
180 obj.processResult(result, fileName)
185 'processResult' : function (result, fileName) {
186 removeSpinner( 'destcheck' );
187 this.setWarning(result.responseText);
188 this.responseCache[fileName] = result.responseText;
191 'setWarning' : function (warning) {
192 var warningElt = document.getElementById( 'wpDestFile-warning' );
193 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
195 this.setInnerHTML(warningElt, warning);
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 == ' ' ) {
200 ackElt[0].value = '';
202 ackElt[0].value = '1';
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;
214 window.fillDestFilename = function(id) {
215 if (!wgUploadAutoFill) {
218 if (!document.getElementById) {
221 // Remove any previously flagged errors
222 var e = document.getElementById( 'mw-upload-permitted' );
223 if( e ) e.className = '';
225 var e = document.getElementById( 'mw-upload-prohibited' );
226 if( e ) e.className = '';
228 var path = document.getElementById(id).value;
229 // Find trailing part
230 var slash = path.lastIndexOf('/');
231 var backslash = path.lastIndexOf('\\');
233 if (slash == -1 && backslash == -1) {
235 } else if (slash > backslash) {
236 fname = path.substring(slash+1, 10000);
238 fname = path.substring(backslash+1, 10000);
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
244 if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
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() ) {
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';
262 var e = document.getElementById( 'mw-upload-prohibited' );
263 if( e ) e.className = 'error';
265 // Clear wpDestFile as well
266 var e = document.getElementById( 'wpDestFile' );
267 if( e ) e.value = '';
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 ) );
281 var destFile = document.getElementById('wpDestFile');
283 destFile.value = fname;
284 wgUploadWarningObj.checkNow(fname) ;
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;
295 wgUploadAutoFill = false;
299 window.wgUploadLicenseObj = {
301 'responseCache' : { '' : '' },
303 'fetchPreview': function( license ) {
304 if( !wgAjaxLicensePreview ) return;
305 for (cached in this.responseCache) {
306 if (cached == license) {
307 this.showPreview( this.responseCache[license] );
311 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
313 var title = document.getElementById('wpDestFile').value;
314 if ( !title ) title = 'File:Sample.jpg';
316 var url = wgScriptPath + '/api' + wgScriptExtension
317 + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
318 + '&title=' + encodeURIComponent( title )
319 + '&prop=text&pst&format=json';
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 );
326 req.open( 'GET', url, true );
330 'processResult' : function( result, license ) {
331 removeSpinner( 'license' );
332 this.responseCache[license] = result['parse']['text']['*'];
333 this.showPreview( this.responseCache[license] );
337 'showPreview' : function( preview ) {
338 var previewPanel = document.getElementById( 'mw-license-preview' );
339 if( previewPanel.innerHTML != preview )
340 previewPanel.innerHTML = preview;
345 addOnloadHook( wgUploadSetup );