Welcome to mirror list, hosted at ThFree Co, Russian Federation.

adm.regexprs.edit.js.php « js « views « include « php « frontends - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3991a6ae8798cd818d0db9ed492228434909726b (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<script type="text/x-jquery-tmpl" id="expressionRow">
	<tr id="exprRow_#{id}">
		<td class="pre-wrap break-lines">#{expression}</td>
		<td>#{type}</td>
		<td>#{case_sensitive}</td>
		<td class="nowrap">
			<button class="input link_menu exprEdit" type="button" data-id="#{id}"><?php echo _('Edit'); ?></button>&nbsp;
			<button class="input link_menu exprRemove" type="button" data-id="#{id}"><?php echo _('Remove'); ?></button>
		</td>
	</tr>
</script>

<script type="text/x-jquery-tmpl" id="testTableRow">
	<tr class="even_row">
		<td class="pre-wrap break-lines">#{expression}</td>
		<td>#{type}</td>
		<td><span class="bold #{resultClass}">#{result}</span></td>
	</tr>
</script>

<script type="text/x-jquery-tmpl" id="testCombinedTableRow">
	<tr class="odd_row">
		<td colspan="2"><?php echo _('Combined result'); ?></td>
		<td><span class="bold #{resultClass}">#{result}</span></td>
	</tr>
</script>

<script>
	(function($) {
		/**
		 * Class for single expression from global regular expression.
		 * @constructor
		 *
		 * @param {Object} expression Expression data.
		 *                 If expression has 'expressionid' it means that it exists in DB,
		 *                 otherwise it's treated as new expression.
		 *
		 * @property {String} id Unique id of expression.
		 *                    For new expression it's generated, for existing it's equal to 'expressionid'.
		 * @property {Object} data Expression data same as in DB table.
		 */
		function Expression(expression) {
			this.data = expression;

			this.id = (typeof expression.expressionid === 'undefined')
				? getUniqueId()
				: expression.expressionid;

			this.render(true);
		}

		Expression.prototype = {

			/**
			 * Template for expression in expressions list.
			 *
			 * @type {Object}
			 */
			expressionRowTpl: new Template($('#expressionRow').html()),

			/**
			 * Render expression row in list of expressions.
			 *
			 * @param {Boolean} isNew If true it appends row to list, otherwise it search for expression row and replace it.
			 */
			render: function(isNew) {
				var tplData = {
					id: this.id,
					expression: this.data.expression,
					type: this.type2str(),
					case_sensitive: this.case2str()
				};

				if (isNew) {
					$('#exprTable tr.footer').before(this.expressionRowTpl.evaluate(tplData));
				}
				else {
					$('#exprRow_' + this.id).replaceWith(this.expressionRowTpl.evaluate(tplData));
				}
			},

			/**
			 * Remove expression row.
			 */
			remove: function() {
				$('#exprRow_' + this.id).remove();
			},

			/**
			 * Update expression 'data' property with new values and rerender expression row.
			 *
			 * @param {Object} data New expression data values
			 */
			update: function(data) {
				$.extend(this.data, data);
				this.render();
			},

			/**
			 * Converts expression_type numeric value to string.
			 *
			 * @return {String}
			 */
			type2str: function() {
				var str;

				switch (+this.data.expression_type) {
					case <?php echo EXPRESSION_TYPE_INCLUDED; ?>:
						str = <?php echo CJs::encodeJson(_('Character string included')); ?>;
						break;
					case <?php echo EXPRESSION_TYPE_ANY_INCLUDED; ?>:
						str = <?php echo CJs::encodeJson(_('Any character string included')); ?>;
						break;
					case <?php echo EXPRESSION_TYPE_NOT_INCLUDED; ?>:
						str = <?php echo CJs::encodeJson(_('Character string not included')); ?>;
						break;
					case <?php echo EXPRESSION_TYPE_TRUE; ?>:
						str = <?php echo CJs::encodeJson(_('Result is TRUE')); ?>;
						break;
					case <?php echo EXPRESSION_TYPE_FALSE; ?>:
						str = <?php echo CJs::encodeJson(_('Result is FALSE')); ?>;
						break;
				}

				if (+this.data.expression_type === <?php echo EXPRESSION_TYPE_ANY_INCLUDED; ?>) {
					str += ' (' + <?php echo CJs::encodeJson(_('delimiter')); ?> + '="' + this.data.exp_delimiter + '")';
				}

				return str;
			},

			/**
			 * Converts expression case_sensitive numeric value to string.
			 *
			 * @return {String}
			 */
			case2str: function() {
				if (+this.data.case_sensitive) {
					return <?php echo CJs::encodeJson(_('Yes')); ?>;
				}
				else {
					return <?php echo CJs::encodeJson(_('No')); ?>;
				}
			},

			/**
			 * Compare with object.
			 *
			 * @param {Object} obj
			 *
			 * @return {Boolean}
			 */
			equals: function(obj) {
				return (this.data.expression === obj.expression
						&& this.data.expression_type === obj.expression_type
						&& this.data.case_sensitive === obj.case_sensitive
						&& this.data.exp_delimiter === obj.exp_delimiter);
			}
		};

		/**
		 * Object to manage expression related GUI elements.
		 * @type {Object}
		 */
		window.zabbixRegExp = {

			/**
			 * List of Expression objects with keys equal to Expression.id.
			 * @type {Object}
			 */
			expressions: {},

			/**
			 * When upen expression form, it holds expression id if we update any or null if we create new.
			 * @type {String|Null}
			 */
			selectedID: null,

			/**
			 * Template for expression row of testing results table.
			 * @type {String}
			 */
			testTableRowTpl: new Template($('#testTableRow').html()),

			/**
			 * Template for combined result row in testing results table.
			 * @type {String}
			 */
			testCombinedTableRowTpl: new Template($('#testCombinedTableRow').html()),

			/**
			 * Add expressions to manipulate with.
			 * For each expression data new Expression object is created.
			 *
			 * @param {Array} expressions List of expressions with DB data
			 */
			addExpressions: function(expressions) {
				var expr;

				for (var i = 0, ln = expressions.length; i < ln; i++) {
					expr = new Expression(expressions[i]);
					this.expressions[expr.id] = expr;
				}
			},

			/**
			 * Validate expression data.
			 *  - expression cannot be empty
			 *  - expression must be unique
			 *
			 * @param {Object} data
			 */
			validateExpression: function(data) {
				if (data.expression === '') {
					alert(<?php echo CJs::encodeJson(_('Expression cannot be empty')); ?>);
					return false;
				}

				for (var id in this.expressions) {
					// if we update expression, no error if equals itself
					if (id != this.selectedID && this.expressions[id].equals(data)) {
						alert(<?php echo CJs::encodeJson(_('Identical expression already exists')); ?>);
						return false;
					}
				}

				return true;
			},

			/**
			 * Show expression edit form.
			 *
			 * @param {String[]} id Id of expression which data should be shown in form.
			 *                      If id is not passed, form is filled with default values
			 *                      and on save new expression should be created.
			 */
			showForm: function(id) {
				var data;

				if (typeof id === 'undefined') {
					data = {
						expression: '',
						expression_type: '0',
						exp_delimiter: ',',
						case_sensitive: '1'
					};
					this.selectedID = null;

					$('#saveExpression').val(<?php echo CJs::encodeJson(_('Add')); ?>);
				}
				else {
					data = this.expressions[id].data;
					this.selectedID = id;

					$('#saveExpression').val(<?php echo CJs::encodeJson(_('Update')); ?>);
				}

				$('#expressionNew').val(data.expression);
				$('#typeNew').val(data.expression_type);
				$('#delimiterNew').val(data.exp_delimiter);
				$('#case_sensitiveNew').prop('checked', +data.case_sensitive);

				// when type is updated fire change event to show/hide delimiter row
				$('#typeNew').change();

				$('#exprForm').removeClass('hidden');
			},

			/**
			 * Hide expression form.
			 */
			hideForm: function() {
				$('#exprForm').addClass('hidden');
			},

			/**
			 * Either update data of existing expression or create new expression with data in form.
			 */
			saveForm: function() {
				var data = {
					expression: $('#expressionNew').val(),
					expression_type: $('#typeNew').val(),
					exp_delimiter: $('#delimiterNew').val(),
					case_sensitive: $('#case_sensitiveNew').prop('checked') ? '1' : '0'
				};

				if (this.validateExpression(data)) {
					if (this.selectedID === null) {
						this.addExpressions([data]);
					}
					else {
						this.expressions[this.selectedID].update(data);
					}

					this.hideForm();
				}
			},

			/**
			 * Remove expression.
			 *
			 * @param {String} id Id of expression
			 */
			removeExpression: function(id) {
				this.expressions[id].remove();
				delete this.expressions[id];
			},

			/**
			 * Send all expressions data to server with test string.
			 *
			 * @param {String} string Test string to test expression against
			 */
			testExpressions: function(string) {
				var url,
					ajaxData = {
						testString: string,
						expressions: {}
					};

				if ($.isEmptyObject(this.expressions)) {
					$('#testResultTable tr:not(.header)').remove();
				}
				else {
					url = new Curl();

					$('#testResultTable').css({opacity: 0.5});
					$('#testPreloader').show();

					for (var id in this.expressions) {
						ajaxData.expressions[id] = this.expressions[id].data;
					}

					$.post(
						'adm.regexps.php?output=ajax&ajaxaction=test&sid=' + url.getArgument('sid'),
						{ajaxdata: ajaxData},
						$.proxy(this.showTestResults, this),
						'json'
					);
				}
			},

			/**
			 * Update test results table with data received form server.
			 *
			 * @param {Object} response ajax response
			 */
			showTestResults: function(response) {
				var tplData, expr, exprResult, hasErrors;

				$('#testResultTable tr:not(.header)').remove();

				hasErrors = false;

				for (var id in this.expressions) {
					var result;
					expr = this.expressions[id];
					exprResult = response.data.expressions[id];

					if (response.data.errors[id]) {
						hasErrors = true;
						result = response.data.errors[id];
					}
					else {
						result = exprResult ? <?php echo CJs::encodeJson(_('TRUE')); ?> : <?php echo CJs::encodeJson(_('FALSE')); ?>;
					}

					tplData = {
						expression: expr.data.expression,
						type: expr.type2str(),
						result: result,
						resultClass: exprResult ? 'green' : 'red'
					};

					$('#testResultTable').append(this.testTableRowTpl.evaluate(tplData));
				}

				if (hasErrors) {
					tplData = {
						resultClass: 'red',
						result: <?php echo CJs::encodeJson(_('UNKNOWN')); ?>
					};
				}
				else {
					tplData = {
						resultClass: response.data.final ? 'green' : 'red',
						result: response.data.final ? <?php echo CJs::encodeJson(_('TRUE')); ?> : <?php echo CJs::encodeJson(_('FALSE')); ?>
					};
				}

				$('#testResultTable').append(this.testCombinedTableRowTpl.evaluate(tplData));
				$('#testResultTable').css({opacity: 1});
				$('#testPreloader').hide();
			}
		};
	}(jQuery));

	jQuery(function($) {
		$('#exprTable').on('click', 'button.exprRemove', function() {
			zabbixRegExp.removeExpression($(this).attr('data-id'));
		});

		$('#exprTable').on('click', 'input.exprAdd', function() {
			zabbixRegExp.showForm();
		});

		$('#exprTable').on('click', 'button.exprEdit', function() {
			zabbixRegExp.showForm($(this).attr('data-id'));
		});

		$('#saveExpression').click(function() {
			zabbixRegExp.saveForm();
		});

		$('#cancelExpression').click(function() {
			zabbixRegExp.hideForm();
		});

		$('#testExpression, #tab_test').click(function() {
			zabbixRegExp.testExpressions($('#test_string').val());
		});

		// on submit we need to add all expressions data as hidden fields to form
		$('#zabbixRegExpForm').submit(function() {
			var form = $('#zabbixRegExpForm'),
				expr,
				counter = 0;

			for (var id in zabbixRegExp.expressions) {
				expr = zabbixRegExp.expressions[id].data;

				for (var fieldName in expr) {
					$('<input>').attr({
						type: 'hidden',
						name: 'expressions[' + counter + '][' + fieldName + ']'
					}).val(expr[fieldName]).appendTo(form);
				}

				counter++;
			}
		});

		// on clone we remove regexpid hidden field and also expressionid from expressions
		// it's needed because after clone all expressions should be added as new for cloned reg. exp
		$('#clone').click(function() {
			$('#regexpid, #clone, #delete').remove();
			$('#cancel').addClass('ui-corner-left');

			for (var id in zabbixRegExp.expressions) {
				delete zabbixRegExp.expressions[id].data['expressionid'];
			}
		});

		// handler for type select in form, show/hide delimiter select
		$('#typeNew').change(function() {
			if ($(this).val() === '<?php echo EXPRESSION_TYPE_ANY_INCLUDED; ?>') {
				$('#delimiterNewRow').show();
			}
			else {
				$('#delimiterNewRow').hide();
			}
		});
	});
</script>