js: Make helptexts easy to render from js
[ninja.git] / modules / lsfilter / media / js / lib.js
blob29d1bd07e40bb50c0a978276567fea8292c419f3
1 function helpicon(controller, key)
3         var link = $('<a class="helptext_target" style="border: 0" href="#"/>');
4         link.append(icon12('help'));
5         bind_helptext(link, controller, key);
6         return link;
9 function icon12(name, title, link)
11         var img = $('<span />');
12         img.addClass('icon-12');
13         img.addClass('x12-' + name);
14         if (title) img.attr('title', title);
15         if (link) {
16                 img = link.clone().append(img);
17                 img.css('border', '0');
18         }
19         return img;
21 function icon16(name, title, link, base)
23         if( !base ) base = 'x16';
24         var img = $('<span />');
25         img.addClass('icon-16');
26         img.addClass(base + '-' + name);
27         if (title) img.attr('title', title);
28         if (link) {
29                 img = link.append(img);
30                 img.css('border', '0');
31         }
32         return img;
34 function icon(url, link)
36         var img = $('<img />');
37         img.attr('src', _logo_path + url); // FIXME
38         img.css('height', '16px');
39         img.css('width', '16px');
40         if (link) {
41                 img = link.append(img);
42                 img.css('border', '0');
43         }
44         return img;
46 function link(rel_url, args)
48         var get_data = "";
49         var delim = "?";
50         for ( var key in args) {
51                 get_data += delim + key + "=" + encodeURIComponent(args[key]);
52                 delim = "&";
53         }
55         var el = $('<a />');
56         el.attr('href', _site_domain + _index_page + "/" + rel_url + get_data);
57         return el;
59 function link_fnc(fnc)
61         return $('<a />').click(fnc);
63 function link_query(query)
65         var link = $('<a />');
66         link.attr('href', _site_domain + _index_page + '/listview?q='
67                         + encodeURIComponent(query));
68         link.attr('data-query', query);
69         link.addClass('query_link');
70         return link;
72 function extinfo_link(args)
74         return link('extinfo/details', args);
76 function format_timestamp(timestamp)
78         // remember: server's offset is local - UTC, client's offset is UTC - local,
79         // so the sign should be the same.
80         var timestamp_int = parseInt(timestamp);
82         var dateobj = new Date((timestamp_int + _server_utc_offset) * 1000);
83         dateobj = new Date(dateobj.getTime() + (dateobj.getTimezoneOffset() * 60000));
84         var ret = dateobj.format(_date_format);
85         dateobj = null;
86         return ret;
88 function format_interval(interval)
90         if (interval < 0) return _('N/A');
91         var str = "";
92         if (interval % 60 !== 0) str = (interval % 60) + "s " + str;
93         interval = Math.floor(interval / 60);
94         if (interval % 60 !== 0) str = (interval % 60) + "m " + str;
95         interval = Math.floor(interval / 60);
96         if (interval % 24 !== 0) str = (interval % 24) + "h " + str;
97         interval = Math.floor(interval / 24);
98         if (interval !== 0) str = (interval) + "d " + str;
99         return str;
102 function comment_icon( host, service ) {
103         var obj_name = service ? host+';'+service : host;
104         var query = '[comments] host.name="' + host + '"' + (service?' and service.description="'+service+'"':'');
105         var link_data = {};
106         link_data.host = host;
107         if(service)
108                 link_data.service = service;
110         return extinfo_link(link_data)
111                 .append(icon16('add-comment', _('Comments')))
112                 .css('border', '0px')
113                 .qtip($.extend(true, {}, qtip_default, {
114                         content: {
115                                 text: function(ev, api) {
116                                         $.ajax({
117                                                 url: _site_domain + _index_page + "/ajax/fetch_comments/",
118                                                 data: {host: obj_name}
119                                         })
120                                         .done(function(html) {
121                                                 api.set('content.text', html);
122                                         })
123                                         .fail(function(xhr, status, error) {
124                                                 api.set('content.text', status + ': ' + error);
125                                         });
127                                         return '<img src="' + _site_domain + loading_img + '" alt="' + _loading_str + '" />';
128                                 }
129                         }
130                 }));
133 function pnp_popup(elem, args)
135         var get_data = [];
136         for ( var key in args) {
137                 get_data.push(key + "=" + encodeURIComponent(args[key].replace(/[ :\/\\]/g, "_")));
138         }
140         $(elem).qtip($.extend(true, {}, qtip_default, {
141                 content: {
142                         text: function(ev, api) {
143                                 $.ajax({
144                                         url: _site_domain + _index_page + "/ajax/pnp_image/",
145                                         data: {param: get_data.join("&")},
146                                         type: 'POST'
147                                 })
148                                 .done(function(html) {
149                                         api.set('content.text', html);
150                                 })
151                                 .fail(function(xhr, status, error) {
152                                         api.set('content.text', status + ': ' + error);
153                                 });
155                                 return '<img src="' + _site_domain + loading_img + '" alt="' + _loading_str + '" />';
156                         }
157                 }
158         }));
161 jQuery.fn.update_text = function(text) {
162         if( _escape_html_tags ) {
163                 return this.text(text);
164         } else {
165                 return this.html(text);
166         }
169 jQuery.fn.querylink = function(text) {
170         if( _escape_html_tags ) {
171                 return this.text(text);
172         } else {
173                 return this.html(text);
174         }
177 jQuery.fn.replaceContent = function(new_data) {
178         this.empty();
179         this.append(new_data);
180         return this;
188 function render_summary_state(ul, state, stats, substates)
190         if (stats.stats[state] === 0) return;
192         var li = $('<li />').append(
193                         link_query(stats.queries[state]).append(icon16('shield-' + state))
194                                         .append(
195                                                         $('<span />')
196                                                                         .text(stats.stats[state] + " " + state)));
198         var delim = ' ( ';
199         var suffix = '';
201         for ( var tag in substates) {
202                 var key = state + tag;
203                 var type = substates[tag];
205                 if (stats.stats[key]) {
206                         li.append(delim);
207                         li.append(link_query(stats.queries[key]).text(
208                                         stats.stats[key] + ' ' + type));
209                         delim = ', ';
210                         suffix = ' ) ';
211                 }
212         }
214         li.append(suffix);
216         ul.append(li);
219 function render_service_status_summary(stats)
221         var ul = $('<ul class="listview-summary" />');
223         render_summary_state(ul, 'ok', stats, {});
224         render_summary_state(ul, 'warning', stats, {
225                 '_and_ack': _('acknowledged'),
226                 '_and_disabled_active': _('disabled active'),
227                 '_and_scheduled': _('scheduled'),
228                 '_and_unhandled': _('unhandled'),
229                 '_on_down_host': _('on down host')
230         });
231         render_summary_state(ul, 'critical', stats, {
232                 '_and_ack': _('acknowledged'),
233                 '_and_disabled_active': _('disabled active'),
234                 '_and_scheduled': _('scheduled'),
235                 '_and_unhandled': _('unhandled'),
236                 '_on_down_host': _('on down host')
237         });
238         render_summary_state(ul, 'unknown', stats, {
239                 '_and_ack': _('acknowledged'),
240                 '_and_disabled_active': _('disabled active'),
241                 '_and_scheduled': _('scheduled'),
242                 '_and_unhandled': _('unhandled'),
243                 '_on_down_host': _('on down host')
244         });
245         render_summary_state(ul, 'pending', stats, {});
247         return ul;
250 function render_host_status_summary(stats)
252         var ul = $('<ul class="listview-summary" />');
254         render_summary_state(ul, 'up', stats, {});
255         render_summary_state(ul, 'down', stats, {});
256         render_summary_state(ul, 'unreachable', stats, {});
257         render_summary_state(ul, 'pending', stats, {});
259         return ul;
262 var listview_multi_select_header = $('<input type="checkbox" id="select_all" class="listview_multiselect_checkbox_all" />');
264 var listview_multi_select_cell_renderer = function(args)
266         var checkbox = $(
267                         '<input type="checkbox" name="object_select[]" class="listview_multiselect_checkbox" />')
268                         .attr('value', args.obj.key);
269         if ( lsfilter_multiselect.box_selected(args.obj.key) ) {
270                 checkbox.prop('checked', true);
271                 if (args.row.hasClass('odd'))
272                         args.row.addClass('selected_odd');
273                 else
274                         args.row.addClass('selected_even');
275         }
276         return $('<td style="width: 1em; padding: 0 3px" />').append(checkbox);