lsfilter: Use server's timezone
[ninja.git] / modules / lsfilter / media / js / lib.js
blob05268485a314c7874fb35d6465d82e999f75636f
1 function icon12(name, title, link)
3         var img = $('<span />');
4         img.addClass('icon-12');
5         img.addClass('x12-' + name);
6         if (title) img.attr('title', title);
7         if (link) {
8                 img = link.clone().append(img);
9                 img.css('border', '0');
10         }
11         return img;
13 function icon16(name, title, link, base)
15         if( !base ) base = 'x16';
16         var img = $('<span />');
17         img.addClass('icon-16');
18         img.addClass(base + '-' + name);
19         if (title) img.attr('title', title);
20         if (link) {
21                 img = link.append(img);
22                 img.css('border', '0');
23         }
24         return img;
26 function icon(url, link)
28         var img = $('<img />');
29         img.attr('src', _logo_path + url); // FIXME
30         img.css('height', '16px');
31         img.css('width', '16px');
32         if (link) {
33                 img = link.append(img);
34                 img.css('border', '0');
35         }
36         return img;
38 function link(rel_url, args)
40         var get_data = "";
41         var delim = "?";
42         for ( var key in args) {
43                 get_data += delim + key + "=" + encodeURIComponent(args[key]);
44                 delim = "&";
45         }
47         var el = $('<a />');
48         el.attr('href', _site_domain + _index_page + "/" + rel_url + get_data);
49         return el;
51 function link_fnc(fnc)
53         return $('<a />').click(fnc);
55 function link_query(query)
57         var link = $('<a />');
58         link.attr('href', _site_domain + _index_page + '/listview?q='
59                         + encodeURIComponent(query));
60         link.attr('data-query', query);
61         link.addClass('query_link');
62         return link;
64 function extinfo_link(args)
66         return link('extinfo/details', args);
68 function format_timestamp(timestamp)
70         // remember: server's offset is local - UTC, client's offset is UTC - local,
71         // so the sign should be the same.
72         var dateobj = new Date((timestamp + _server_utc_offset) * 1000);
73         dateobj = new Date(dateobj.getTime() + (dateobj.getTimezoneOffset() * 60000));
74         var ret = dateobj.format(_date_format);
75         dateobj = null;
76         return ret;
78 function format_interval(interval)
80         if (interval < 0) return _('N/A');
81         var str = "";
82         if (interval % 60 !== 0) str = (interval % 60) + "s " + str;
83         interval = Math.floor(interval / 60);
84         if (interval % 60 !== 0) str = (interval % 60) + "m " + str;
85         interval = Math.floor(interval / 60);
86         if (interval % 24 !== 0) str = (interval % 24) + "h " + str;
87         interval = Math.floor(interval / 24);
88         if (interval !== 0) str = (interval) + "d " + str;
89         return str;
92 function comment_icon( host, service ) {
93         var obj_name = service ? host+';'+service : host;
94         var query = '[comments] host.name="' + host + '"' + (service?' and service.description="'+service+'"':'');
95         var link_data = {};
96         link_data.host = host;
97         if(service)
98                 link_data.service = service;
100         return extinfo_link(link_data)
101                 .append(icon16('add-comment', _('Comments')))
102                 .css('border', '0px')
103                 .qtip($.extend(true, {}, qtip_default, {
104                         content: {
105                                 text: function(ev, api) {
106                                         $.ajax({
107                                                 url: _site_domain + _index_page + "/ajax/fetch_comments/",
108                                                 data: {host: obj_name}
109                                         })
110                                         .done(function(html) {
111                                                 api.set('content.text', html);
112                                         })
113                                         .fail(function(xhr, status, error) {
114                                                 api.set('content.text', status + ': ' + error);
115                                         });
117                                         return '<img src="' + _site_domain + loading_img + '" alt="' + _loading_str + '" />';
118                                 }
119                         }
120                 }));
123 function pnp_popup(elem, args)
125         var get_data = [];
126         for ( var key in args) {
127                 get_data.push(key + "=" + encodeURIComponent(args[key].replace(/[ :\/\\]/g, "_")));
128         }
130         $(elem).qtip($.extend(true, {}, qtip_default, {
131                 content: {
132                         text: function(ev, api) {
133                                 $.ajax({
134                                         url: _site_domain + _index_page + "/ajax/pnp_image/",
135                                         data: {param: get_data.join("&")},
136                                         type: 'POST'
137                                 })
138                                 .done(function(html) {
139                                         api.set('content.text', html);
140                                 })
141                                 .fail(function(xhr, status, error) {
142                                         api.set('content.text', status + ': ' + error);
143                                 });
145                                 return '<img src="' + _site_domain + loading_img + '" alt="' + _loading_str + '" />';
146                         }
147                 }
148         }));
151 jQuery.fn.update_text = function(text) {
152         if( _escape_html_tags ) {
153                 return this.text(text);
154         } else {
155                 return this.html(text);
156         }
159 jQuery.fn.querylink = function(text) {
160         if( _escape_html_tags ) {
161                 return this.text(text);
162         } else {
163                 return this.html(text);
164         }
167 jQuery.fn.replaceContent = function(new_data) {
168         this.empty();
169         this.append(new_data);
170         return this;
178 function render_summary_state(ul, state, stats, substates)
180         if (stats.stats[state] === 0) return;
182         var li = $('<li />').append(
183                         link_query(stats.queries[state]).append(icon16('shield-' + state))
184                                         .append(
185                                                         $('<span />')
186                                                                         .text(stats.stats[state] + " " + state)));
188         var delim = ' ( ';
189         var suffix = '';
191         for ( var tag in substates) {
192                 var key = state + tag;
193                 var type = substates[tag];
195                 if (stats.stats[key]) {
196                         li.append(delim);
197                         li.append(link_query(stats.queries[key]).text(
198                                         stats.stats[key] + ' ' + type));
199                         delim = ', ';
200                         suffix = ' ) ';
201                 }
202         }
204         li.append(suffix);
206         ul.append(li);
209 function render_service_status_summary(stats)
211         var ul = $('<ul class="listview-summary" />');
213         render_summary_state(ul, 'ok', stats, {});
214         render_summary_state(ul, 'warning', stats, {
215                 '_and_ack': _('acknowledged'),
216                 '_and_disabled_active': _('disabled active'),
217                 '_and_scheduled': _('scheduled'),
218                 '_and_unhandled': _('unhandled'),
219                 '_on_down_host': _('on down host')
220         });
221         render_summary_state(ul, 'critical', stats, {
222                 '_and_ack': _('acknowledged'),
223                 '_and_disabled_active': _('disabled active'),
224                 '_and_scheduled': _('scheduled'),
225                 '_and_unhandled': _('unhandled'),
226                 '_on_down_host': _('on down host')
227         });
228         render_summary_state(ul, 'unknown', stats, {
229                 '_and_ack': _('acknowledged'),
230                 '_and_disabled_active': _('disabled active'),
231                 '_and_scheduled': _('scheduled'),
232                 '_and_unhandled': _('unhandled'),
233                 '_on_down_host': _('on down host')
234         });
235         render_summary_state(ul, 'pending', stats, {});
237         return ul;
240 function render_host_status_summary(stats)
242         var ul = $('<ul class="listview-summary" />');
244         render_summary_state(ul, 'up', stats, {});
245         render_summary_state(ul, 'down', stats, {});
246         render_summary_state(ul, 'unreachable', stats, {});
247         render_summary_state(ul, 'pending', stats, {});
249         return ul;
252 var listview_multi_select_header = $('<input type="checkbox" id="select_all" class="listview_multiselect_checkbox_all" />');
254 var listview_multi_select_cell_renderer = function(args)
256         var checkbox = $(
257                         '<input type="checkbox" name="object_select[]" class="listview_multiselect_checkbox" />')
258                         .attr('value', args.obj.key);
259         if ( lsfilter_multiselect.box_selected(args.obj.key) ) {
260                 checkbox.prop('checked', true);
261                 if (args.row.hasClass('odd'))
262                         args.row.addClass('selected_odd');
263                 else
264                         args.row.addClass('selected_even');
265         }
266         return $('<td style="width: 1em; padding: 0 3px" />').append(checkbox);