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/x-jquery-tmpl" id="filter-tag-row-tmpl">
28	<?= CTagFilterFieldHelper::getTemplate(); ?>
29</script>
30
31<script type="text/javascript">
32	function latestPage() {
33		this.refresh_url = '<?= $data['refresh_url'] ?>';
34		this.refresh_data = <?= json_encode($data['refresh_data']) ?>;
35		this.refresh_interval = <?= $data['refresh_interval'] ?>;
36		this.running = false;
37		this.timeout = null;
38	}
39
40	latestPage.prototype.getCurrentForm = function() {
41		return $('form[name=items]');
42	};
43
44	latestPage.prototype.addMessages = function(messages) {
45		$('.wrapper main').before(messages);
46	};
47
48	latestPage.prototype.removeMessages = function() {
49		$('.wrapper .msg-bad').remove();
50	};
51
52	latestPage.prototype.refresh = function() {
53		this.setLoading();
54
55		var deferred = $.ajax({
56			url: this.refresh_url,
57			data: this.refresh_data,
58			type: 'post',
59			dataType: 'json'
60		});
61
62		return this.bindDataEvents(deferred);
63	};
64
65	latestPage.prototype.setLoading = function() {
66		this.getCurrentForm().addClass('is-loading is-loading-fadein delayed-15s');
67	};
68
69	latestPage.prototype.clearLoading = function() {
70		this.getCurrentForm().removeClass('is-loading is-loading-fadein delayed-15s');
71	};
72
73	latestPage.prototype.doRefresh = function(body) {
74		this.getCurrentForm().replaceWith(body);
75		chkbxRange.init();
76	};
77
78	latestPage.prototype.bindDataEvents = function(deferred) {
79		var that = this;
80
81		deferred
82			.done(function(response) {
83				that.onDataDone.call(that, response);
84			})
85			.fail(function(jqXHR) {
86				that.onDataFail.call(that, jqXHR);
87			})
88			.always(this.onDataAlways.bind(this));
89
90		return deferred;
91	};
92
93	latestPage.prototype.onDataDone = function(response) {
94		this.clearLoading();
95		this.removeMessages();
96		this.doRefresh(response.body);
97
98		if ('messages' in response) {
99			this.addMessages(response.messages);
100		}
101	};
102
103	latestPage.prototype.onDataFail = function(jqXHR) {
104		// Ignore failures caused by page unload.
105		if (jqXHR.status == 0) {
106			return;
107		}
108
109		this.clearLoading();
110
111		var messages = $(jqXHR.responseText).find('.msg-global');
112
113		if (messages.length) {
114			this.getCurrentForm().html(messages);
115		}
116		else {
117			this.getCurrentForm().html(jqXHR.responseText);
118		}
119	};
120
121	latestPage.prototype.onDataAlways = function() {
122		if (this.running) {
123			this.scheduleRefresh();
124		}
125	};
126
127	latestPage.prototype.scheduleRefresh = function() {
128		this.unscheduleRefresh();
129		this.timeout = setTimeout((function() {
130			this.timeout = null;
131			this.refresh();
132		}).bind(this), this.refresh_interval);
133	};
134
135	latestPage.prototype.unscheduleRefresh = function() {
136		if (this.timeout !== null) {
137			clearTimeout(this.timeout);
138			this.timeout = null;
139		}
140	};
141
142	latestPage.prototype.start = function() {
143		if (this.refresh_interval != 0) {
144			this.running = true;
145			this.scheduleRefresh();
146		}
147	};
148
149	latestPage.prototype.stop = function() {
150		this.running = false;
151		this.unscheduleRefresh();
152	};
153
154	latestPage.prototype.liveFilter = function() {
155		var $filter_hostids = $('#filter_hostids_'),
156			$filter_show_without_data = $('#filter_show_without_data');
157
158		$filter_hostids.on('change', function() {
159			var no_hosts_selected = !$(this).multiSelect('getData').length;
160
161			if (no_hosts_selected) {
162				$filter_show_without_data.prop('checked', true);
163			}
164
165			$filter_show_without_data.prop('disabled', no_hosts_selected);
166		});
167
168		$('#filter-tags')
169			.dynamicRows({template: '#filter-tag-row-tmpl'})
170			.on('afteradd.dynamicRows', function() {
171				var rows = this.querySelectorAll('.form_row');
172				new CTagFilterItem(rows[rows.length - 1]);
173			});
174
175		// Init existing fields once loaded.
176		document.querySelectorAll('#filter-tags .form_row').forEach(row => {
177			new CTagFilterItem(row);
178		});
179	};
180
181	$(function() {
182		window.latest_page = new latestPage();
183		window.latest_page.liveFilter();
184	});
185</script>
186