a15a096038a8e6d2f2426d6f97390ae60c90788f
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 }
8 function format_instance_list(instances)
9 {
10 var ret = "<ul class=\"instance_list\">";
11 var i;
13 if (instances.length == 0)
14 return ("");
16 for (i = 0; i < instances.length; i++)
17 ret += format_instance (instances[i]);
19 ret += "</ul>";
21 return (ret);
22 }
24 function format_graph(graph)
25 {
26 return ("<li class=\"graph\">" + graph.title + format_instance_list (graph.instances) + "</li>");
27 }
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 }
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 */
54 function zoom_redraw (jq_obj)
55 {
56 var url = jq_obj.data ("base_url");
58 if ((jq_obj == null) || (url == null))
59 return (false);
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');
66 jq_obj.attr ("src", url);
67 return (true);
68 }
70 function zoom_reset (graph_id, diff)
71 {
72 var jq_obj;
73 var end;
74 var begin;
76 jq_obj = $("#" + graph_id);
77 if (jq_obj == null)
78 return (false);
80 end = new Number ((new Date ()).getTime () / 1000);
81 begin = new Number (end - diff);
83 jq_obj.data ('begin', begin.toFixed (0));
84 jq_obj.data ('end', end.toFixed (0));
86 return (zoom_redraw (jq_obj));
87 }
89 function zoom_hour (graph_id)
90 {
91 zoom_reset (graph_id, 3600);
92 }
94 function zoom_day (graph_id)
95 {
96 zoom_reset (graph_id, 86400);
97 }
99 function zoom_week (graph_id)
100 {
101 zoom_reset (graph_id, 7 * 86400);
102 }
104 function zoom_month (graph_id)
105 {
106 zoom_reset (graph_id, 31 * 86400);
107 }
109 function zoom_year (graph_id)
110 {
111 zoom_reset (graph_id, 366 * 86400);
112 }
114 function zoom_relative (graph_id, factor_begin, factor_end)
115 {
116 var jq_obj;
117 var end;
118 var begin;
119 var diff;
121 jq_obj = $("#" + graph_id);
122 if (jq_obj == null)
123 return (false);
125 begin = jq_obj.data ('begin');
126 end = jq_obj.data ('end');
127 if ((begin == null) || (end == null))
128 return (zoom_day (graph_id));
130 begin = new Number (begin);
131 end = new Number (end);
133 diff = end - begin;
134 if ((diff <= 300) && (factor_begin > 0.0) && (factor_end < 0.0))
135 return (true);
137 jq_obj.data ('begin', begin + (diff * factor_begin));
138 jq_obj.data ('end', end + (diff * factor_end));
140 return (zoom_redraw (jq_obj));
141 }
143 function zoom_earlier (graph_id) /* {{{ */
144 {
145 return (zoom_relative (graph_id, -0.2, -0.2));
146 } /* }}} function zoom_earlier */
148 function zoom_later (graph_id) /* {{{ */
149 {
150 return (zoom_relative (graph_id, +0.2, +0.2));
151 } /* }}} function zoom_later */
153 function zoom_in (graph_id) /* {{{ */
154 {
155 return (zoom_relative (graph_id, +0.2, -0.2));
156 } /* }}} function zoom_earlier */
158 function zoom_out (graph_id) /* {{{ */
159 {
160 return (zoom_relative (graph_id, (-1.0 / 3.0), (1.0 / 3.0)));
161 } /* }}} function zoom_earlier */
163 $(document).ready(function() {
164 /* $("#layout-middle-right").html ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>"); */
165 $("#search-form").append ("<ul id=\"search-suggest\" class=\"graph_list\"></ul>");
166 $("#search-suggest").hide ();
168 $("#search-input").blur (function()
169 {
170 window.setTimeout (function ()
171 {
172 $("#search-suggest").hide ();
173 }, 500);
174 });
176 $("#search-input").focus (function()
177 {
178 var term = $("#search-input").val ();
179 if (term.length < 2)
180 {
181 $("#search-suggest").hide ();
182 }
183 else
184 {
185 $("#search-suggest").show ();
186 }
187 });
189 $("#search-input").keyup (function()
190 {
191 update_search_suggestions ();
192 });
194 var graph_count = 0;
195 $(".graph-img").each (function (index, elem)
196 {
197 var id = "graph" + graph_count;
198 graph_count++;
200 $(this).find ("img").each (function (img_index, img_elem)
201 {
202 var base_url;
204 $(this).attr ("id", id);
206 base_url = $(this).attr ("src").replace (/;(begin|end)=[^;]*/g, '');
207 $(this).data ("base_url", base_url);
208 });
210 $(this).append ("<div class=\"graph-buttons presets\">"
211 + "<div class=\"graph-button\" onClick=\"zoom_hour ('"+id+"');\">H</div>"
212 + "<div class=\"graph-button\" onClick=\"zoom_day ('"+id+"');\">D</div>"
213 + "<div class=\"graph-button\" onClick=\"zoom_week ('"+id+"');\">W</div>"
214 + "<div class=\"graph-button\" onClick=\"zoom_month ('"+id+"');\">M</div>"
215 + "<div class=\"graph-button\" onClick=\"zoom_year ('"+id+"');\">Y</div>"
216 + "<div class=\"graph-button\" >!</div>"
217 + "</div>"
218 + "<div class=\"graph-buttons navigation\">"
219 + "<div class=\"graph-button\" onClick=\"zoom_earlier ('"+id+"');\">←</div>"
220 + "<div class=\"graph-button\" onClick=\"zoom_out ('"+id+"');\">−</div>"
221 + "<div class=\"graph-button\" onClick=\"zoom_in ('"+id+"');\">+</div>"
222 + "<div class=\"graph-button\" onClick=\"zoom_later ('"+id+"');\">→</div>"
223 + "</div>"
224 );
225 });
226 });
228 /* vim: set sw=2 sts=2 et fdm=marker : */