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
11 // . turn back on syntax highlighting
18 $.fn.QueryCode = function( method ) {
19 /* Method calling logic */
20 if ( methods[method] ) {
21 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
22 } else if ( typeof method === 'object' || ! method ) {
23 return methods.init.apply( this, arguments );
25 $.error( 'Method ' + method + ' does not exist on jQuery.QueryCode' );
30 init : function (options) {
31 if (debug) console.log("temporarily turned off SyntaxHighlighter ...");
32 // SyntaxHighlighter.all();
33 return this.each(function() {
35 var data=$this.data('QueryCode');
37 // Subscribe to query updates
38 var channel='/results/' + options.query_uuid + '/updated';
39 /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
40 $.subscribe(channel, $this, update_plugin);
41 if (debug) console.log('subscribing to ' + channel);
42 $this.data('QueryCode', {options: options});
43 // react to changes to the language selector
44 $this.find(".querycode-lang").change(change_language);
45 // publish so we refresh ourselves
46 $.publish(channel,"please_init_yourself");
53 // destroy : function( ) {
54 // if (debug) console.log("QueryCode.destroy...");
56 // update : function( content ) {
57 // if (debug) console.log("QueryCode.update...");
62 // we retrieve the plugindiv as e.data - cf the 2nd arg to subscribe
63 // in fact we don't really read the published message
64 function update_plugin (e, _) {
65 var $plugindiv=e.data;
66 do_update ($plugindiv);
69 // linked to 'change' on the selector; this=the selector dom
70 function change_language (e) {
71 var $plugindiv = $(this).closest(".plugin");
72 do_update($plugindiv);
75 function do_update ($plugindiv) {
77 var lang=$plugindiv.find(".querycode-lang").val();
78 var dom=$plugindiv.find(".querycode-viz");
79 var query_uuid = $plugindiv.data().QueryCode.options.query_uuid;
80 var query=manifold.find_query(query_uuid);
81 funname="translate_query_as_" + lang;
84 console.log("Cannot find translator function for lang " + lang);
89 if (debug) console.log("turned off SyntaxHighlighter.highlight");
90 // SyntaxHighlighter.highlight()
96 function translate_query_as_ruby (query) {
97 var output = '# Connection to XMLRPC server\n';
98 output += 'require "xmlrpc/client"\n';
99 output += 'require "pp"\n';
101 output += 'XMLRPC::Config.module_eval do\n';
102 output += ' remove_const :ENABLE_NIL_PARSER\n';
103 output += ' const_set :ENABLE_NIL_PARSER, true\n';
105 output += 'srv = XMLRPC::Client.new2("' + MANIFOLD_URL + '")\n';
107 output += '# Authentication token\n';
108 output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
112 $.each(query.filters, function(i, value) {
113 if (ifs != '') ifs += ', ';
117 ifs += value[0] + '" => "' + value[2] + '"';
119 ifs = '{' + ifs + '}';
122 $.each(query.fields, function(index, value) {
125 ofs += '"' + value + '"';
127 ofs = '[' + ofs + ']';
129 output += 'pp srv.call("' + mixed_case(query.action) +'", auth, "' + query.subject + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
131 var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
136 function translate_query_as_python (query) {
137 var output = '# Connection to XMLRPC server\n';
138 output += 'import xmlrpclib\n';
139 output += 'srv = xmlrpclib.ServerProxy("' + MANIFOLD_URL + '", allow_none=True)\n\n';
140 output += '# Authentication token\n';
141 output += 'auth = {"AuthMethod": "password", "Username": "name.surname@domain.name", "AuthString": "mypassword"}\n\n';
144 $.each(query.filters, function(i, value) {
148 //if (value[1] != "=")
150 ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
152 ifs = '[' + ifs + ']';
155 $.each(query.fields, function(index, value) {
158 ofs += '"' + value + '"';
160 ofs = '[' + ofs + ']';
162 output += 'srv.' + mixed_case(query.action) + '(auth, "' + query.subject + '", ' + ifs + ', {}, ' + ofs + ')';
163 var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
167 function mixed_case (txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
169 })(jQuery); // end closure wrapper