no spinner on querycode
[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 // . turn back on syntax highlighting
21
22 (function($) {
23   
24     var methods = {
25         init : function (options) {
26             console.log("temporarily turned off SyntaxHighlighter ...");
27 //          SyntaxHighlighter.all();
28             return this.each(function() {
29                 var $this=$(this);
30                 var data=$this.data('QueryCode');
31                 if ( ! data ) {
32                     // Subscribe to query updates
33                     var channel='/results/' + options.query_uuid + '/updated';
34                     /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
35                     $.subscribe(channel, $this, update_plugin);
36                     if (querycode_debug) window.console.log('subscribing to ' + channel);
37                     $this.data('QueryCode', {options: options});
38                     // react to changes to the language selector
39                     $this.find(".querycode-lang").change(change_language);
40                     // publish so we refresh ourselves
41                     $.publish(channel,"please_init_yourself");
42                 }
43             });
44
45
46         }, 
47
48 //      destroy : function( ) {
49 //          if (querycode_debug) console.log("QueryCode.destroy...");
50 //      },
51 //      update : function( content ) { 
52 //          if (querycode_debug) console.log("QueryCode.update...");
53 //      },
54         
55     } // methods
56                           
57     $.fn.QueryCode = function( method ) {
58         /* Method calling logic */
59         if ( methods[method] ) {
60             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
61         } else if ( typeof method === 'object' || ! method ) {
62             return methods.init.apply( this, arguments );
63         } else {
64             $.error( 'Method ' +  method + ' does not exist on jQuery.QueryCode' );
65         }    
66     };
67
68     // we retrieve the plugindiv as e.data - cf the 2nd arg to subscribe
69     function update_plugin (e) {
70         var $plugindiv=e.data;
71         do_update ($plugindiv);
72     }
73
74     // linked to 'change' on the selector; this=the selector dom
75     function change_language (e) {
76         var $plugindiv = $(this).closest(".plugin");
77         do_update($plugindiv);
78     }
79  
80     function do_update ($plugindiv) {
81
82         var lang=$plugindiv.find(".querycode-lang").val();
83         var dom=$plugindiv.find(".querycode-viz");
84         var query = $plugindiv.data().QueryCode.options.query;
85         funname="translate_query_as_" + lang;
86         fun=eval(funname);
87         if ( ! fun) {
88             console.log("Cannot find translator function for lang " + lang);
89             return;
90         }
91         html_code=fun(query);
92         dom.html(html_code);
93         console.log("turned off SyntaxHighlighter.highlight");
94 //      SyntaxHighlighter.highlight()
95
96     }
97
98
99     // private stuff
100     function translate_query_as_ruby (query) {
101         debug_object("query_ruby entering -- query=" + query, query);
102         var output = '# Connection to XMLRPC server\n';
103         output += 'require "xmlrpc/client"\n';
104         output += 'require "pp"\n';
105         output += '\n';
106         output += 'XMLRPC::Config.module_eval do\n';
107         output += '  remove_const :ENABLE_NIL_PARSER\n';
108         output += '  const_set :ENABLE_NIL_PARSER, true\n';
109         output += 'end\n';
110         output += 'srv = XMLRPC::Client.new2("https://www.top-hat.info/API/")\n';
111         //output += 'tophat = xmlrpclib.ServerProxy("' . (TOPHAT_API_PORT == 443 ? 'http' : 'https') . '://' . TOPHAT_API_HOST . ':' . TOPHAT_API_PORT . TOPHAT_API_PATH . '", allow_none=True)\n\n';
112         output += '\n';
113         output += '# Authentication token\n';
114         output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
115         output += '\n';
116
117         ifs = '';
118         $.each(query.filters, function(i, value) {
119             if (ifs != '') ifs += ', ';
120             ifs += '"';
121             if (value[1] != "=")
122                 ifs += value[1];
123             ifs += value[0] + '" => "' + value[2] + '"';
124         });
125         ifs = '{' + ifs + '}';
126         
127         ofs = '';
128         $.each(query.fields, function(index, value) {
129             if (ofs != '')
130                 ofs += ', ';
131             ofs += '"' + value + '"';
132         });
133         ofs = '[' + ofs + ']';
134
135         output += 'pp srv.call("' + query.action +'", auth, "' + query.method + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
136
137         var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
138         return output;
139
140     }
141
142     function translate_query_as_python (query) {
143         // xxx tmp
144         var TOPHAT_API_HOST="hostname", TOPHAT_API_PORT=443, TOPHAT_API_PATH="/path";
145         var proto = (TOPHAT_API_PORT == 443 ? 'https' : 'http');
146         var output = '# Connection to XMLRPC server\n';
147         output += 'import xmlrpclib\n';
148         output += 'srv = xmlrpclib.ServerProxy("' + proto + '://' + TOPHAT_API_HOST + ':' + TOPHAT_API_PORT + TOPHAT_API_PATH + '", allow_none=True)\n\n';
149         output += '# Authentication token\n';
150         output += 'auth = {"AuthMethod": "password", "Username": "name.surname@domain.name", "AuthString": "mypassword"}\n\n';
151
152         ifs = '';
153         $.each(query.filters, function(i, value) {
154             if (ifs != '')
155                 ifs += ', ';
156             //ifs += '"'
157             //if (value[1] != "=")
158             //    ifs += value[1];
159             ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
160         });
161         ifs = '[' + ifs + ']';
162         
163         ofs = '';
164         $.each(query.fields, function(index, value) {
165             if (ofs != '')
166                 ofs += ', ';
167             ofs += '"' + value + '"';
168         });
169         ofs = '[' + ofs + ']';
170
171         output += 'srv.' + query.action + '(auth, "' + query.method + '", ' + ifs + ', {}, ' + ofs + ')';
172         var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
173         return output;
174     }
175     
176 })(jQuery); // end closure wrapper
177