forget about manage.py collectstatic and come up with our own brew
[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     $.fn.QueryCode = function( method ) {
25         /* Method calling logic */
26         if ( methods[method] ) {
27             return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
28         } else if ( typeof method === 'object' || ! method ) {
29             return methods.init.apply( this, arguments );
30         } else {
31             $.error( 'Method ' +  method + ' does not exist on jQuery.QueryCode' );
32         }    
33     };
34
35     var methods = {
36         init : function (options) {
37             console.log("temporarily turned off SyntaxHighlighter ...");
38 //          SyntaxHighlighter.all();
39             return this.each(function() {
40                 var $this=$(this);
41                 var data=$this.data('QueryCode');
42                 if ( ! data ) {
43                     // Subscribe to query updates
44                     var channel='/results/' + options.query_uuid + '/updated';
45                     /* passing $this as 2nd arg: callbacks will retrieve $this as e.data */
46                     $.subscribe(channel, $this, update_plugin);
47                     if (querycode_debug) window.console.log('subscribing to ' + channel);
48                     $this.data('QueryCode', {options: options});
49                     // react to changes to the language selector
50                     $this.find(".querycode-lang").change(change_language);
51                     // publish so we refresh ourselves
52                     $.publish(channel,"please_init_yourself");
53                 }
54             });
55
56
57         }, 
58
59 //      destroy : function( ) {
60 //          if (querycode_debug) console.log("QueryCode.destroy...");
61 //      },
62 //      update : function( content ) { 
63 //          if (querycode_debug) console.log("QueryCode.update...");
64 //      },
65         
66     } // methods
67                           
68     // we retrieve the plugindiv as e.data - cf the 2nd arg to subscribe
69     // in fact we don't really read the published message
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
83         var lang=$plugindiv.find(".querycode-lang").val();
84         var dom=$plugindiv.find(".querycode-viz");
85         var query = $plugindiv.data().QueryCode.options.query;
86         funname="translate_query_as_" + lang;
87         fun=eval(funname);
88         if ( ! fun) {
89             console.log("Cannot find translator function for lang " + lang);
90             return;
91         }
92         html_code=fun(query);
93         dom.html(html_code);
94         console.log("turned off SyntaxHighlighter.highlight");
95 //      SyntaxHighlighter.highlight()
96
97     }
98
99
100     // private stuff
101     function translate_query_as_ruby (query) {
102         debug_object("query_ruby entering -- query=" + query, query);
103         var output = '# Connection to XMLRPC server\n';
104         output += 'require "xmlrpc/client"\n';
105         output += 'require "pp"\n';
106         output += '\n';
107         output += 'XMLRPC::Config.module_eval do\n';
108         output += '  remove_const :ENABLE_NIL_PARSER\n';
109         output += '  const_set :ENABLE_NIL_PARSER, true\n';
110         output += 'end\n';
111         output += 'srv = XMLRPC::Client.new2("' + MANIFOLD_URL + '")\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("' + title_case(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         var output = '# Connection to XMLRPC server\n';
144         output += 'import xmlrpclib\n';
145         output += 'srv = xmlrpclib.ServerProxy("' + MANIFOLD_URL + '", allow_none=True)\n\n';
146         output += '# Authentication token\n';
147         output += 'auth = {"AuthMethod": "password", "Username": "name.surname@domain.name", "AuthString": "mypassword"}\n\n';
148
149         ifs = '';
150         $.each(query.filters, function(i, value) {
151             if (ifs != '')
152                 ifs += ', ';
153             //ifs += '"'
154             //if (value[1] != "=")
155             //    ifs += value[1];
156             ifs += '["' + value[0] + '", "' + value[1] + '", "' + value[2] + '"]';
157         });
158         ifs = '[' + ifs + ']';
159         
160         ofs = '';
161         $.each(query.fields, function(index, value) {
162             if (ofs != '')
163                 ofs += ', ';
164             ofs += '"' + value + '"';
165         });
166         ofs = '[' + ofs + ']';
167
168         output += 'srv.' + title_case(query.action) + '(auth, "' + query.method + '", ' + ifs + ', {}, ' + ofs + ')';
169         var output = '<pre class="brush: python; toolbar: false;">' + output + "</pre>";
170         return output;
171     }
172
173     function title_case (txt){ return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}
174     
175 })(jQuery); // end closure wrapper
176