pour manifold-async.js in the mix
[unfold.git] / engine / static / js / manifold-async.js
1 // Helper functions for asynchronous requests
2
3 var api_url = '/manifold/api/json/'
4 var api_render_url = '/manifold/render/json'
5
6 function manifold_array_size(obj) {
7     var size = 0, key;
8     for (key in obj) {
9         if (obj.hasOwnProperty(key)) size++;
10     }
11     return size;
12 };
13
14 // Executes all async. queries contained in manifold_async_query_array, which is
15 // an array of hash (action, method, ts, filter, fields)
16 //
17 function manifold_async_exec(arr)
18 {
19
20     // start spinners
21     //onObjectAvailable('Spinners', function(){ Spinners.create('.loading').play(); }, this, true);
22     jQuery('.loading').spin();
23
24     // We use js function closure to be able to pass the query (array) to the
25     // callback function used when data is received
26     var manifold_async_success_wrapper = function(query, id) {
27         return function(data, textStatus) {
28             manifold_async_success(data, query, id);
29         };
30     };
31
32     // Loop through query array and issue XML/RPC queries
33     jQuery.each(arr, function(index, elt) {
34         // we do rendering by default
35         jQuery.post(api_url, {'query': elt.query.to_hash()}, manifold_async_success_wrapper(elt.query, elt.id));
36     })
37 }
38
39 function manifold_async_exec_render(arr)
40 {
41
42     // start spinners
43     //onObjectAvailable('Spinners', function(){ Spinners.create('.loading').play(); }, this, true);
44     jQuery('.loading').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 manifold_async_success_wrapper = function(query, id) {
49         return function(data, textStatus) {
50             manifold_async_success(data, query, id);
51         };
52     };
53
54     // Loop through query array and issue XML/RPC queries
55     jQuery.each(arr, function(index, elt) {
56         // we do rendering by default
57         jQuery.post(api_render_url, {'query': elt.query.to_hash()}, manifold_async_success_wrapper(elt.query, elt.id));
58     })
59 }
60
61 function manifold_async_render(data, query)
62 {
63     // We use js function closure to be able to pass the query (array) to the
64     // callback function used when data is received
65     var manifold_async_render_success_wrapper = function(query) {
66         return function(data, textStatus) {
67             manifold_async_render_success(data, query);
68         };
69     };
70
71     jQuery.post(api_render_url, {'data': data, 'query': query.to_hash()}, manifold_async_render_success_wrapper(data, query));
72 }
73
74 function manifold_async_error(str) {
75     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>';
76     jQuery('#manifold_message').html(out);
77     //onObjectAvailable('Spinners', function(){ Spinners.get('.loading').remove(); }, this, true);
78     jQuery('.loading').spin();
79 }
80
81 function apply_format(key, value, type, method) {
82     // type = type, key = 
83     var link = {
84         'platform': {'_all': 'platforms'},
85         'src_hostname': {'traceroute': 'agents', '_other': 'hostname'},
86         'dst_hostname': {'traceroute': 'agents', '_other': 'hostname'},
87         'src_ip': {'traceroute': 'agents', '_other': 'ip'},
88         'dst_ip': {'traceroute': 'agents', '_other': 'ip'},
89         'as_name': {'_all': 'as'},
90         'asn': {'_all': 'as'},
91         'city': {'_all': 'cities'},
92         'continent': {'_all': 'continents'},
93         'continent_code': {'_all': 'continents'},
94         'country': {'_all': 'countries'},
95         'country_code': {'_all': 'countries'},
96         'hostname': {'agents': 'agents', 'nodes': 'node', '_other': 'hostname'},
97         'ip': {'agents': 'agents', '_other': 'ip'},
98         'network_hrn': {'_all': 'network'},
99         'region': {'_all': 'regions'},
100         'region_code': {'_all': 'regions'},
101         'slice_hrn': {'_all': 'slice'},
102     };
103     if (link[type]) {
104         // creates problems sorting ?
105         if (link[type]['_all']) {
106             var urlpart = link[type]['_all'];
107         } else {
108             if (link[type][method]) {
109                 var urlpart = link[type][method];
110             } else {
111                 if (link[type]['_other']) {
112                     var urlpart = link[type]['_other'];
113                 } else {
114                     return key;
115                 }
116             }
117         }
118         return '<a href="/view/' + urlpart + '/' + key +'">' + value + '</a>';
119     } else {
120         return key;
121     }
122 }
123
124 function manifold_html_a(key, value, type) {
125     if (type == 'network_hrn') {
126         return "<a href='/view/network/" + key + "'>" + value + '</a>';
127     } else if (type == 'slice_hrn') {
128         return "<a href='/view/slice/" + key + "'>" + value + '</a>';
129     } else {
130
131     }
132 }
133
134 function manifold_html_li(type, value, is_cached) {
135     var cached = '';
136     if (is_cached)
137         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>';
138     if (type == 'slice_hrn') {
139         return "<li class='icn icn-play'>" + value + cached + "</li>";
140     } else if (type == 'network_hrn') {
141         return "<li class='icn icn-play'>" + value + cached + "</li>";
142     } else {
143         return "<li>" + value + "</li>";
144     }
145 }
146
147
148 function manifold_html_ul(data, key, value, type, method, is_cached) {
149     var out = "<ul>";
150     for (var i = 0; i < data.length; i++) {
151         out += manifold_html_li(key, apply_format(data[i][key], data[i][value], key, method), is_cached);
152         //out += manifold_html_li(key, manifold_html_a(data[i][key], data[i][value], key), is_cached);
153     }
154     out += "</ul>";
155
156     return out;
157 }
158
159 function manifold_async_render_list(data, method, is_cached) {
160     // we suppose we only have one column, or we need more precisions
161     var col = [];
162     if (manifold_array_size(data[0]) == 1) {
163         for (var k in data[0]) {
164             key = k;
165             value = k;
166         }
167     } else {
168         for (var k in data[0]) {
169             if (k.substr(-4) == '_hrn') {
170                 key = k;
171             } else {
172                 value = k;
173             }
174         }
175     }
176     var out = manifold_html_ul(data, key, value, key, method, is_cached);
177     var element = '#manifold__list__' + key + '__' + value;
178     jQuery(element).html(out);
179     // FIXME spinners
180     //onObjectAvailable('Spinners', function(){ Spinners.get(element).remove(); }, this, true);
181     jQuery('.loading').spin();
182 }
183
184
185
186 function manifold_update_template(data) 
187 {
188     jQuery.each(data, function(key, value) {
189         if ((typeof value == 'string') || (typeof value == 'number') || (typeof value == 'boolean')) {
190             // Simple field
191             jQuery('#manifold__' + key).html(value);
192         } else if (value == null) {
193             jQuery('#manifold__' + key).html("N/A");
194         } else { 
195             manifold_update_table('#manifold__' + key, value);
196         }
197     });
198 }
199
200 function manifold_async_success(data, query, id) {
201     if (data) {
202
203         if (!!id) {
204             /* Directly inform the requestor */
205             jQuery('#' + id).trigger('results', [data]);
206         } else {
207             /* Publish an update announce */
208             jQuery.publish("/results/" + query.uuid + "/changed", [data, query]);
209         }
210
211         // Is there a linked query ?
212         //if ((query.done == 'now') && (query.ts == 'latest')) {
213         //    var new_query = [query_json.replace("latest", "now")];
214         //    manifold_async_exec(new_query);
215         //}
216     }
217 }
218
219 function __old__manifold_async_render_success(data, query) {
220     if (data) {
221
222         // We loop through all the fields to update the corresponding
223         // locations in the page
224         if (typeof(data[0].error) != 'undefined') {
225             manifold_async_error(data[0].error);
226         }
227
228         /* Publish an update announce */
229         jQuery.publish("/rendering/changed", [data, query]);
230
231         // Is there a linked query ?
232         if ((query.done == 'now') && (query.ts == 'latest')) {
233             var new_query = [query_json.replace("latest", "now")];
234             manifold_async_exec(new_query);
235         }
236     }
237 }