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