2 * Legacy emulation for the now depricated skins/common/upload.js
9 $.extend( true, mw
.legacy
, {
11 /* Global Variables */
13 'wgUploadWarningObj': {
15 /* Global Variables */
17 'responseCache' : { '' : ' ' },
25 'keypress': function () {
26 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
28 // Find file to upload
29 var destFile
= document
.getElementById( 'wpDestFile' );
30 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
31 if ( !destFile
|| !warningElt
) return ;
32 this.nameToCheck
= destFile
.value
;
34 if ( this.timeoutID
) {
35 window
.clearTimeout( this.timeoutID
);
37 // Check response cache
38 for ( cached
in this.responseCache
) {
39 if ( this.nameToCheck
== cached
) {
40 this.setWarning( this.responseCache
[this.nameToCheck
] );
44 this.timeoutID
= window
.setTimeout( 'wgUploadWarningObj.timeout()', this.delay
);
46 'checkNow': function ( fname
) {
47 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
48 if ( this.timeoutID
) {
49 window
.clearTimeout( this.timeoutID
);
51 this.nameToCheck
= fname
;
54 'timeout' : function() {
55 if ( !wgAjaxUploadDestCheck
|| !sajax_init_object() ) return;
56 injectSpinner( document
.getElementById( 'wpDestFile' ), 'destcheck' );
58 // Get variables into local scope so that they will be preserved for the
59 // anonymous callback. fileName is copied so that multiple overlapping
60 // ajax requests can be supported.
62 var fileName
= this.nameToCheck
;
63 sajax_do_call( 'SpecialUpload::ajaxGetExistsWarning', [this.nameToCheck
],
65 obj
.processResult( result
, fileName
)
69 'processResult' : function ( result
, fileName
) {
70 removeSpinner( 'destcheck' );
71 this.setWarning( result
.responseText
);
72 this.responseCache
[fileName
] = result
.responseText
;
74 'setWarning' : function ( warning
) {
75 var warningElt
= document
.getElementById( 'wpDestFile-warning' );
76 var ackElt
= document
.getElementsByName( 'wpDestFileWarningAck' );
78 this.setInnerHTML( warningElt
, warning
);
80 // Set a value in the form indicating that the warning is acknowledged and
81 // doesn't need to be redisplayed post-upload
82 if ( warning
== '' || warning
== ' ' ) {
85 ackElt
[0].value
= '1';
88 'setInnerHTML' : function ( element
, text
) {
89 // Check for no change to avoid flicker in IE 7
90 if ( element
.innerHTML
!= text
) {
91 element
.innerHTML
= text
;
95 var wgUploadLicenseObj
= {
97 /* Global Variables */
99 'responseCache' : { '' : '' },
103 'fetchPreview': function( license
) {
104 if ( !wgAjaxLicensePreview
) return;
105 for ( cached
in this.responseCache
) {
106 if ( cached
== license
) {
107 this.showPreview( this.responseCache
[license
] );
111 injectSpinner( document
.getElementById( 'wpLicense' ), 'license' );
112 var title
= document
.getElementById( 'wpDestFile' ).value
;
113 if ( !title
) title
= 'File:Sample.jpg';
114 var url
= wgScriptPath
+ '/api' + wgScriptExtension
115 + '?action=parse&text={{' + encodeURIComponent( license
) + '}}'
116 + '&title=' + encodeURIComponent( title
)
117 + '&prop=text&pst&format=json';
118 var req
= sajax_init_object();
119 req
.onreadystatechange = function() {
120 if ( req
.readyState
== 4 && req
.status
== 200 )
121 wgUploadLicenseObj
.processResult( eval( '( ' + req
.responseText
+ ' )' ), license
);
123 req
.open( 'GET', url
, true );
126 'processResult' : function( result
, license
) {
127 removeSpinner( 'license' );
128 this.responseCache
[license
] = result
['parse']['text']['*'];
129 this.showPreview( this.responseCache
[license
] );
131 'showPreview' : function( preview
) {
132 var previewPanel
= document
.getElementById( 'mw-license-preview' );
133 if( previewPanel
.innerHTML
!= preview
)
134 previewPanel
.innerHTML
= preview
;
140 'licenseSelectorCheck': function() {
141 var selector
= document
.getElementById( 'wpLicense' );
142 var selection
= selector
.options
[selector
.selectedIndex
].value
;
143 if( selector
.selectedIndex
> 0 ) {
144 if( selection
== '' ) {
145 // Option disabled, but browser is broken and doesn't respect this
146 selector
.selectedIndex
= 0;
149 // We might show a preview
150 wgUploadLicenseObj
.fetchPreview( selection
);
152 'wgUploadSetup': function() {
153 // Disable URL box if the URL copy upload source type is not selected
154 var e
= document
.getElementById( 'wpSourceTypeurl' );
157 var ein
= document
.getElementById( 'wpUploadFileURL' );
159 ein
.setAttribute( 'disabled', 'disabled' );
162 // For MSIE/Mac: non-breaking spaces cause the <option> not to render.
163 // But for some reason, setting the text to itself works
164 var selector
= document
.getElementById( 'wpLicense' );
166 var ua
= navigator
.userAgent
;
167 var isMacIe
= ( ua
.indexOf( 'MSIE' ) != -1 ) && ( ua
.indexOf( 'Mac' ) != -1 );
169 for ( var i
= 0; i
< selector
.options
.length
; i
++ ) {
170 selector
.options
[i
].text
= selector
.options
[i
].text
;
174 // Toggle source type
175 var sourceTypeCheckboxes
= document
.getElementsByName( 'wpSourceType' );
176 for ( var i
= 0; i
< sourceTypeCheckboxes
.length
; i
++ ) {
177 sourceTypeCheckboxes
[i
].onchange
= toggleUploadInputs
;
179 // AJAX wpDestFile warnings
180 if ( wgAjaxUploadDestCheck
) {
181 // Insert an event handler that fetches upload warnings when wpDestFile
183 document
.getElementById( 'wpDestFile' ).onchange = function ( e
) {
184 wgUploadWarningObj
.checkNow( this.value
);
186 // Insert a row where the warnings will be displayed just below the
188 var optionsTable
= document
.getElementById( 'mw-htmlform-description' ).tBodies
[0];
189 var row
= optionsTable
.insertRow( 1 );
190 var td
= document
.createElement( 'td' );
191 td
.id
= 'wpDestFile-warning';
193 row
.appendChild( td
);
195 if ( wgAjaxLicensePreview
) {
196 // License selector check
197 document
.getElementById( 'wpLicense' ).onchange
= licenseSelectorCheck
;
198 // License selector table row
199 var wpLicense
= document
.getElementById( 'wpLicense' );
200 var wpLicenseRow
= wpLicense
.parentNode
.parentNode
;
201 var wpLicenseTbody
= wpLicenseRow
.parentNode
;
202 var row
= document
.createElement( 'tr' );
203 var td
= document
.createElement( 'td' );
204 row
.appendChild( td
);
205 td
= document
.createElement( 'td' );
206 td
.id
= 'mw-license-preview';
207 row
.appendChild( td
);
208 wpLicenseTbody
.insertBefore( row
, wpLicenseRow
.nextSibling
);
210 // fillDestFile setup
211 for ( var i
= 0; i
< wgUploadSourceIds
.length
; i
++ )
212 document
.getElementById( wgUploadSourceIds
[i
] ).onchange = function ( e
) {
213 fillDestFilename( this.id
);
217 * Iterate over all upload source fields and disable all except the selected one.
220 * The id of the selected radio button
223 'toggleUploadInputs': function() {
224 // Iterate over all rows with UploadSourceField
226 if ( document
.getElementsByClassName
) {
227 rows
= document
.getElementsByClassName( 'mw-htmlform-field-UploadSourceField' );
229 // Older browsers don't support getElementsByClassName
231 var allRows
= document
.getElementsByTagName( 'tr' );
232 for ( var i
= 0; i
< allRows
.length
; i
++ ) {
233 if ( allRows
[i
].className
== 'mw-htmlform-field-UploadSourceField' )
234 rows
.push( allRows
[i
] );
237 for ( var i
= 0; i
< rows
.length
; i
++ ) {
238 var inputs
= rows
[i
].getElementsByTagName( 'input' );
239 // Check if this row is selected
240 var isChecked
= true; // Default true in case wpSourceType is not found
241 for ( var j
= 0; j
< inputs
.length
; j
++ ) {
242 if ( inputs
[j
].name
== 'wpSourceType' )
243 isChecked
= inputs
[j
].checked
;
245 // Disable all unselected rows
246 for ( var j
= 0; j
< inputs
.length
; j
++ ) {
247 if ( inputs
[j
].type
!= 'radio' )
248 inputs
[j
].disabled
= !isChecked
;
252 'fillDestFilename': function( id
) {
253 if ( !wgUploadAutoFill
) {
256 if ( !document
.getElementById
) {
259 // Remove any previously flagged errors
260 var e
= document
.getElementById( 'mw-upload-permitted' );
261 if( e
) e
.className
= '';
262 var e
= document
.getElementById( 'mw-upload-prohibited' );
263 if( e
) e
.className
= '';
264 var path
= document
.getElementById( id
).value
;
265 // Find trailing part
266 var slash
= path
.lastIndexOf( '/' );
267 var backslash
= path
.lastIndexOf( '\\' );
269 if ( slash
== -1 && backslash
== -1 ) {
271 } else if ( slash
> backslash
) {
272 fname
= path
.substring( slash
+1, 10000 );
274 fname
= path
.substring( backslash
+1, 10000 );
276 // Clear the filename if it does not have a valid extension.
277 // URLs are less likely to have a useful extension, so don't include them in the
279 if( wgStrictFileExtensions
&& wgFileExtensions
&& id
!= 'wpUploadFileURL' ) {
281 if ( fname
.lastIndexOf( '.' ) != -1 ) {
282 var ext
= fname
.substr( fname
.lastIndexOf( '.' ) + 1 );
283 for ( var i
= 0; i
< wgFileExtensions
.length
; i
++ ) {
284 if ( wgFileExtensions
[i
].toLowerCase() == ext
.toLowerCase() ) {
291 // Not a valid extension
292 // Clear the upload and set mw-upload-permitted to error
293 document
.getElementById( id
).value
= '';
294 var e
= document
.getElementById( 'mw-upload-permitted' );
295 if ( e
) e
.className
= 'error';
296 var e
= document
.getElementById( 'mw-upload-prohibited' );
297 if ( e
) e
.className
= 'error';
298 // Clear wpDestFile as well
299 var e
= document
.getElementById( 'wpDestFile' )
300 if ( e
) e
.value
= '';
305 // Capitalise first letter and replace spaces by underscores
306 // FIXME: $wgCapitalizedNamespaces
307 fname
= fname
.charAt( 0 ).toUpperCase().concat( fname
.substring( 1,10000 ) ).replace( / /g
, '_' );
309 var destFile
= document
.getElementById( 'wpDestFile' );
311 destFile
.value
= fname
;
312 wgUploadWarningObj
.checkNow( fname
) ;
315 'toggleFilenameFiller': function() {
316 if ( !document
.getElementById
) return;
317 var upfield
= document
.getElementById( 'wpUploadFile' );
318 var destName
= document
.getElementById( 'wpDestFile' ).value
;
319 if ( destName
=='' || destName
==' ' ) {
320 wgUploadAutoFill
= true;
322 wgUploadAutoFill
= false;
329 $( document
).ready( function() {
330 mw
.legacy
.wgUploadSetup();
333 } )( jQuery
, mediaWiki
);