src/dp_rrdtool.c: Adapt to new callback prototype.
[collection4.git] / share / collection.js
1 function format_instance(inst)
2 {
3   return ("<li class=\"instance\"><a href=\"" + location.pathname
4       + "?action=show_instance;" + inst.params + "\">" + inst.description
5       + "</a></li>");
6 }
7
8 function format_instance_list(instances)
9 {
10   var ret = "<ul class=\"instance_list\">";
11   var i;
12
13   if (instances.length == 0)
14     return ("");
15
16   for (i = 0; i < instances.length; i++)
17     ret += format_instance (instances[i]);
18   
19   ret += "</ul>";
20
21   return (ret);
22 }
23
24 function format_graph(graph)
25 {
26   return ("<li class=\"graph\">" + graph.title + format_instance_list (graph.instances) + "</li>");
27 }
28
29 function update_search_suggestions ()
30 {
31   var term = $("#search-input").val ();
32   if (term.length < 2)
33   {
34     $("#search-suggest").hide ();
35     return (true);
36   }
37
38   $("#search-suggest").show ();
39   $.getJSON ("collection.fcgi",
40     { "action": "search_json", "q": term},
41     function(data)
42     {
43       var i;
44       $("#search-suggest").html ("");
45       for (i = 0; i < data.length; i++)
46       {
47         var graph = data[i];
48         $("#search-suggest").append (format_graph (graph));
49       }
50     }
51   );
52 } /* update_search_suggestions */
53
54 function zoom_redraw (jq_obj) /* {{{ */
55 {
56   var url = jq_obj.data ("base_url");
57
58   if ((jq_obj == null) || (url == null))
59     return (false);
60
61   if (jq_obj.data ('begin') != null)
62     url += ";begin=" + jq_obj.data ('begin');
63   if (jq_obj.data ('end') != null)
64     url += ";end=" + jq_obj.data ('end');
65
66   jq_obj.attr ("src", url);
67   return (true);
68 } /* }}} function zoom_redraw */
69
70 function zoom_reset (graph_id, diff) /* {{{ */
71 {
72   var jq_obj;
73   var end;
74   var begin;
75
76   jq_obj = $("#" + graph_id);
77   if (jq_obj == null)
78     return (false);
79
80   end = new Number ((new Date ()).getTime () / 1000);
81   begin = new Number (end - diff);
82
83   jq_obj.data ('begin', begin.toFixed (0));
84   jq_obj.data ('end', end.toFixed (0));
85
86   return (zoom_redraw (jq_obj));
87 } /* }}} function zoom_reset */
88
89 function zoom_hour (graph_id) /* {{{ */
90 {
91   zoom_reset (graph_id, 3600);
92 } /* }}} function zoom_hour */
93
94 function zoom_day (graph_id) /* {{{ */
95 {
96   zoom_reset (graph_id, 86400);
97 } /* }}} function zoom_day */
98
99 function zoom_week (graph_id) /* {{{ */
100 {
101   zoom_reset (graph_id, 7 * 86400);
102 } /* }}} function zoom_week */
103
104 function zoom_month (graph_id) /* {{{ */
105 {
106   zoom_reset (graph_id, 31 * 86400);
107 } /* }}} function zoom_month */
108
109 function zoom_year (graph_id) /* {{{ */
110 {
111   zoom_reset (graph_id, 366 * 86400);
112 } /* }}} function zoom_year */
113
114 function zoom_relative (graph_id, factor_begin, factor_end) /* {{{ */
115 {
116   var jq_obj;
117   var end;
118   var begin;
119   var diff;
120
121   jq_obj = $("#" + graph_id);
122   if (jq_obj == null)
123     return (false);
124
125   begin = jq_obj.data ('begin');
126   end = jq_obj.data ('end');
127   if ((begin == null) || (end == null))
128     return (zoom_day (graph_id));
129
130   begin = new Number (begin);
131   end = new Number (end);
132
133   diff = end - begin;
134   if ((diff <= 300) && (factor_begin > 0.0) && (factor_end < 0.0))
135     return (true);
136
137   jq_obj.data ('begin', begin + (diff * factor_begin));
138   jq_obj.data ('end', end + (diff * factor_end));
139
140   return (zoom_redraw (jq_obj));
141 } /* }}} function zoom_relative */
142
143 function zoom_reference (graph_id) /* {{{ */
144 {
145   var jq_obj;
146   var end;
147   var begin;
148
149   jq_obj = $("#" + graph_id);
150   if (jq_obj == null)
151     return (false);
152
153   begin = jq_obj.data ('begin');
154   end = jq_obj.data ('end');
155   if ((begin == null) || (end == null))
156     return (false);
157
158   $(".graph-img img").each (function ()
159   {
160     $(this).data ('begin', begin);
161     $(this).data ('end', end);
162     zoom_redraw ($(this));
163   });
164 } /* }}} function zoom_reference */
165
166 function zoom_earlier (graph_id) /* {{{ */
167 {
168   return (zoom_relative (graph_id, -0.2, -0.2));
169 } /* }}} function zoom_earlier */
170
171 function zoom_later (graph_id) /* {{{ */
172 {
173   return (zoom_relative (graph_id, +0.2, +0.2));
174 } /* }}} function zoom_later */
175
176 function zoom_in (graph_id) /* {{{ */
177 {
178   return (zoom_relative (graph_id, +0.2, -0.2));
179 } /* }}} function zoom_earlier */
180
181 function zoom_out (graph_id) /* {{{ */
182 {
183   return (zoom_relative (graph_id, (-1.0 / 3.0), (1.0 / 3.0)));
184 } /* }}} function zoom_earlier */
185
186 $(document).ready(function() {
187     /* $("#layout-middle-right").html ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>"); */
188     $("#search-form").append ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>");
189     $("#search-suggest").hide ();
190
191     $("#search-input").blur (function()
192     {
193       window.setTimeout (function ()
194       {
195         $("#search-suggest").hide ();
196       }, 500);
197     });
198
199     $("#search-input").focus (function()
200     {
201       var term = $("#search-input").val ();
202       if (term.length < 2)
203       {
204         $("#search-suggest").hide ();
205       }
206       else
207       {
208         $("#search-suggest").show ();
209       }
210     });
211
212     $("#search-input").keyup (function()
213     {
214       update_search_suggestions ();
215     });
216
217     var graph_count = 0;
218     $(".graph-img").each (function (index, elem)
219     {
220       var id = "graph" + graph_count;
221       graph_count++;
222
223       $(this).find ("img").each (function (img_index, img_elem)
224       {
225         var base_url;
226
227         $(this).attr ("id", id);
228
229         base_url = $(this).attr ("src").replace (/;(begin|end)=[^;]*/g, '');
230         $(this).data ("base_url", base_url);
231       });
232
233       $(this).append ("<div class=\"graph-buttons presets\">"
234         + "<div class=\"graph-button\" onClick=\"zoom_hour  ('"+id+"');\">H</div>"
235         + "<div class=\"graph-button\" onClick=\"zoom_day   ('"+id+"');\">D</div>"
236         + "<div class=\"graph-button\" onClick=\"zoom_week  ('"+id+"');\">W</div>"
237         + "<div class=\"graph-button\" onClick=\"zoom_month ('"+id+"');\">M</div>"
238         + "<div class=\"graph-button\" onClick=\"zoom_year  ('"+id+"');\">Y</div>"
239         + "<div class=\"graph-button\" onClick=\"zoom_reference ('"+id+"');\">!</div>"
240         + "</div>"
241         + "<div class=\"graph-buttons navigation\">"
242         + "<div class=\"graph-button\" onClick=\"zoom_earlier ('"+id+"');\">←</div>"
243         + "<div class=\"graph-button\" onClick=\"zoom_out     ('"+id+"');\">−</div>"
244         + "<div class=\"graph-button\" onClick=\"zoom_in      ('"+id+"');\">+</div>"
245         + "<div class=\"graph-button\" onClick=\"zoom_later   ('"+id+"');\">→</div>"
246         + "</div>"
247         );
248     });
249 });
250
251 /* vim: set sw=2 sts=2 et fdm=marker : */