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;
38 // AJAX wpDestFile warnings
39 if ( wgAjaxUploadDestCheck ) {
40 // Insert an event handler that fetches upload warnings when wpDestFile
42 document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
43 wgUploadWarningObj.checkNow(this.value);
45 // Insert a row where the warnings will be displayed just below the
47 var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
48 var row = optionsTable.insertRow( 1 );
49 var td = document.createElement( 'td' );
50 td.id = 'wpDestFile-warning';
53 row.appendChild( td );
56 var wpLicense = document.getElementById( 'wpLicense' );
57 if ( wgAjaxLicensePreview && wpLicense ) {
58 // License selector check
59 wpLicense.onchange = licenseSelectorCheck;
61 // License selector table row
62 var wpLicenseRow = wpLicense.parentNode.parentNode;
63 var wpLicenseTbody = wpLicenseRow.parentNode;
65 var row = document.createElement( 'tr' );
66 var td = document.createElement( 'td' );
67 row.appendChild( td );
68 td = document.createElement( 'td' );
69 td.id = 'mw-license-preview';
70 row.appendChild( td );
72 wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
77 for ( var i = 0; i < wgUploadSourceIds.length; i++ )
78 document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
79 fillDestFilename( this.id );
84 window.wgUploadWarningObj = {
85 'responseCache' : { '' : ' ' },
91 'keypress': function () {
92 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
94 // Find file to upload
95 var destFile = document.getElementById('wpDestFile');
96 var warningElt = document.getElementById( 'wpDestFile-warning' );
97 if ( !destFile || !warningElt ) return ;
99 this.nameToCheck = destFile.value ;
102 if ( this.timeoutID ) {
103 window.clearTimeout( this.timeoutID );
105 // Check response cache
106 for (cached in this.responseCache) {
107 if (this.nameToCheck == cached) {
108 this.setWarning(this.responseCache[this.nameToCheck]);
113 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
116 'checkNow': function (fname) {
117 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
118 if ( this.timeoutID ) {
119 window.clearTimeout( this.timeoutID );
121 this.nameToCheck = fname;
125 'timeout' : function() {
126 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
127 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
129 // Get variables into local scope so that they will be preserved for the
130 // anonymous callback. fileName is copied so that multiple overlapping
131 // ajax requests can be supported.
133 var fileName = this.nameToCheck;
134 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
136 obj.processResult(result, fileName)
141 'processResult' : function (result, fileName) {
142 removeSpinner( 'destcheck' );
143 this.setWarning(result.responseText);
144 this.responseCache[fileName] = result.responseText;
147 'setWarning' : function (warning) {
148 var warningElt = document.getElementById( 'wpDestFile-warning' );
149 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
151 this.setInnerHTML(warningElt, warning);
153 // Set a value in the form indicating that the warning is acknowledged and
154 // doesn't need to be redisplayed post-upload
155 if ( warning == '' || warning == ' ' ) {
156 ackElt[0].value = '';
158 ackElt[0].value = '1';
162 'setInnerHTML' : function (element, text) {
163 // Check for no change to avoid flicker in IE 7
164 if (element.innerHTML != text) {
165 element.innerHTML = text;
170 window.fillDestFilename = function(id) {
171 if (!wgUploadAutoFill) {
174 if (!document.getElementById) {
177 // Remove any previously flagged errors
178 var e = document.getElementById( 'mw-upload-permitted' );
179 if( e ) e.className = '';
181 var e = document.getElementById( 'mw-upload-prohibited' );
182 if( e ) e.className = '';
184 var path = document.getElementById(id).value;
185 // Find trailing part
186 var slash = path.lastIndexOf('/');
187 var backslash = path.lastIndexOf('\\');
189 if (slash == -1 && backslash == -1) {
191 } else if (slash > backslash) {
192 fname = path.substring(slash+1, 10000);
194 fname = path.substring(backslash+1, 10000);
197 // Clear the filename if it does not have a valid extension.
198 // URLs are less likely to have a useful extension, so don't include them in the
200 if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
202 if( fname.lastIndexOf( '.' ) != -1 ) {
203 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
204 for( var i = 0; i < wgFileExtensions.length; i++ ) {
205 if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
212 // Not a valid extension
213 // Clear the upload and set mw-upload-permitted to error
214 document.getElementById(id).value = '';
215 var e = document.getElementById( 'mw-upload-permitted' );
216 if( e ) e.className = 'error';
218 var e = document.getElementById( 'mw-upload-prohibited' );
219 if( e ) e.className = 'error';
221 // Clear wpDestFile as well
222 var e = document.getElementById( 'wpDestFile' );
223 if( e ) e.value = '';
229 // Replace spaces by underscores
230 fname = fname.replace( / /g, '_' );
231 // Capitalise first letter if needed
232 if ( wgCapitalizeUploads ) {
233 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) );
237 var destFile = document.getElementById('wpDestFile');
239 destFile.value = fname;
240 wgUploadWarningObj.checkNow(fname) ;
244 window.toggleFilenameFiller = function() {
245 if(!document.getElementById) return;
246 var upfield = document.getElementById('wpUploadFile');
247 var destName = document.getElementById('wpDestFile').value;
248 wgUploadAutoFill = ( destName == '' || destName == ' ' );
251 window.wgUploadLicenseObj = {
253 'responseCache' : { '' : '' },
255 'fetchPreview': function( license ) {
256 if( !wgAjaxLicensePreview ) return;
257 for (cached in this.responseCache) {
258 if (cached == license) {
259 this.showPreview( this.responseCache[license] );
263 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
265 var title = document.getElementById('wpDestFile').value;
266 if ( !title ) title = 'File:Sample.jpg';
268 var url = wgScriptPath + '/api' + wgScriptExtension
269 + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
270 + '&title=' + encodeURIComponent( title )
271 + '&prop=text&pst&format=json';
273 var req = sajax_init_object();
274 req.onreadystatechange = function() {
275 if ( req.readyState == 4 && req.status == 200 )
276 wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
278 req.open( 'GET', url, true );
282 'processResult' : function( result, license ) {
283 removeSpinner( 'license' );
284 this.responseCache[license] = result['parse']['text']['*'];
285 this.showPreview( this.responseCache[license] );
288 'showPreview' : function( preview ) {
289 var previewPanel = document.getElementById( 'mw-license-preview' );
290 if( previewPanel.innerHTML != preview )
291 previewPanel.innerHTML = preview;
296 addOnloadHook( wgUploadSetup );