* updates for strict compliance mode chunk uploading
[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 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 * @@todo we should be able to configure its "targets" via parent config
93 */
94 doDestCheck:function(){
95 var _this = this;
96 $j('#wpDestFile-warning').empty();
97 //show loading
98 $j('#wpDestFile').after('<img id = "mw-spinner-wpDestFile" src ="'+ stylepath + '/common/images/spinner.gif" />');
99 //try and get a thumb of the current file (check its destination)
100 do_api_req({
101 'data':{
102 'titles': 'File:' + $j('#wpDestFile').val(),//@@todo we may need a more clever way to get a the filename
103 'prop': 'imageinfo',
104 'iiprop':'url|mime|size',
105 'iiurlwidth': 150
106 },
107 'url': _this.api_url
108 },function(data){
109 //remove spinner:
110 $j('#mw-spinner-wpDestFile').remove();
111 if(data && data.query && data.query.pages){
112 if( data.query.pages[-1] ){
113 //all good no file there
114 }else{
115 for(var page_id in data.query.pages){
116 if( data.query.normalized){
117 var ntitle = data.query.normalized[0].to;
118 }else{
119 var ntitle = data.query.pages[ page_id ].title;
120 }
121 var img = data.query.pages[ page_id ].imageinfo[0];
122 $j('#wpDestFile-warning').html(
123 '<ul>' +
124 '<li>'+
125 gM('fileexists', ntitle) +
126 '</li>'+
127 '<div class="thumb tright">' +
128 '<div style="width: ' + ( parseInt(img.thumbwidth)+2 ) + 'px;" class="thumbinner">' +
129 '<a title="' + ntitle + '" class="image" href="' + img.descriptionurl + '">' +
130 '<img width="' + img.thumbwidth + '" height="' + img.thumbheight + '" border="0" class="thumbimage" ' +
131 'src="' + img.thumburl + '"' +
132 ' alt="' + ntitle + '"/>' +
133 '</a>' +
134 '<div class="thumbcaption">' +
135 '<div class="magnify">' +
136 '<a title="' + gM('thumbnail-more') + '" class="internal" ' +
137 'href="' + img.descriptionurl +'"><img width="15" height="11" alt="" ' +
138 'src="' + stylepath +"/>" +
139 '</a>'+
140 '</div>'+
141 gM('fileexists-thumb') +
142 '</div>' +
143 '</div>'+
144 '</div>' +
145 '</ul>'
146 );
147 }
148 }
149 }
150 });
151 },
152 /**
153 * doDestinationFill fills in a destination file-name based on a source asset name.
154 * @@todo we should be able to configure its "targets" via parent config
155 */
156 doDestinationFill:function( targetElm ){
157 js_log("doDestinationFill")
158 //remove any previously flagged errors
159 $j('#mw-upload-permitted,#mw-upload-prohibited').hide();
160
161 var path = $j(targetElm).val();
162 // Find trailing part
163 var slash = path.lastIndexOf('/');
164 var backslash = path.lastIndexOf('\\');
165 var fname;
166 if (slash == -1 && backslash == -1) {
167 fname = path;
168 } else if (slash > backslash) {
169 fname = path.substring(slash+1, 10000);
170 } else {
171 fname = path.substring(backslash+1, 10000);
172 }
173 //urls are less likely to have a usefull extension don't include them in the extention check
174 if( wgFileExtensions && $j(targetElm).attr('id') != 'wpUploadFileURL' ){
175 var found = false;
176 if( fname.lastIndexOf('.')!=-1 ){
177 var ext = fname.substr( fname.lastIndexOf('.')+1 );
178 for(var i=0; i < wgFileExtensions.length; i++){
179 if( wgFileExtensions[i].toLowerCase() == ext.toLowerCase() )
180 found = true;
181 }
182 }
183 if(!found){
184 //clear the upload set mw-upload-permitted to error
185 $j(targetElm).val('');
186 $j('#mw-upload-permitted,#mw-upload-prohibited').show().addClass('error');
187 //clear the wpDestFile as well:
188 $j('#wpDestFile').val('');
189 return false;
190 }
191 }
192 // Capitalise first letter and replace spaces by underscores
193 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
194 // Output result
195 $j('#wpDestFile').val( fname );
196
197 //do a destination check
198 this.doDestCheck();
199 }
200 }
201