removed output-disable in dbms-output fetching procedure
[mediawiki.git] / js2 / mwEmbed / libAddMedia / simpleUploadForm.js
blobe3013ad5b71c3f778add99c129c903c732dbc310
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         "mwe-watch-this-file" : "Watch this file",
17         "mwe-ignore-any-warnings" : "Ignore any warnings"
18 } );
20 var default_form_options = {
21         'enable_fogg'    : true,
22         'licence_options':['cc-by-sa'],
23         'api_target' : false,
24         'ondone_cb' : null
27 ( function( $ ) {
28         $.fn.simpleUploadForm = function( opt , callback ) {
29                 var _this = this;
30                 // set the options:
31                 for ( var i in default_form_options ) {
32                         if ( !opt[i] )
33                                 opt[i] = default_form_options[i];
34                 }
36                 // first do a reality check on the options:
37                 if ( !opt.api_target ) {
38                         $( this.selector ).html( 'Error: Missing api target' );
39                         return false;
40                 }
42                 // @@todo this is just a proof of concept
43                 // much todo to improved this web form
44                 get_mw_token( 'File:MyRandomFileTokenCheck', opt.api_target, function( eToken ) {
45                         if ( !eToken || eToken == '+\\' ) {
46                                 $( this.selector ).html( gM( 'mwe-error_not_loggedin' ) );
47                                 return false;
48                         }
50                         // build an upload form:
51                         var o = '<div>' +
52                                                 '<form id="suf-upload" enctype="multipart/form-data" action="' + opt.api_target + '" method="post">'  +
53                                                 // hidden input:
54                                                 '<input type="hidden" name="action" value="upload">' +
55                                                 '<input type="hidden" name="format" value="jsonfm">' +
56                                                 '<input type="hidden" name="token" value="' + eToken + '">' +
57                         
58                                                 // form name set:
59                                                 '<label for="wpUploadFile">' + gM( 'mwe-select_file' ) + '</label><br>' +
60                                                 '<input id="wpUploadFile" type="file" style="display: inline;" name="wpUploadFile" size="15"/><br>' +
61                         
62                                                 '<label for="wpDestFile">' + gM( 'mwe-destfilename' ) + '</label><br>' +
63                                                 '<input id="wpDestFile" type="text" id="wpDestFile" name="wpDestFile" size="30" /><br>' +
64                         
65                                                 '<label for="wpUploadDescription">' + gM( 'mwe-summary' ) + ':</label><br>' +
66                                                 '<textarea id="wpUploadDescription" cols="30" rows="3" name="wpUploadDescription" tabindex="3"/><br>' +
67                                                 
68                                                 '<input type="checkbox" value="true" id="wpWatchthis" name="watch" tabindex="7"/>' +
69                                                 '<label for="wpWatchthis">' + gM( 'mwe-watch-this-file' ) + '</label>' +
70                                                 
71                                                 '<input type="checkbox" value="true" id="wpIgnoreWarning" name="ignorewarnings" tabindex="8"/>' +
72                                                 '<label for="wpIgnoreWarning">' + gM( 'mwe-ignore-any-warnings' ) + '</label></br>' +
73                                                 
74                                                 '<div id="wpDestFile-warning"></div>' +
75                                                 '<div style="clear:both;"></div>' + '<p>' +
76                         
77                                                 gM( 'mwe-select_ownwork' ) + '<br>' +
78                                                 '<input type="checkbox" id="wpLicence" name="wpLicence" value="cc-by-sa">' + gM( 'mwe-licence_cc-by-sa' ) + '</p>' +
79                         
80                                                 '<input type="submit" accesskey="s" value="' + gM( 'mwe-upload' ) + '" name="wpUploadBtn" id="wpUploadBtn"  tabindex="9"/>' +
81                                                 // close the form and div
82                                                 '</form>' +
83                                 '</div>';
85                         // set the target with the form output:
86                         $( _this.selector ).html( o );
87                         // by default dissable:
88                         $j( '#wpUploadBtn' ).attr( 'disabled', 'disabled' );
90                         // set up basic licence binding:
91                         $j( '#wpLicence' ).click( function() {
92                                 if ( $j( this ).is( ':checked' ) ) {
93                                         $j( '#wpUploadBtn' ).removeAttr( 'disabled' );
94                                 } else {
95                                         $j( '#wpUploadBtn' ).attr( 'disabled', 'disabled' );
96                                 }
97                         } );
98                         // do destination fill:
99                         // @@should integrate with doDestinationFill on upload page
100                         $j( "#wpUploadFile" ).change( function() {
101                                 var path = $j( this ).val();
102                                 // Find trailing part
103                                 var slash = path.lastIndexOf( '/' );
104                                 var backslash = path.lastIndexOf( '\\' );
105                                 var fname;
106                                 if ( slash == -1 && backslash == -1 ) {
107                                         fname = path;
108                                 } else if ( slash > backslash ) {
109                                         fname = path.substring( slash + 1, 10000 );
110                                 } else {
111                                         fname = path.substring( backslash + 1, 10000 );
112                                 }
113                                 fname = fname.charAt( 0 ).toUpperCase().concat( fname.substring( 1, 10000 ) ).replace( / /g, '_' );
114                                 // Output result
115                                 $j( "#wpDestFile" ).val( fname );
116                                 // do destination check
117                                 $j( "#wpDestFile" ).doDestCheck( {
118                                         'warn_target':'#wpDestFile-warning'
119                                 } );
120                         } );
123                         // do destination check:
124                         $j( "#wpDestFile" ).change( function() {
125                                 $j( "#wpDestFile" ).doDestCheck( {
126                                         'warn_target':'#wpDestFile-warning'
127                                 } );
128                         } );
130                         if ( typeof opt.ondone_cb == 'undefined' )
131                                 opt.ondone_cb = false;
133                         // set up the binding per the config
134                         if ( opt.enable_fogg ) {
135                                 $j( "#wpUploadFile" ).firefogg( {
136                                         // an api url (we won't submit directly to action of the form)
137                                         'api_url' : opt.api_target,
138                                         'form_rewrite': true,
139                                         'target_edit_from' : '#suf-upload',
140                                         'new_source_cb' : function( orgFilename, oggName ) {
141                                                 $j( "#wpDestFile" ).val( oggName ).doDestCheck( {
142                                                         warn_target: "#wpDestFile-warning"
143                                                 } );
144                                         },
145                                         'done_upload_cb' : opt.ondone_cb
146                                 } );
147                         }
148                 } );
149         }
150 } )( jQuery );