3574b30e9a7bb8672ccb763118e5c6bf94bf9fe0
[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_uuid = $plugindiv.data().QueryCode.options.query_uuid;
80         var query=manifold.find_query(query_uuid);
81         funname="translate_query_as_" + lang;
82         fun=eval(funname);
83         if (! fun) {
84             console.log("Cannot find translator function for lang " + lang);
85             return;
86         }
87         html_code=fun(query);
88         dom.html(html_code);
89         console.log("turned off SyntaxHighlighter.highlight");
90 //      SyntaxHighlighter.highlight()
91
92     }
93
94
95     // private stuff
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';
100         output += '\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';
104         output += 'end\n';
105         output += 'srv = XMLRPC::Client.new2("' + MANIFOLD_URL + '")\n';
106         output += '\n';
107         output += '# Authentication token\n';
108         output += 'auth = {"AuthMethod" => "password", "Username" => "guest", "AuthString" => "guest"}\n';
109         output += '\n';
110
111         ifs = '';
112         $.each(query.filters, function(i, value) {
113             if (ifs != '') ifs += ', ';
114             ifs += '"';
115             if (value[1] != "=")
116                 ifs += value[1];
117             ifs += value[0] + '" => "' + value[2] + '"';
118         });
119         ifs = '{' + ifs + '}';
120         
121         ofs = '';
122         $.each(query.fields, function(index, value) {
123             if (ofs != '')
124                 ofs += ', ';
125             ofs += '"' + value + '"';
126         });
127         ofs = '[' + ofs + ']';
128
129         output += 'pp srv.call("' + mixed_case(query.action) +'", auth, "' + query.subject + '", "' + query.timestamp + '", ' + ifs + ', ' + ofs + ')';
130
131         var output = '<pre class="brush: ruby; toolbar: false;">' + output + "</pre>";
132         return output;
133
134     }
135
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';
142
143         ifs = '';
144         $.each(query.filters, function(i, value) {
145             if (ifs != '')
146                 ifs += ', ';
147             //ifs += '"'
148             //if (value[1] != "=")
149             //    ifs += value[1];
150             ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
151         });
152         ifs = '[' + ifs + ']';
153         
154         ofs = '';
155         $.each(query.fields, function(index, value) {
156             if (ofs != '')
157                 ofs += ', ';
158             ofs += '"' + value + '"';
159         });
160         ofs = '[' + ofs + ']';
161
162         output += 'srv.' + mixed_case(query.action) + '(auth, "' + query.subject + '", ' + ifs + ', {}, ' + ofs + ')';
163         var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
164         return output;
165     }
166
167     function mixed_case (txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
168     
169 })(jQuery); // end closure wrapper
170