2 * This script is run on [[Special:Upload]].
3 * It controls the invocation of the mvUploader class based on local config.
5 js2AddOnloadHook( function() {
8 var mwUploadFormTarget
= '#mw-upload-form';
9 // Set up the upload form bindings once all DOM manipulation is done
10 var mwUploadHelper
= {
13 // If wgEnableFirefogg is not boolean false, set to true
14 if( typeof wgEnableFirefogg
== 'undefined' )
15 wgEnableFirefogg
= true;
17 if( wgEnableFirefogg
) {
18 // Set up the upload handler to Firefogg. Should work with the HTTP uploads too.
19 $j( '#wpUploadFile' ).firefogg( {
20 // An API URL (we won't submit directly to action of the form)
21 'api_url': wgServer
+ wgScriptPath
+ '/api.php',
23 'target_edit_from': mwUploadFormTarget
,
24 'new_source_cb': function( orgFilename
, oggName
) {
25 if( $j( '#wpDestFile' ).val() == "" )
26 $j( '#wpDestFile' ).val( oggName
);
27 $j( '#wpDestFile' ).doDestCheck({
28 'warn_target': '#wpDestFile-warning'
33 // Add basic upload profile support ( http status monitoring, progress box for
34 // browsers that support it, etc.)
35 if( $j( '#wpUploadFileURL' ).length
!= 0 ) {
36 $j( '#wpUploadFileURL' ).baseUploadInterface( {
37 'api_url': wgServer
+ wgScriptPath
+ '/api.php',
38 'target_edit_from': mwUploadFormTarget
43 if( wgAjaxUploadDestCheck
) {
44 // Do destination check
45 $j( '#wpDestFile' ).change( function() {
46 $j( '#wpDestFile' ).doDestCheck({
47 'warn_target':'#wpDestFile-warning'
52 // Check if we have HTTP enabled & setup enable/disable toggle:
53 if( $j( '#wpUploadFileURL' ).length
!= 0 ) {
54 // Set the initial toggleUpType
55 _this
.toggleUpType( true );
57 $j( "input[name='wpSourceType']" ).click( function() {
58 _this
.toggleUpType( this.id
== 'wpSourceTypeFile' );
61 $j( '#wpUploadFile,#wpUploadFileURL' )
63 _this
.toggleUpType( this.id
== 'wpUploadFile' );
65 // Also setup the onChange event binding:
67 if ( wgUploadAutoFill
) {
68 mwUploadHelper
.doDestinationFill( this );
73 * Set the upload radio buttons
77 toggleUpType: function( set ) {
78 $j( '#wpSourceTypeFile' ).attr( 'checked', set );
79 $j( '#wpUploadFile' ).attr( 'disabled', !set );
81 $j( '#wpSourceTypeURL' ).attr( 'checked', !set );
82 $j( '#wpUploadFileURL' ).attr( 'disabled', set );
84 // If Firefogg is enabled, toggle action according to wpSourceTypeFile selection
85 if( wgEnableFirefogg
) {
86 $j( '#wpUploadFile' ).firefogg({
87 'firefogg_form_action': $j( '#wpSourceTypeFile' ).attr( 'checked' )
92 * Fill in a destination file-name based on a source asset name.
94 doDestinationFill: function( targetElm
) {
95 js_log( "doDestinationFill" )
96 // Remove any previously flagged errors
97 $j( '#mw-upload-permitted,#mw-upload-prohibited' ).hide();
99 var path
= $j( targetElm
).val();
100 // Find trailing part
101 var slash
= path
.lastIndexOf( '/' );
102 var backslash
= path
.lastIndexOf( '\\' );
104 if ( slash
== -1 && backslash
== -1 ) {
106 } else if ( slash
> backslash
) {
107 fname
= path
.substring( slash
+1, 10000 );
109 fname
= path
.substring( backslash
+1, 10000 );
111 // URLs are less likely to have a useful extension. Don't include them in the extension check.
112 if( wgFileExtensions
&& $j( targetElm
).attr( 'id' ) != 'wpUploadFileURL' ) {
114 if( fname
.lastIndexOf( '.' ) != -1 ) {
115 var ext
= fname
.substr( fname
.lastIndexOf( '.' ) + 1 );
116 for( var i
= 0; i
< wgFileExtensions
.length
; i
++ ) {
117 if( wgFileExtensions
[i
].toLowerCase() == ext
.toLowerCase() )
122 // Clear the upload. Set mw-upload-permitted to error.
123 $j( targetElm
).val( '' );
124 $j( '#mw-upload-permitted,#mw-upload-prohibited' ).show().addClass( 'error' );
125 $j( '#wpDestFile' ).val( '' );
129 // Capitalise first letter and replace spaces by underscores
130 fname
= fname
.charAt( 0 ).toUpperCase().concat( fname
.substring( 1, 10000 ) ).replace( / /g
, '_' );
132 $j( '#wpDestFile' ).val( fname
);
134 // Do a destination check
135 $j( '#wpDestFile' ).doDestCheck({
136 'warn_target': '#wpDestFile-warning'