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 if ( wgAjaxLicensePreview ) {
63 // License selector check
64 document.getElementById( 'wpLicense' ).onchange = licenseSelectorCheck;
66 // License selector table row
67 var wpLicense = document.getElementById( 'wpLicense' );
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.
92 * @param enabledId The id of the selected radio button
95 window.toggleUploadInputs = function() {
96 // Iterate over all rows with UploadSourceField
98 if ( document.getElementsByClassName ) {
99 rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
101 // Older browsers don't support getElementsByClassName
104 var allRows = document.getElementsByTagName( 'tr' );
105 for ( var i = 0; i < allRows.length; i++ ) {
106 if ( allRows[i].className == 'mw-htmlform-field-UploadSourceField' )
107 rows.push( allRows[i] );
111 for ( var i = 0; i < rows.length; i++ ) {
112 var inputs = rows[i].getElementsByTagName( 'input' );
114 // Check if this row is selected
115 var isChecked = true; // Default true in case wpSourceType is not found
116 for ( var j = 0; j < inputs.length; j++ ) {
117 if ( inputs[j].name == 'wpSourceType' )
118 isChecked = inputs[j].checked;
121 // Disable all unselected rows
122 for ( var j = 0; j < inputs.length; j++ ) {
123 if ( inputs[j].type != 'radio')
124 inputs[j].disabled = !isChecked;
129 window.wgUploadWarningObj = {
130 'responseCache' : { '' : ' ' },
136 'keypress': function () {
137 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
139 // Find file to upload
140 var destFile = document.getElementById('wpDestFile');
141 var warningElt = document.getElementById( 'wpDestFile-warning' );
142 if ( !destFile || !warningElt ) return ;
144 this.nameToCheck = destFile.value ;
147 if ( this.timeoutID ) {
148 window.clearTimeout( this.timeoutID );
150 // Check response cache
151 for (cached in this.responseCache) {
152 if (this.nameToCheck == cached) {
153 this.setWarning(this.responseCache[this.nameToCheck]);
158 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
161 'checkNow': function (fname) {
162 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
163 if ( this.timeoutID ) {
164 window.clearTimeout( this.timeoutID );
166 this.nameToCheck = fname;
170 'timeout' : function() {
171 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
172 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
174 // Get variables into local scope so that they will be preserved for the
175 // anonymous callback. fileName is copied so that multiple overlapping
176 // ajax requests can be supported.
178 var fileName = this.nameToCheck;
179 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
181 obj.processResult(result, fileName)
186 'processResult' : function (result, fileName) {
187 removeSpinner( 'destcheck' );
188 this.setWarning(result.responseText);
189 this.responseCache[fileName] = result.responseText;
192 'setWarning' : function (warning) {
193 var warningElt = document.getElementById( 'wpDestFile-warning' );
194 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
196 this.setInnerHTML(warningElt, warning);
198 // Set a value in the form indicating that the warning is acknowledged and
199 // doesn't need to be redisplayed post-upload
200 if ( warning == '' || warning == ' ' ) {
201 ackElt[0].value = '';
203 ackElt[0].value = '1';
207 'setInnerHTML' : function (element, text) {
208 // Check for no change to avoid flicker in IE 7
209 if (element.innerHTML != text) {
210 element.innerHTML = text;
215 window.fillDestFilename = function(id) {
216 if (!wgUploadAutoFill) {
219 if (!document.getElementById) {
222 // Remove any previously flagged errors
223 var e = document.getElementById( 'mw-upload-permitted' );
224 if( e ) e.className = '';
226 var e = document.getElementById( 'mw-upload-prohibited' );
227 if( e ) e.className = '';
229 var path = document.getElementById(id).value;
230 // Find trailing part
231 var slash = path.lastIndexOf('/');
232 var backslash = path.lastIndexOf('\\');
234 if (slash == -1 && backslash == -1) {
236 } else if (slash > backslash) {
237 fname = path.substring(slash+1, 10000);
239 fname = path.substring(backslash+1, 10000);
242 // Clear the filename if it does not have a valid extension.
243 // URLs are less likely to have a useful extension, so don't include them in the
245 if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
247 if( fname.lastIndexOf( '.' ) != -1 ) {
248 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
249 for( var i = 0; i < wgFileExtensions.length; i++ ) {
250 if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
257 // Not a valid extension
258 // Clear the upload and set mw-upload-permitted to error
259 document.getElementById(id).value = '';
260 var e = document.getElementById( 'mw-upload-permitted' );
261 if( e ) e.className = 'error';
263 var e = document.getElementById( 'mw-upload-prohibited' );
264 if( e ) e.className = 'error';
266 // Clear wpDestFile as well
267 var e = document.getElementById( 'wpDestFile' )
268 if( e ) e.value = '';
274 // Replace spaces by underscores
275 fname = fname.replace( / /g, '_' );
276 // Capitalise first letter if needed
277 if ( wgCapitalizeUploads ) {
278 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) );
282 var destFile = document.getElementById('wpDestFile');
284 destFile.value = fname;
285 wgUploadWarningObj.checkNow(fname) ;
289 window.toggleFilenameFiller = function() {
290 if(!document.getElementById) return;
291 var upfield = document.getElementById('wpUploadFile');
292 var destName = document.getElementById('wpDestFile').value;
293 if (destName=='' || destName==' ') {
294 wgUploadAutoFill = true;
296 wgUploadAutoFill = false;
300 window.wgUploadLicenseObj = {
302 'responseCache' : { '' : '' },
304 'fetchPreview': function( license ) {
305 if( !wgAjaxLicensePreview ) return;
306 for (cached in this.responseCache) {
307 if (cached == license) {
308 this.showPreview( this.responseCache[license] );
312 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
314 var title = document.getElementById('wpDestFile').value;
315 if ( !title ) title = 'File:Sample.jpg';
317 var url = wgScriptPath + '/api' + wgScriptExtension
318 + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
319 + '&title=' + encodeURIComponent( title )
320 + '&prop=text&pst&format=json';
322 var req = sajax_init_object();
323 req.onreadystatechange = function() {
324 if ( req.readyState == 4 && req.status == 200 )
325 wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
327 req.open( 'GET', url, true );
331 'processResult' : function( result, license ) {
332 removeSpinner( 'license' );
333 this.responseCache[license] = result['parse']['text']['*'];
334 this.showPreview( this.responseCache[license] );
338 'showPreview' : function( preview ) {
339 var previewPanel = document.getElementById( 'mw-license-preview' );
340 if( previewPanel.innerHTML != preview )
341 previewPanel.innerHTML = preview;
346 addOnloadHook( wgUploadSetup );