Avail feature updated
[ninja.git] / application / media / js / dojo.js
blob82da6b9761eff8f2f2a1e6a852c0f7b18df039d8
1 (function (_site_domain, _index_page) {
3         "use strict";
5         /* QUICKLINK EXTENSION */
6         var uh_prob_title = "Unhandled Problems",
7                 uh_prob_state_element = null;
8         function query_for_states () {
10                 var basepath = _site_domain + _index_page,
11                         link = $('#uh_problems'),
12                         query = link.attr('href');
14                 query = query.split('q=')[1];
16                 $.ajax({
17                         url : basepath + "/listview/fetch_ajax",
18                         dataType : 'json',
19                         data : {
20                                 "query" : query,
21                                 "limit" : 100,
22                                 "columns": ['description']
23                         },
24                         success : function(data) {
26                                 if (data && data.totals.service_all[1] > 0) {
28                                         uh_prob_title = data.totals.service_all[1] + ' unacknowledged services in Critical/Warning state!';
29                                         link.attr('title', uh_prob_title);
31                                         if (uh_prob_state_element) {
32                                                 uh_prob_state_element.remove();
33                                         }
34                                         uh_prob_state_element = $("<span style='margin: 0; position: absolute; color: #000; text-shadow: 0 0 2px #fff; font-weight: bold; font-size: 10px; padding: 1px 1px 0 0; right: 0px; bottom: 0px;' />");
35                                         if(data.totals.service_all[1] < 100 ) {
36                                                 uh_prob_state_element.text(data.totals.service_all[1]);
37                                         }
38                                         link.append(uh_prob_state_element);
40                                         if (data.totals.service_state_critical[1] > 0) {
41                                                 link.find(':first-child').removeClass().addClass('icon-16 x16-shield-critical');
42                                         } else if (data.totals.service_state_warning[1] > 0) {
43                                                 link.find(':first-child').removeClass().addClass('icon-16 x16-shield-warning');
44                                         }
45                                 }
46                         }
47                 });
48         }
50         setInterval(query_for_states, 10000);
51         query_for_states();
53         var global_quicklinks = [];
55         var fix_empty_quicklink_border = function() {
56                 // there's a magical dot (about 1x1 pixel) hanging in the air,
57                 // disturbing probably just me. It's caused by having a quicklinks
58                 // container without any quicklinks
59                 var quicklinks = $('#dojo-quicklink-external');
60                 var tr = quicklinks.parent();
61                 if(quicklinks.find('li').length) {
62                         tr.css('borderWidth', 1);
63                 } else {
64                         tr.css('borderWidth', 0);
65                 }
66         };
68         function quicklinks_save_all () {
69                 $.ajax(_site_domain + _index_page + '/ajax/save_page_setting', {
70                         data: {
71                                 'type': 'dojo-quicklinks',
72                                 'page': 'tac',
73                                 'setting': JSON.stringify(global_quicklinks)
74                         },
75                         type: 'POST',
76                         complete: function() {
77                                 $('#dojo-add-quicklink-href').attr('value','');
78                                 $('#dojo-add-quicklink-title').attr('value','');
79                                 $('#dojo-add-quicklink-icon').attr('value','');
80                                 fix_empty_quicklink_border();
81                         }
82                 });
83         }
85         $('#dojo-add-quicklink').hover(function () {
86                 this.style.opacity = '1.0';
87         }, function () {
88                 this.style.opacity = '0.5';
89         });
91         $('#dojo-icon-container').on('click', 'span', function() {
92                 var span = $(this);
93                 $('#dojo-add-quicklink-icon').val(span.data('icon'));
95                 // we have to change the background of the td, since the span already
96                 // has the icon image as its background
97                 var all_tds = $('#dojo-icon-container td');
98                 all_tds.removeClass('highlight');
99                 span.parents('td').addClass('highlight');
100         });
102         $('#dojo-add-quicklink').fancybox({
103                 titleShow: false,
104                 overlayOpacity: 0,
105                 onComplete: function() {
106                         $('#dojo-quicklink-remove').html('');
107                         for (var i = 0; i < global_quicklinks.length; i += 1) {
108                                 var l = global_quicklinks[i];
109                                 var vid = l.title + ':'+ l.href;
110                                 var quicklink = $('<li><label><input type="checkbox" /><span class="icon-16 x16-'+l.icon+'"></span>' + l.title + '</label> (<a target="_blank" class="external"></a>)</li>');
111                                 quicklink
112                                         .find('input')
113                                                 .attr('value', vid)
114                                                 .attr('id', vid)
115                                                 .attr('title', l.title);
116                                 quicklink
117                                         .find('a')
118                                                 .attr('href', l.href)
119                                                 .text(l.href);
120                                 $('#dojo-quicklink-remove').append(quicklink);
121                         }
122                 },
123                 onClose: function() {
124                         $('#dojo-add-quicklink-href').attr('value','');
125                         $('#dojo-add-quicklink-title').attr('value','');
126                         $('#dojo-add-quicklink-icon').attr('value','');
127                         fix_empty_quicklink_border();
128                 }
129         });
131         $('#dojo-add-quicklink-menu form').submit(function (ev) {
132                 ev.preventDefault();
133                 var href = $('#dojo-add-quicklink-href').attr('value'),
134                                 title = $('#dojo-add-quicklink-title').attr('value'),
135                                 icon = $('#dojo-add-quicklink-icon').attr('value'),
136                                 target = $('#dojo-add-quicklink-target').attr('value'),
137                                 changed = false;
138                 var error = '';
139                 if (href && title && icon) { 
140                         var i = global_quicklinks.length;
141                         for (i; i--;) {
142                                 if (global_quicklinks[i].href === href) {
143                                         error += 'This href is already used in a quicklink. <br />';
144                                 }
145                                 if (global_quicklinks[i].title === title) {
146                                         error += 'This title is already in use, titles must be unique. <br />';
147                                 }
148                         }
149                         if (error.length === 0) {
150                                 global_quicklinks.push({'href': href,'title': title,'icon': icon,'target': target});
151                                 var quicklink = $('<li><a class="image-link"><span class="icon-16 x16-' + icon + '"></span></a></li>');
152                                 quicklink
153                                         .find('a')
154                                                 .attr('target', target)
155                                                 .attr('href', href)
156                                                 .attr('title', title);
157                                 $('#dojo-quicklink-external').append(quicklink);
158                                 changed = true;
159                         } else {
160                                 $.jGrowl(error);
161                                 return;
162                         }
163                 }
164                 $('#dojo-quicklink-remove input[type="checkbox"]').each(function () {
165                         var i = global_quicklinks.length;
166                         var vid = '';
167                         if (this.checked) {
168                                 for (i; i--;) {
169                                         vid = global_quicklinks[i].title + ':' + global_quicklinks[i].href;
170                                         if (this.value === vid) {
171                                                 $('#dojo-quicklink-external li a[title="'+this.title+'"]').parent().remove();
172                                                 global_quicklinks.splice(i, 1);
173                                                 changed = true;
174                                         }
175                                 }
176                         }
178                 });
179                 if (changed)  {
180                         quicklinks_save_all();
181                 }
182                 if(!error) {
183                         $.fancybox.close();
184                 }
185         });
187         $.ajax(_site_domain + _index_page + '/ajax/get_setting', {
188                         data: {
189                                 'type': 'dojo-quicklinks',
190                                 'page': 'tac'
191                         },
192                         type: 'POST',
193                         complete: function() {
194                                 fix_empty_quicklink_border();
195                         },
196                         success: function (obj) {
198                                 var links = [];
200                                 if (obj['dojo-quicklinks']) {
201                                         links = obj['dojo-quicklinks'];
202                                         for (var i = 0; i < links.length; i += 1) {
203                                                 var quicklink = $('<li><a class="image-link"><span class="icon-16 x16-'+links[i].icon+'"></span></a></li>');
204                                                 quicklink
205                                                         .find('a')
206                                                                 .attr('target', links[i].target)
207                                                                 .attr('href', links[i].href)
208                                                                 .attr('title', links[i].title);
210                                                 $('#dojo-quicklink-external').append(quicklink);
211                                         }
212                                 }
213                                 global_quicklinks = links;
214                         }
215                 });
217 }(window._site_domain, window._index_page));