2 function debug_dict_keys (msg, o) {
4 for (var k in o) keys.push(k);
5 messages.debug ("debug_dict_keys: " + msg + " keys= " + keys);
7 function debug_dict (msg, o) {
8 for (var k in o) messages.debug ("debug_dict: " + msg + " [" + k + "]=" + o[k]);
10 function debug_value (msg, value) {
11 messages.debug ("debug_value: " + msg + " " + value);
13 function debug_query (msg, query) {
14 if (query === undefined) messages.debug ("debug_query: " + msg + " -> undefined");
15 else if (query == null) messages.debug ("debug_query: " + msg + " -> null");
16 else if ('query_uuid' in query) messages.debug ("debug_query: " + msg + query.__repr());
17 else messages.debug ("debug_query: " + msg + " query= " + query);
20 /* ------------------------------------------------------------ */
23 * This namespace holds functions for globally managing query objects
30 spin: function(locator, active /*= true */) {
31 active = typeof active !== 'undefined' ? active : true;
34 $(locator).spin(manifold.spin_presets);
38 } catch (err) { messages.debug("Cannot turn spins on/off " + err); }
42 * Associative array storing the set of queries active on the page
48 * Insert a query in the global hash table associating uuids to queries.
49 * If the query has no been analyzed yet, let's do it.
50 * \fn insert_query(query)
52 * \param ManifoldQuery query Query to be added
54 insert_query : function (query) {
55 if (query.analyzed_query == null) {
56 query.analyze_subqueries();
58 manifold.all_queries[query.query_uuid]=query;
62 * Returns the query associated to a UUID
63 * \fn find_query(query_uuid)
65 * \param string query_uuid The UUID of the query to be returned
67 find_query : function (query_uuid) {
68 return manifold.all_queries[query_uuid];
71 // trigger a query asynchroneously
72 proxy_url : '/manifold/proxy/json/',
74 asynchroneous_debug : true,
77 * \brief We use js function closure to be able to pass the query (array)
78 * to the callback function used when data is received
80 success_closure: function(query, publish_uuid, callback /*domid*/)
82 return function(data, textStatus) {
83 manifold.asynchroneous_success(data, query, publish_uuid, callback /*domid*/);
87 // Executes all async. queries
88 // input queries are specified as a list of {'query_uuid': <query_uuid>, 'id': <possibly null>}
89 asynchroneous_exec : function (query_publish_dom_tuples) {
92 // in case the spin stuff was not loaded, let's make sure we proceed to the exit
94 if (manifold.asynchroneous_debug)
95 messages.debug("Turning on spin with " + jQuery(".need-spin").length + " matches for .need-spin");
96 jQuery('.need-spin').spin(manifold.spin_presets);
97 } catch (err) { messages.debug("Cannot turn on spins " + err); }
99 // Loop through input array, and use publish_uuid to publish back results
100 jQuery.each(query_publish_dom_tuples, function(index, tuple) {
101 var query=manifold.find_query(tuple.query_uuid);
102 var query_json=JSON.stringify (query);
103 var publish_uuid=tuple.publish_uuid;
104 // by default we publish using the same uuid of course
105 if (publish_uuid==undefined) publish_uuid=query.query_uuid;
106 if (manifold.asynchroneous_debug) {
107 messages.debug("sending POST on " + manifold.proxy_url + " to be published on " + publish_uuid);
108 messages.debug("... ctd... with actual query= " + query.__repr());
110 // not quite sure what happens if we send a string directly, as POST data is named..
111 // this gets reconstructed on the proxy side with ManifoldQuery.fill_from_POST
112 jQuery.post(manifold.proxy_url, {'json':query_json} , manifold.success_closure(query, publish_uuid, tuple.callback /*domid*/));
117 * \brief Forward a query to the manifold backend
118 * \param query (dict) the query to be executed asynchronously
119 * \param callback (function) the function to be called when the query terminates
121 * \param domid (string) the domid to be notified about the results (null for using the pub/sub system
123 forward: function(query, callback /*domid*/) {
124 var query_json = JSON.stringify(query);
125 $.post(manifold.proxy_url, {'json': query_json} , manifold.success_closure(query, query.query_uuid, callback/*domid*/));
129 * Returns whether a query expects a unique results.
130 * This is the case when the filters contain a key of the object
131 * \fn query_expects_unique_result(query)
133 * \param ManifoldQuery query Query for which we are testing whether it expects a unique result
135 query_expects_unique_result: function(query) {
136 /* XXX we need functions to query metadata */
137 //var keys = MANIFOLD_METADATA[query.object]['keys']; /* array of array of field names */
138 /* TODO requires keys in metadata */
144 * \fn publish_result(query, results)
146 * \param ManifoldQuery query Query which has received results
147 * \param array results results corresponding to query
149 publish_result: function(query, result) {
150 /* Publish an update announce */
151 var channel="/results/" + query.query_uuid + "/changed";
152 if (manifold.asynchroneous_debug) messages.debug("publishing result on " + channel);
153 jQuery.publish(channel, [result, query]);
157 * Recursively publish result
158 * \fn publish_result_rec(query, result)
160 * \param ManifoldQuery query Query which has received result
161 * \param array result result corresponding to query
163 publish_result_rec: function(query, result) {
164 /* If the result is not unique, only publish the top query;
165 * otherwise, publish the main object as well as subqueries
166 * XXX how much recursive are we ?
168 if (manifold.query_expects_unique_result(query)) {
169 /* Also publish subqueries */
170 jQuery.each(query.subqueries, function(object, subquery) {
171 manifold.publish_result_rec(subquery, result[0][object]);
172 /* TODO remove object from result */
175 manifold.publish_result(query, result);
178 // if set domid allows the result to be directed to just one plugin
179 // most of the time publish_uuid will be query.query_uuid
180 // however in some cases we wish to publish the result under a different uuid
181 // e.g. an updater wants to publish its result as if from the original (get) query
182 asynchroneous_success : function (data, query, publish_uuid, callback /*domid*/) {
183 // xxx should have a nicer declaration of that enum in sync with the python code somehow
185 /* If a callback has been specified, we redirect results to it */
186 if (!!callback) { callback(data); return; }
188 if (data.code == 2) { // ERROR
189 alert("Your session has expired, please log in again");
190 window.location="/logout/";
193 if (data.code == 1) { // WARNING
194 messages.error("Some errors have been received from the manifold backend at " + MANIFOLD_URL + " [" + data.description + "]");
195 // publish error code and text message on a separate channel for whoever is interested
196 jQuery.publish("/results/" + publish_uuid + "/failed", [data.code, data.description] );
198 // once everything is checked we can use the 'value' part of the manifoldresult
199 var result=data.value;
201 //if (!!callback /* domid */) {
202 // /* Directly inform the requestor */
203 // if (manifold.asynchroneous_debug) messages.debug("directing result to callback");
205 // //if (manifold.asynchroneous_debug) messages.debug("directing result to " + domid);
206 // //jQuery('#' + domid).trigger('results', [result]);
208 /* XXX Jordan XXX I don't need publish_uuid here... What is it used for ? */
209 /* query is the query we sent to the backend; we need to find the
210 * corresponding analyezd_query in manifold.all_queries
212 tmp_query = manifold.find_query(query.query_uuid);
213 manifold.publish_result_rec(tmp_query.analyzed_query, result);
219 }; // manifold object
220 /* ------------------------------------------------------------ */
222 // extend jQuery/$ with pubsub capabilities
223 /* https://gist.github.com/661855 */
228 $.subscribe = function( channel, selector, data, fn) {
229 /* borrowed from jQuery */
230 if ( data == null && fn == null ) {
233 data = selector = undefined;
234 } else if ( fn == null ) {
235 if ( typeof selector === "string" ) {
236 // ( channel, selector, fn )
240 // ( channel, data, fn )
243 selector = undefined;
248 /* We use an indirection function that will clone the object passed in
249 * parameter to the subscribe callback
251 * FIXME currently we only clone query objects which are the only ones
252 * supported and editable, we might have the same issue with results but
253 * the page load time will be severely affected...
255 o.on.apply(o, [channel, selector, data, function() {
256 for(i = 1; i < arguments.length; i++) {
257 if ( arguments[i].constructor.name == 'ManifoldQuery' )
258 arguments[i] = arguments[i].clone();
260 fn.apply(o, arguments);
264 $.unsubscribe = function() {
265 o.off.apply(o, arguments);
268 $.publish = function() {
269 o.trigger.apply(o, arguments);
274 /* ------------------------------------------------------------ */
276 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
277 //make sure to expose csrf in our outcoming ajax/post requests
279 beforeSend: function(xhr, settings) {
280 function getCookie(name) {
281 var cookieValue = null;
282 if (document.cookie && document.cookie != '') {
283 var cookies = document.cookie.split(';');
284 for (var i = 0; i < cookies.length; i++) {
285 var cookie = jQuery.trim(cookies[i]);
286 // Does this cookie string begin with the name we want?
287 if (cookie.substring(0, name.length + 1) == (name + '=')) {
288 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
295 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
296 // Only send the token to relative URLs i.e. locally.
297 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));