2 * Description: display code for a target query in python or ruby
3 * Copyright (c) 2012 UPMC Sorbonne Universite - INRIA
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 );
19 $.error( 'Method ' + method + ' does not exist on jQuery.QueryCode' );
24 init : function (options) {
25 if (debug) messages.debug("SyntaxHighlighter.all ...");
26 SyntaxHighlighter.all();
27 return this.each(function() {
29 var data=$this.data('QueryCode');
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");
47 // destroy : function( ) {
48 // if (debug) messages.debug("QueryCode.destroy...");
50 // update : function( content ) {
51 // if (debug) messages.debug("QueryCode.update...");
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);
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);
69 function do_update ($plugindiv) {
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;
78 messages.debug("Cannot find translator function for lang " + lang);
83 if (debug) messages.debug("SyntaxHighlighter.highlight");
84 SyntaxHighlighter.highlight()
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';
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';
98 output += 'srv = XMLRPC::Client.new2("' + MANIFOLD_URL + '")\n';
100 output += '# Authentication token\n';
101 output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
105 $.each(query.filters, function(i, value) {
106 if (ifs != '') ifs += ', ';
110 ifs += value[0] + '" => "' + value[2] + '"';
112 ifs = '{' + ifs + '}';
115 $.each(query.fields, function(index, value) {
118 ofs += '"' + value + '"';
120 ofs = '[' + ofs + ']';
122 output += 'pp srv.call("' + mixed_case(query.action) +'", auth, "' + query.object + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
124 var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
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';
137 $.each(query.filters, function(i, value) {
141 //if (value[1] != "=")
143 ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
145 ifs = '[' + ifs + ']';
148 $.each(query.fields, function(index, value) {
151 ofs += '"' + value + '"';
153 ofs = '[' + ofs + ']';
155 output += 'srv.' + mixed_case(query.action) + '(auth, "' + query.object + '", ' + ifs + ', {}, ' + ofs + ')';
156 var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
160 function mixed_case (txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
162 })(jQuery); // end closure wrapper