2 function debug_dict (msg, o) {
4 for (var k in o) keys.push(k);
5 console.log ("debug_dict: " + msg + " keys= " + keys);
7 function debug_value (msg, value) {
8 console.log ("debug_value: " + msg + " " + value);
10 function debug_query (msg, query) {
11 if (query === undefined) console.log ("debug_query: " + msg + " -> undefined");
12 else if (query == null) console.log ("debug_query: " + msg + " -> null");
13 else if ('query_uuid' in query) console.log ("debug_query: " + msg + " query_uuid= " + query.query_uuid);
14 else console.log ("debug_query: " + msg + " query= " + query);
17 /* ------------------------------------------------------------ */
18 // this namespace holds functions for globally managing query objects
23 insert_query : function (query) {
24 manifold.all_queries[query.query_uuid]=query;
26 find_query : function (query_uuid) {
27 return manifold.all_queries[query_uuid];
29 debug_all_queries : function (msg) {
30 for (var query_uuid in manifold.all_queries) {
31 console.log("manifold.debug " + msg + " " + query_uuid + " -> " + manifold.all_queries[query_uuid]);
35 // trigger a query asynchroneously
36 proxy_url : '/manifold/proxy/json/',
38 asynchroneous_debug : true,
40 // Executes all async. queries
41 // input queries are specified as a list of {'query_uuid': <query_uuid>, 'id': <possibly null>}
42 asynchroneous_exec : function (query_uuid_domids) {
44 jQuery('.need-spin').spin();
46 // We use js function closure to be able to pass the query (array) to the
47 // callback function used when data is received
48 var success_closure = function(query, id) {
49 return function(data, textStatus) {manifold.asynchroneous_success(data, query, id);}};
51 // Loop through query array and use ajax to send back query_uuid_domids (to frontend) with json
52 jQuery.each(query_uuid_domids, function(index, tuple) {
53 var query=manifold.find_query(tuple.query_uuid);
54 var hash=query.to_hash();
55 if (manifold.asynchroneous_debug)
56 console.log ("sending POST on " + manifold.proxy_url + " iterating on " + tuple.query_uuid + " -> " + hash);
57 jQuery.post(manifold.proxy_url, {'query': hash}, success_closure(query, tuple.id));
61 asynchroneous_success : function (data, query, id) {
64 /* Directly inform the requestor */
65 jQuery('#' + id).trigger('results', [data]);
67 /* Publish an update announce */
68 jQuery.publish("/results/" + query.query_uuid + "/changed", [data, query]);
75 /* ------------------------------------------------------------ */
77 // extend jQuery/$ with pubsub capabilities
78 /* https://gist.github.com/661855 */
83 $.subscribe = function( types, selector, data, fn) {
84 /* borrowed from jQuery */
85 if ( data == null && fn == null ) {
88 data = selector = undefined;
89 } else if ( fn == null ) {
90 if ( typeof selector === "string" ) {
91 // ( types, selector, fn )
95 // ( types, data, fn )
103 /* We use an indirection function that will clone the object passed in
104 * parameter to the subscribe callback
106 * FIXME currently we only clone query objects which are the only ones
107 * supported and editable, we might have the same issue with results but
108 * the page load time will be severely affected...
110 o.on.apply(o, [types, selector, data, function() {
111 for(i = 1; i < arguments.length; i++) {
112 if ( arguments[i].constructor.name == 'Query' )
113 arguments[i] = arguments[i].clone();
115 fn.apply(o, arguments);
119 $.unsubscribe = function() {
120 o.off.apply(o, arguments);
123 $.publish = function() {
124 o.trigger.apply(o, arguments);
129 /* ------------------------------------------------------------ */
131 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
132 //make sure to expose csrf in our outcoming ajax/post requests
134 beforeSend: function(xhr, settings) {
135 function getCookie(name) {
136 var cookieValue = null;
137 if (document.cookie && document.cookie != '') {
138 var cookies = document.cookie.split(';');
139 for (var i = 0; i < cookies.length; i++) {
140 var cookie = jQuery.trim(cookies[i]);
141 // Does this cookie string begin with the name we want?
142 if (cookie.substring(0, name.length + 1) == (name + '=')) {
143 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
150 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
151 // Only send the token to relative URLs i.e. locally.
152 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));