2 var ajaxUploadDestCheck
= mw
.config
.get( 'wgAjaxUploadDestCheck' ),
3 fileExtensions
= mw
.config
.get( 'wgFileExtensions' );
5 window
.licenseSelectorCheck = function() {
6 var selector
= document
.getElementById( "wpLicense" );
7 var selection
= selector
.options
[selector
.selectedIndex
].value
;
8 if( selector
.selectedIndex
> 0 ) {
9 if( selection
== "" ) {
10 // Option disabled, but browser is broken and doesn't respect this
11 selector
.selectedIndex
= 0;
14 // We might show a preview
15 wgUploadLicenseObj
.fetchPreview( selection
);
18 function uploadSetup() {
19 // Disable URL box if the URL copy upload source type is not selected
20 var e
= document
.getElementById( 'wpSourceTypeurl' );
23 var ein
= document
.getElementById( 'wpUploadFileURL' );
25 ein
.setAttribute( 'disabled', 'disabled' );
29 // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
30 // But for some reason, setting the text to itself works
31 var selector
= document
.getElementById("wpLicense");
33 var ua
= navigator
.userAgent
;
34 var isMacIe
= (ua
.indexOf("MSIE") != -1) && (ua
.indexOf("Mac") != -1);
36 for (var i
= 0; i
< selector
.options
.length
; i
++) {
37 selector
.options
[i
].text
= selector
.options
[i
].text
;
42 // AJAX wpDestFile warnings
43 if ( ajaxUploadDestCheck
) {
44 // Insert an event handler that fetches upload warnings when wpDestFile
46 document
.getElementById( 'wpDestFile' ).onchange = function ( e
) {
47 wgUploadWarningObj
.checkNow(this.value
);
49 // Insert a row where the warnings will be displayed just below the
51 var optionsTable
= document
.getElementById( 'mw-htmlform-description' ).tBodies
[0];
52 var row
= optionsTable
.insertRow( 1 );
53 var td
= document
.createElement( 'td' );
54 td
.id
= 'wpDestFile-warning';
57 row
.appendChild( td
);
60 var wpLicense
= document
.getElementById( 'wpLicense' );
61 if ( mw
.config
.get( 'wgAjaxLicensePreview' ) && wpLicense
) {
62 // License selector check
63 wpLicense
.onchange
= licenseSelectorCheck
;
65 // License selector table row
66 var wpLicenseRow
= wpLicense
.parentNode
.parentNode
;
67 var wpLicenseTbody
= wpLicenseRow
.parentNode
;
69 var row
= document
.createElement( 'tr' );
70 var td
= document
.createElement( 'td' );
71 row
.appendChild( td
);
72 td
= document
.createElement( 'td' );
73 td
.id
= 'mw-license-preview';
74 row
.appendChild( td
);
76 wpLicenseTbody
.insertBefore( row
, wpLicenseRow
.nextSibling
);
82 uploadSourceIds
= mw
.config
.get( 'wgUploadSourceIds' ),
83 len
= uploadSourceIds
.length
;
84 for ( i
= 0; i
< len
; i
+= 1 )
85 document
.getElementById( uploadSourceIds
[i
] ).onchange = function (e
) {
86 fillDestFilename( this.id
);
91 window
.wgUploadWarningObj
= {
92 'responseCache' : { '' : ' ' },
98 'keypress': function () {
99 if ( !ajaxUploadDestCheck
) return;
101 // Find file to upload
102 var destFile
= document
.getElementById('wpDestFile');
103 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
104 if ( !destFile
|| !warningElt
) return ;
106 this.nameToCheck
= destFile
.value
;
109 if ( this.timeoutID
) {
110 window
.clearTimeout( this.timeoutID
);
112 // Check response cache
113 for (cached
in this.responseCache
) {
114 if (this.nameToCheck
== cached
) {
115 this.setWarning(this.responseCache
[this.nameToCheck
]);
120 this.timeoutID
= window
.setTimeout( 'wgUploadWarningObj.timeout()', this.delay
);
123 'checkNow': function (fname
) {
124 if ( !ajaxUploadDestCheck
) return;
125 if ( this.timeoutID
) {
126 window
.clearTimeout( this.timeoutID
);
128 this.nameToCheck
= fname
;
132 'timeout' : function() {
133 if ( !ajaxUploadDestCheck
|| this.nameToCheck
=== '' ) return;
134 injectSpinner( document
.getElementById( 'wpDestFile' ), 'destcheck' );
136 var uploadWarningObj
= this;
137 ( new mw
.Api
).get( {
139 titles
: ( new mw
.Title( this.nameToCheck
, mw
.config
.get( 'wgNamespaceIds' ).file
) ).getPrefixedText(),
141 iiprop
: 'uploadwarning',
143 } ).done( function ( result
) {
145 if ( result
.query
) {
146 resultOut
= result
.query
.pages
[result
.query
.pageids
[0]].imageinfo
[0];
148 uploadWarningObj
.processResult( resultOut
, uploadWarningObj
.nameToCheck
);
152 'processResult' : function ( result
, fileName
) {
153 removeSpinner( 'destcheck' );
154 this.setWarning( result
.html
);
155 this.responseCache
[fileName
] = result
.html
;
158 'setWarning' : function (warning
) {
159 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
160 var ackElt
= document
.getElementsByName( 'wpDestFileWarningAck' );
162 this.setInnerHTML(warningElt
, warning
);
164 // Set a value in the form indicating that the warning is acknowledged and
165 // doesn't need to be redisplayed post-upload
166 if ( warning
== '' ) {
167 ackElt
[0].value
= '';
169 ackElt
[0].value
= '1';
173 'setInnerHTML' : function (element
, text
) {
174 // Check for no change to avoid flicker in IE 7
175 if (element
.innerHTML
!= text
) {
176 element
.innerHTML
= text
;
181 window
.fillDestFilename = function(id
) {
182 if ( !mw
.config
.get( 'wgUploadAutoFill' ) ) {
185 if (!document
.getElementById
) {
188 // Remove any previously flagged errors
189 var e
= document
.getElementById( 'mw-upload-permitted' );
190 if( e
) e
.className
= '';
192 var e
= document
.getElementById( 'mw-upload-prohibited' );
193 if( e
) e
.className
= '';
195 var path
= document
.getElementById(id
).value
;
196 // Find trailing part
197 var slash
= path
.lastIndexOf('/');
198 var backslash
= path
.lastIndexOf('\\');
200 if (slash
== -1 && backslash
== -1) {
202 } else if (slash
> backslash
) {
203 fname
= path
.substring(slash
+1, 10000);
205 fname
= path
.substring(backslash
+1, 10000);
208 // Clear the filename if it does not have a valid extension.
209 // URLs are less likely to have a useful extension, so don't include them in the
211 if ( mw
.config
.get( 'wgStrictFileExtensions' ) && fileExtensions
&& id
!== 'wpUploadFileURL' ) {
213 if ( fname
.lastIndexOf( '.' ) !== -1 ) {
214 var ext
= fname
.substr( fname
.lastIndexOf( '.' ) + 1 );
215 for ( var i
= 0; i
< fileExtensions
.length
; i
+= 1 ) {
216 if ( fileExtensions
[i
].toLowerCase() === ext
.toLowerCase() ) {
223 // Not a valid extension
224 // Clear the upload and set mw-upload-permitted to error
225 document
.getElementById(id
).value
= '';
226 var e
= document
.getElementById( 'mw-upload-permitted' );
227 if( e
) e
.className
= 'error';
229 var e
= document
.getElementById( 'mw-upload-prohibited' );
230 if( e
) e
.className
= 'error';
232 // Clear wpDestFile as well
233 var e
= document
.getElementById( 'wpDestFile' );
234 if( e
) e
.value
= '';
240 // Replace spaces by underscores
241 fname
= fname
.replace( / /g
, '_' );
242 // Capitalise first letter if needed
243 if ( mw
.config
.get( 'wgCapitalizeUploads' ) ) {
244 fname
= fname
.charAt( 0 ).toUpperCase().concat( fname
.substring( 1, 10000 ) );
248 var destFile
= document
.getElementById( 'wpDestFile' );
250 // Call decodeURIComponent function to remove possible URL-encoded characters
251 // from the file name (bug 30390). Especially likely with upload-form-url.
252 // decodeURIComponent can throw an exception in input is invalid utf-8
254 destFile
.value
= decodeURIComponent( fname
);
256 destFile
.value
= fname
;
258 wgUploadWarningObj
.checkNow( fname
);
262 window
.toggleFilenameFiller = function() {
263 if(!document
.getElementById
) return;
264 var upfield
= document
.getElementById('wpUploadFile');
265 var destName
= document
.getElementById('wpDestFile').value
;
266 wgUploadAutoFill
= ( destName
== '' || destName
== ' ' );
269 window
.wgUploadLicenseObj
= {
271 'responseCache' : { '' : '' },
273 'fetchPreview': function( license
) {
274 if ( !mw
.config
.get( 'wgAjaxLicensePreview' ) ) return;
275 for (cached
in this.responseCache
) {
276 if (cached
== license
) {
277 this.showPreview( this.responseCache
[license
] );
281 injectSpinner( document
.getElementById( 'wpLicense' ), 'license' );
283 var title
= document
.getElementById('wpDestFile').value
;
284 if ( !title
) title
= 'File:Sample.jpg';
286 ( new mw
.Api
).get( {
288 text
: '{{' + license
+ '}}',
292 } ).done( function ( result
) {
293 wgUploadLicenseObj
.processResult( result
, license
);
297 'processResult' : function( result
, license
) {
298 removeSpinner( 'license' );
299 this.responseCache
[license
] = result
['parse']['text']['*'];
300 this.showPreview( this.responseCache
[license
] );
303 'showPreview' : function( preview
) {
304 var previewPanel
= document
.getElementById( 'mw-license-preview' );
305 if( previewPanel
.innerHTML
!= preview
)
306 previewPanel
.innerHTML
= preview
;
313 }( mediaWiki
, jQuery
) );