b4165cb1b02bb14736e7e59aa473dcd5d623ee64
[unfold.git] / plugins / static / js / querycode.js
1 /**
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
7  * License: GPLv3
8  */
9
10 querycode_debug=false;
11 querycode_debug=true;
12
13 function debug_object (msg, o) {
14     var keys=[];
15     for (var k in o) keys.push(k);
16     console.log (msg + " Keys : " + keys);
17 }
18
19 // xxx TODO
20 // . the spinner is still turning after the first refresh - find a means to shut it down completely
21 // . turn back on syntax highlighting
22
23 (function($) {
24   
25     var methods = {
26         init : function (options) {
27             console.log("temporarily turned off SyntaxHighlighter ...");
28 //          SyntaxHighlighter.all();
29             return this.each(function() {
30                 var $this=$(this);
31                 var data=$this.data('QueryCode');
32                 if ( ! data ) {
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");
43                 }
44             });
45
46
47         }, 
48
49 //      destroy : function( ) {
50 //          if (querycode_debug) console.log("QueryCode.destroy...");
51 //      },
52 //      update : function( content ) { 
53 //          if (querycode_debug) console.log("QueryCode.update...");
54 //      },
55         
56     } // methods
57                           
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 );
64         } else {
65             $.error( 'Method ' +  method + ' does not exist on jQuery.QueryCode' );
66         }    
67     };
68
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);
73     }
74
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);
79     }
80  
81     function do_update ($plugindiv) {
82         // just in case
83         $plugindiv.closest('.need-spin').spin(false);
84
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;
89         fun=eval(funname);
90         if ( ! fun) {
91             console.log("Cannot find translator function for lang " + lang);
92             return;
93         }
94         html_code=fun(query);
95         dom.html(html_code);
96         console.log("turned off SyntaxHighlighter.highlight");
97 //      SyntaxHighlighter.highlight()
98
99     }
100
101
102     // private stuff
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';
108         output += '\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';
112         output += 'end\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';
115         output += '\n';
116         output += '# Authentication token\n';
117         output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
118         output += '\n';
119
120         ifs = '';
121         $.each(query.filters, function(i, value) {
122             if (ifs != '') ifs += ', ';
123             ifs += '"';
124             if (value[1] != "=")
125                 ifs += value[1];
126             ifs += value[0] + '" => "' + value[2] + '"';
127         });
128         ifs = '{' + ifs + '}';
129         
130         ofs = '';
131         $.each(query.fields, function(index, value) {
132             if (ofs != '')
133                 ofs += ', ';
134             ofs += '"' + value + '"';
135         });
136         ofs = '[' + ofs + ']';
137
138         output += 'pp srv.call("' + query.action +'", auth, "' + query.method + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
139
140         var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
141         return output;
142
143     }
144
145     function translate_query_as_python (query) {
146         // xxx tmp
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';
154
155         ifs = '';
156         $.each(query.filters, function(i, value) {
157             if (ifs != '')
158                 ifs += ', ';
159             //ifs += '"'
160             //if (value[1] != "=")
161             //    ifs += value[1];
162             ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
163         });
164         ifs = '[' + ifs + ']';
165         
166         ofs = '';
167         $.each(query.fields, function(index, value) {
168             if (ofs != '')
169                 ofs += ', ';
170             ofs += '"' + value + '"';
171         });
172         ofs = '[' + ofs + ']';
173
174         output += 'srv.' + query.action + '(auth, "' + query.method + '", ' + ifs + ', {}, ' + ofs + ')';
175         var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
176         return output;
177     }
178     
179 })(jQuery); // end closure wrapper
180