in query, 'method' is now called 'subject'
[unfold.git] / manifold / js / manifold.js
1 // utilities 
2 function debug_dict (msg, o) {
3     var keys=[];
4     for (var k in o) keys.push(k);
5     console.log ("debug_dict: " + msg + " keys= " + keys);
6 }
7 function debug_value (msg, value) {
8     console.log ("debug_value: " + msg + " " + value);
9 }
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);
15 }
16
17 /* ------------------------------------------------------------ */
18 // this namespace holds functions for globally managing query objects
19 var manifold = {
20
21     all_queries: {},
22
23     insert_query : function (query) { 
24         manifold.all_queries[query.query_uuid]=query; 
25    },
26     find_query : function (query_uuid) { 
27         return manifold.all_queries[query_uuid];
28     },
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]);
32         }
33     },
34
35     // trigger a query asynchroneously
36     proxy_url : '/manifold/proxy/json/',
37
38     asynchroneous_debug : true,
39
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) {
43         // start spinners
44         jQuery('.need-spin').spin();
45         
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);}};
50         
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 + " with query= " + query.__repr(query));
57             jQuery.post(manifold.proxy_url, {'query': hash}, success_closure(query, tuple.id));
58         })
59             },
60
61     asynchroneous_success : function (data, query, id) {
62         if (data) {
63             if (!!id) {
64                 /* Directly inform the requestor */
65                 jQuery('#' + id).trigger('results', [data]);
66             } else {
67                 /* Publish an update announce */
68                 jQuery.publish("/results/" + query.query_uuid + "/changed", [data, query]);
69             }
70
71         }
72     },
73
74 }; // manifold object
75 /* ------------------------------------------------------------ */
76
77 // extend jQuery/$ with pubsub capabilities
78 /* https://gist.github.com/661855 */
79 (function($) {
80
81   var o = $({});
82
83   $.subscribe = function( types, selector, data, fn) {
84     /* borrowed from jQuery */
85     if ( data == null && fn == null ) {
86         // ( types, fn )
87         fn = selector;
88         data = selector = undefined;
89     } else if ( fn == null ) {
90         if ( typeof selector === "string" ) {
91             // ( types, selector, fn )
92             fn = data;
93             data = undefined;
94         } else {
95             // ( types, data, fn )
96             fn = data;
97             data = selector;
98             selector = undefined;
99         }
100     }
101     /* </ugly> */
102
103     /* We use an indirection function that will clone the object passed in
104      * parameter to the subscribe callback 
105      * 
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...
109      */
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();
114         }
115         fn.apply(o, arguments);
116     }]);
117   };
118
119   $.unsubscribe = function() {
120     o.off.apply(o, arguments);
121   };
122
123   $.publish = function() {
124     o.trigger.apply(o, arguments);
125   };
126
127 }(jQuery));
128
129 /* ------------------------------------------------------------ */
130
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
133 $.ajaxSetup({ 
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));
144                      break;
145                  }
146              }
147          }
148          return cookieValue;
149          }
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'));
153          }
154      } 
155 });
156