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 // AJAX wpDestFile warnings
40 if ( wgAjaxUploadDestCheck ) {
41 // Insert an event handler that fetches upload warnings when wpDestFile
43 document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
44 wgUploadWarningObj.checkNow(this.value);
46 // Insert a row where the warnings will be displayed just below the
48 var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
49 var row = optionsTable.insertRow( 1 );
50 var td = document.createElement( 'td' );
51 td.id = 'wpDestFile-warning';
54 row.appendChild( td );
57 var wpLicense = document.getElementById( 'wpLicense' );
58 if ( wgAjaxLicensePreview && wpLicense ) {
59 // License selector check
60 wpLicense.onchange = licenseSelectorCheck;
62 // License selector table row
63 var wpLicenseRow = wpLicense.parentNode.parentNode;
64 var wpLicenseTbody = wpLicenseRow.parentNode;
66 var row = document.createElement( 'tr' );
67 var td = document.createElement( 'td' );
68 row.appendChild( td );
69 td = document.createElement( 'td' );
70 td.id = 'mw-license-preview';
71 row.appendChild( td );
73 wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
78 for ( var i = 0; i < wgUploadSourceIds.length; i++ )
79 document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
80 fillDestFilename( this.id );
85 window.wgUploadWarningObj = {
86 'responseCache' : { '' : ' ' },
92 'keypress': function () {
93 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
95 // Find file to upload
96 var destFile = document.getElementById('wpDestFile');
97 var warningElt = document.getElementById( 'wpDestFile-warning' );
98 if ( !destFile || !warningElt ) return ;
100 this.nameToCheck = destFile.value ;
103 if ( this.timeoutID ) {
104 window.clearTimeout( this.timeoutID );
106 // Check response cache
107 for (cached in this.responseCache) {
108 if (this.nameToCheck == cached) {
109 this.setWarning(this.responseCache[this.nameToCheck]);
114 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
117 'checkNow': function (fname) {
118 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
119 if ( this.timeoutID ) {
120 window.clearTimeout( this.timeoutID );
122 this.nameToCheck = fname;
126 'timeout' : function() {
127 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
128 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
130 // Get variables into local scope so that they will be preserved for the
131 // anonymous callback. fileName is copied so that multiple overlapping
132 // ajax requests can be supported.
134 var fileName = this.nameToCheck;
135 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
137 obj.processResult(result, fileName)
142 'processResult' : function (result, fileName) {
143 removeSpinner( 'destcheck' );
144 this.setWarning(result.responseText);
145 this.responseCache[fileName] = result.responseText;
148 'setWarning' : function (warning) {
149 var warningElt = document.getElementById( 'wpDestFile-warning' );
150 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
152 this.setInnerHTML(warningElt, warning);
154 // Set a value in the form indicating that the warning is acknowledged and
155 // doesn't need to be redisplayed post-upload
156 if ( warning == '' || warning == ' ' ) {
157 ackElt[0].value = '';
159 ackElt[0].value = '1';
163 'setInnerHTML' : function (element, text) {
164 // Check for no change to avoid flicker in IE 7
165 if (element.innerHTML != text) {
166 element.innerHTML = text;
171 window.fillDestFilename = function(id) {
172 if (!wgUploadAutoFill) {
175 if (!document.getElementById) {
178 // Remove any previously flagged errors
179 var e = document.getElementById( 'mw-upload-permitted' );
180 if( e ) e.className = '';
182 var e = document.getElementById( 'mw-upload-prohibited' );
183 if( e ) e.className = '';
185 var path = document.getElementById(id).value;
186 // Find trailing part
187 var slash = path.lastIndexOf('/');
188 var backslash = path.lastIndexOf('\\');
190 if (slash == -1 && backslash == -1) {
192 } else if (slash > backslash) {
193 fname = path.substring(slash+1, 10000);
195 fname = path.substring(backslash+1, 10000);
198 // Clear the filename if it does not have a valid extension.
199 // URLs are less likely to have a useful extension, so don't include them in the
201 if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
203 if( fname.lastIndexOf( '.' ) != -1 ) {
204 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
205 for( var i = 0; i < wgFileExtensions.length; i++ ) {
206 if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
213 // Not a valid extension
214 // Clear the upload and set mw-upload-permitted to error
215 document.getElementById(id).value = '';
216 var e = document.getElementById( 'mw-upload-permitted' );
217 if( e ) e.className = 'error';
219 var e = document.getElementById( 'mw-upload-prohibited' );
220 if( e ) e.className = 'error';
222 // Clear wpDestFile as well
223 var e = document.getElementById( 'wpDestFile' );
224 if( e ) e.value = '';
230 // Replace spaces by underscores
231 fname = fname.replace( / /g, '_' );
232 // Capitalise first letter if needed
233 if ( wgCapitalizeUploads ) {
234 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) );
238 var destFile = document.getElementById('wpDestFile');
240 destFile.value = fname;
241 wgUploadWarningObj.checkNow(fname) ;
245 window.toggleFilenameFiller = function() {
246 if(!document.getElementById) return;
247 var upfield = document.getElementById('wpUploadFile');
248 var destName = document.getElementById('wpDestFile').value;
249 wgUploadAutoFill = ( destName == '' || destName == ' ' );
252 window.wgUploadLicenseObj = {
254 'responseCache' : { '' : '' },
256 'fetchPreview': function( license ) {
257 if( !wgAjaxLicensePreview ) return;
258 for (cached in this.responseCache) {
259 if (cached == license) {
260 this.showPreview( this.responseCache[license] );
264 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
266 var title = document.getElementById('wpDestFile').value;
267 if ( !title ) title = 'File:Sample.jpg';
269 var url = wgScriptPath + '/api' + wgScriptExtension
270 + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
271 + '&title=' + encodeURIComponent( title )
272 + '&prop=text&pst&format=json';
274 var req = sajax_init_object();
275 req.onreadystatechange = function() {
276 if ( req.readyState == 4 && req.status == 200 )
277 wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
279 req.open( 'GET', url, true );
283 'processResult' : function( result, license ) {
284 removeSpinner( 'license' );
285 this.responseCache[license] = result['parse']['text']['*'];
286 this.showPreview( this.responseCache[license] );
290 'showPreview' : function( preview ) {
291 var previewPanel = document.getElementById( 'mw-license-preview' );
292 if( previewPanel.innerHTML != preview )
293 previewPanel.innerHTML = preview;
298 addOnloadHook( wgUploadSetup );