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