e8719c3a9d5db49440dd518d4d52b58df3fdd24d
[unfold.git] / plugins / querycode / 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 // xxx TODO
11 // . turn back on syntax highlighting
12
13 (function($) {
14   
15     var debug=false;
16     //debug=true;
17
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 );
24         } else {
25             $.error( 'Method ' +  method + ' does not exist on jQuery.QueryCode' );
26         }    
27     };
28
29     var methods = {
30         init : function (options) {
31             console.log("temporarily turned off SyntaxHighlighter ...");
32 //          SyntaxHighlighter.all();
33             return this.each(function() {
34                 var $this=$(this);
35                 var data=$this.data('QueryCode');
36                 if ( ! data ) {
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");
47                 }
48             });
49
50
51         }, 
52
53 //      destroy : function( ) {
54 //          if (debug) console.log("QueryCode.destroy...");
55 //      },
56 //      update : function( content ) { 
57 //          if (debug) console.log("QueryCode.update...");
58 //      },
59         
60     } // methods
61                           
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);
67     }
68
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);
73     }
74  
75     function do_update ($plugindiv) {
76
77         var lang=$plugindiv.find(".querycode-lang").val();
78         var dom=$plugindiv.find(".querycode-viz");
79         var query = $plugindiv.data().QueryCode.options.query;
80         funname="translate_query_as_" + lang;
81         fun=eval(funname);
82         if ( ! fun) {
83             console.log("Cannot find translator function for lang " + lang);
84             return;
85         }
86         html_code=fun(query);
87         dom.html(html_code);
88         console.log("turned off SyntaxHighlighter.highlight");
89 //      SyntaxHighlighter.highlight()
90
91     }
92
93
94     // private stuff
95     function translate_query_as_ruby (query) {
96         var output = '# Connection to XMLRPC server\n';
97         output += 'require "xmlrpc/client"\n';
98         output += 'require "pp"\n';
99         output += '\n';
100         output += 'XMLRPC::Config.module_eval do\n';
101         output += '  remove_const :ENABLE_NIL_PARSER\n';
102         output += '  const_set :ENABLE_NIL_PARSER, true\n';
103         output += 'end\n';
104         output += 'srv = XMLRPC::Client.new2("' + MANIFOLD_URL + '")\n';
105         output += '\n';
106         output += '# Authentication token\n';
107         output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
108         output += '\n';
109
110         ifs = '';
111         $.each(query.filters, function(i, value) {
112             if (ifs != '') ifs += ', ';
113             ifs += '"';
114             if (value[1] != "=")
115                 ifs += value[1];
116             ifs += value[0] + '" => "' + value[2] + '"';
117         });
118         ifs = '{' + ifs + '}';
119         
120         ofs = '';
121         $.each(query.fields, function(index, value) {
122             if (ofs != '')
123                 ofs += ', ';
124             ofs += '"' + value + '"';
125         });
126         ofs = '[' + ofs + ']';
127
128         output += 'pp srv.call("' + mixed_case(query.action) +'", auth, "' + query.method + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
129
130         var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
131         return output;
132
133     }
134
135     function translate_query_as_python (query) {
136         var output = '# Connection to XMLRPC server\n';
137         output += 'import xmlrpclib\n';
138         output += 'srv = xmlrpclib.ServerProxy("' + MANIFOLD_URL + '", allow_none=True)\n\n';
139         output += '# Authentication token\n';
140         output += 'auth = {"AuthMethod": "password", "Username": "name.surname@domain.name", "AuthString": "mypassword"}\n\n';
141
142         ifs = '';
143         $.each(query.filters, function(i, value) {
144             if (ifs != '')
145                 ifs += ', ';
146             //ifs += '"'
147             //if (value[1] != "=")
148             //    ifs += value[1];
149             ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
150         });
151         ifs = '[' + ifs + ']';
152         
153         ofs = '';
154         $.each(query.fields, function(index, value) {
155             if (ofs != '')
156                 ofs += ', ';
157             ofs += '"' + value + '"';
158         });
159         ofs = '[' + ofs + ']';
160
161         output += 'srv.' + mixed_case(query.action) + '(auth, "' + query.method + '", ' + ifs + ', {}, ' + ofs + ')';
162         var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
163         return output;
164     }
165
166     function mixed_case (txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
167     
168 })(jQuery); // end closure wrapper
169