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