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 // . the spinner is still turning after the first refresh - find a means to shut it down completely
21 // . turn back on syntax highlighting
26 init : function (options) {
27 console.log("temporarily turned off SyntaxHighlighter ...");
28 // SyntaxHighlighter.all();
29 return this.each(function() {
31 var data=$this.data('QueryCode');
33 // Subscribe to query updates
34 var channel='/results/' + options.query_uuid + '/updated';
35 /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
36 $.subscribe(channel, $this, update_plugin);
37 if (querycode_debug) window.console.log('subscribing to ' + channel);
38 $this.data('QueryCode', {options: options});
39 // react to changes to the language selector
40 $this.find(".querycode-lang").change(change_language);
41 // publish so we refresh ourselves
42 $.publish(channel,"please_init_yourself");
49 // destroy : function( ) {
50 // if (querycode_debug) console.log("QueryCode.destroy...");
52 // update : function( content ) {
53 // if (querycode_debug) console.log("QueryCode.update...");
58 $.fn.QueryCode = function( method ) {
59 /* Method calling logic */
60 if ( methods[method] ) {
61 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
62 } else if ( typeof method === 'object' || ! method ) {
63 return methods.init.apply( this, arguments );
65 $.error( 'Method ' + method + ' does not exist on jQuery.QueryCode' );
69 // we retrieve the plugindiv as e.data - cf the 2nd arg to subscribe
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 $plugindiv.closest('.need-spin').spin(false);
85 var lang=$plugindiv.find(".querycode-lang").val();
86 var dom=$plugindiv.find(".querycode-viz");
87 var query = $plugindiv.data().QueryCode.options.query;
88 funname="translate_query_as_" + lang;
91 console.log("Cannot find translator function for lang " + lang);
96 console.log("turned off SyntaxHighlighter.highlight");
97 // SyntaxHighlighter.highlight()
103 function translate_query_as_ruby (query) {
104 debug_object("query_ruby entering -- query=" + query, query);
105 var output = '# Connection to XMLRPC server\n';
106 output += 'require "xmlrpc/client"\n';
107 output += 'require "pp"\n';
109 output += 'XMLRPC::Config.module_eval do\n';
110 output += ' remove_const :ENABLE_NIL_PARSER\n';
111 output += ' const_set :ENABLE_NIL_PARSER, true\n';
113 output += 'srv = XMLRPC::Client.new2("https://www.top-hat.info/API/")\n';
114 //output += 'tophat = xmlrpclib.ServerProxy("' . (TOPHAT_API_PORT == 443 ? 'http' : 'https') . '://' . TOPHAT_API_HOST . ':' . TOPHAT_API_PORT . TOPHAT_API_PATH . '", allow_none=True)\n\n';
116 output += '# Authentication token\n';
117 output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
121 $.each(query.filters, function(i, value) {
122 if (ifs != '') ifs += ', ';
126 ifs += value[0] + '" => "' + value[2] + '"';
128 ifs = '{' + ifs + '}';
131 $.each(query.fields, function(index, value) {
134 ofs += '"' + value + '"';
136 ofs = '[' + ofs + ']';
138 output += 'pp srv.call("' + query.action +'", auth, "' + query.method + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
140 var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
145 function translate_query_as_python (query) {
147 var TOPHAT_API_HOST="hostname", TOPHAT_API_PORT=443, TOPHAT_API_PATH="/path";
148 var proto = (TOPHAT_API_PORT == 443 ? 'https' : 'http');
149 var output = '# Connection to XMLRPC server\n';
150 output += 'import xmlrpclib\n';
151 output += 'srv = xmlrpclib.ServerProxy("' + proto + '://' + TOPHAT_API_HOST + ':' + TOPHAT_API_PORT + TOPHAT_API_PATH + '", allow_none=True)\n\n';
152 output += '# Authentication token\n';
153 output += 'auth = {"AuthMethod": "password", "Username": "name.surname@domain.name", "AuthString": "mypassword"}\n\n';
156 $.each(query.filters, function(i, value) {
160 //if (value[1] != "=")
162 ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
164 ifs = '[' + ifs + ']';
167 $.each(query.fields, function(index, value) {
170 ofs += '"' + value + '"';
172 ofs = '[' + ofs + ']';
174 output += 'srv.' + query.action + '(auth, "' + query.method + '", ' + ifs + ', {}, ' + ofs + ')';
175 var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
179 })(jQuery); // end closure wrapper