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
10 querycode_debug=false;
13 function debug_object (msg, o) {
15 for (var k in o) keys.push(k);
16 console.log (msg + " Keys : " + keys);
20 // . turn back on syntax highlighting
24 $.fn.QueryCode = function( method ) {
25 /* Method calling logic */
26 if ( methods[method] ) {
27 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
28 } else if ( typeof method === 'object' || ! method ) {
29 return methods.init.apply( this, arguments );
31 $.error( 'Method ' + method + ' does not exist on jQuery.QueryCode' );
36 init : function (options) {
37 console.log("temporarily turned off SyntaxHighlighter ...");
38 // SyntaxHighlighter.all();
39 return this.each(function() {
41 var data=$this.data('QueryCode');
43 // Subscribe to query updates
44 var channel='/results/' + options.query_uuid + '/updated';
45 /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
46 $.subscribe(channel, $this, update_plugin);
47 if (querycode_debug) window.console.log('subscribing to ' + channel);
48 $this.data('QueryCode', {options: options});
49 // react to changes to the language selector
50 $this.find(".querycode-lang").change(change_language);
51 // publish so we refresh ourselves
52 $.publish(channel,"please_init_yourself");
59 // destroy : function( ) {
60 // if (querycode_debug) console.log("QueryCode.destroy...");
62 // update : function( content ) {
63 // if (querycode_debug) console.log("QueryCode.update...");
68 // we retrieve the plugindiv as e.data - cf the 2nd arg to subscribe
69 // in fact we don't really read the published message
70 function update_plugin (e, _) {
71 var $plugindiv=e.data;
72 do_update ($plugindiv);
75 // linked to 'change' on the selector; this=the selector dom
76 function change_language (e) {
77 var $plugindiv = $(this).closest(".plugin");
78 do_update($plugindiv);
81 function do_update ($plugindiv) {
83 var lang=$plugindiv.find(".querycode-lang").val();
84 var dom=$plugindiv.find(".querycode-viz");
85 var query = $plugindiv.data().QueryCode.options.query;
86 funname="translate_query_as_" + lang;
89 console.log("Cannot find translator function for lang " + lang);
94 console.log("turned off SyntaxHighlighter.highlight");
95 // SyntaxHighlighter.highlight()
101 function translate_query_as_ruby (query) {
102 debug_object("query_ruby entering -- query=" + query, query);
103 var output = '# Connection to XMLRPC server\n';
104 output += 'require "xmlrpc/client"\n';
105 output += 'require "pp"\n';
107 output += 'XMLRPC::Config.module_eval do\n';
108 output += ' remove_const :ENABLE_NIL_PARSER\n';
109 output += ' const_set :ENABLE_NIL_PARSER, true\n';
111 output += 'srv = XMLRPC::Client.new2("' + MANIFOLD_URL + '")\n';
113 output += '# Authentication token\n';
114 output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
118 $.each(query.filters, function(i, value) {
119 if (ifs != '') ifs += ', ';
123 ifs += value[0] + '" => "' + value[2] + '"';
125 ifs = '{' + ifs + '}';
128 $.each(query.fields, function(index, value) {
131 ofs += '"' + value + '"';
133 ofs = '[' + ofs + ']';
135 output += 'pp srv.call("' + title_case(query.action) +'", auth, "' + query.method + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
137 var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
142 function translate_query_as_python (query) {
143 var output = '# Connection to XMLRPC server\n';
144 output += 'import xmlrpclib\n';
145 output += 'srv = xmlrpclib.ServerProxy("' + MANIFOLD_URL + '", allow_none=True)\n\n';
146 output += '# Authentication token\n';
147 output += 'auth = {"AuthMethod": "password", "Username": "name.surname@domain.name", "AuthString": "mypassword"}\n\n';
150 $.each(query.filters, function(i, value) {
154 //if (value[1] != "=")
156 ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
158 ifs = '[' + ifs + ']';
161 $.each(query.fields, function(index, value) {
164 ofs += '"' + value + '"';
166 ofs = '[' + ofs + ']';
168 output += 'srv.' + title_case(query.action) + '(auth, "' + query.method + '", ' + ifs + ', {}, ' + ofs + ')';
169 var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
173 function title_case (txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
175 })(jQuery); // end closure wrapper