Whitespace changes
[mediawiki.git] / js2 / uploadPage.js
blob9318b59ba48a9b6ace261dca3fa3560673dcb85b
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                                                 mwUploadHelper.doDestCheck();
29                                 },
30                                 'detect_cb':function( fogg_installed ){
31                                         if(fogg_installed){
32                                                 _this.firefogg_installed=true;
33                                         }else{
34                                                 _this.firefogg_installed=false;
35                                         }
36                                 }
37                         });             
38                         
39                 }else{                  
40                         //Add basic upload profile support ( http status monitoring, progress box for browsers that support it etc.) 
41                         if($j('#wpUploadFileURL').length != 0){
42                                 $j('#wpUploadFileURL').baseUploadInterface({ 
43                                         'api_url'   : wgServer + wgScriptPath + '/api.php',
44                                         'target_edit_from' : mwUploadFormTarget
45                                 });                             
46                         }
47                 }
48                 
49                 if( wgAjaxUploadDestCheck ){
50                         //do destination check:          
51                         $j('#wpDestFile').change( mwUploadHelper.doDestCheck );
52                 }
53                 
54                 //check if we have http enabled & setup enable/disable toggle:
55                 if($j('#wpUploadFileURL').length != 0){                          
56                         //set the initial toggleUpType          
57                         _this.toggleUpType(true);       
58                         
59                         $j("input[name='wpSourceType']").click(function(){                      
60                                 _this.toggleUpType( this.id == 'wpSourceTypeFile' );
61                         });                     
62                 }         
63                 $j('#wpUploadFile,#wpUploadFileURL').focus(function(){          
64                         _this.toggleUpType( this.id == 'wpUploadFile' );        
65                 }).change(function(){ //also setup the onChange event binding:                           
66                         if ( wgUploadAutoFill ) {
67                                 mwUploadHelper.doDestinationFill( this );               
68                         }               
69                 });                        
70         },
71         /**
72          * toggleUpType sets 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 enbaled: toggle action per form select of http upload vs firefogg upload  
84                 if( wgEnableFirefogg ){
85                         $j('#wpUploadFile').firefogg({
86                                         'firefogg_form_action': $j('#wpSourceTypeFile').attr('checked')
87                         });
88                 }
89         },   
90         /**
91          * doDestCheck checks the destination
92          */
93         doDestCheck:function(){         
94                 var _this = this;
95                 $j('#wpDestFile-warning').empty();
96                 //show loading
97                 $j('#wpDestFile').after('<img id = "mw-spinner-wpDestFile" src ="'+ stylepath + '/common/images/spinner.gif" />');
98                 //try and get a thumb of the current file (check its destination)                               
99                 do_api_req({
100                         'data':{ 
101                                 'titles': 'File:' + $j('#wpDestFile').val(),//@@todo we may need a more clever way to get a the filename
102                                 'prop':  'imageinfo',
103                                 'iiprop':'url|mime|size',
104                                 'iiurlwidth': 150 
105                         },
106                         'url': _this.api_url
107                 },function(data){
108                         //remove spinner:
109                         $j('#mw-spinner-wpDestFile').remove();                  
110                         if(data && data.query && data.query.pages){
111                                 if( data.query.pages[-1] ){
112                                         //all good no file there
113                                 }else{
114                                         for(var page_id in data.query.pages){
115                                                 if( data.query.normalized){
116                                                         var ntitle = data.query.normalized[0].to;
117                                                 }else{
118                                                         var ntitle = data.query.pages[ page_id ].title;
119                                                 }       
120                                                 var img = data.query.pages[ page_id ].imageinfo[0];                                                             
121                                                 $j('#wpDestFile-warning').html(
122                                                         '<ul>' +
123                                                                 '<li>'+
124                                                                         gM('fileexists', ntitle) + 
125                                                                 '</li>'+
126                                                                 '<div class="thumb tright">' +
127                                                                         '<div style="width: ' + ( parseInt(img.thumbwidth)+2 ) + 'px;" class="thumbinner">' +
128                                                                                 '<a title="' + ntitle + '" class="image" href="' + img.descriptionurl + '">' +
129                                                                                         '<img width="' + img.thumbwidth + '" height="' + img.thumbheight + '" border="0" class="thumbimage" ' +
130                                                                                         'src="' + img.thumburl + '"' +
131                                                                                         '        alt="' + ntitle + '"/>' +
132                                                                                 '</a>' +
133                                                                                 '<div class="thumbcaption">' +
134                                                                                         '<div class="magnify">' +
135                                                                                                 '<a title="' + gM('thumbnail-more') + '" class="internal" ' +
136                                                                                                         'href="' + img.descriptionurl +'"><img width="15" height="11" alt="" ' +
137                                                                                                         'src="' + stylepath + "/common/images/magnify-clip.png\" />" +
138                                                                                                 '</a>'+
139                                                                                         '</div>'+
140                                                                                         gM('fileexists-thumb') +
141                                                                                 '</div>' +
142                                                                         '</div>'+
143                                                                 '</div>' +
144                                                         '</ul>'
145                                                 );
146                                         }
147                                 }
148                         }
149                 });                     
150         },
151         /**
152          * doDestinationFill fills in a destination file-name based on a source asset name. 
153          */
154         doDestinationFill:function( targetElm ){
155                 js_log("doDestinationFill")
156                 //remove any previously flagged errors
157                 $j('#mw-upload-permitted,#mw-upload-prohibited').hide();                                        
158                 
159                 var path = $j(targetElm).val();
160                 // Find trailing part
161                 var slash = path.lastIndexOf('/');
162                 var backslash = path.lastIndexOf('\\');
163                 var fname;
164                 if (slash == -1 && backslash == -1) {
165                         fname = path;
166                 } else if (slash > backslash) {
167                         fname = path.substring(slash+1, 10000);
168                 } else {
169                         fname = path.substring(backslash+1, 10000);
170                 }               
171                 //urls are less likely to have a usefull extension don't include them in the extention check
172                 if( wgFileExtensions && $j(targetElm).attr('id') != 'wpUploadFileURL' ){                
173                         var found = false;              
174                         if( fname.lastIndexOf('.')!=-1 ){               
175                                 var ext = fname.substr( fname.lastIndexOf('.')+1 );                     
176                                 for(var i=0; i < wgFileExtensions.length; i++){                                         
177                                         if(  wgFileExtensions[i].toLowerCase()   ==  ext.toLowerCase() )
178                                                 found = true;
179                                 }
180                         }
181                         if(!found){
182                                 //clear the upload set mw-upload-permitted to error
183                                 $j(targetElm).val('');
184                                 $j('#mw-upload-permitted,#mw-upload-prohibited').show().addClass('error');                                                                                              
185                                 //clear the wpDestFile as well: 
186                                 $j('#wpDestFile').val('');                                                              
187                                 return false;
188                         }               
189                 }                               
190                 // Capitalise first letter and replace spaces by underscores
191                 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');      
192                 // Output result
193                 $j('#wpDestFile').val( fname );
194                                 
195                 //do a destination check 
196                 this.doDestCheck();
197         }
199