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 wgUploadWarningObj
= {
40 'responseCache' : { '' : ' ' },
46 'keypress': function () {
47 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
49 // Find file to upload
50 var destFile
= document
.getElementById('wpDestFile');
51 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
52 if ( !destFile
|| !warningElt
) return ;
54 this.nameToCheck
= destFile
.value
;
57 if ( this.timeoutID
) {
58 window
.clearTimeout( this.timeoutID
);
60 // Check response cache
61 for (cached
in this.responseCache
) {
62 if (this.nameToCheck
== cached
) {
63 this.setWarning(this.responseCache
[this.nameToCheck
]);
68 this.timeoutID
= window
.setTimeout( 'wgUploadWarningObj.timeout()', this.delay
);
71 'checkNow': function (fname
) {
72 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
73 if ( this.timeoutID
) {
74 window
.clearTimeout( this.timeoutID
);
76 this.nameToCheck
= fname
;
80 'timeout' : function() {
81 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
82 injectSpinner( document
.getElementById( 'wpDestFile' ), 'destcheck' );
84 // Get variables into local scope so that they will be preserved for the
85 // anonymous callback. fileName is copied so that multiple overlapping
86 // ajax requests can be supported.
88 var fileName
= this.nameToCheck
;
89 sajax_do_call( 'UploadForm::ajaxGetExistsWarning', [this.nameToCheck
],
91 obj
.processResult(result
, fileName
)
96 'processResult' : function (result
, fileName
) {
97 removeSpinner( 'destcheck' );
98 this.setWarning(result
.responseText
);
99 this.responseCache
[fileName
] = result
.responseText
;
102 'setWarning' : function (warning
) {
103 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
104 var ackElt
= document
.getElementById( 'wpDestFileWarningAck' );
105 this.setInnerHTML(warningElt
, warning
);
107 // Set a value in the form indicating that the warning is acknowledged and
108 // doesn't need to be redisplayed post-upload
109 if ( warning
== '' || warning
== ' ' ) {
115 'setInnerHTML' : function (element
, text
) {
116 // Check for no change to avoid flicker in IE 7
117 if (element
.innerHTML
!= text
) {
118 element
.innerHTML
= text
;
123 function fillDestFilename(id
) {
124 if (!wgUploadAutoFill
) {
127 if (!document
.getElementById
) {
130 // Remove any previously flagged errors
131 var e
= document
.getElementById( 'mw-upload-permitted' );
132 if( e
) e
.className
= '';
134 var e
= document
.getElementById( 'mw-upload-prohibited' );
135 if( e
) e
.className
= '';
137 var path
= document
.getElementById(id
).value
;
138 // Find trailing part
139 var slash
= path
.lastIndexOf('/');
140 var backslash
= path
.lastIndexOf('\\');
142 if (slash
== -1 && backslash
== -1) {
144 } else if (slash
> backslash
) {
145 fname
= path
.substring(slash
+1, 10000);
147 fname
= path
.substring(backslash
+1, 10000);
150 // Clear the filename if it does not have a valid extension.
151 // URLs are less likely to have a useful extension, so don't include them in the
153 if( wgFileExtensions
&& id
!= 'wpUploadFileURL' ) {
155 if( fname
.lastIndexOf( '.' ) != -1 ) {
156 var ext
= fname
.substr( fname
.lastIndexOf( '.' ) + 1 );
157 for( var i
= 0; i
< wgFileExtensions
.length
; i
++ ) {
158 if( wgFileExtensions
[i
].toLowerCase() == ext
.toLowerCase() ) {
165 // Not a valid extension
166 // Clear the upload and set mw-upload-permitted to error
167 document
.getElementById(id
).value
= '';
168 var e
= document
.getElementById( 'mw-upload-permitted' );
169 if( e
) e
.className
= 'error';
171 var e
= document
.getElementById( 'mw-upload-prohibited' );
172 if( e
) e
.className
= 'error';
174 // Clear wpDestFile as well
175 var e
= document
.getElementById( 'wpDestFile' )
176 if( e
) e
.value
= '';
182 // Capitalise first letter and replace spaces by underscores
183 fname
= fname
.charAt(0).toUpperCase().concat(fname
.substring(1,10000)).replace(/ /g
, '_');
186 var destFile
= document
.getElementById('wpDestFile');
188 destFile
.value
= fname
;
189 wgUploadWarningObj
.checkNow(fname
) ;
193 function toggleFilenameFiller() {
194 if(!document
.getElementById
) return;
195 var upfield
= document
.getElementById('wpUploadFile');
196 var destName
= document
.getElementById('wpDestFile').value
;
197 if (destName
=='' || destName
==' ') {
198 wgUploadAutoFill
= true;
200 wgUploadAutoFill
= false;
204 var wgUploadLicenseObj
= {
206 'responseCache' : { '' : '' },
208 'fetchPreview': function( license
) {
209 if( !wgAjaxLicensePreview
|| !sajax_init_object() ) return;
210 for (cached
in this.responseCache
) {
211 if (cached
== license
) {
212 this.showPreview( this.responseCache
[license
] );
216 injectSpinner( document
.getElementById( 'wpLicense' ), 'license' );
217 sajax_do_call( 'UploadForm::ajaxGetLicensePreview', [license
],
219 wgUploadLicenseObj
.processResult( result
, license
);
224 'processResult' : function( result
, license
) {
225 removeSpinner( 'license' );
226 this.showPreview( result
.responseText
);
227 this.responseCache
[license
] = result
.responseText
;
230 'showPreview' : function( preview
) {
231 var previewPanel
= document
.getElementById( 'mw-license-preview' );
232 if( previewPanel
.innerHTML
!= preview
)
233 previewPanel
.innerHTML
= preview
;
238 addOnloadHook( wgUploadSetup
);