Split allowing &suggest requests in API opensearch from $wgEnableMWSuggest to a separ...
[mediawiki.git] / js2 / uploadPage.js
bloba41b2ddc34cff4a7c034aa9ce4e199b8d1c449fb
1 /*
2  * This script is run on [[Special:Upload]].
3  * It controls the invocation of the mvUploader class based on local config.
4  */
6 js2AddOnloadHook( function() {
7         js_log("never ran js2hook");
8         mwUploadHelper.init();
9 });
11 var mwUploadFormTarget = '#mw-upload-form';
12 // Set up the upload form bindings once all DOM manipulation is done
13 var mwUploadHelper = {
14         init: function() {
15                 var _this = this;
16                 // If wgEnableFirefogg is not boolean false, set to true
17                 if( typeof wgEnableFirefogg == 'undefined' )
18                 wgEnableFirefogg = true;
20                 if( wgEnableFirefogg ) {
21                         // Set up the upload handler to Firefogg. Should work with the HTTP uploads too.
22                         $j( '#wpUploadFile' ).firefogg( {
23                                 // An API URL (we won't submit directly to action of the form)
24                                 'api_url': wgServer + wgScriptPath + '/api.php',
25                                 'form_rewrite': true,
26                                 'target_edit_from': mwUploadFormTarget,
27                                 'new_source_cb': function( orgFilename, oggName ) {                                     
28                                         $j( '#wpDestFile' ).val( oggName );
29                                         $j( '#wpDestFile' ).doDestCheck({
30                                                 'warn_target': '#wpDestFile-warning'
31                                         } );
32                                 }
33                         } );
34                 } else {
35                         // Add basic upload profile support ( http status monitoring, progress box for
36                         // browsers that support it, etc.)
37                         if( $j( '#wpUploadFileURL' ).length != 0 ) {
38                                 $j( '#wpUploadFileURL' ).baseUploadInterface( {
39                                         'api_url': wgServer + wgScriptPath + '/api.php',
40                                         'target_edit_from': mwUploadFormTarget
41                                 } );
42                         }
43                 }
45                 if( wgAjaxUploadDestCheck ) {
46                         // Do destination check
47                         $j( '#wpDestFile' ).change( function() {
48                                 $j( '#wpDestFile' ).doDestCheck({
49                                         'warn_target':'#wpDestFile-warning'
50                                 } );
51                         } );
52                 }
54                 // Check if we have HTTP enabled & setup enable/disable toggle:
55                 if( $j( '#wpUploadFileURL' ).length != 0 ) {
56                         // Set the initial toggleUpType
57                         _this.toggleUpType( true );
59                         $j( "input[name='wpSourceType']" ).click( function() {
60                                 _this.toggleUpType( this.id == 'wpSourceTypeFile' );
61                         } );
62                 }
63                 $j( '#wpUploadFile,#wpUploadFileURL' )
64                 .focus( function() {
65                         _this.toggleUpType( this.id == 'wpUploadFile' );
66                 })
67                 // Also setup the onChange event binding:
68                 .change( function() {
69                         if ( wgUploadAutoFill ) {
70                                 mwUploadHelper.doDestinationFill( this );
71                         }
72                 } );
73         },
74         /**
75         * Set the upload radio buttons
76         *
77         * boolean set
78         */
79         toggleUpType: function( set ) {
80                 $j( '#wpSourceTypeFile' ).attr( 'checked', set );
81                 $j( '#wpUploadFile' ).attr( 'disabled', !set );
83                 $j( '#wpSourceTypeURL' ).attr( 'checked', !set );
84                 $j( '#wpUploadFileURL' ).attr( 'disabled', set );
86                 // If Firefogg is enabled, toggle action according to wpSourceTypeFile selection
87                 if( wgEnableFirefogg ) {                        
88                         $j( '#wpUploadFile' ).firefogg({
89                                 'firefogg_form_action': $j( '#wpSourceTypeFile' ).attr( 'checked' )
90                         } );
91                 }
92         },
93         /**
94         * Fill in a destination file-name based on a source asset name.
95         */
96         doDestinationFill: function( targetElm ) {
97                 js_log( "doDestinationFill" )
98                 // Remove any previously flagged errors
99                 $j( '#mw-upload-permitted,#mw-upload-prohibited' ).hide();
101                 var path = $j( targetElm ).val();
102                 // Find trailing part
103                 var slash = path.lastIndexOf( '/' );
104                 var backslash = path.lastIndexOf( '\\' );
105                 var fname;
106                 if ( slash == -1 && backslash == -1 ) {
107                         fname = path;
108                 } else if ( slash > backslash ) {
109                         fname = path.substring( slash+1, 10000 );
110                 } else {
111                         fname = path.substring( backslash+1, 10000 );
112                 }
113                 // URLs are less likely to have a useful extension. Don't include them in the extension check.
114                 if( wgFileExtensions && $j( targetElm ).attr( 'id' ) != 'wpUploadFileURL' ) {
115                         var found = false;
116                         if( fname.lastIndexOf( '.' ) != -1 ) {
117                                 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
118                                 for( var i = 0; i < wgFileExtensions.length; i++ ) {
119                                         if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() )
120                                         found = true;
121                                 }
122                         }
123                         if( !found ) {
124                                 // Clear the upload. Set mw-upload-permitted to error.
125                                 $j( targetElm ).val( '' );
126                                 $j( '#mw-upload-permitted,#mw-upload-prohibited' ).show().addClass( 'error' );
127                                 $j( '#wpDestFile' ).val( '' );
128                                 return false;
129                         }
130                 }
131                 // Capitalise first letter and replace spaces by underscores
132                 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) ).replace( / /g, '_' );
133                 // Output result
134                 $j( '#wpDestFile' ).val( fname );
136                 // Do a destination check
137                 $j( '#wpDestFile' ).doDestCheck({
138                         'warn_target': '#wpDestFile-warning'
139                 } );
140         }