* added in language mapping for commons upload form language hack
[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 mwUploadFormTarget = '#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 'target_edit_from': mwUploadFormTarget,
22 'new_source_cb': function( orgFilename, oggName ) {
23 $j( '#wpDestFile' ).val( oggName );
24 $j( '#wpDestFile' ).doDestCheck( {
25 'warn_target': '#wpDestFile-warning'
26 } );
27 }
28 } );
29 } else {
30 // Add basic upload profile support ( http status monitoring, progress box for
31 // browsers that support it, etc.)
32 if ( $j( '#wpUploadFileURL' ).length != 0 ) {
33 $j( '#wpUploadFileURL' ).baseUploadInterface( {
34 'api_url': wgServer + wgScriptPath + '/api.php',
35 'target_edit_from': mwUploadFormTarget
36 } );
37 }
38 }
39
40 if ( wgAjaxUploadDestCheck ) {
41 // Do destination check
42 $j( '#wpDestFile' ).change( function() {
43 $j( '#wpDestFile' ).doDestCheck( {
44 'warn_target':'#wpDestFile-warning'
45 } );
46 } );
47 }
48
49 // Check if we have HTTP enabled & setup enable/disable toggle:
50 if ( $j( '#wpUploadFileURL' ).length != 0 ) {
51 // Set the initial toggleUpType
52 _this.toggleUpType( true );
53
54 $j( "input[name='wpSourceType']" ).click( function() {
55 _this.toggleUpType( this.id == 'wpSourceTypeFile' );
56 } );
57 }
58 $j( '#wpUploadFile,#wpUploadFileURL' )
59 .focus( function() {
60 _this.toggleUpType( this.id == 'wpUploadFile' );
61 } )
62 // Also setup the onChange event binding:
63 .change( function() {
64 if ( wgUploadAutoFill ) {
65 mwUploadHelper.doDestinationFill( this );
66 }
67 } );
68 },
69 /**
70 * Set the upload radio buttons
71 *
72 * @boolean set
73 */
74 toggleUpType: function( set ) {
75 $j( '#wpSourceTypeFile' ).attr( 'checked', set );
76 $j( '#wpUploadFile' ).attr( 'disabled', !set );
77
78 $j( '#wpSourceTypeURL' ).attr( 'checked', !set );
79 $j( '#wpUploadFileURL' ).attr( 'disabled', set );
80
81 // If Firefogg is enabled, toggle action according to wpSourceTypeFile selection
82 if ( wgEnableFirefogg ) {
83 $j( '#wpUploadFile' ).firefogg( {
84 'firefogg_form_action': $j( '#wpSourceTypeFile' ).attr( 'checked' )
85 } );
86 }
87 },
88 /**
89 * Fill in a destination file-name based on a source asset name.
90 */
91 doDestinationFill: function( targetElm ) {
92 js_log( "doDestinationFill" )
93 // Remove any previously flagged errors
94 $j( '#mw-upload-permitted,#mw-upload-prohibited' ).hide();
95
96 var path = $j( targetElm ).val();
97 // Find trailing part
98 var slash = path.lastIndexOf( '/' );
99 var backslash = path.lastIndexOf( '\\' );
100 var fname;
101 if ( slash == - 1 && backslash == - 1 ) {
102 fname = path;
103 } else if ( slash > backslash ) {
104 fname = path.substring( slash + 1, 10000 );
105 } else {
106 fname = path.substring( backslash + 1, 10000 );
107 }
108 // URLs are less likely to have a useful extension. Don't include them in the extension check.
109 if ( wgFileExtensions && $j( targetElm ).attr( 'id' ) != 'wpUploadFileURL' ) {
110 var found = false;
111 if ( fname.lastIndexOf( '.' ) != - 1 ) {
112 var ext = fname.substr( fname.lastIndexOf( '.' ) + 1 );
113 for ( var i = 0; i < wgFileExtensions.length; i++ ) {
114 if ( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() )
115 found = true;
116 }
117 }
118 if ( !found ) {
119 // Clear the upload. Set mw-upload-permitted to error.
120 $j( targetElm ).val( '' );
121 $j( '#mw-upload-permitted,#mw-upload-prohibited' ).show().addClass( 'error' );
122 $j( '#wpDestFile' ).val( '' );
123 return false;
124 }
125 }
126 // Capitalise first letter and replace spaces by underscores
127 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) ).replace(/ /g, '_' );
128 // Output result
129 $j( '#wpDestFile' ).val( fname );
130
131 // Do a destination check
132 $j( '#wpDestFile' ).doDestCheck( {
133 'warn_target': '#wpDestFile-warning'
134 } );
135 }
136 }
137
138
139 js2AddOnloadHook( function() {
140 mwUploadHelper.init();
141 } );