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