rename js Query into ManifoldQuery
[myslice.git] / unfold / js / manifold-async.js
1 manifold_async_debug=false;
2
3 // Helper functions for asynchronous requests
4
5 var api_url = '/manifold/api/json/'
6
7 // Executes all async. queries
8 // input queries are specified as a list of {'query': new Query(..), 'id': <possibly null>}
9 function manifold_async_exec(queries) {
10     if (manifold_async_debug) console.log('manifold_async_exec length='+ queries.length);
11     // start spinners
12     jQuery('.need-spin').spin();
13
14     // We use js function closure to be able to pass the query (array) to the
15     // callback function used when data is received
16     var manifold_async_success_closure = function(query, id) {
17         return function(data, textStatus) {manifold_async_success(data, query, id);}
18     };
19
20     // Loop through query array and use ajax to send back queries (to frontend) with json
21     jQuery.each(queries, function(index, tuple) {
22         hash=tuple.query.to_hash();
23         if (manifold_async_debug) console.log ("sending POST on " + api_url + " iterating on " + tuple + " -> " + hash);
24         jQuery.post(api_url, {'query': hash}, manifold_async_success_closure(tuple.query, tuple.id));
25     })
26 }
27
28 /* not used
29 function manifold_async_error(str) {
30     var out = '<div class="error"><h2>Error</h2><dl id="system-message"><dt class="error">Notice</dt><dd class="error message"><ul><li>' + jQuery('<div />').text(str).html() + '</li></ul></dd></dl></div>';
31     jQuery('#manifold_message').html(out);
32     //onObjectAvailable('Spinners', function(){ Spinners.get('.loading').remove(); }, this, true);
33     jQuery('.need-spin').spin(false);
34 }
35 */
36
37 /* what the hell is this doing here ?
38 function apply_format(key, value, type, method) {
39     // type = type, key = 
40     var link = {
41         'platform': {'_all': 'platforms'},
42         'src_hostname': {'traceroute': 'agents', '_other': 'hostname'},
43         'dst_hostname': {'traceroute': 'agents', '_other': 'hostname'},
44         'src_ip': {'traceroute': 'agents', '_other': 'ip'},
45         'dst_ip': {'traceroute': 'agents', '_other': 'ip'},
46         'as_name': {'_all': 'as'},
47         'asn': {'_all': 'as'},
48         'city': {'_all': 'cities'},
49         'continent': {'_all': 'continents'},
50         'continent_code': {'_all': 'continents'},
51         'country': {'_all': 'countries'},
52         'country_code': {'_all': 'countries'},
53         'hostname': {'agents': 'agents', 'nodes': 'node', '_other': 'hostname'},
54         'ip': {'agents': 'agents', '_other': 'ip'},
55         'network_hrn': {'_all': 'network'},
56         'region': {'_all': 'regions'},
57         'region_code': {'_all': 'regions'},
58         'slice_hrn': {'_all': 'slice'},
59     };
60     if (link[type]) {
61         // creates problems sorting ?
62         if (link[type]['_all']) {
63             var urlpart = link[type]['_all'];
64         } else {
65             if (link[type][method]) {
66                 var urlpart = link[type][method];
67             } else {
68                 if (link[type]['_other']) {
69                     var urlpart = link[type]['_other'];
70                 } else {
71                     return key;
72                 }
73             }
74         }
75         return '<a href="/view/' + urlpart + '/' + key +'">' + value + '</a>';
76     } else {
77         return key;
78     }
79 }
80 */
81
82 function manifold_html_a(key, value, type) {
83     if (type == 'network_hrn') {
84         return "<a href='/view/network/" + key + "'>" + value + '</a>';
85     } else if (type == 'slice_hrn') {
86         return "<a href='/view/slice/" + key + "'>" + value + '</a>';
87     } else {
88
89     }
90 }
91
92 function manifold_html_li(type, value, is_cached) {
93     var cached = '';
94     if (is_cached)
95         cached='<div class="cache"><span><b>Cached information from the database</b><br/>Timestamp: XX/XX/XX XX:XX:XX<br/><br/><i>Refresh in progress...</i></span></div>';
96     if (type == 'slice_hrn') {
97         return "<li class='icn icn-play'>" + value + cached + "</li>";
98     } else if (type == 'network_hrn') {
99         return "<li class='icn icn-play'>" + value + cached + "</li>";
100     } else {
101         return "<li>" + value + "</li>";
102     }
103 }
104
105
106 function manifold_html_ul(data, key, value, type, method, is_cached) {
107     var out = "<ul>";
108     for (var i = 0; i < data.length; i++) {
109         out += manifold_html_li(key, apply_format(data[i][key], data[i][value], key, method), is_cached);
110         //out += manifold_html_li(key, manifold_html_a(data[i][key], data[i][value], key), is_cached);
111     }
112     out += "</ul>";
113
114     return out;
115 }
116
117 function manifold_update_template(data) 
118 {
119     jQuery.each(data, function(key, value) {
120         if ((typeof value == 'string') || (typeof value == 'number') || (typeof value == 'boolean')) {
121             // Simple field
122             jQuery('#manifold__' + key).html(value);
123         } else if (value == null) {
124             jQuery('#manifold__' + key).html("N/A");
125         } else { 
126             manifold_update_table('#manifold__' + key, value);
127         }
128     });
129 }
130
131 function manifold_async_success(data, query, id) {
132     if (data) {
133
134         if (!!id) {
135             /* Directly inform the requestor */
136             jQuery('#' + id).trigger('results', [data]);
137         } else {
138             /* Publish an update announce */
139             jQuery.publish("/results/" + query.uuid + "/changed", [data, query]);
140         }
141
142         // Is there a linked query ?
143         //if ((query.done == 'now') && (query.ts == 'latest')) {
144         //    var new_query = [query_json.replace("latest", "now")];
145         //    manifold_async_exec(new_query);
146         //}
147     }
148 }
149
150 //http://stackoverflow.com/questions/5100539/django-csrf-check-failing-with-an-ajax-post-request
151 //make sure to expose csrf in our outcoming ajax/post requests
152 $.ajaxSetup({ 
153      beforeSend: function(xhr, settings) {
154          function getCookie(name) {
155              var cookieValue = null;
156              if (document.cookie && document.cookie != '') {
157                  var cookies = document.cookie.split(';');
158                  for (var i = 0; i < cookies.length; i++) {
159                      var cookie = jQuery.trim(cookies[i]);
160                      // Does this cookie string begin with the name we want?
161                  if (cookie.substring(0, name.length + 1) == (name + '=')) {
162                      cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
163                      break;
164                  }
165              }
166          }
167          return cookieValue;
168          }
169          if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
170              // Only send the token to relative URLs i.e. locally.
171              xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
172          }
173      } 
174 });