1 function licenseSelectorCheck() {
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 function wgUploadSetup() {
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 var sourceTypeCheckboxes
= document
.getElementsByName( 'wpSourceType' );
40 for ( var i
= 0; i
< sourceTypeCheckboxes
.length
; i
++ ) {
41 sourceTypeCheckboxes
[i
].onchange
= toggleUploadInputs
;
44 // AJAX wpDestFile warnings
45 if ( wgAjaxUploadDestCheck
) {
46 document
.getElementById( 'wpDestFile' ).onchange = function ( e
) {
47 wgUploadWarningObj
.checkNow(this.value
);
49 var optionsTable
= document
.getElementById( 'mw-htmlform-options' ).tBodies
[0];
50 var row
= document
.createElement( 'tr' );
51 var td
= document
.createElement( 'td' );
52 td
.id
= 'wpDestFile-warning';
54 row
.appendChild( td
);
55 optionsTable
.appendChild( row
);
58 if ( wgAjaxLicensePreview
) {
59 // License selector check
60 document
.getElementById( 'wpLicense' ).onchange
= licenseSelectorCheck
;
62 // License selector table row
63 var wpLicense
= document
.getElementById( 'wpLicense' );
64 var wpLicenseRow
= wpLicense
.parentNode
.parentNode
;
65 var wpLicenseTbody
= wpLicenseRow
.parentNode
;
67 var row
= document
.createElement( 'tr' );
68 var td
= document
.createElement( 'td' );
69 row
.appendChild( td
);
70 td
= document
.createElement( 'td' );
71 td
.id
= 'mw-license-preview';
72 row
.appendChild( td
);
74 wpLicenseTbody
.insertBefore( row
, wpLicenseRow
.nextSibling
);
79 for ( var i
= 0; i
< wgUploadSourceIds
.length
; i
++ )
80 document
.getElementById( wgUploadSourceIds
[i
] ).onchange = function (e
) {
81 fillDestFilename( this.id
);
86 * Iterate over all upload source fields and disable all except the selected one.
88 * @param enabledId The id of the selected radio button
91 function toggleUploadInputs() {
92 // Iterate over all rows with UploadSourceField
94 if ( document
.getElementsByClassName
) {
95 rows
= document
.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
97 // Older browsers don't support getElementsByClassName
100 var allRows
= document
.getElementsByTagName( 'tr' );
101 for ( var i
= 0; i
< allRows
.length
; i
++ ) {
102 if ( allRows
[i
].className
== 'mw-htmlform-field-UploadSourceField' )
103 rows
.push( allRows
[i
] );
107 for ( var i
= 0; i
< rows
.length
; i
++ ) {
108 var inputs
= rows
[i
].getElementsByTagName( 'input' );
110 // Check if this row is selected
111 var isChecked
= true; // Default true in case wpSourceType is not found
112 for ( var j
= 0; j
< inputs
.length
; j
++ ) {
113 if ( inputs
[j
].name
== 'wpSourceType' )
114 isChecked
= inputs
[j
].checked
;
117 // Disable all unselected rows
118 for ( var j
= 0; j
< inputs
.length
; j
++ ) {
119 if ( inputs
[j
].type
!= 'radio')
120 inputs
[j
].disabled
= !isChecked
;
125 var wgUploadWarningObj
= {
126 'responseCache' : { '' : ' ' },
132 'keypress': function () {
133 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
135 // Find file to upload
136 var destFile
= document
.getElementById('wpDestFile');
137 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
138 if ( !destFile
|| !warningElt
) return ;
140 this.nameToCheck
= destFile
.value
;
143 if ( this.timeoutID
) {
144 window
.clearTimeout( this.timeoutID
);
146 // Check response cache
147 for (cached
in this.responseCache
) {
148 if (this.nameToCheck
== cached
) {
149 this.setWarning(this.responseCache
[this.nameToCheck
]);
154 this.timeoutID
= window
.setTimeout( 'wgUploadWarningObj.timeout()', this.delay
);
157 'checkNow': function (fname
) {
158 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
159 if ( this.timeoutID
) {
160 window
.clearTimeout( this.timeoutID
);
162 this.nameToCheck
= fname
;
166 'timeout' : function() {
167 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
168 injectSpinner( document
.getElementById( 'wpDestFile' ), 'destcheck' );
170 // Get variables into local scope so that they will be preserved for the
171 // anonymous callback. fileName is copied so that multiple overlapping
172 // ajax requests can be supported.
174 var fileName
= this.nameToCheck
;
175 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck
],
177 obj
.processResult(result
, fileName
)
182 'processResult' : function (result
, fileName
) {
183 removeSpinner( 'destcheck' );
184 this.setWarning(result
.responseText
);
185 this.responseCache
[fileName
] = result
.responseText
;
188 'setWarning' : function (warning
) {
189 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
190 this.setInnerHTML(warningElt
, warning
);
192 'setInnerHTML' : function (element
, text
) {
193 // Check for no change to avoid flicker in IE 7
194 if (element
.innerHTML
!= text
) {
195 element
.innerHTML
= text
;
200 function fillDestFilename(id
) {
201 if (!wgUploadAutoFill
) {
204 if (!document
.getElementById
) {
207 // Remove any previously flagged errors
208 var e
= document
.getElementById( 'mw-upload-permitted' );
209 if( e
) e
.className
= '';
211 var e
= document
.getElementById( 'mw-upload-prohibited' );
212 if( e
) e
.className
= '';
214 var path
= document
.getElementById(id
).value
;
215 // Find trailing part
216 var slash
= path
.lastIndexOf('/');
217 var backslash
= path
.lastIndexOf('\\');
219 if (slash
== -1 && backslash
== -1) {
221 } else if (slash
> backslash
) {
222 fname
= path
.substring(slash
+1, 10000);
224 fname
= path
.substring(backslash
+1, 10000);
227 // Clear the filename if it does not have a valid extension.
228 // URLs are less likely to have a useful extension, so don't include them in the
230 if( wgFileExtensions
&& id
!= 'wpUploadFileURL' ) {
232 if( fname
.lastIndexOf( '.' ) != -1 ) {
233 var ext
= fname
.substr( fname
.lastIndexOf( '.' ) + 1 );
234 for( var i
= 0; i
< wgFileExtensions
.length
; i
++ ) {
235 if( wgFileExtensions
[i
].toLowerCase() == ext
.toLowerCase() ) {
242 // Not a valid extension
243 // Clear the upload and set mw-upload-permitted to error
244 document
.getElementById(id
).value
= '';
245 var e
= document
.getElementById( 'mw-upload-permitted' );
246 if( e
) e
.className
= 'error';
248 var e
= document
.getElementById( 'mw-upload-prohibited' );
249 if( e
) e
.className
= 'error';
251 // Clear wpDestFile as well
252 var e
= document
.getElementById( 'wpDestFile' )
253 if( e
) e
.value
= '';
259 // Capitalise first letter and replace spaces by underscores
260 // FIXME: $wgCapitalizedNamespaces
261 fname
= fname
.charAt(0).toUpperCase().concat(fname
.substring(1,10000)).replace(/ /g
, '_');
264 var destFile
= document
.getElementById('wpDestFile');
266 destFile
.value
= fname
;
267 wgUploadWarningObj
.checkNow(fname
) ;
271 function toggleFilenameFiller() {
272 if(!document
.getElementById
) return;
273 var upfield
= document
.getElementById('wpUploadFile');
274 var destName
= document
.getElementById('wpDestFile').value
;
275 if (destName
=='' || destName
==' ') {
276 wgUploadAutoFill
= true;
278 wgUploadAutoFill
= false;
282 var wgUploadLicenseObj
= {
284 'responseCache' : { '' : '' },
286 'fetchPreview': function( license
) {
287 if( !wgAjaxLicensePreview
|| !sajax_init_object() ) return;
288 for (cached
in this.responseCache
) {
289 if (cached
== license
) {
290 this.showPreview( this.responseCache
[license
] );
294 injectSpinner( document
.getElementById( 'wpLicense' ), 'license' );
295 sajax_do_call( 'SpecialUpload::ajaxGetLicensePreview', [license
],
297 wgUploadLicenseObj
.processResult( result
, license
);
302 'processResult' : function( result
, license
) {
303 removeSpinner( 'license' );
304 this.showPreview( result
.responseText
);
305 this.responseCache
[license
] = result
.responseText
;
308 'showPreview' : function( preview
) {
309 var previewPanel
= document
.getElementById( 'mw-license-preview' );
310 if( previewPanel
.innerHTML
!= preview
)
311 previewPanel
.innerHTML
= preview
;
316 addOnloadHook( wgUploadSetup
);