1<?php 2/* 3** Zabbix 4** Copyright (C) 2001-2021 Zabbix SIA 5** 6** This program is free software; you can redistribute it and/or modify 7** it under the terms of the GNU General Public License as published by 8** the Free Software Foundation; either version 2 of the License, or 9** (at your option) any later version. 10** 11** This program is distributed in the hope that it will be useful, 12** but WITHOUT ANY WARRANTY; without even the implied warranty of 13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14** GNU General Public License for more details. 15** 16** You should have received a copy of the GNU General Public License 17** along with this program; if not, write to the Free Software 18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19**/ 20 21 22/** 23 * @var CView $this 24 */ 25 26?> 27<script type="text/javascript"> 28 jQuery(function($) { 29 function hostPage() { 30 let filter_options = <?= json_encode($data['filter_options']) ?>; 31 32 this.refresh_url = new Curl('<?= $data['refresh_url'] ?>', false); 33 this.refresh_interval = <?= $data['refresh_interval'] ?>; 34 this.running = false; 35 this.timeout = null; 36 this.deferred = null; 37 38 const url = new Curl('zabbix.php', false); 39 url.setArgument('action', 'host.view.refresh'); 40 this.refresh_simple_url = url.getUrl(); 41 42 if (filter_options) { 43 this.refresh_counters = this.createCountersRefresh(1); 44 this.filter = new CTabFilter($('#monitoring_hosts_filter')[0], filter_options); 45 this.filter.on(TABFILTER_EVENT_URLSET, (ev) => { 46 this.refresh_url = new Curl('', false); 47 48 this.unscheduleRefresh(); 49 this.refresh(); 50 51 var filter_item = this.filter._active_item; 52 53 if (this.filter._active_item.hasCounter()) { 54 $.post(this.refresh_simple_url, { 55 filter_counters: 1, 56 counter_index: filter_item._index 57 }).done((json) => { 58 if (json.filter_counters) { 59 filter_item.updateCounter(json.filter_counters.pop()); 60 } 61 }); 62 } 63 }); 64 } 65 } 66 67 hostPage.prototype = { 68 createCountersRefresh: function(timeout) { 69 if (this.refresh_counters) { 70 clearTimeout(this.refresh_counters); 71 this.refresh_counters = null; 72 } 73 74 return setTimeout(() => this.getFiltersCounters(), timeout); 75 }, 76 getFiltersCounters: function() { 77 return $.post(this.refresh_simple_url, { 78 filter_counters: 1 79 }).done((json) => { 80 if (json.filter_counters) { 81 this.filter.updateCounters(json.filter_counters); 82 } 83 }).always(() => { 84 if (this.refresh_interval > 0) { 85 this.refresh_counters = this.createCountersRefresh(this.refresh_interval); 86 } 87 }); 88 }, 89 getCurrentForm: function() { 90 return $('form[name=host_view]'); 91 }, 92 addMessages: function(messages) { 93 $('.wrapper main').before(messages); 94 }, 95 removeMessages: function() { 96 $('.wrapper .msg-bad').remove(); 97 }, 98 refresh: function() { 99 this.setLoading(); 100 101 const params = this.refresh_url.getArgumentsObject(); 102 const exclude = ['action', 'filter_src', 'filter_show_counter', 'filter_custom_time', 'filter_name']; 103 const post_data = Object.keys(params) 104 .filter(key => !exclude.includes(key)) 105 .reduce((post_data, key) => { 106 post_data[key] = (typeof params[key] === 'object') 107 ? [...params[key]].filter(i => i) 108 : params[key]; 109 return post_data; 110 }, {}); 111 112 this.deferred = $.ajax({ 113 url: this.refresh_simple_url, 114 data: post_data, 115 type: 'post', 116 dataType: 'json' 117 }); 118 119 return this.bindDataEvents(this.deferred); 120 }, 121 setLoading: function() { 122 this.getCurrentForm().addClass('is-loading is-loading-fadein delayed-15s'); 123 }, 124 clearLoading: function() { 125 this.getCurrentForm().removeClass('is-loading is-loading-fadein delayed-15s'); 126 }, 127 doRefresh: function(body) { 128 this.getCurrentForm().replaceWith(body); 129 }, 130 bindDataEvents: function(deferred) { 131 var that = this; 132 133 deferred 134 .done(function(response) { 135 that.onDataDone.call(that, response); 136 }) 137 .fail(function(jqXHR) { 138 that.onDataFail.call(that, jqXHR); 139 }) 140 .always(this.onDataAlways.bind(this)); 141 142 return deferred; 143 }, 144 onDataDone: function(response) { 145 this.clearLoading(); 146 this.removeMessages(); 147 this.doRefresh(response.body); 148 149 if ('messages' in response) { 150 this.addMessages(response.messages); 151 } 152 }, 153 onDataFail: function(jqXHR) { 154 // Ignore failures caused by page unload. 155 if (jqXHR.status == 0) { 156 return; 157 } 158 159 this.clearLoading(); 160 161 var messages = $(jqXHR.responseText).find('.msg-global'); 162 163 if (messages.length) { 164 this.getCurrentForm().html(messages); 165 } 166 else { 167 this.getCurrentForm().html(jqXHR.responseText); 168 } 169 }, 170 onDataAlways: function() { 171 if (this.running) { 172 this.deferred = null; 173 this.scheduleRefresh(); 174 } 175 }, 176 scheduleRefresh: function() { 177 this.unscheduleRefresh(); 178 179 if (this.refresh_interval > 0) { 180 this.timeout = setTimeout((function() { 181 this.timeout = null; 182 this.refresh(); 183 }).bind(this), this.refresh_interval); 184 } 185 }, 186 unscheduleRefresh: function() { 187 if (this.timeout !== null) { 188 clearTimeout(this.timeout); 189 this.timeout = null; 190 } 191 192 if (this.deferred) { 193 this.deferred.abort(); 194 } 195 }, 196 start: function() { 197 this.running = true; 198 this.refresh(); 199 } 200 }; 201 202 window.host_page = new hostPage(); 203 window.host_page.start(); 204 }); 205</script> 206