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