8495a02f6f429a5e21b9fced1adbd2a6085ae5b7
[lhc/web/wiklou.git] / js2 / mwEmbed / libAddMedia / simpleUploadForm.js
1 /*
2 * simple form output jquery binding
3 * enables dynamic form output to a given target
4 *
5 */
6
7 loadGM({
8 "select_file" : "Select File",
9 "more_licence_options" : "For more licence options view the <a href=\"$1\">normal upload page</a>",
10 "select_ownwork" : "I am uploading entirely my own work, and licencing it under:",
11 "licence_cc-by-sa" : "Creative Commons Share Alike (3.0)",
12 "upload" : "Upload File",
13 "destfilename" : "Destination filename:",
14 "summary" : "Summary",
15 "error_not_loggedin" : "You don't appear to be logged in or don't have upload privlages."
16 });
17
18 var default_form_options = {
19 'enable_fogg' : true,
20 'licence_options':['cc-by-sa'],
21 'api_target' : false,
22 'ondone_cb' : null
23 };
24
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 }
33
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 }
39
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
44 if( !eToken || eToken == '+\\' ){
45 $(this.selector).html( gM('error_not_loggedin') );
46 return false;
47 }
48
49 //build the api based upload form:
50 var o = '<div style="margin: 0 auto;width:450px;">'+
51 '<form id="suf-upload" enctype="multipart/form-data" action="' + opt.api_target + '" method="post">' +
52 //hidden input:
53 '<input type="hidden" name="action" value="upload">'+
54 '<input type="hidden" name="format" value="jsonfm">'+
55 '<input type="hidden" name="token" value="'+ eToken +'">' +
56
57 //api form name set:
58 '<label for="file">' + gM('select_file') + '</label><br>'+
59 '<input type="file" style="display: inline;" name="file" size="15"/><br>' +
60
61 '<label for="filename">' +gM('destfilename') + '</label><br>'+
62 '<input type="text" name="filename" size="30" /><br>'+
63
64 '<label for="comment">' + gM('summary') + ':</label><br>' +
65 '<textarea cols="30" rows="3" name="comment" tabindex="3"/><br>'+
66
67 gM('select_ownwork') + '<br>' +
68 '<input type="checkbox" id="wpLicence" name="wpLicence" value="cc-by-sa">' + gM('licence_cc-by-sa') + '<br>' +
69
70 '<input type="submit" accesskey="s" value="' + gM('upload') + '" name="wpUploadBtn" id="wpUploadBtn" tabindex="9"/>' +
71 //close the form and div
72 '</form></div>';
73
74 //set the target with the form output:
75 $( _this.selector ).html( o );
76 //by default dissable:
77 $j('#wpUploadBtn').attr('disabled', 'disabled');
78
79 //set up basic binding:
80 $j('#wpLicence').click(function(){
81 if( $j(this).is(':checked') ){
82 $j('#wpUploadBtn').removeAttr('disabled');
83 }else{
84 $j('#wpUploadBtn').attr('disabled', 'disabled');
85 }
86 });
87
88 if(typeof opt.ondone_cb == 'undefined')
89 opt.ondone_cb = false;
90
91 //set up the binding per the config
92 if( opt.enable_fogg ){
93 $j('#wpUploadFile').firefogg({
94 //an api url (we won't submit directly to action of the form)
95 'api_url' : opt.api_target,
96 'form_rewrite': true,
97 'target_edit_from' : '#suf-upload',
98 'new_source_cb' : function( orgFilename, oggName ){
99 $j('#wpDestFile').val( oggName );
100 //@@TODO:
101 //mwUploadHelper.doDestCheck();
102 },
103 'done_upload_cb' : opt.ondone_cb
104 });
105 }else{
106 //simple web form rewrite
107 }
108 });
109 }
110 })(jQuery);