File: class.overlaycollection.js

package info (click to toggle)
zabbix 1%3A7.0.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 272,688 kB
  • sloc: sql: 946,050; ansic: 389,440; php: 292,698; javascript: 83,388; sh: 5,680; makefile: 3,285; java: 1,420; cpp: 694; perl: 64; xml: 56
file content (138 lines) | stat: -rw-r--r-- 3,427 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/


/**
 * A stack implementation for identified overlay objects.
 *
 * @prop {int} length   Overlay collection size.
 * @prop {array} stack  Array of overlay identifiers.
 * @prop {object} map   Overlay objects keyed by their identifiers.
 */
function OverlayCollection() {
	this.stack = [];
	this.map = {};
}

Object.defineProperty(OverlayCollection.prototype, 'length', {
	get: function () {
		return this.stack.length;
	},
	writeable: false
});

/**
 * Fetches overlay object positioned at top of stack.
 *
 * @return {object|undefined}  Overlay object currently in top of stack.
 */
OverlayCollection.prototype.end = function() {
	return this.getById(this.stack[this.length - 1]);
};

/**
 * Retrieve overlay by it's ID.
 *
 * @param {string} id  Overlay identifier.
 *
 * @return {Overlay|undefined}  Overlay object.
 */
OverlayCollection.prototype.getById = function(id) {
	return this.map[id];
};

/**
 * Adds new overlay to the stack or, if it already exists, moves it to the top of the stack.
 *
 * @param {object} Overlay object.
 */
OverlayCollection.prototype.pushUnique = function(overlay) {
	if (this.map[overlay.dialogueid]) {
		this._restackEnd(overlay.dialogueid);
	}
	else {
		this._write(overlay, this.length);
	}
};

/**
 * Removes an overlay object, returns a reference.
 *
 * @param {string} id  An overlay identifier.
 *
 * @return {object|undefined}  Overlay object that is no longer in this stack.
 */
OverlayCollection.prototype.removeById = function(id) {
	var overlay = this.getById(id);

	if (overlay) {
		delete this.map[id];
		this.stack.splice(this._fetchIndex(id), 1);
	}

	return overlay;
};

/**
 * Get unused overlay id.
 *
 * @return {string}
 */
OverlayCollection.prototype.getNextId = function() {
	var overlayid = Math.random().toString(36).substring(7);

	while (this.stack.indexOf(overlayid) !== -1) {
		overlayid = Math.random().toString(36).substring(7);
	}

	return overlayid;
};

/**
 * Retrieves overlay Z-index by ID.
 *
 * @param {string} id  Overlay object identifier.
 *
 * @throws Error
 *
 * @return {int}  Position in stack.
 */
OverlayCollection.prototype._fetchIndex = function(id) {
	for (var i = this.length - 1; i >= 0; i--) {
		if (this.stack[i] == id) {
			return i;
		}
	}

	throw new Error('Fetching nonexistent overlay: ' + id);
};

/**
 * Moves an overlay to the top of stack.
 *
 * @param {string} id  An overlay identifier.
 */
OverlayCollection.prototype._restackEnd = function(id) {
	this.stack.splice(this._fetchIndex(id), 1);
	this.stack.push(id);
};

/**
 * @param {object} overlay  Overlay object.
 * @param {int} position  Z-index for overlay object.
 */
OverlayCollection.prototype._write = function(overlay, position) {
	this.stack[position] = overlay.dialogueid;
	this.map[overlay.dialogueid] = overlay;
};