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 // License selector check
59 document
.getElementById( 'wpLicense' ).onchange
= licenseSelectorCheck
;
62 for ( var i
= 0; i
< wgUploadSourceIds
.length
; i
++ )
63 document
.getElementById( wgUploadSourceIds
[i
] ).onchange = function (e
) {
64 fillDestFilename( this.id
);
69 * Iterate over all upload source fields and disable all except the selected one.
71 * @param enabledId The id of the selected radio button
74 function toggleUploadInputs() {
75 // Iterate over all rows with UploadSourceField
77 if ( document
.getElementsByClassName
) {
78 rows
= document
.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
80 // Older browsers don't support getElementsByClassName
83 var allRows
= document
.getElementsByTagName( 'tr' );
84 for ( var i
= 0; i
< allRows
.length
; i
++ ) {
85 if ( allRows
[i
].className
== 'mw-htmlform-field-UploadSourceField' )
86 rows
.push( allRows
[i
] );
90 for ( var i
= 0; i
< rows
.length
; i
++ ) {
91 var inputs
= rows
[i
].getElementsByTagName( 'input' );
93 // Check if this row is selected
94 var isChecked
= true; // Default true in case wpSourceType is not found
95 for ( var j
= 0; j
< inputs
.length
; j
++ ) {
96 if ( inputs
[j
].name
== 'wpSourceType' )
97 isChecked
= inputs
[j
].checked
;
100 // Disable all unselected rows
101 for ( var j
= 0; j
< inputs
.length
; j
++ ) {
102 if ( inputs
[j
].type
!= 'radio')
103 inputs
[j
].disabled
= !isChecked
;
108 var wgUploadWarningObj
= {
109 'responseCache' : { '' : ' ' },
115 'keypress': function () {
116 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
118 // Find file to upload
119 var destFile
= document
.getElementById('wpDestFile');
120 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
121 if ( !destFile
|| !warningElt
) return ;
123 this.nameToCheck
= destFile
.value
;
126 if ( this.timeoutID
) {
127 window
.clearTimeout( this.timeoutID
);
129 // Check response cache
130 for (cached
in this.responseCache
) {
131 if (this.nameToCheck
== cached
) {
132 this.setWarning(this.responseCache
[this.nameToCheck
]);
137 this.timeoutID
= window
.setTimeout( 'wgUploadWarningObj.timeout()', this.delay
);
140 'checkNow': function (fname
) {
141 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
142 if ( this.timeoutID
) {
143 window
.clearTimeout( this.timeoutID
);
145 this.nameToCheck
= fname
;
149 'timeout' : function() {
150 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
151 injectSpinner( document
.getElementById( 'wpDestFile' ), 'destcheck' );
153 // Get variables into local scope so that they will be preserved for the
154 // anonymous callback. fileName is copied so that multiple overlapping
155 // ajax requests can be supported.
157 var fileName
= this.nameToCheck
;
158 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck
],
160 obj
.processResult(result
, fileName
)
165 'processResult' : function (result
, fileName
) {
166 removeSpinner( 'destcheck' );
167 this.setWarning(result
.responseText
);
168 this.responseCache
[fileName
] = result
.responseText
;
171 'setWarning' : function (warning
) {
172 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
173 this.setInnerHTML(warningElt
, warning
);
175 'setInnerHTML' : function (element
, text
) {
176 // Check for no change to avoid flicker in IE 7
177 if (element
.innerHTML
!= text
) {
178 element
.innerHTML
= text
;
183 function fillDestFilename(id
) {
184 if (!wgUploadAutoFill
) {
187 if (!document
.getElementById
) {
190 // Remove any previously flagged errors
191 var e
= document
.getElementById( 'mw-upload-permitted' );
192 if( e
) e
.className
= '';
194 var e
= document
.getElementById( 'mw-upload-prohibited' );
195 if( e
) e
.className
= '';
197 var path
= document
.getElementById(id
).value
;
198 // Find trailing part
199 var slash
= path
.lastIndexOf('/');
200 var backslash
= path
.lastIndexOf('\\');
202 if (slash
== -1 && backslash
== -1) {
204 } else if (slash
> backslash
) {
205 fname
= path
.substring(slash
+1, 10000);
207 fname
= path
.substring(backslash
+1, 10000);
210 // Clear the filename if it does not have a valid extension.
211 // URLs are less likely to have a useful extension, so don't include them in the
213 if( wgFileExtensions
&& id
!= 'wpUploadFileURL' ) {
215 if( fname
.lastIndexOf( '.' ) != -1 ) {
216 var ext
= fname
.substr( fname
.lastIndexOf( '.' ) + 1 );
217 for( var i
= 0; i
< wgFileExtensions
.length
; i
++ ) {
218 if( wgFileExtensions
[i
].toLowerCase() == ext
.toLowerCase() ) {
225 // Not a valid extension
226 // Clear the upload and set mw-upload-permitted to error
227 document
.getElementById(id
).value
= '';
228 var e
= document
.getElementById( 'mw-upload-permitted' );
229 if( e
) e
.className
= 'error';
231 var e
= document
.getElementById( 'mw-upload-prohibited' );
232 if( e
) e
.className
= 'error';
234 // Clear wpDestFile as well
235 var e
= document
.getElementById( 'wpDestFile' )
236 if( e
) e
.value
= '';
242 // Capitalise first letter and replace spaces by underscores
243 // FIXME: $wgCapitalizedNamespaces
244 fname
= fname
.charAt(0).toUpperCase().concat(fname
.substring(1,10000)).replace(/ /g
, '_');
247 var destFile
= document
.getElementById('wpDestFile');
249 destFile
.value
= fname
;
250 wgUploadWarningObj
.checkNow(fname
) ;
254 function toggleFilenameFiller() {
255 if(!document
.getElementById
) return;
256 var upfield
= document
.getElementById('wpUploadFile');
257 var destName
= document
.getElementById('wpDestFile').value
;
258 if (destName
=='' || destName
==' ') {
259 wgUploadAutoFill
= true;
261 wgUploadAutoFill
= false;
265 var wgUploadLicenseObj
= {
267 'responseCache' : { '' : '' },
269 'fetchPreview': function( license
) {
270 if( !wgAjaxLicensePreview
|| !sajax_init_object() ) return;
271 for (cached
in this.responseCache
) {
272 if (cached
== license
) {
273 this.showPreview( this.responseCache
[license
] );
277 injectSpinner( document
.getElementById( 'wpLicense' ), 'license' );
278 sajax_do_call( 'SpecialUpload::ajaxGetLicensePreview', [license
],
280 wgUploadLicenseObj
.processResult( result
, license
);
285 'processResult' : function( result
, license
) {
286 removeSpinner( 'license' );
287 this.showPreview( result
.responseText
);
288 this.responseCache
[license
] = result
.responseText
;
291 'showPreview' : function( preview
) {
292 var previewPanel
= document
.getElementById( 'mw-license-preview' );
293 if( previewPanel
.innerHTML
!= preview
)
294 previewPanel
.innerHTML
= preview
;
299 addOnloadHook( wgUploadSetup
);