2 * MySlice QueryCode plugin
3 * URL: http://trac.myslice.info
4 * Description: display code for a target query in python or ruby
5 * Author: The MySlice Team
6 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
15 $.fn.QueryCode = function( method ) {
16 /* Method calling logic */
17 if ( methods[method] ) {
18 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
19 } else if ( typeof method === 'object' || ! method ) {
20 return methods.init.apply( this, arguments );
22 $.error( 'Method ' + method + ' does not exist on jQuery.QueryCode' );
27 init : function (options) {
28 if (debug) console.log("SyntaxHighlighter.all ...");
29 SyntaxHighlighter.all();
30 return this.each(function() {
32 var data=$this.data('QueryCode');
34 // Subscribe to query updates
35 var channel='/results/' + options.query_uuid + '/updated';
36 /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
37 $.subscribe(channel, $this, update_plugin);
38 if (debug) console.log('subscribing to ' + channel);
39 $this.data('QueryCode', {options: options});
40 // react to changes to the language selector
41 $this.find(".querycode-lang").change(change_language);
42 // publish so we refresh ourselves
43 $.publish(channel,"please_init_yourself");
50 // destroy : function( ) {
51 // if (debug) console.log("QueryCode.destroy...");
53 // update : function( content ) {
54 // if (debug) console.log("QueryCode.update...");
59 // we retrieve the plugindiv as e.data - cf the 2nd arg to subscribe
60 // in fact we don't really read the published message
61 function update_plugin (e, _) {
62 var $plugindiv=e.data;
63 do_update ($plugindiv);
66 // linked to 'change' on the selector; this=the selector dom
67 function change_language (e) {
68 var $plugindiv = $(this).closest(".plugin");
69 do_update($plugindiv);
72 function do_update ($plugindiv) {
74 var lang=$plugindiv.find(".querycode-lang").val();
75 var dom=$plugindiv.find(".querycode-viz");
76 var query_uuid = $plugindiv.data().QueryCode.options.query_uuid;
77 var query=manifold.find_query(query_uuid);
78 funname="translate_query_as_" + lang;
81 console.log("Cannot find translator function for lang " + lang);
86 if (debug) console.log("SyntaxHighlighter.highlight");
87 SyntaxHighlighter.highlight()
92 function translate_query_as_ruby (query) {
93 var output = '# Connection to XMLRPC server\n';
94 output += 'require "xmlrpc/client"\n';
95 output += 'require "pp"\n';
97 output += 'XMLRPC::Config.module_eval do\n';
98 output += ' remove_const :ENABLE_NIL_PARSER\n';
99 output += ' const_set :ENABLE_NIL_PARSER, true\n';
101 output += 'srv = XMLRPC::Client.new2("' + MANIFOLD_URL + '")\n';
103 output += '# Authentication token\n';
104 output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
108 $.each(query.filters, function(i, value) {
109 if (ifs != '') ifs += ', ';
113 ifs += value[0] + '" => "' + value[2] + '"';
115 ifs = '{' + ifs + '}';
118 $.each(query.fields, function(index, value) {
121 ofs += '"' + value + '"';
123 ofs = '[' + ofs + ']';
125 output += 'pp srv.call("' + mixed_case(query.action) +'", auth, "' + query.subject + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
127 var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
132 function translate_query_as_python (query) {
133 var output = '# Connection to XMLRPC server\n';
134 output += 'import xmlrpclib\n';
135 output += 'srv = xmlrpclib.ServerProxy("' + MANIFOLD_URL + '", allow_none=True)\n\n';
136 output += '# Authentication token\n';
137 output += 'auth = {"AuthMethod": "password", "Username": "name.surname@domain.name", "AuthString": "mypassword"}\n\n';
140 $.each(query.filters, function(i, value) {
144 //if (value[1] != "=")
146 ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
148 ifs = '[' + ifs + ']';
151 $.each(query.fields, function(index, value) {
154 ofs += '"' + value + '"';
156 ofs = '[' + ofs + ']';
158 output += 'srv.' + mixed_case(query.action) + '(auth, "' + query.subject + '", ' + ifs + ', {}, ' + ofs + ')';
159 var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
163 function mixed_case (txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
165 })(jQuery); // end closure wrapper