2 * uploadPage.js to be run on specialUpload page.
3 * controls the invocation of the mvUploader class based on local config.
5 js2AddOnloadHook( function(){
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,
14 //if not boolean false set to true:
15 if(typeof wgEnableFirefogg == 'undefined')
16 wgEnableFirefogg = true;
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',
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();
30 'detect_cb':function( fogg_installed ){
32 _this.firefogg_installed=true;
34 _this.firefogg_installed=false;
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
49 if( wgAjaxUploadDestCheck ){
50 //do destination check:
51 $j('#wpDestFile').change( mwUploadHelper.doDestCheck );
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);
59 $j("input[name='wpSourceType']").click(function(){
60 _this.toggleUpType( this.id == 'wpSourceTypeFile' );
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 );
72 * toggleUpType sets the upload radio buttons
76 toggleUpType:function( set ){
77 $j('#wpSourceTypeFile').attr('checked', set);
78 $j('#wpUploadFile').attr('disabled', !set);
80 $j('#wpSourceTypeURL').attr('checked', !set);
81 $j('#wpUploadFileURL').attr('disabled', set);
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')
91 * doDestCheck checks the destination
93 doDestCheck:function(){
95 $j('#wpDestFile-warning').empty();
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)
101 'titles': 'File:' + $j('#wpDestFile').val(),//@@todo we may need a more clever way to get a the filename
103 'iiprop':'url|mime|size',
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
114 for(var page_id in data.query.pages){
115 if( data.query.normalized){
116 var ntitle = data.query.normalized[0].to;
118 var ntitle = data.query.pages[ page_id ].title;
120 var img = data.query.pages[ page_id ].imageinfo[0];
121 $j('#wpDestFile-warning').html(
124 gM('fileexists', ntitle) +
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 + '"/>' +
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\" />" +
140 gM('fileexists-thumb') +
152 * doDestinationFill fills in a destination file-name based on a source asset name.
154 doDestinationFill:function( targetElm ){
155 js_log("doDestinationFill")
156 //remove any previously flagged errors
157 $j('#mw-upload-permitted,#mw-upload-prohibited').hide();
159 var path = $j(targetElm).val();
160 // Find trailing part
161 var slash = path.lastIndexOf('/');
162 var backslash = path.lastIndexOf('\\');
164 if (slash == -1 && backslash == -1) {
166 } else if (slash > backslash) {
167 fname = path.substring(slash+1, 10000);
169 fname = path.substring(backslash+1, 10000);
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' ){
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() )
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('');
190 // Capitalise first letter and replace spaces by underscores
191 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
193 $j('#wpDestFile').val( fname );
195 //do a destination check