Revert functional change from r56594. That broke more than it tried to fix somehow.
[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 js2AddOnloadHook( function() {
6 mwUploadHelper.init();
7 });
8 var mwUploadFormTarget = '#mw-upload-form';
9 // Set up the upload form bindings once all DOM manipulation is done
10 var mwUploadHelper = {
11 init: function() {
12 var _this = this;
13 // If wgEnableFirefogg is not boolean false, set to true
14 if( typeof wgEnableFirefogg == 'undefined' )
15 wgEnableFirefogg = true;
16
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',
22 'form_rewrite': true,
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'
29 });
30 }
31 });
32 } else {
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
39 });
40 }
41 }
42
43 if( wgAjaxUploadDestCheck ) {
44 // Do destination check
45 $j( '#wpDestFile' ).change( function() {
46 $j( '#wpDestFile' ).doDestCheck({
47 'warn_target':'#wpDestFile-warning'
48 });
49 });
50 }
51
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 );
56
57 $j( "input[name='wpSourceType']" ).click( function() {
58 _this.toggleUpType( this.id == 'wpSourceTypeFile' );
59 });
60 }
61 $j( '#wpUploadFile,#wpUploadFileURL' )
62 .focus( function() {
63 _this.toggleUpType( this.id == 'wpUploadFile' );
64 })
65 // Also setup the onChange event binding:
66 .change( function() {
67 if ( wgUploadAutoFill ) {
68 mwUploadHelper.doDestinationFill( this );
69 }
70 });
71 },
72 /**
73 * Set the upload radio buttons
74 *
75 * boolean set
76 */
77 toggleUpType: function( set ) {
78 $j( '#wpSourceTypeFile' ).attr( 'checked', set );
79 $j( '#wpUploadFile' ).attr( 'disabled', !set );
80
81 $j( '#wpSourceTypeURL' ).attr( 'checked', !set );
82 $j( '#wpUploadFileURL' ).attr( 'disabled', set );
83
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' )
88 });
89 }
90 },
91 /**
92 * Fill in a destination file-name based on a source asset name.
93 */
94 doDestinationFill: function( targetElm ) {
95 js_log( "doDestinationFill" )
96 // Remove any previously flagged errors
97 $j( '#mw-upload-permitted,#mw-upload-prohibited' ).hide();
98
99 var path = $j( targetElm ).val();
100 // Find trailing part
101 var slash = path.lastIndexOf( '/' );
102 var backslash = path.lastIndexOf( '\\' );
103 var fname;
104 if ( slash == -1 && backslash == -1 ) {
105 fname = path;
106 } else if ( slash > backslash ) {
107 fname = path.substring( slash+1, 10000 );
108 } else {
109 fname = path.substring( backslash+1, 10000 );
110 }
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' ) {
113 var found = false;
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() )
118 found = true;
119 }
120 }
121 if( !found ) {
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( '' );
126 return false;
127 }
128 }
129 // Capitalise first letter and replace spaces by underscores
130 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) ).replace( / /g, '_' );
131 // Output result
132 $j( '#wpDestFile' ).val( fname );
133
134 // Do a destination check
135 $j( '#wpDestFile' ).doDestCheck({
136 'warn_target': '#wpDestFile-warning'
137 });
138 }
139 }
140