2 function debug_dict_keys (msg, o) {
4 for (var k in o) keys.push(k);
5 console.log ("debug_dict_keys: " + msg + " keys= " + keys);
7 function debug_dict (msg, o) {
8 for (var k in o) console.log ("debug_dict: " + msg + " [" + k + "]=" + o[k]);
10 function debug_value (msg, value) {
11 console.log ("debug_value: " + msg + " " + value);
13 function debug_query (msg, query) {
14 if (query === undefined) console.log ("debug_query: " + msg + " -> undefined");
15 else if (query == null) console.log ("debug_query: " + msg + " -> null");
16 else if ('query_uuid' in query) console.log ("debug_query: " + msg + " query_uuid= " + query.query_uuid);
17 else console.log ("debug_query: " + msg + " query= " + query);
20 /* ------------------------------------------------------------ */
21 // this namespace holds functions for globally managing query objects
26 insert_query : function (query) {
27 manifold.all_queries[query.query_uuid]=query;
29 find_query : function (query_uuid) {
30 return manifold.all_queries[query_uuid];
32 debug_all_queries : function (msg) {
33 for (var query_uuid in manifold.all_queries) {
34 console.log("manifold.debug " + msg + " " + query_uuid + " -> " + manifold.all_queries[query_uuid]);
38 // trigger a query asynchroneously
39 proxy_url : '/manifold/proxy/json/',
41 asynchroneous_debug : true,
43 // Executes all async. queries
44 // input queries are specified as a list of {'query_uuid': <query_uuid>, 'id': <possibly null>}
45 asynchroneous_exec : function (query_uuid_domids) {
47 jQuery('.need-spin').spin();
49 // We use js function closure to be able to pass the query (array) to the
50 // callback function used when data is received
51 var success_closure = function(query, id) {
52 return function(data, textStatus) {manifold.asynchroneous_success(data, query, id);}};
54 // Loop through query array and use ajax to send back query_uuid_domids (to frontend) with json
55 jQuery.each(query_uuid_domids, function(index, tuple) {
56 var query=manifold.find_query(tuple.query_uuid);
57 var query_json=JSON.stringify (query);
58 if (manifold.asynchroneous_debug) {
59 console.log ("sending POST on " + manifold.proxy_url + " with query= " + query.__repr());
61 // not quite sure what happens if we send a string directly, as POST data is named..
62 // this gets reconstructed on the proxy side with ManifoldQuery.fill_from_POST
63 jQuery.post(manifold.proxy_url, {'json':query_json} , success_closure(query, tuple.id));
67 asynchroneous_success : function (data, query, id) {
70 /* Directly inform the requestor */
71 jQuery('#' + id).trigger('results', [data]);
73 /* Publish an update announce */
74 jQuery.publish("/results/" + query.query_uuid + "/changed", [data, query]);
81 /* ------------------------------------------------------------ */
83 // extend jQuery/$ with pubsub capabilities
84 /* https://gist.github.com/661855 */
89 $.subscribe = function( types, selector, data, fn) {
90 /* borrowed from jQuery */
91 if ( data == null && fn == null ) {
94 data = selector = undefined;
95 } else if ( fn == null ) {
96 if ( typeof selector === "string" ) {
97 // ( types, selector, fn )
101 // ( types, data, fn )
104 selector = undefined;
109 /* We use an indirection function that will clone the object passed in
110 * parameter to the subscribe callback
112 * FIXME currently we only clone query objects which are the only ones
113 * supported and editable, we might have the same issue with results but
114 * the page load time will be severely affected...
116 o.on.apply(o, [types, selector, data, function() {
117 for(i = 1; i < arguments.length; i++) {
118 if ( arguments[i].constructor.name == 'Query' )
119 arguments[i] = arguments[i].clone();
121 fn.apply(o, arguments);
125 $.unsubscribe = function() {
126 o.off.apply(o, arguments);
129 $.publish = function() {
130 o.trigger.apply(o, arguments);
135 /* ------------------------------------------------------------ */
137 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
138 //make sure to expose csrf in our outcoming ajax/post requests
140 beforeSend: function(xhr, settings) {
141 function getCookie(name) {
142 var cookieValue = null;
143 if (document.cookie && document.cookie != '') {
144 var cookies = document.cookie.split(';');
145 for (var i = 0; i < cookies.length; i++) {
146 var cookie = jQuery.trim(cookies[i]);
147 // Does this cookie string begin with the name we want?
148 if (cookie.substring(0, name.length + 1) == (name + '=')) {
149 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
156 if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
157 // Only send the token to relative URLs i.e. locally.
158 xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));