oops
[myslice.git] / manifold / js / manifold.js
1 // utilities 
2 function debug_dict_keys (msg, o) {
3     var keys=[];
4     for (var k in o) keys.push(k);
5     console.log ("debug_dict_keys: " + msg + " keys= " + keys);
6 }
7 function debug_dict (msg, o) {
8     for (var k in o) console.log ("debug_dict: " + msg + " [" + k + "]=" + o[k]);
9 }
10 function debug_value (msg, value) {
11     console.log ("debug_value: " + msg + " " + value);
12 }
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.__repr());
17     else console.log ("debug_query: " + msg + " query= " + query);
18 }
19
20 /* ------------------------------------------------------------ */
21 // this namespace holds functions for globally managing query objects
22 var manifold = {
23
24     all_queries: {},
25
26     insert_query : function (query) { 
27         manifold.all_queries[query.query_uuid]=query; 
28    },
29     find_query : function (query_uuid) { 
30         return manifold.all_queries[query_uuid];
31     },
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]);
35         }
36     },
37
38     // trigger a query asynchroneously
39     proxy_url : '/manifold/proxy/json/',
40
41     asynchroneous_debug : false,
42
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) {
46         // start spinners
47
48         if (manifold.asynchroneous_debug) console.log("Turning on spin with " + jQuery(".need-spin").length + " matches for .need-spin");
49         jQuery('.need-spin').spin(spin_presets);
50         
51         // We use js function closure to be able to pass the query (array) to the
52         // callback function used when data is received
53         var success_closure = function(query, id) {
54             return function(data, textStatus) {manifold.asynchroneous_success(data, query, id);}};
55         
56         // Loop through query array and use ajax to send back query_uuid_domids (to frontend) with json
57         jQuery.each(query_uuid_domids, function(index, tuple) {
58             var query=manifold.find_query(tuple.query_uuid);
59             var query_json=JSON.stringify (query);
60             if (manifold.asynchroneous_debug) {
61                 console.log ("sending POST on " + manifold.proxy_url + " with query= " + query.__repr());
62             }
63             // not quite sure what happens if we send a string directly, as POST data is named..
64             // this gets reconstructed on the proxy side with ManifoldQuery.fill_from_POST
65             jQuery.post(manifold.proxy_url, {'json':query_json} , success_closure(query, tuple.id));
66         })
67             },
68
69     asynchroneous_success : function (data, query, id) {
70         if (manifold.asynchroneous_debug) console.log ("received manifold result with code " + data.code);
71         // xxx should have a nicer declaration of that enum in sync with the python code somehow
72         if (data.code == 1) {
73             alert("Your session has expired, please log in again");
74             window.location="/logout/";
75             return;
76         } else if (data.code != 0) {
77             alert("Error received from manifold backend at " + MANIFOLD_URL + " [" + data.output + "]");
78             return;
79         }
80         // once everything is checked we can use the 'value' part of the manifoldresult
81         data=data.value;
82         if (data) {
83             if (!!id) {
84                 /* Directly inform the requestor */
85                 jQuery('#' + id).trigger('results', [data]);
86             } else {
87                 /* Publish an update announce */
88                 jQuery.publish("/results/" + query.query_uuid + "/changed", [data, query]);
89             }
90
91         }
92     },
93
94 }; // manifold object
95 /* ------------------------------------------------------------ */
96
97 // extend jQuery/$ with pubsub capabilities
98 /* https://gist.github.com/661855 */
99 (function($) {
100
101   var o = $({});
102
103   $.subscribe = function( types, selector, data, fn) {
104     /* borrowed from jQuery */
105     if ( data == null && fn == null ) {
106         // ( types, fn )
107         fn = selector;
108         data = selector = undefined;
109     } else if ( fn == null ) {
110         if ( typeof selector === "string" ) {
111             // ( types, selector, fn )
112             fn = data;
113             data = undefined;
114         } else {
115             // ( types, data, fn )
116             fn = data;
117             data = selector;
118             selector = undefined;
119         }
120     }
121     /* </ugly> */
122
123     /* We use an indirection function that will clone the object passed in
124      * parameter to the subscribe callback 
125      * 
126      * FIXME currently we only clone query objects which are the only ones
127      * supported and editable, we might have the same issue with results but
128      * the page load time will be severely affected...
129      */
130     o.on.apply(o, [types, selector, data, function() { 
131         for(i = 1; i < arguments.length; i++) {
132             if ( arguments[i].constructor.name == 'Query' )
133                 arguments[i] = arguments[i].clone();
134         }
135         fn.apply(o, arguments);
136     }]);
137   };
138
139   $.unsubscribe = function() {
140     o.off.apply(o, arguments);
141   };
142
143   $.publish = function() {
144     o.trigger.apply(o, arguments);
145   };
146
147 }(jQuery));
148
149 /* ------------------------------------------------------------ */
150
151 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
152 //make sure to expose csrf in our outcoming ajax/post requests
153 $.ajaxSetup({ 
154      beforeSend: function(xhr, settings) {
155          function getCookie(name) {
156              var cookieValue = null;
157              if (document.cookie && document.cookie != '') {
158                  var cookies = document.cookie.split(';');
159                  for (var i = 0; i < cookies.length; i++) {
160                      var cookie = jQuery.trim(cookies[i]);
161                      // Does this cookie string begin with the name we want?
162                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
163                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
164                      break;
165                  }
166              }
167          }
168          return cookieValue;
169          }
170          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
171              // Only send the token to relative URLs i.e. locally.
172              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
173          }
174      } 
175 });
176