Merge "Allow lines empty but for tabs and comments to be ignored."
[lhc/web/wiklou.git] / skins / common / upload.js
1 ( function ( mw, $ ) {
2 var ajaxUploadDestCheck = mw.config.get( 'wgAjaxUploadDestCheck' ),
3 fileExtensions = mw.config.get( 'wgFileExtensions' );
4
5 window.licenseSelectorCheck = function() {
6 var selector = document.getElementById( "wpLicense" );
7 var selection = selector.options[selector.selectedIndex].value;
8 if( selector.selectedIndex > 0 ) {
9 if( selection == "" ) {
10 // Option disabled, but browser is broken and doesn't respect this
11 selector.selectedIndex = 0;
12 }
13 }
14 // We might show a preview
15 wgUploadLicenseObj.fetchPreview( selection );
16 };
17
18 function uploadSetup() {
19 // Disable URL box if the URL copy upload source type is not selected
20 var e = document.getElementById( 'wpSourceTypeurl' );
21 if( e ) {
22 if( !e.checked ) {
23 var ein = document.getElementById( 'wpUploadFileURL' );
24 if(ein)
25 ein.setAttribute( 'disabled', 'disabled' );
26 }
27 }
28
29 // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
30 // But for some reason, setting the text to itself works
31 var selector = document.getElementById("wpLicense");
32 if (selector) {
33 var ua = navigator.userAgent;
34 var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
35 if (isMacIe) {
36 for (var i = 0; i < selector.options.length; i++) {
37 selector.options[i].text = selector.options[i].text;
38 }
39 }
40 }
41
42 // AJAX wpDestFile warnings
43 if ( ajaxUploadDestCheck ) {
44 // Insert an event handler that fetches upload warnings when wpDestFile
45 // has been changed
46 document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
47 wgUploadWarningObj.checkNow(this.value);
48 };
49 // Insert a row where the warnings will be displayed just below the
50 // wpDestFile row
51 var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
52 var row = optionsTable.insertRow( 1 );
53 var td = document.createElement( 'td' );
54 td.id = 'wpDestFile-warning';
55 td.colSpan = 2;
56
57 row.appendChild( td );
58 }
59
60 var wpLicense = document.getElementById( 'wpLicense' );
61 if ( mw.config.get( 'wgAjaxLicensePreview' ) && wpLicense ) {
62 // License selector check
63 wpLicense.onchange = licenseSelectorCheck;
64
65 // License selector table row
66 var wpLicenseRow = wpLicense.parentNode.parentNode;
67 var wpLicenseTbody = wpLicenseRow.parentNode;
68
69 var row = document.createElement( 'tr' );
70 var td = document.createElement( 'td' );
71 row.appendChild( td );
72 td = document.createElement( 'td' );
73 td.id = 'mw-license-preview';
74 row.appendChild( td );
75
76 wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
77 }
78
79
80 // fillDestFile setup
81 var i,
82 uploadSourceIds = mw.config.get( 'wgUploadSourceIds' ),
83 len = uploadSourceIds.length;
84 for ( i = 0; i < len; i += 1 )
85 document.getElementById( uploadSourceIds[i] ).onchange = function (e) {
86 fillDestFilename( this.id );
87 };
88 };
89
90
91 window.wgUploadWarningObj = {
92 'responseCache' : { '' : '&nbsp;' },
93 'nameToCheck' : '',
94 'typing': false,
95 'delay': 500, // ms
96 'timeoutID': false,
97
98 'keypress': function () {
99 if ( !ajaxUploadDestCheck ) return;
100
101 // Find file to upload
102 var destFile = document.getElementById('wpDestFile');
103 var warningElt = document.getElementById( 'wpDestFile-warning' );
104 if ( !destFile || !warningElt ) return ;
105
106 this.nameToCheck = destFile.value ;
107
108 // Clear timer
109 if ( this.timeoutID ) {
110 window.clearTimeout( this.timeoutID );
111 }
112 // Check response cache
113 for (cached in this.responseCache) {
114 if (this.nameToCheck == cached) {
115 this.setWarning(this.responseCache[this.nameToCheck]);
116 return;
117 }
118 }
119
120 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
121 },
122
123 'checkNow': function (fname) {
124 if ( !ajaxUploadDestCheck ) return;
125 if ( this.timeoutID ) {
126 window.clearTimeout( this.timeoutID );
127 }
128 this.nameToCheck = fname;
129 this.timeout();
130 },
131
132 'timeout' : function() {
133 if ( !ajaxUploadDestCheck || this.nameToCheck === '' ) return;
134 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
135
136 var uploadWarningObj = this;
137 ( new mw.Api ).get( {
138 action: 'query',
139 titles: ( new mw.Title( this.nameToCheck, mw.config.get( 'wgNamespaceIds' ).file ) ).getPrefixedText(),
140 prop: 'imageinfo',
141 iiprop: 'uploadwarning',
142 indexpageids: '',
143 } ).done( function ( result ) {
144 var result = '';
145 if ( result.query ) {
146 result = result.query.pages[result.query.pageids[0]].imageinfo[0];
147 }
148 uploadWarningObj.processResult( result, uploadWarningObj.nameToCheck );
149 } );
150 },
151
152 'processResult' : function ( result, fileName ) {
153 removeSpinner( 'destcheck' );
154 this.setWarning( result.html );
155 this.responseCache[fileName] = result.html;
156 },
157
158 'setWarning' : function (warning) {
159 var warningElt = document.getElementById( 'wpDestFile-warning' );
160 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
161
162 this.setInnerHTML(warningElt, warning);
163
164 // Set a value in the form indicating that the warning is acknowledged and
165 // doesn't need to be redisplayed post-upload
166 if ( warning == '' ) {
167 ackElt[0].value = '';
168 } else {
169 ackElt[0].value = '1';
170 }
171
172 },
173 'setInnerHTML' : function (element, text) {
174 // Check for no change to avoid flicker in IE 7
175 if (element.innerHTML != text) {
176 element.innerHTML = text;
177 }
178 }
179 };
180
181 window.fillDestFilename = function(id) {
182 if ( !mw.config.get( 'wgUploadAutoFill' ) ) {
183 return;
184 }
185 if (!document.getElementById) {
186 return;
187 }
188 // Remove any previously flagged errors
189 var e = document.getElementById( 'mw-upload-permitted' );
190 if( e ) e.className = '';
191
192 var e = document.getElementById( 'mw-upload-prohibited' );
193 if( e ) e.className = '';
194
195 var path = document.getElementById(id).value;
196 // Find trailing part
197 var slash = path.lastIndexOf('/');
198 var backslash = path.lastIndexOf('\\');
199 var fname;
200 if (slash == -1 && backslash == -1) {
201 fname = path;
202 } else if (slash > backslash) {
203 fname = path.substring(slash+1, 10000);
204 } else {
205 fname = path.substring(backslash+1, 10000);
206 }
207
208 // Clear the filename if it does not have a valid extension.
209 // URLs are less likely to have a useful extension, so don't include them in the
210 // extension check.
211 if ( mw.config.get( 'wgStrictFileExtensions' ) && fileExtensions && id !== 'wpUploadFileURL' ) {
212 var found = false;
213 if ( fname.lastIndexOf( '.' ) !== -1 ) {
214 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
215 for ( var i = 0; i < fileExtensions.length; i += 1 ) {
216 if ( fileExtensions[i].toLowerCase() === ext.toLowerCase() ) {
217 found = true;
218 break;
219 }
220 }
221 }
222 if( !found ) {
223 // Not a valid extension
224 // Clear the upload and set mw-upload-permitted to error
225 document.getElementById(id).value = '';
226 var e = document.getElementById( 'mw-upload-permitted' );
227 if( e ) e.className = 'error';
228
229 var e = document.getElementById( 'mw-upload-prohibited' );
230 if( e ) e.className = 'error';
231
232 // Clear wpDestFile as well
233 var e = document.getElementById( 'wpDestFile' );
234 if( e ) e.value = '';
235
236 return false;
237 }
238 }
239
240 // Replace spaces by underscores
241 fname = fname.replace( / /g, '_' );
242 // Capitalise first letter if needed
243 if ( mw.config.get( 'wgCapitalizeUploads' ) ) {
244 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) );
245 }
246
247 // Output result
248 var destFile = document.getElementById( 'wpDestFile' );
249 if ( destFile ) {
250 // Call decodeURIComponent function to remove possible URL-encoded characters
251 // from the file name (bug 30390). Especially likely with upload-form-url.
252 // decodeURIComponent can throw an exception in input is invalid utf-8
253 try {
254 destFile.value = decodeURIComponent( fname );
255 } catch ( e ) {
256 destFile.value = fname;
257 }
258 wgUploadWarningObj.checkNow( fname );
259 }
260 };
261
262 window.toggleFilenameFiller = function() {
263 if(!document.getElementById) return;
264 var upfield = document.getElementById('wpUploadFile');
265 var destName = document.getElementById('wpDestFile').value;
266 wgUploadAutoFill = ( destName == '' || destName == ' ' );
267 };
268
269 window.wgUploadLicenseObj = {
270
271 'responseCache' : { '' : '' },
272
273 'fetchPreview': function( license ) {
274 if ( !mw.config.get( 'wgAjaxLicensePreview' ) ) return;
275 for (cached in this.responseCache) {
276 if (cached == license) {
277 this.showPreview( this.responseCache[license] );
278 return;
279 }
280 }
281 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
282
283 var title = document.getElementById('wpDestFile').value;
284 if ( !title ) title = 'File:Sample.jpg';
285
286 ( new mw.Api ).get( {
287 action: 'parse',
288 text: '{{' + license + '}}',
289 title: title,
290 prop: 'text',
291 pst: ''
292 } ).done( function ( result ) {
293 wgUploadLicenseObj.processResult( result, license );
294 } );
295 },
296
297 'processResult' : function( result, license ) {
298 removeSpinner( 'license' );
299 this.responseCache[license] = result['parse']['text']['*'];
300 this.showPreview( this.responseCache[license] );
301 },
302
303 'showPreview' : function( preview ) {
304 var previewPanel = document.getElementById( 'mw-license-preview' );
305 if( previewPanel.innerHTML != preview )
306 previewPanel.innerHTML = preview;
307 }
308
309 };
310
311 $( uploadSetup );
312
313 }( mediaWiki, jQuery ) );