summaryrefslogtreecommitdiff
path: root/skins/common/upload.js
blob: f40f62e68b47d7712613e256e1b38701aa07e0fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
function licenseSelectorCheck() {
	var selector = document.getElementById( "wpLicense" );
	var selection = selector.options[selector.selectedIndex].value;
	if( selector.selectedIndex > 0 ) {
		if( selection == "" ) {
			// Option disabled, but browser is broken and doesn't respect this
			selector.selectedIndex = 0;
		}
	}
	// We might show a preview
	wgUploadLicenseObj.fetchPreview( selection );
}

function wgUploadSetup() {
	// Disable URL box if the URL copy upload source type is not selected
	var e = document.getElementById( 'wpSourceTypeURL' );
	if( e ) {
		if( !e.checked ) {
			var ein = document.getElementById( 'wpUploadFileURL' );
			if(ein)
				ein.setAttribute( 'disabled', 'disabled' );
		}
	}

	// For MSIE/Mac: non-breaking spaces cause the <option> not to render.
	// But for some reason, setting the text to itself works
	var selector = document.getElementById("wpLicense");
	if (selector) {
		var ua = navigator.userAgent;
		var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
		if (isMacIe) {
			for (var i = 0; i < selector.options.length; i++) {
				selector.options[i].text = selector.options[i].text;
			}
		}
	}
	
	// Toggle source type
	var sourceTypeCheckboxes = document.getElementsByName( 'wpSourceType' );
	for ( var i = 0; i < sourceTypeCheckboxes.length; i++ ) {
		sourceTypeCheckboxes[i].onchange = toggleUploadInputs;
	}
	
	// AJAX wpDestFile warnings
	if ( wgAjaxUploadDestCheck ) {
		// Insert an event handler that fetches upload warnings when wpDestFile
		// has been changed
		document.getElementById( 'wpDestFile' ).onchange = function ( e ) { 
			wgUploadWarningObj.checkNow(this.value);
		};
		// Insert a row where the warnings will be displayed just below the 
		// wpDestFile row
		var optionsTable = document.getElementById( 'mw-htmlform-description' ).tBodies[0];
		var row = optionsTable.insertRow( 1 );
		var td = document.createElement( 'td' );
		td.id = 'wpDestFile-warning';
		td.colSpan = 2;
		
		row.appendChild( td );
	}
	
	if ( wgAjaxLicensePreview ) {
		// License selector check
		document.getElementById( 'wpLicense' ).onchange = licenseSelectorCheck;
	
		// License selector table row
		var wpLicense = document.getElementById( 'wpLicense' );
		var wpLicenseRow = wpLicense.parentNode.parentNode;
		var wpLicenseTbody = wpLicenseRow.parentNode;
		
		var row = document.createElement( 'tr' );
		var td = document.createElement( 'td' );
		row.appendChild( td );
		td = document.createElement( 'td' );
		td.id = 'mw-license-preview';
		row.appendChild( td );
		
		wpLicenseTbody.insertBefore( row, wpLicenseRow.nextSibling );
	}
	
	
	// fillDestFile setup
	for ( var i = 0; i < wgUploadSourceIds.length; i++ )
		document.getElementById( wgUploadSourceIds[i] ).onchange = function (e) {
			fillDestFilename( this.id );
		};
}

/**
 * Iterate over all upload source fields and disable all except the selected one.
 * 
 * @param enabledId The id of the selected radio button 
 * @return emptiness
 */
function toggleUploadInputs() {
	// Iterate over all rows with UploadSourceField
	var rows;
	if ( document.getElementsByClassName ) {
		rows = document.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
	} else {
		// Older browsers don't support getElementsByClassName
		rows = new Array();
		
		var allRows = document.getElementsByTagName( 'tr' );
		for ( var i = 0; i < allRows.length; i++ ) {
			if ( allRows[i].className == 'mw-htmlform-field-UploadSourceField' )
				rows.push( allRows[i] );
		}
	}
	
	for ( var i = 0; i < rows.length; i++ ) {
		var inputs = rows[i].getElementsByTagName( 'input' );
		
		// Check if this row is selected
		var isChecked = true; // Default true in case wpSourceType is not found
		for ( var j = 0; j < inputs.length; j++ ) {
			if ( inputs[j].name == 'wpSourceType' )
				isChecked = inputs[j].checked;
		}
		
		// Disable all unselected rows
		for ( var j = 0; j < inputs.length; j++ ) {
			if ( inputs[j].type != 'radio')
				inputs[j].disabled = !isChecked;
		}
	}
}

var wgUploadWarningObj = {
	'responseCache' : { '' : '&nbsp;' },
	'nameToCheck' : '',
	'typing': false,
	'delay': 500, // ms
	'timeoutID': false,

	'keypress': function () {
		if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;

		// Find file to upload
		var destFile = document.getElementById('wpDestFile');
		var warningElt = document.getElementById( 'wpDestFile-warning' );
		if ( !destFile || !warningElt ) return ;

		this.nameToCheck = destFile.value ;

		// Clear timer
		if ( this.timeoutID ) {
			window.clearTimeout( this.timeoutID );
		}
		// Check response cache
		for (cached in this.responseCache) {
			if (this.nameToCheck == cached) {
				this.setWarning(this.responseCache[this.nameToCheck]);
				return;
			}
		}

		this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
	},

	'checkNow': function (fname) {
		if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
		if ( this.timeoutID ) {
			window.clearTimeout( this.timeoutID );
		}
		this.nameToCheck = fname;
		this.timeout();
	},

	'timeout' : function() {
		if ( !wgAjaxUploadDestCheck || !sajax_init_object() ) return;
		injectSpinner( document.getElementById( 'wpDestFile' ), 'destcheck' );

		// Get variables into local scope so that they will be preserved for the
		// anonymous callback. fileName is copied so that multiple overlapping
		// ajax requests can be supported.
		var obj = this;
		var fileName = this.nameToCheck;
		sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck],
			function (result) {
				obj.processResult(result, fileName)
			}
		);
	},

	'processResult' : function (result, fileName) {
		removeSpinner( 'destcheck' );
		this.setWarning(result.responseText);
		this.responseCache[fileName] = result.responseText;
	},

	'setWarning' : function (warning) {
		var warningElt = document.getElementById( 'wpDestFile-warning' );
		var ackElt = document.getElementsByName( 'wpDestFileWarningAck' );

		this.setInnerHTML(warningElt, warning);
		
		// Set a value in the form indicating that the warning is acknowledged and
		// doesn't need to be redisplayed post-upload
		if ( warning == '' || warning == '&nbsp;' ) {
			ackElt[0].value = '';
		} else {
			ackElt[0].value = '1';
		}

	},
	'setInnerHTML' : function (element, text) {
		// Check for no change to avoid flicker in IE 7
		if (element.innerHTML != text) {
			element.innerHTML = text;
		}
	}
}

function fillDestFilename(id) {
	if (!wgUploadAutoFill) {
		return;
	}
	if (!document.getElementById) {
		return;
	}
	// Remove any previously flagged errors
	var e = document.getElementById( 'mw-upload-permitted' );
	if( e ) e.className = '';

	var e = document.getElementById( 'mw-upload-prohibited' );
	if( e ) e.className = '';

	var path = document.getElementById(id).value;
	// Find trailing part
	var slash = path.lastIndexOf('/');
	var backslash = path.lastIndexOf('\\');
	var fname;
	if (slash == -1 && backslash == -1) {
		fname = path;
	} else if (slash > backslash) {
		fname = path.substring(slash+1, 10000);
	} else {
		fname = path.substring(backslash+1, 10000);
	}

	// Clear the filename if it does not have a valid extension.
	// URLs are less likely to have a useful extension, so don't include them in the 
	// extension check.
	if( wgFileExtensions && id != 'wpUploadFileURL' ) {
		var found = false;
		if( fname.lastIndexOf( '.' ) != -1 ) {
			var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
			for( var i = 0; i < wgFileExtensions.length; i++ ) {
				if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() ) {
					found = true;
					break;
				}
			}
		}
		if( !found ) {
			// Not a valid extension
			// Clear the upload and set mw-upload-permitted to error
			document.getElementById(id).value = '';
			var e = document.getElementById( 'mw-upload-permitted' );
			if( e ) e.className = 'error';

			var e = document.getElementById( 'mw-upload-prohibited' );
			if( e ) e.className = 'error';

			// Clear wpDestFile as well
			var e = document.getElementById( 'wpDestFile' )
			if( e ) e.value = '';

			return false;
		}
	}

	// Capitalise first letter and replace spaces by underscores
	// FIXME: $wgCapitalizedNamespaces
	fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');

	// Output result
	var destFile = document.getElementById('wpDestFile');
	if (destFile) {
		destFile.value = fname;
		wgUploadWarningObj.checkNow(fname) ;
	}
}

function toggleFilenameFiller() {
	if(!document.getElementById) return;
	var upfield = document.getElementById('wpUploadFile');
	var destName = document.getElementById('wpDestFile').value;
	if (destName=='' || destName==' ') {
		wgUploadAutoFill = true;
	} else {
		wgUploadAutoFill = false;
	}
}

var wgUploadLicenseObj = {

	'responseCache' : { '' : '' },

	'fetchPreview': function( license ) {
		if( !wgAjaxLicensePreview ) return;
		for (cached in this.responseCache) {
			if (cached == license) {
				this.showPreview( this.responseCache[license] );
				return;
			}
		}
		injectSpinner( document.getElementById( 'wpLicense' ), 'license' );
		
		var title = document.getElementById('wpDestFile').value;
		if ( !title ) title = 'File:Sample.jpg';
		
		var url = wgScriptPath + '/api' + wgScriptExtension
			+ '?action=parse&text={{' + encodeURIComponent( license ) + '}}'
			+ '&title=' + encodeURIComponent( title ) 
			+ '&prop=text&pst&format=json';
		
		var req = sajax_init_object();
		req.onreadystatechange = function() {
			if ( req.readyState == 4 && req.status == 200 )
				wgUploadLicenseObj.processResult( eval( '(' + req.responseText + ')' ), license );
		};
		req.open( 'GET', url, true );
		req.send( '' );
	},

	'processResult' : function( result, license ) {
		removeSpinner( 'license' );
		this.responseCache[license] = result['parse']['text']['*'];
		this.showPreview( this.responseCache[license] );

	},

	'showPreview' : function( preview ) {
		var previewPanel = document.getElementById( 'mw-license-preview' );
		if( previewPanel.innerHTML != preview )
			previewPanel.innerHTML = preview;
	}

}

addOnloadHook( wgUploadSetup );