unfold: better management of static files thanks to finders for plugins and third...
[myslice.git] / plugins / querycode / static / js / querycode.js
1 /**
2  * Description: display code for a target query in python or ruby
3  * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */
6
7 (function($) {
8   
9     var debug=false;
10     //debug=true;
11
12     $.fn.QueryCode = function( method ) {
13         /* Method calling logic */
14         if ( methods[method] ) {
15             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
16         } else if ( typeof method === 'object' || ! method ) {
17             return methods.init.apply( this, arguments );
18         } else {
19             $.error( 'Method ' +  method + ' does not exist on jQuery.QueryCode' );
20         }    
21     };
22
23     var methods = {
24         init : function (options) {
25             if (debug) messages.debug("SyntaxHighlighter.all ...");
26             SyntaxHighlighter.all();
27             return this.each(function() {
28                 var $this=$(this);
29                 var data=$this.data('QueryCode');
30                 if ( ! data ) {
31                     // Subscribe to query updates
32                     var channel='/results/' + options.query_uuid + '/updated';
33                     /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
34                     $.subscribe(channel, $this, update_plugin);
35                     if (debug) messages.debug('subscribing to ' + channel);
36                     $this.data('QueryCode', {options: options});
37                     // react to changes to the language selector
38                     $this.find(".querycode-lang").change(change_language);
39                     // publish so we refresh ourselves
40                     $.publish(channel,"please_init_yourself");
41                 }
42             });
43
44
45         }, 
46
47 //      destroy : function( ) {
48 //          if (debug) messages.debug("QueryCode.destroy...");
49 //      },
50 //      update : function( content ) { 
51 //          if (debug) messages.debug("QueryCode.update...");
52 //      },
53         
54     } // methods
55                           
56     // we retrieve the plugindiv as e.data - cf the 2nd arg to subscribe
57     // in fact we don't really read the published message
58     function update_plugin (e, _) {
59         var $plugindiv=e.data;
60         do_update ($plugindiv);
61     }
62
63     // linked to 'change' on the selector; this=the selector dom
64     function change_language (e) {
65         var $plugindiv = $(this).closest(".plugin");
66         do_update($plugindiv);
67     }
68  
69     function do_update ($plugindiv) {
70
71         var lang=$plugindiv.find(".querycode-lang").val();
72         var dom=$plugindiv.find(".querycode-viz");
73         var query_uuid = $plugindiv.data().QueryCode.options.query_uuid;
74         var query=manifold.find_query(query_uuid);
75         funname="translate_query_as_" + lang;
76         fun=eval(funname);
77         if (! fun) {
78             messages.debug("Cannot find translator function for lang " + lang);
79             return;
80         }
81         html_code=fun(query);
82         dom.html(html_code);
83         if (debug) messages.debug("SyntaxHighlighter.highlight");
84         SyntaxHighlighter.highlight()
85     }
86
87
88     // private stuff
89     function translate_query_as_ruby (query) {
90         var output = '# Connection to XMLRPC server\n';
91         output += 'require "xmlrpc/client"\n';
92         output += 'require "pp"\n';
93         output += '\n';
94         output += 'XMLRPC::Config.module_eval do\n';
95         output += '  remove_const :ENABLE_NIL_PARSER\n';
96         output += '  const_set :ENABLE_NIL_PARSER, true\n';
97         output += 'end\n';
98         output += 'srv = XMLRPC::Client.new2("' + MANIFOLD_URL + '")\n';
99         output += '\n';
100         output += '# Authentication token\n';
101         output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
102         output += '\n';
103
104         ifs = '';
105         $.each(query.filters, function(i, value) {
106             if (ifs != '') ifs += ', ';
107             ifs += '"';
108             if (value[1] != "=")
109                 ifs += value[1];
110             ifs += value[0] + '" => "' + value[2] + '"';
111         });
112         ifs = '{' + ifs + '}';
113         
114         ofs = '';
115         $.each(query.fields, function(index, value) {
116             if (ofs != '')
117                 ofs += ', ';
118             ofs += '"' + value + '"';
119         });
120         ofs = '[' + ofs + ']';
121
122         output += 'pp srv.call("' + mixed_case(query.action) +'", auth, "' + query.object + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
123
124         var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
125         return output;
126
127     }
128
129     function translate_query_as_python (query) {
130         var output = '# Connection to XMLRPC server\n';
131         output += 'import xmlrpclib\n';
132         output += 'srv = xmlrpclib.ServerProxy("' + MANIFOLD_URL + '", allow_none=True)\n\n';
133         output += '# Authentication token\n';
134         output += 'auth = {"AuthMethod": "password", "Username": "name.surname@domain.name", "AuthString": "mypassword"}\n\n';
135
136         ifs = '';
137         $.each(query.filters, function(i, value) {
138             if (ifs != '')
139                 ifs += ', ';
140             //ifs += '"'
141             //if (value[1] != "=")
142             //    ifs += value[1];
143             ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
144         });
145         ifs = '[' + ifs + ']';
146         
147         ofs = '';
148         $.each(query.fields, function(index, value) {
149             if (ofs != '')
150                 ofs += ', ';
151             ofs += '"' + value + '"';
152         });
153         ofs = '[' + ofs + ']';
154
155         output += 'srv.' + mixed_case(query.action) + '(auth, "' + query.object + '", ' + ifs + ', {}, ' + ofs + ')';
156         var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
157         return output;
158     }
159
160     function mixed_case (txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
161     
162 })(jQuery); // end closure wrapper
163