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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/*
** Zabbix
** Copyright (C) 2001-2014 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.
**/
/*
* Automatic checkbox range selection
*/
var chkbxRange = {
startbox: null, // start checkbox obj
chkboxes: {}, // ckbx list
prefix: null, // prefix for cookie name
pageGoName: null, // which checkboxes should be counted by Go button and saved to cookies
selectedIds: {}, // ids of selected objects
goButton: null,
cookieName: null,
init: function() {
// cookie name
var path = new Curl();
var filename = basename(path.getPath(), '.php');
this.cookieName = 'cb_' + filename + (this.prefix ? '_' + this.prefix : '');
this.resetOtherPageCookies();
// initialize checkboxes
var chkboxes = jQuery('.tableinfo .checkbox:not(:disabled)');
if (chkboxes.length > 0) {
for (var i = 0; i < chkboxes.length; i++) {
this.implement(chkboxes[i]);
}
}
// load selected checkboxes from cookies or cache
if (this.pageGoName != null) {
this.selectedIds = cookie.readJSON(this.cookieName);
// check if checkboxes should be selected from cookies
if (!jQuery.isEmptyObject(this.selectedIds)) {
var objectIds = jQuery.map(this.selectedIds, function(id) { return id });
}
// no checkboxes selected from cookies, check browser cache if checkboxes are still checked and update state
else {
var checkedFromCache = jQuery('.tableinfo tr:not(.header) .checkbox:checked:not(:disabled)');
var objectIds = jQuery.map(checkedFromCache, jQuery.proxy(function(checkbox) {
return this.getObjectIdFromName(checkbox.name);
}, this));
}
this.checkObjects(this.pageGoName, objectIds, true);
this.update(this.pageGoName);
}
// bind event to the "Go" button
this.goButton = $('goButton');
if (!is_null(this.goButton)) {
addListener(this.goButton, 'click', this.submitGo.bindAsEventListener(this), false);
}
},
implement: function(obj) {
// skip the "select all" checkbox
if (obj.name.indexOf('all_') > -1) {
return;
}
var objName = this.getObjectFromName(obj.name);
if (typeof(this.chkboxes[objName]) === 'undefined') {
this.chkboxes[objName] = [];
}
this.chkboxes[objName].push(obj);
addListener(obj, 'click', this.handleClick.bindAsEventListener(this), false);
if (objName == this.pageGoName) {
var objId = jQuery(obj).val();
if (isset(objId, this.selectedIds)) {
obj.checked = true;
}
}
},
/**
* Handles a click on one of the checkboxes.
*
* @param e
*/
handleClick: function(e) {
e = e || window.event;
var checkbox = Event.element(e);
PageRefresh.restart();
var object = this.getObjectFromName(checkbox.name);
var objectId = this.getObjectIdFromName(checkbox.name);
// range selection
if ((e.ctrlKey || e.shiftKey) && this.startbox != null) {
this.checkObjectRange(object, this.startbox, checkbox, this.startbox.checked);
}
// an individual checkbox
else {
this.checkObjects(object, [objectId], checkbox.checked);
}
this.update(object);
this.saveCookies(object);
this.startbox = checkbox;
},
/**
* Extracts the name of an object from the name of a checkbox.
*
* @param {string} name
*
* @returns {string}
*/
getObjectFromName: function(name) {
return name.split('[')[0];
},
/**
* Extracts the ID of an object from the name of a checkbox.
*
* @param {string} name
*
* @returns {string}
*/
getObjectIdFromName: function(name) {
var id = name.split('[')[1];
id = id.substring(0, id.lastIndexOf(']'));
return id;
},
/**
* Toggle all checkboxes of the given objects.
*
* Checks all of the checkboxes that belong to these objects and highlights the table row.
*
* @param {string} object
* @param {Array} objectIds array of objects IDs as integers
* @param {bool} checked
*/
checkObjects: function(object, objectIds, checked) {
jQuery.each(this.chkboxes[object], jQuery.proxy(function(i, checkbox) {
var objectId = this.getObjectIdFromName(checkbox.name);
if (objectIds.indexOf(objectId) > -1) {
checkbox.checked = checked;
jQuery(checkbox).closest('tr').toggleClass('selected', checked);
if (checked) {
this.selectedIds[objectId] = objectId;
}
else {
delete this.selectedIds[objectId];
}
}
}, this));
},
/**
* Toggle all objects between the two checkboxes.
*
* @param {string} object
* @param {object} startCheckbox
* @param {object} endCheckbox
* @param {bool} checked
*/
checkObjectRange: function(object, startCheckbox, endCheckbox, checked) {
var checkboxes = this.chkboxes[object];
var startCheckboxIndex = checkboxes.indexOf(startCheckbox);
var endCheckboxIndex = checkboxes.indexOf(endCheckbox);
var start = Math.min(startCheckboxIndex, endCheckboxIndex);
var end = Math.max(startCheckboxIndex, endCheckboxIndex);
var objectIds = [];
for (var i = start; i <= end; i++) {
objectIds.push(this.getObjectIdFromName(checkboxes[i].name));
}
this.checkObjects(object, objectIds, checked);
},
/**
* Toggle all of the checkboxes belonging to the given object group.
*
* @param {string} object
*
* @param {bool} checked
*/
checkObjectAll: function(object, checked) {
// main checkbox exists and is clickable, but other checkboxes may not exist and object may be empty
if (typeof this.chkboxes[object] !== 'undefined') {
var objectIds = jQuery.map(this.chkboxes[object], jQuery.proxy(function(checkbox) {
return this.getObjectIdFromName(checkbox.name);
}, this));
this.checkObjects(object, objectIds, checked);
}
},
/**
* Update the general state after toggling a checkbox.
*
* @param {string} object
*/
update: function(object) {
// update main checkbox state
this.updateMainCheckbox();
if (this.pageGoName == object) {
this.updateGoButton();
}
},
/**
* Update the state of the "Go" controls.
*/
updateGoButton: function() {
var count = 0;
jQuery.each(this.selectedIds, function() {
count++;
});
// update go button
var goButton = jQuery('#goButton');
goButton.val(goButton.val().split(' ')[0] + ' (' + count + ')')
.prop('disabled', count == 0);
jQuery('#action').prop('disabled', count == 0);
},
// check if all checkboxes are selected and select main checkbox, else disable checkbox, select options and button
updateMainCheckbox: function() {
var mainCheckbox = jQuery('.tableinfo .header .checkbox:not(:disabled)');
if (!mainCheckbox.length) {
return;
}
var countAvailable = jQuery('.tableinfo tr:not(.header) .checkbox:not(:disabled)').length;
if (countAvailable > 0) {
var countChecked = jQuery('.tableinfo tr:not(.header) .checkbox:not(:disabled):checked').length;
mainCheckbox = mainCheckbox[0];
mainCheckbox.checked = (countChecked == countAvailable);
if (mainCheckbox.checked) {
jQuery('.tableinfo .header').addClass('selectedMain');
}
else {
jQuery('.tableinfo .header').removeClass('selectedMain');
}
}
else {
mainCheckbox.disabled = true;
}
},
/**
* Save the state of the checkboxes belonging to the given object group in cookies.
*
* @param {string} object
*/
saveCookies: function(object) {
if (this.pageGoName == object) {
cookie.createJSON(this.cookieName, this.selectedIds);
}
},
clearSelectedOnFilterChange: function() {
cookie.eraseArray(this.cookieName);
},
/**
* Reset all selections on other pages.
*/
resetOtherPageCookies: function() {
for (var key in cookie.cookies) {
var cookiePair = key.split('=');
if (cookiePair[0].indexOf('cb_') > -1 && cookiePair[0].indexOf(this.cookieName) == -1) {
cookie.erase(key);
}
}
},
submitGo: function(e) {
e = e || window.event;
var goSelect = $('go');
var confirmText = goSelect.options[goSelect.selectedIndex].getAttribute('confirm');
if (!is_null(confirmText) && !confirm(confirmText)) {
Event.stop(e);
return false;
}
var form = jQuery('#goButton').closest('form');
for (var key in this.selectedIds) {
if (!empty(this.selectedIds[key])) {
create_var(form.name, this.pageGoName + '[' + key + ']', key, false);
}
}
return true;
}
};
|