File: class.cswitcher.js

package info (click to toggle)
zabbix 1%3A3.0.7%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 60,008 kB
  • ctags: 38,245
  • sloc: php: 125,527; ansic: 120,253; sql: 40,319; sh: 5,620; makefile: 1,138; java: 957; cpp: 211; perl: 41; xml: 29
file content (122 lines) | stat: -rw-r--r-- 3,386 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
** Zabbix
** Copyright (C) 2001-2016 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
**/

var CSwitcher = Class.create({
	switcherName : '',
	switchers : {},

	initialize : function(name) {
		this.switcherName = name;

		var element = $(this.switcherName),
			toggles = $$('button[data-switcherid]'),
			switcherids = cookie.readArray(this.switcherName),
			state_all = 0;

		if (element == null) {
			return;
		}

		addListener(element, 'click', this.showHide.bindAsEventListener(this));

		for (var i = 0; i < toggles.length; i++) {
			addListener(toggles[i], 'click', this.showHide.bindAsEventListener(this));

			var switcherid = toggles[i].getAttribute('data-switcherid');

			this.switchers[switcherid] = {};
			this.switchers[switcherid]['state'] = (switcherids.indexOf(switcherid) != -1 ? 1 : 0);

			if (this.switchers[switcherid]['state'] == 1) {
				toggles[i].firstChild.className = 'arrow-down';

				var elements = $$('tr[data-parentid=' + switcherid + ']');

				for (var j = 0; j < elements.length; j++) {
					elements[j].style.display = '';
				}

				state_all = 1;
			}
		}

		if (state_all == 1) {
			element.firstChild.className = 'arrow-down';
		}

		this.storeCookie();
	},

	showHide : function(e) {
		PageRefresh.restart();

		var obj = Event.element(e);

		if (obj.className != 'treeview') {
			obj = obj.parentElement;
		}

		var switcherid = obj.getAttribute('data-switcherid'),
			state = (obj.firstChild.className == 'arrow-right' ? 1 : 0),
			state_all = (state == 1 ? 1 : 0);

		obj.firstChild.className = (state == 1 ? 'arrow-down' : 'arrow-right');

		var toggles = $$(switcherid == null ? 'button[data-switcherid]' : 'button[data-switcherid=' + switcherid + ']');

		for (var i = 0; i < toggles.length; i++) {
			var toggle_switcherid = toggles[i].getAttribute('data-switcherid');

			if (this.switchers[toggle_switcherid]['state'] != state) {
				this.switchers[toggle_switcherid]['state'] = state;
				toggles[i].firstChild.className = (state == 1 ? 'arrow-down' : 'arrow-right');

				var elements = $$('tr[data-parentid=' + toggle_switcherid + ']');

				for (var j = 0; j < elements.length; j++) {
					elements[j].style.display = (state == 1 ? '' : 'none');
				}
			}
		}

		if (state_all != 1) {
			for (var i in this.switchers) {
				if (this.switchers[i]['state'] == 1) {
					state_all = 1;
				}
			}
		}

		$(this.switcherName).firstChild.className = (state_all == 1 ? 'arrow-down' : 'arrow-right');

		this.storeCookie();
	},

	storeCookie : function() {
		var storeArray = [];

		for (var i in this.switchers) {
			if (this.switchers[i]['state'] == 1) {
				storeArray.push(i);
			}
		}

		cookie.createArray(this.switcherName, storeArray);
	}
});