Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / mwEmbed / libAddMedia / simpleUploadForm.js
blob70325b2fd4c67ce835fca3a0eb004a391d30e52f
1 /*
2  * simple form output jquery binding
3  * enables dynamic form output to a given target
4  *
5  */
7 loadGM({
8         "mwe-select_file" : "Select file",
9         "mwe-more_licence_options" : "For more licence options, view the <a href=\"$1\">normal upload page<\/a>",
10         "mwe-select_ownwork" : "I am uploading entirely my own work, and licencing it under:",
11         "mwe-licence_cc-by-sa" : "Creative Commons Share Alike (3.0)",
12         "mwe-upload" : "Upload file",
13         "mwe-destfilename" : "Destination filename:",
14         "mwe-summary" : "Summary",
15         "mwe-error_not_loggedin" : "You do not appear to be logged in or do not have upload privileges."
16 });
18 var default_form_options = {
19         'enable_fogg'    : true,
20         'licence_options':['cc-by-sa'],
21         'api_target' : false,
22         'ondone_cb' : null
25 (function($) {
26         $.fn.simpleUploadForm = function( opt , callback){
27                 var _this = this;
28                 //set the options:
29                 for(var i in default_form_options){
30                         if(!opt[i])
31                                 opt[i] = default_form_options[i];
32                 }
34                 //first do a reality check on the options:
35                 if( !opt.api_target ){
36                         $(this.selector).html('Error: Missing api target');
37                         return false;
38                 }
40                 //@@todo this is just a proof of concept
41                 //much todo to improved this web form
42                 get_mw_token('File:MyRandomFileTokenCheck', opt.api_target, function(eToken){
43                         if( !eToken || eToken == '+\\' ){
44                                 $(this.selector).html( gM('mwe-error_not_loggedin') );
45                                 return false;
46                         }
48                         //build an upload form:
49                         var o = '<div>'+
50                                                 '<form id="suf-upload" enctype="multipart/form-data" action="' + opt.api_target + '" method="post">'  +
51                                                 //hidden input:
52                                                 '<input type="hidden" name="action" value="upload">'+
53                                                 '<input type="hidden" name="format" value="jsonfm">'+
54                                                 '<input type="hidden" name="token" value="'+ eToken +'">' +
55                         
56                                                 //form name set:
57                                                 '<label for="wpUploadFile">' + gM('mwe-select_file') + '</label><br>'+
58                                                 '<input id="wpUploadFile" type="file" style="display: inline;" name="wpUploadFile" size="15"/><br>' +
59                         
60                                                 '<label for="wpDestFile">' +gM('mwe-destfilename') + '</label><br>'+
61                                                 '<input id="wpDestFile" type="text" id="wpDestFile" name="wpDestFile" size="30" /><br>'+
62                         
63                                                 '<label for="wpUploadDescription">' + gM('mwe-summary') + ':</label><br>' +
64                                                 '<textarea id="wpUploadDescription" cols="30" rows="3" name="wpUploadDescription" tabindex="3"/><br>'+
65                                                 
66                                                 '<input type="checkbox" value="true" id="wpWatchthis" name="watch" tabindex="7"/>'+
67                                                 '<label for="wpWatchthis">Watch this file</label>'+
68                                                 
69                                                 '<input type="checkbox" value="true" id="wpIgnoreWarning" name="ignorewarnings" tabindex="8"/>'+
70                                                 '<label for="wpIgnoreWarning">Ignore any warnings</label></br>'+
71                                                 
72                                                 '<div id="wpDestFile-warning"></div>' +
73                                                 '<div style="clear:both;"></div>' +
74                         
75                                                 gM('mwe-select_ownwork') + '<br>' +
76                                                 '<input type="checkbox" id="wpLicence" name="wpLicence" value="cc-by-sa">' + gM('mwe-licence_cc-by-sa') + '<br>' +
77                         
78                                                 '<input type="submit" accesskey="s" value="' + gM('mwe-upload') + '" name="wpUploadBtn" id="wpUploadBtn"  tabindex="9"/>' +
79                                                 //close the form and div
80                                                 '</form>' +
81                                 '</div>';
83                         //set the target with the form output:
84                         $( _this.selector ).html( o );
85                         //by default dissable:
86                         $j('#wpUploadBtn').attr('disabled', 'disabled');
88                         //set up basic licence binding:
89                         $j('#wpLicence').click(function(){
90                                 if( $j(this).is(':checked') ){
91                                         $j('#wpUploadBtn').removeAttr('disabled');
92                                 }else{
93                                         $j('#wpUploadBtn').attr('disabled', 'disabled');
94                                 }
95                         });
96                         //do destination fill:
97                         //@@should integrate with doDestinationFill on upload page
98                         $j("#wpUploadFile").change(function(){
99                                 var path = $j(this).val();
100                                 // Find trailing part
101                                 var slash = path.lastIndexOf('/');
102                                 var backslash = path.lastIndexOf('\\');
103                                 var fname;
104                                 if (slash == -1 && backslash == -1) {
105                                         fname = path;
106                                 } else if (slash > backslash) {
107                                         fname = path.substring(slash+1, 10000);
108                                 } else {
109                                         fname = path.substring(backslash+1, 10000);
110                                 }
111                                 fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
112                                 // Output result
113                                 $j("#wpDestFile").val( fname );
114                                 //do destination check
115                                 $j("#wpDestFile").doDestCheck({
116                                         'warn_target':'#wpDestFile-warning'
117                                 });
118                         });
121                         //do destination check:
122                         $j("#wpDestFile").change(function(){
123                                 $j("#wpDestFile").doDestCheck({
124                                         'warn_target':'#wpDestFile-warning'
125                                 });
126                         });
128                         if(typeof opt.ondone_cb == 'undefined')
129                                 opt.ondone_cb = false;
131                         //set up the binding per the config
132                         if( opt.enable_fogg ){
133                                 $j("#wpUploadFile").firefogg({
134                                         //an api url (we won't submit directly to action of the form)
135                                         'api_url' : opt.api_target,
136                                         'form_rewrite': true,
137                                         'target_edit_from' : '#suf-upload',
138                                         'new_source_cb' : function( orgFilename, oggName ){
139                                                 $j("#wpDestFile").val( oggName ).doDestCheck({
140                                                         warn_target: "#wpDestFile-warning"
141                                                 });
142                                         },
143                                         'done_upload_cb' : opt.ondone_cb
144                                 });
145                         }
146                 });
147         }
148 })(jQuery);