Follow-up r89835: Accidently comitted from a deeper dir than the diff. Comitting...
[mediawiki.git] / skins / common / upload.js
blob99917c75bd488f9c3aa3ff4ff025ee6f0dc65dce
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;
8                 }
9         }
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' );
17         if( e ) {
18                 if( !e.checked ) {
19                         var ein = document.getElementById( 'wpUploadFileURL' );
20                         if(ein)
21                                 ein.setAttribute( 'disabled', 'disabled' );
22                 }
23         }
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");
28         if (selector) {
29                 var ua = navigator.userAgent;
30                 var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
31                 if (isMacIe) {
32                         for (var i = 0; i < selector.options.length; i++) {
33                                 selector.options[i].text = selector.options[i].text;
34                         }
35                 }
36         }
37         
38         
39         // AJAX wpDestFile warnings
40         if ( wgAjaxUploadDestCheck ) {
41                 // Insert an event handler that fetches upload warnings when wpDestFile
42                 // has been changed
43                 document.getElementById( 'wpDestFile' ).onchange = function ( e ) { 
44                         wgUploadWarningObj.checkNow(this.value);
45                 };
46                 // Insert a row where the warnings will be displayed just below the 
47                 // wpDestFile row
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';
52                 td.colSpan = 2;
53                 
54                 row.appendChild( td );
55         }
56         
57         var wpLicense = document.getElementById( 'wpLicense' );
58         if ( wgAjaxLicensePreview && wpLicense ) {
59                 // License selector check
60                 wpLicense.onchange = licenseSelectorCheck;
61         
62                 // License selector table row
63                 var wpLicenseRow = wpLicense.parentNode.parentNode;
64                 var wpLicenseTbody = wpLicenseRow.parentNode;
65                 
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 );
72                 
73                 wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
74         }
75         
76         
77         // fillDestFile setup
78         for ( var i = 0; i < wgUploadSourceIds.length; i++ )
79                 document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
80                         fillDestFilename( this.id );
81                 };
85 window.wgUploadWarningObj = {
86         'responseCache' : { '' : '&nbsp;' },
87         'nameToCheck' : '',
88         'typing': false,
89         'delay': 500, // ms
90         'timeoutID': false,
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 ;
102                 // Clear timer
103                 if ( this.timeoutID ) {
104                         window.clearTimeout( this.timeoutID );
105                 }
106                 // Check response cache
107                 for (cached in this.responseCache) {
108                         if (this.nameToCheck == cached) {
109                                 this.setWarning(this.responseCache[this.nameToCheck]);
110                                 return;
111                         }
112                 }
114                 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
115         },
117         'checkNow': function (fname) {
118                 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
119                 if ( this.timeoutID ) {
120                         window.clearTimeout( this.timeoutID );
121                 }
122                 this.nameToCheck = fname;
123                 this.timeout();
124         },
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.
133                 var obj = this;
134                 var fileName = this.nameToCheck;
135                 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
136                         function (result) {
137                                 obj.processResult(result, fileName)
138                         }
139                 );
140         },
142         'processResult' : function (result, fileName) {
143                 removeSpinner( 'destcheck' );
144                 this.setWarning(result.responseText);
145                 this.responseCache[fileName] = result.responseText;
146         },
148         'setWarning' : function (warning) {
149                 var warningElt = document.getElementById( 'wpDestFile-warning' );
150                 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
152                 this.setInnerHTML(warningElt, warning);
153                 
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 == '&nbsp;' ) {
157                         ackElt[0].value = '';
158                 } else {
159                         ackElt[0].value = '1';
160                 }
162         },
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;
167                 }
168         }
171 window.fillDestFilename = function(id) {
172         if (!wgUploadAutoFill) {
173                 return;
174         }
175         if (!document.getElementById) {
176                 return;
177         }
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('\\');
189         var fname;
190         if (slash == -1 && backslash == -1) {
191                 fname = path;
192         } else if (slash > backslash) {
193                 fname = path.substring(slash+1, 10000);
194         } else {
195                 fname = path.substring(backslash+1, 10000);
196         }
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 
200         // extension check.
201         if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
202                 var found = false;
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() ) {
207                                         found = true;
208                                         break;
209                                 }
210                         }
211                 }
212                 if( !found ) {
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 = '';
226                         return false;
227                 }
228         }
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 ) );
235         }
237         // Output result
238         var destFile = document.getElementById('wpDestFile');
239         if (destFile) {
240                 destFile.value = fname;
241                 wgUploadWarningObj.checkNow(fname) ;
242         }
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] );
261                                 return;
262                         }
263                 }
264                 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
265                 
266                 var title = document.getElementById('wpDestFile').value;
267                 if ( !title ) title = 'File:Sample.jpg';
268                 
269                 var url = wgScriptPath + '/api' + wgScriptExtension
270                         + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
271                         + '&title=' + encodeURIComponent( title ) 
272                         + '&prop=text&pst&format=json';
273                 
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 );
278                 };
279                 req.open( 'GET', url, true );
280                 req.send( '' );
281         },
283         'processResult' : function( result, license ) {
284                 removeSpinner( 'license' );
285                 this.responseCache[license] = result['parse']['text']['*'];
286                 this.showPreview( this.responseCache[license] );
288         },
290         'showPreview' : function( preview ) {
291                 var previewPanel = document.getElementById( 'mw-license-preview' );
292                 if( previewPanel.innerHTML != preview )
293                         previewPanel.innerHTML = preview;
294         }
298 addOnloadHook( wgUploadSetup );