Fix Bug #31547 - Category links need less spacing and better wrapping
[lhc/web/wiklou.git] / skins / common / upload.js
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 );
12 };
13
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 }
24
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 // AJAX wpDestFile warnings
39 if ( wgAjaxUploadDestCheck ) {
40 // Insert an event handler that fetches upload warnings when wpDestFile
41 // has been changed
42 document.getElementById( 'wpDestFile' ).onchange = function ( e ) {
43 wgUploadWarningObj.checkNow(this.value);
44 };
45 // Insert a row where the warnings will be displayed just below the
46 // wpDestFile row
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';
51 td.colSpan = 2;
52
53 row.appendChild( td );
54 }
55
56 var wpLicense = document.getElementById( 'wpLicense' );
57 if ( wgAjaxLicensePreview && wpLicense ) {
58 // License selector check
59 wpLicense.onchange = licenseSelectorCheck;
60
61 // License selector table row
62 var wpLicenseRow = wpLicense.parentNode.parentNode;
63 var wpLicenseTbody = wpLicenseRow.parentNode;
64
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 );
71
72 wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
73 }
74
75
76 // fillDestFile setup
77 for ( var i = 0; i < wgUploadSourceIds.length; i++ )
78 document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
79 fillDestFilename( this.id );
80 };
81 };
82
83
84 window.wgUploadWarningObj = {
85 'responseCache' : { '' : '&nbsp;' },
86 'nameToCheck' : '',
87 'typing': false,
88 'delay': 500, // ms
89 'timeoutID': false,
90
91 'keypress': function () {
92 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
93
94 // Find file to upload
95 var destFile = document.getElementById('wpDestFile');
96 var warningElt = document.getElementById( 'wpDestFile-warning' );
97 if ( !destFile || !warningElt ) return ;
98
99 this.nameToCheck = destFile.value ;
100
101 // Clear timer
102 if ( this.timeoutID ) {
103 window.clearTimeout( this.timeoutID );
104 }
105 // Check response cache
106 for (cached in this.responseCache) {
107 if (this.nameToCheck == cached) {
108 this.setWarning(this.responseCache[this.nameToCheck]);
109 return;
110 }
111 }
112
113 this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
114 },
115
116 'checkNow': function (fname) {
117 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
118 if ( this.timeoutID ) {
119 window.clearTimeout( this.timeoutID );
120 }
121 this.nameToCheck = fname;
122 this.timeout();
123 },
124
125 'timeout' : function() {
126 if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
127 injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );
128
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.
132 var obj = this;
133 var fileName = this.nameToCheck;
134 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
135 function (result) {
136 obj.processResult(result, fileName)
137 }
138 );
139 },
140
141 'processResult' : function (result, fileName) {
142 removeSpinner( 'destcheck' );
143 this.setWarning(result.responseText);
144 this.responseCache[fileName] = result.responseText;
145 },
146
147 'setWarning' : function (warning) {
148 var warningElt = document.getElementById( 'wpDestFile-warning' );
149 var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );
150
151 this.setInnerHTML(warningElt, warning);
152
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 == '&nbsp;' ) {
156 ackElt[0].value = '';
157 } else {
158 ackElt[0].value = '1';
159 }
160
161 },
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;
166 }
167 }
168 };
169
170 window.fillDestFilename = function(id) {
171 if (!wgUploadAutoFill) {
172 return;
173 }
174 if (!document.getElementById) {
175 return;
176 }
177 // Remove any previously flagged errors
178 var e = document.getElementById( 'mw-upload-permitted' );
179 if( e ) e.className = '';
180
181 var e = document.getElementById( 'mw-upload-prohibited' );
182 if( e ) e.className = '';
183
184 var path = document.getElementById(id).value;
185 // Find trailing part
186 var slash = path.lastIndexOf('/');
187 var backslash = path.lastIndexOf('\\');
188 var fname;
189 if (slash == -1 && backslash == -1) {
190 fname = path;
191 } else if (slash > backslash) {
192 fname = path.substring(slash+1, 10000);
193 } else {
194 fname = path.substring(backslash+1, 10000);
195 }
196
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
199 // extension check.
200 if( wgStrictFileExtensions && wgFileExtensions && id != 'wpUploadFileURL' ) {
201 var found = false;
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() ) {
206 found = true;
207 break;
208 }
209 }
210 }
211 if( !found ) {
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';
217
218 var e = document.getElementById( 'mw-upload-prohibited' );
219 if( e ) e.className = 'error';
220
221 // Clear wpDestFile as well
222 var e = document.getElementById( 'wpDestFile' );
223 if( e ) e.value = '';
224
225 return false;
226 }
227 }
228
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 ) );
234 }
235
236 // Output result
237 var destFile = document.getElementById('wpDestFile');
238 if (destFile) {
239 destFile.value = fname;
240 wgUploadWarningObj.checkNow(fname) ;
241 }
242 };
243
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 == ' ' );
249 };
250
251 window.wgUploadLicenseObj = {
252
253 'responseCache' : { '' : '' },
254
255 'fetchPreview': function( license ) {
256 if( !wgAjaxLicensePreview ) return;
257 for (cached in this.responseCache) {
258 if (cached == license) {
259 this.showPreview( this.responseCache[license] );
260 return;
261 }
262 }
263 injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
264
265 var title = document.getElementById('wpDestFile').value;
266 if ( !title ) title = 'File:Sample.jpg';
267
268 var url = wgScriptPath + '/api' + wgScriptExtension
269 + '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
270 + '&title=' + encodeURIComponent( title )
271 + '&prop=text&pst&format=json';
272
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 );
277 };
278 req.open( 'GET', url, true );
279 req.send( '' );
280 },
281
282 'processResult' : function( result, license ) {
283 removeSpinner( 'license' );
284 this.responseCache[license] = result['parse']['text']['*'];
285 this.showPreview( this.responseCache[license] );
286 },
287
288 'showPreview' : function( preview ) {
289 var previewPanel = document.getElementById( 'mw-license-preview' );
290 if( previewPanel.innerHTML != preview )
291 previewPanel.innerHTML = preview;
292 }
293
294 };
295
296 addOnloadHook( wgUploadSetup );