iobroker.fullcalendar 0.2.2 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1358 +1,1383 @@
1
- var path = location.pathname;
2
- var parts = path.split('/');
3
- parts.splice(-3);
4
-
5
- var socket = io.connect('/', {path: parts.join('/') + '/socket.io'});
6
- var instance = window.location.search.slice(1) || 0;
7
- var common = null; // common information of adapter
8
- var host = null; // host object on which the adapter runs
9
- var changed = false;
10
- var systemConfig;
11
- var certs = [];
12
- var adapter = '';
13
- var onChangeSupported = false;
14
-
15
- var tmp = window.location.pathname.split('/');
16
- adapter = tmp[tmp.length - 2];
17
- var _adapterInstance = 'system.adapter.' + adapter + '.' + instance;
18
-
19
- $(document).ready(function () {
20
- 'use strict';
21
-
22
- if (typeof noConfigDialog === 'undefined') {
23
- // Extend dictionary with standard words for adapter
24
- if (typeof systemDictionary === 'undefined') systemDictionary = {};
25
-
26
- systemDictionary.save = {"en": "Save", "de": "Speichern", "ru": "Сохранить"};
27
- systemDictionary.saveclose = {"en": "Save and close", "de": "Speichern und schließen", "ru": "Сохранить и выйти"};
28
- systemDictionary.none = {"en": "none", "de": "keins", "ru": ""};
29
- systemDictionary.all = {"en": "all", "de": "alle", "ru": "все"};
30
- systemDictionary['Device list'] = {"en": "Device list", "de": "Geräteliste", "ru": "Список устройств"};
31
- systemDictionary['new device'] = {"en": "new device", "de": "Neues Gerät", "ru": "Новое устройство"};
32
- systemDictionary.edit = {"en": "edit", "de": "Ändern", "ru": "Изменить"};
33
- systemDictionary.delete = {"en": "delete", "de": "Löschen", "ru": "Удалить"};
34
- systemDictionary.ok = {"en": "Ok", "de": "Ok", "ru": "Ok"};
35
- systemDictionary.cancel = {"en": "Cancel", "de": "Abbrechen", "ru": "Отмена"};
36
- systemDictionary.Message = {"en": "Message", "de": "Mitteilung", "ru": "Сообщение"};
37
- systemDictionary.close = {"en": "Close", "de": "Schließen", "ru": "Закрыть"};
38
- systemDictionary.htooltip = {"en": "Click for help", "de": "Anclicken", "ru": "Перейти по ссылке"};
39
-
40
- //socket.on('connection', function () {
41
- loadSystemConfig(function () {
42
- if (typeof translateAll === 'function') translateAll();
43
- loadSettings(prepareTooltips);
44
- });
45
- //});
46
- var $body = $('body');
47
- $body.wrapInner('<div style="height: calc(100% - 44px); width: 100%; overflow:auto"></div>');
48
- $body.prepend('<div class="header ui-tabs-nav ui-widget ui-widget-header ui-corner-all" style="padding: 2px" >' +
49
- '<button id="save" class="translateB">save</button>&nbsp;' +
50
- '<button id="saveclose" class="translateB">saveclose</button>&nbsp;' +
51
- '<button id="close" class="translateB" style="float: right;">cancel</button>&nbsp;' +
52
- '</div>');
53
-
54
- $('button#save').button({icons: {primary: 'ui-icon-disk'}}).click(function () {
55
- if (typeof save === 'undefined') {
56
- alert('Please implement save function in your admin/index.html');
57
- return;
58
- }
59
- save(function (obj, common, redirect) {
60
- if (redirect && parent && parent.adapterRedirect) {
61
- parent.adapterRedirect(redirect);
62
- }
63
- saveSettings(obj, common);
64
- });
65
- });
66
- $('button#saveclose').button({icons: {
67
- primary: 'ui-icon-disk',
68
- secondary: 'ui-icon-close'
69
- }}).click(function () {
70
- if (typeof save === 'undefined') {
71
- alert('Please implement save function in your admin/index.html');
72
- return;
73
- }
74
- save(function (obj, common, redirect) {
75
- if (redirect && parent && parent.adapterRedirect) {
76
- parent.adapterRedirect(redirect);
77
- }
78
- saveSettings(obj, common, function () {
79
- window.close();
80
- if (parent && parent.$iframeDialog) {
81
- parent.$iframeDialog.dialog('close');
82
- }
83
- });
84
- });
85
- });
86
- $('button#close').button({icons: {primary: 'ui-icon-close'}}).click(function () {
87
- window.close();
88
- if (typeof parent !== 'undefined' && parent && parent.$iframeDialog) {
89
- parent.$iframeDialog.dialog('close');
90
- }
91
- });
92
- }
93
-
94
- // callback if something changed
95
- function onChange(isChanged) {
96
- onChangeSupported = true;
97
- if (typeof isChanged === 'boolean' && isChanged === false) {
98
- changed = false;
99
- $('#save').button('disable');
100
- $('#saveclose').button('disable');
101
- $('#close .ui-button-text').html(_('close'));
102
- } else {
103
- changed = true;
104
- $('#save').button('enable');
105
- $('#saveclose').button('enable');
106
- $('#close .ui-button-text').html(_('cancel'));
107
- }
108
- }
109
- });
110
-
111
- function saveSettings(native, common, callback) {
112
- if (typeof common === 'function') {
113
- callback = common;
114
- common = null;
115
- }
116
-
117
- socket.emit('getObject', _adapterInstance, function (err, oldObj) {
118
- if (!oldObj) oldObj = {};
119
-
120
- for (var a in native) {
121
- oldObj.native[a] = native[a];
122
- }
123
-
124
- if (common) {
125
- for (var b in common) {
126
- oldObj.common[b] = common[b];
127
- }
128
- }
129
- socket.emit('setObject', _adapterInstance, oldObj, function (err) {
130
- if (err) {
131
- showMessage(err, _('Error'), 'alert');
132
- return;
133
- }
134
- changed = false;
135
- if (onChangeSupported) {
136
- $('#save').button('disable');
137
- $('#saveclose').button('disable');
138
- $('#close .ui-button-text').html(_('close'));
139
- }
140
- if (callback) callback();
141
- });
142
- });
143
- }
144
-
145
- // Read language settings
146
- function loadSystemConfig(callback) {
147
- socket.emit('getObject', 'system.config', function (err, res) {
148
- if (!err && res && res.common) {
149
- systemLang = res.common.language || systemLang;
150
- systemConfig = res;
151
- }
152
- socket.emit('getObject', 'system.certificates', function (err, res) {
153
- if (!err && res) {
154
- if (res.native && res.native.certificates) {
155
- certs = [];
156
- for (var c in res.native.certificates) {
157
- if (res.native.certificates.hasOwnProperty(c) && !res.native.certificates[c]) continue;
158
- var _cert = {
159
- name: c,
160
- type: (res.native.certificates[c].substring(0, '-----BEGIN RSA PRIVATE KEY'.length) === '-----BEGIN RSA PRIVATE KEY' || res.native.certificates[c].substring(0, '-----BEGIN PRIVATE KEY'.length) === '-----BEGIN PRIVATE KEY') ? 'private' : 'public'
161
- };
162
- if (_cert.type === 'public') {
163
- var m = res.native.certificates[c].split('-----END CERTIFICATE-----');
164
- var count = 0;
165
- for (var _m = 0; _m < m.length; _m++) {
166
- if (m[_m].replace(/[\r\n|\r|\n]+/, '').trim()) count++;
167
- }
168
- if (count > 1) _cert.type = 'chained';
169
- }
170
-
171
- certs.push(_cert);
172
- }
173
- }
174
- }
175
- if (callback) callback();
176
- });
177
- });
178
- }
179
-
180
- function loadSettings(callback) {
181
- socket.emit('getObject', _adapterInstance, function (err, res) {
182
- if (!err && res && res.native) {
183
- $('.adapter-instance').html(adapter + '.' + instance);
184
- $('.adapter-config').html('system.adapter.' + adapter + '.' + instance);
185
- common = res.common;
186
- if (res.common && res.common.name) $('.adapter-name').html(res.common.name);
187
- if (typeof load === 'undefined') {
188
- alert('Please implement save function in your admin/index.html');
189
- } else {
190
- load(res.native, onChange);
191
- }
192
- if (typeof callback === 'function') {
193
- callback();
194
- }
195
- } else {
196
- if (typeof callback === 'function') {
197
- callback();
198
- }
199
- alert('error loading settings for ' + _adapterInstance + '\n\n' + err);
200
- }
201
- });
202
- }
203
-
204
- function prepareTooltips() {
205
- $('.admin-icon').each(function () {
206
- var id = $(this).data('id');
207
- if (!id) {
208
- var $prev = $(this).prev();
209
- var $input = $prev.find('input');
210
- if (!$input.length) $input = $prev.find('select');
211
- if (!$input.length) $input = $prev.find('textarea');
212
-
213
- if (!$input.length) {
214
- $prev = $prev.parent();
215
- $input = $prev.find('input');
216
- if (!$input.length) $input = $prev.find('select');
217
- if (!$input.length) $input = $prev.find('textarea');
218
- }
219
- if ($input.length) id = $input.attr('id');
220
- }
221
-
222
- if (!id) return;
223
-
224
- var tooltip = '';
225
- if (systemDictionary['tooltip_' + id]) {
226
- tooltip = systemDictionary['tooltip_' + id][systemLang] || systemDictionary['tooltip_' + id].en;
227
- }
228
-
229
- var icon = '';
230
- var link = $(this).data('link');
231
- if (link) {
232
- if (link === true) {
233
- if (common.readme) {
234
- link = common.readme + '#' + id;
235
- } else {
236
- link = 'https://github.com/ioBroker/ioBroker.' + common.name + '#' + id;
237
- }
238
- }
239
- if (!link.match('^https?:\/\/')) {
240
- if (common.readme) {
241
- link = common.readme + '#' + link;
242
- } else {
243
- link = 'https://github.com/ioBroker/ioBroker.' + common.name + '#' + link;
244
- }
245
- }
246
- icon += '<a class="admin-tooltip-link" target="config_help" href="' + link + '" title="' + (tooltip || systemDictionary.htooltip[systemLang]) + '"><img class="admin-tooltip-icon" src="../../img/info.png" /></a>';
247
- } else if (tooltip) {
248
- icon += '<img class="admin-tooltip-icon" title="' + tooltip + '" src="../../img/info.png"/>';
249
- }
250
-
251
- if (icon) {
252
- $(this).html(icon);
253
- }
254
- });
255
- $('.admin-text').each(function () {
256
- var id = $(this).data('id');
257
- if (!id) {
258
- var $prev = $(this).prev();
259
- var $input = $prev.find('input');
260
- if (!$input.length) $input = $prev.find('select');
261
- if (!$input.length) $input = $prev.find('textarea');
262
- if (!$input.length) {
263
- $prev = $prev.parent();
264
- $input = $prev.find('input');
265
- if (!$input.length) $input = $prev.find('select');
266
- if (!$input.length) $input = $prev.find('textarea');
267
- }
268
- if ($input.length) id = $input.attr('id');
269
- }
270
-
271
- if (!id) return;
272
-
273
- // check if translation for this exist
274
- if (systemDictionary['info_' + id]) {
275
- $(this).html('<span class="admin-tooltip-text">' + (systemDictionary['info_' + id][systemLang] || systemDictionary['info_' + id].en) + '</span>');
276
- }
277
- });
278
- }
279
-
280
- function showMessage(message, title, icon, width) {
281
- var $dialogMessage = $('#dialog-message-settings');
282
- if (!$dialogMessage.length) {
283
- $('body').append('<div id="dialog-message-settings" title="Message" style="display: none">\n' +
284
- '<p>' +
285
- '<span id="dialog-message-icon-settings" class="ui-icon ui-icon-circle-check" style="float :left; margin: 0 7px 50px 0;"></span>\n' +
286
- '<span id="dialog-message-text-settings"></span>\n' +
287
- '</p>\n' +
288
- '</div>');
289
- $dialogMessage = $('#dialog-message-settings');
290
- $dialogMessage.dialog({
291
- autoOpen: false,
292
- modal: true,
293
- buttons: [
294
- {
295
- text: _('Ok'),
296
- click: function () {
297
- $(this).dialog('close');
298
- }
299
- }
300
- ]
301
- });
302
- }
303
- $dialogMessage.dialog('option', 'width', width + 500);
304
-
305
- if (typeof _ != 'undefined') {
306
- $dialogMessage.dialog('option', 'title', title || _('Message'));
307
- } else {
308
- $dialogMessage.dialog('option', 'title', title || 'Message');
309
- }
310
- $('#dialog-message-text-settings').html(message);
311
- if (icon) {
312
- $('#dialog-message-icon-settings').show();
313
- $('#dialog-message-icon-settings').attr('class', '');
314
- $('#dialog-message-icon-settings').addClass('ui-icon ui-icon-' + icon);
315
- } else {
316
- $('#dialog-message-icon-settings').hide();
317
- }
318
- $dialogMessage.dialog('open');
319
- }
320
-
321
- function confirmMessage(message, title, icon, buttons, callback) {
322
- var $dialogConfirm = $('#dialog-confirm-settings');
323
- if (!$dialogConfirm.length) {
324
- $('body').append('<div id="dialog-confirm-settings" title="Message" style="display: none">\n' +
325
- '<p>' +
326
- '<span id="dialog-confirm-icon-settings" class="ui-icon ui-icon-circle-check" style="float :left; margin: 0 7px 50px 0;"></span>\n' +
327
- '<span id="dialog-confirm-text-settings"></span>\n' +
328
- '</p>\n' +
329
- '</div>');
330
- $dialogConfirm = $('#dialog-confirm-settings');
331
- $dialogConfirm.dialog({
332
- autoOpen: false,
333
- modal: true
334
- });
335
- }
336
- if (typeof buttons === 'function') {
337
- callback = buttons;
338
- $dialogConfirm.dialog('option', 'buttons', [
339
- {
340
- text: _('Ok'),
341
- click: function () {
342
- var cb = $(this).data('callback');
343
- $(this).data('callback', null);
344
- $(this).dialog('close');
345
- if (cb) cb(true);
346
- }
347
- },
348
- {
349
- text: _('Cancel'),
350
- click: function () {
351
- var cb = $(this).data('callback');
352
- $(this).data('callback', null);
353
- $(this).dialog('close');
354
- if (cb) cb(false);
355
- }
356
- }
357
-
358
- ]);
359
- } else if (typeof buttons === 'object') {
360
- for (var b = 0; b < buttons.length; b++) {
361
- buttons[b] = {
362
- text: buttons[b],
363
- id: 'dialog-confirm-button-' + b,
364
- click: function (e) {
365
- var id = parseInt(e.currentTarget.id.substring('dialog-confirm-button-'.length), 10);
366
- var cb = $(this).data('callback');
367
- $(this).dialog('close');
368
- if (cb) cb(id);
369
- }
370
- }
371
- }
372
- $dialogConfirm.dialog('option', 'buttons', buttons);
373
- }
374
-
375
- $dialogConfirm.dialog('option', 'title', title || _('Message'));
376
- $('#dialog-confirm-text-settings').html(message);
377
- if (icon) {
378
- $('#dialog-confirm-icon-settings')
379
- .show()
380
- .attr('class', '')
381
- .addClass('ui-icon ui-icon-' + icon);
382
- } else {
383
- $('#dialog-confirm-icon-settings').hide();
384
- }
385
- $dialogConfirm.data('callback', callback);
386
- $dialogConfirm.dialog('open');
387
- }
388
-
389
- function getObject(id, callback) {
390
- socket.emit('getObject', id, function (err, res) {
391
- if (!err && res) {
392
- if (callback) callback(err, res);
393
- } else {
394
- if (callback) callback(null);
395
- }
396
- });
397
- }
398
-
399
- function getState(id, callback) {
400
- socket.emit('getState', id, function (err, res) {
401
- if (!err && res) {
402
- if (callback) callback(err, res);
403
- } else {
404
- if (callback) callback(null);
405
- }
406
- });
407
- }
408
-
409
- function getEnums(_enum, callback) {
410
- socket.emit('getObjectView', 'system', 'enum', {startkey: 'enum.' + _enum, endkey: 'enum.' + _enum + '.\u9999'}, function (err, res) {
411
- if (!err && res) {
412
- var _res = {};
413
- for (var i = 0; i < res.rows.length; i++) {
414
- if (res.rows[i].id === 'enum.' + _enum) continue;
415
- _res[res.rows[i].id] = res.rows[i].value;
416
- }
417
- if (callback) callback(null, _res);
418
- } else {
419
- if (callback) callback(err, []);
420
- }
421
- });
422
- }
423
-
424
- function getGroups(callback) {
425
- socket.emit('getObjectView', 'system', 'group', {startkey: 'system.group.', endkey: 'system.group.\u9999'}, function (err, res) {
426
- if (!err && res) {
427
- var _res = {};
428
- for (var i = 0; i < res.rows.length; i++) {
429
- _res[res.rows[i].id] = res.rows[i].value;
430
- }
431
- if (callback) callback(null, _res);
432
- } else {
433
- if (callback) callback(err, []);
434
- }
435
- });
436
- }
437
-
438
- function getUsers(callback) {
439
- socket.emit('getObjectView', 'system', 'user', {startkey: 'system.user.', endkey: 'system.user.\u9999'}, function (err, res) {
440
- if (!err && res) {
441
- var _res = {};
442
- for (var i = 0; i < res.rows.length; i++) {
443
- _res[res.rows[i].id] = res.rows[i].value;
444
- }
445
- if (callback) callback(null, _res);
446
- } else {
447
- if (callback) callback(err, []);
448
- }
449
- });
450
- }
451
-
452
- function fillUsers(elemId, current, callback) {
453
- getUsers(function (err, users) {
454
- var text = '';
455
- for (var u in users) {
456
- text += '<option value="' + u + '" ' + ((current === u) ? 'selected' : '') + ' >' + users[u].common.name[0].toUpperCase() + users[u].common.name.substring(1) + '</option>\n';
457
- }
458
- $(elemId).html(text);
459
- });
460
- }
461
-
462
- function getIPs(host, callback) {
463
- if (typeof host === 'function') {
464
- callback = host;
465
- host = null;
466
- }
467
-
468
- socket.emit('getHostByIp', host || common.host, function (ip, _host) {
469
- if (_host) {
470
- host = _host;
471
- var IPs4 = [{name: '[IPv4] 0.0.0.0 - ' + _('Listen on all IPs'), address: '0.0.0.0', family: 'ipv4'}];
472
- var IPs6 = [{name: '[IPv6] ::', address: '::', family: 'ipv6'}];
473
- if (host.native.hardware && host.native.hardware.networkInterfaces) {
474
- for (var eth in host.native.hardware.networkInterfaces) {
475
- for (var num = 0; num < host.native.hardware.networkInterfaces[eth].length; num++) {
476
- if (host.native.hardware.networkInterfaces[eth][num].family != 'IPv6') {
477
- IPs4.push({name: '[' + host.native.hardware.networkInterfaces[eth][num].family + '] ' + host.native.hardware.networkInterfaces[eth][num].address + ' - ' + eth, address: host.native.hardware.networkInterfaces[eth][num].address, family: 'ipv4'});
478
- } else {
479
- IPs6.push({name: '[' + host.native.hardware.networkInterfaces[eth][num].family + '] ' + host.native.hardware.networkInterfaces[eth][num].address + ' - ' + eth, address: host.native.hardware.networkInterfaces[eth][num].address, family: 'ipv6'});
480
- }
481
- }
482
- }
483
- }
484
- for (var i = 0; i < IPs6.length; i++) {
485
- IPs4.push(IPs6[i]);
486
- }
487
- callback(IPs4);
488
- }
489
- });
490
- }
491
-
492
- function fillSelectIPs(id, actualAddr, noIPv4, noIPv6, callback) {
493
- getIPs(function (ips) {
494
- var str = '';
495
- for (var i = 0; i < ips.length; i++) {
496
- if (noIPv4 && ips[i].family === 'ipv4') continue;
497
- if (noIPv6 && ips[i].family === 'ipv6') continue;
498
- str += '<option value="' + ips[i].address + '" ' + ((ips[i].address === actualAddr) ? 'selected' : '') + '>' + ips[i].name + '</option>';
499
- }
500
-
501
- $(id).html(str);
502
- if (typeof callback === 'function') callback();
503
- });
504
- }
505
-
506
- function sendTo(_adapter_instance, command, message, callback) {
507
- socket.emit('sendTo', (_adapter_instance || adapter + '.' + instance), command, message, callback);
508
- }
509
-
510
- function sendToHost(host, command, message, callback) {
511
- socket.emit('sendToHost', host || common.host, command, message, callback);
512
- }
513
-
514
- // fills select with names of the certificates and preselect it
515
- function fillSelectCertificates(id, type, actualValued) {
516
- var str = '<option value="">' + _('none') + '</option>';
517
- for (var i = 0; i < certs.length; i++) {
518
- if (certs[i].type != type) continue;
519
- str += '<option value="' + certs[i].name + '" ' + ((certs[i].name === actualValued) ? 'selected' : '') + '>' + certs[i].name + '</option>';
520
- }
521
-
522
- $(id).html(str);
523
- }
524
-
525
- function getAdapterInstances(_adapter, callback) {
526
- if (typeof _adapter === 'function') {
527
- callback = _adapter;
528
- _adapter = null;
529
- }
530
-
531
- socket.emit('getObjectView', 'system', 'instance', {startkey: 'system.adapter.' + (_adapter || adapter), endkey: 'system.adapter.' + (_adapter || adapter) + '.\u9999'}, function (err, doc) {
532
- if (err) {
533
- if (callback) callback ([]);
534
- } else {
535
- if (doc.rows.length === 0) {
536
- if (callback) callback ([]);
537
- } else {
538
- var res = [];
539
- for (var i = 0; i < doc.rows.length; i++) {
540
- res.push(doc.rows[i].value);
541
- }
542
- if (callback) callback (res);
543
- }
544
- }
545
-
546
- });
547
- }
548
-
549
- function getExtendableInstances(_adapter, callback) {
550
- if (typeof _adapter === 'function') {
551
- callback = _adapter;
552
- _adapter = null;
553
- }
554
-
555
- socket.emit('getObjectView', 'system', 'instance', null, function (err, doc) {
556
- if (err) {
557
- if (callback) callback ([]);
558
- } else {
559
- if (doc.rows.length === 0) {
560
- if (callback) callback ([]);
561
- } else {
562
- var res = [];
563
- for (var i = 0; i < doc.rows.length; i++) {
564
- if (doc.rows[i].value.common.webExtendable) {
565
- res.push(doc.rows[i].value);
566
- }
567
- }
568
- if (callback) callback (res);
569
- }
570
- }
571
- });
572
- }
573
-
574
- function getIsAdapterAlive(_adapter, callback) {
575
- if (typeof _adapter === 'function') {
576
- callback = _adapter;
577
- _adapter = null;
578
- }
579
- getState('system.adapter.' + (_adapter || adapter) + '.' + instance + '.alive', function (err, obj) {
580
- if (!obj || !obj.val) {
581
- callback(false);
582
- } else {
583
- callback(true);
584
- }
585
- });
586
- }
587
-
588
- // Adds one entry to the created with editTable table
589
- // tabId - the id of the table used by editTable
590
- // value - is one object to add in form {ip: '3.3.3.3', room: 'enum.room.bla3', desc: 'Bla3'}
591
- // $grid - [optional] - object returned by editTable to speed up the addition
592
- // _isInitial - [optional] - if it is initial fill of the table. To not trigger the onChange
593
- function addToTable(tabId, value, $grid, _isInitial) {
594
- $grid = $grid || $('#' + tabId);
595
- var obj = {_id: $grid[0]._maxIdx++};
596
- var cols = $grid[0]._cols;
597
-
598
- for (var i = 0; i < cols.length; i++) {
599
- if (cols[i] === 'room') {
600
- obj[cols[i]] = ($grid[0]._rooms[value[cols[i]]]) ? $grid[0]._rooms[value[cols[i]]].common.name : value[cols[i]];
601
- } else {
602
- obj[cols[i]] = value[cols[i]];
603
- }
604
- }
605
- obj._commands =
606
- '<button data-' + tabId + '-id="' + obj._id + '" class="' + tabId + '-edit-submit">' + _('edit') + '</button>' +
607
- '<button data-' + tabId + '-id="' + obj._id + '" class="' + tabId + '-delete-submit">' + _('delete') + '</button>' +
608
- '<button data-' + tabId + '-id="' + obj._id + '" class="' + tabId + '-ok-submit" style="display:none">' + _('ok') + '</button>' +
609
- '<button data-' + tabId + '-id="' + obj._id + '" class="' + tabId + '-cancel-submit" style="display:none">' + _('cancel') + '</button>';
610
-
611
- $grid.jqGrid('addRowData', tabId + '_' + obj._id, obj);
612
-
613
- _editInitButtons($grid, tabId, obj._id);
614
-
615
- if (!_isInitial && $grid[0]._onChange) $grid[0]._onChange('add', value);
616
- }
617
-
618
- function _editInitButtons($grid, tabId, objId) {
619
- var search = objId ? '[data-' + tabId + '-id="' + objId + '"]' : '';
620
-
621
- $('.' + tabId + '-edit-submit' + search).unbind('click').button({
622
- icons: {primary: 'ui-icon-pencil'},
623
- text: false
624
- }).click(function () {
625
- var id = $(this).attr('data-' + tabId + '-id');
626
-
627
- $('.' + tabId + '-edit-submit').hide();
628
- $('.' + tabId + '-delete-submit').hide();
629
- $('.' + tabId + '-ok-submit[data-' + tabId + '-id="' + id + '"]').show();
630
- $('.' + tabId + '-cancel-submit[data-' + tabId + '-id="' + id + '"]').show();
631
-
632
- $grid.jqGrid('editRow', tabId + '_' + id, {url: 'clientArray'});
633
- if ($grid[0]._edited.indexOf(id) === -1) {
634
- $grid[0]._edited.push(id);
635
- }
636
- changed = true;
637
- $('#save').button('enable');
638
- $('#saveclose').button('enable');
639
- if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
640
- }).css({'height': '18px', width: '22px'});
641
-
642
- $('.' + tabId + '-delete-submit' + search).unbind('click').button({
643
- icons: {primary: 'ui-icon-trash'},
644
- text: false
645
- }).click(function () {
646
- var id = $(this).attr('data-' + tabId + '-id');
647
- $grid.jqGrid('delRowData', tabId + '_' + id);
648
-
649
- changed = true;
650
- $('#save').button('enable');
651
- $('#saveclose').button('enable');
652
- if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
653
-
654
- var pos = $grid[0]._edited.indexOf(id);
655
- if (pos !== -1) {
656
- $grid[0]._edited.splice(pos, 1);
657
- }
658
- if ($grid[0]._onChange) $grid[0]._onChange('del', id);
659
- }).css({'height': '18px', width: '22px'});
660
-
661
- $('.' + tabId + '-ok-submit' + search).unbind('click').button({
662
- icons: {primary: 'ui-icon-check'},
663
- text: false
664
- }).click(function () {
665
- var id = $(this).attr('data-' + tabId + '-id');
666
-
667
- $('.' + tabId + '-edit-submit').show();
668
- $('.' + tabId + '-delete-submit').show();
669
- $('.' + tabId + '-ok-submit').hide();
670
- $('.' + tabId + '-cancel-submit').hide();
671
-
672
- $grid.jqGrid('saveRow', tabId + '_' + id, {url: 'clientArray'});
673
-
674
- changed = true;
675
- $('#save').button('enable');
676
- $('#saveclose').button('enable');
677
- if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
678
-
679
- var pos = $grid[0]._edited.indexOf(id);
680
- if (pos !== -1) {
681
- $grid[0]._edited.splice(pos, 1);
682
- }
683
- if ($grid[0]._onChange) $grid[0]._onChange('changed', $grid.jqGrid('getRowData', tabId + '_' + id));
684
- }).css({'height': '18px', width: '22px'});
685
-
686
- $('.' + tabId + '-cancel-submit' + search).unbind('click').button({
687
- icons: {primary: 'ui-icon-close'},
688
- text: false
689
- }).click(function () {
690
- var id = $(this).attr('data-' + tabId + '-id');
691
-
692
- $('.' + tabId + '-edit-submit').show();
693
- $('.' + tabId + '-delete-submit').show();
694
- $('.' + tabId + '-ok-submit').hide();
695
- $('.' + tabId + '-cancel-submit').hide();
696
-
697
- $grid.jqGrid('restoreRow', tabId + '_' + id, false);
698
- var pos = $grid[0]._edited.indexOf(id);
699
- if (pos !== -1) {
700
- $grid[0]._edited.splice(pos, 1);
701
- }
702
- }).css({'height': '18px', width: '22px'});
703
- }
704
-
705
- function _editTable(tabId, cols, values, rooms, top, onChange) {
706
- var title = 'Device list';
707
- if (typeof tabId === 'object') {
708
- cols = tabId.cols;
709
- values = tabId.values;
710
- rooms = tabId.rooms;
711
- top = tabId.top;
712
- onChange = tabId.onChange;
713
- if (tabId.title) title = tabId.title;
714
- tabId = tabId.tabId;
715
- }
716
-
717
- initGridLanguage(systemLang);
718
- var colNames = [];
719
- var colModel = [];
720
- var $grid = $('#' + tabId);
721
- var room;
722
-
723
- colNames.push('id');
724
- colModel.push({
725
- name: '_id',
726
- index: '_id',
727
- hidden: true
728
- });
729
- for (var i = 0; i < cols.length; i++) {
730
- var width = null;
731
- var checkbox = null;
732
-
733
- if (typeof cols[i] === 'object') {
734
- width = cols[i].width;
735
- if (cols[i].checkbox) checkbox = true;
736
- cols[i] = cols[i].name;
737
- }
738
- colNames.push(_(cols[i]));
739
- var _obj = {
740
- name: cols[i],
741
- index: cols[i],
742
- // width: 160,
743
- editable: true
744
- };
745
- if (width) _obj.width = width;
746
- if (checkbox) {
747
- _obj.edittype = 'checkbox';
748
- _obj.editoptions = {value: 'true:false'};
749
- }
750
-
751
- if (cols[i] === 'room') {
752
- var list = {'': _('none')};
753
- for (room in rooms) {
754
- list[room] = _(rooms[room].common.name);
755
- }
756
- _obj.stype = 'select';
757
- _obj.edittype = 'select';
758
- _obj.editoptions = {value: list};
759
- _obj.searchoptions = {
760
- sopt: ['eq'],
761
- value: ':' + _('all')
762
- };
763
- for (room in rooms) {
764
- _obj.searchoptions.value += ';' + _(rooms[room].common.name) + ':' + _(rooms[room].common.name);
765
- }
766
- }
767
- colModel.push(_obj);
768
- }
769
- colNames.push('');
770
- colModel.push({name: '_commands', index: '_commands', width: 50, editable: false, align: 'center', search: false});
771
-
772
- $grid[0]._cols = cols;
773
- $grid[0]._rooms = rooms;
774
- $grid[0]._maxIdx = 0;
775
- $grid[0]._top = top;
776
- $grid[0]._edited = [];
777
- $grid[0]._onChange = onChange;
778
-
779
- $grid.jqGrid({
780
- datatype: 'local',
781
- colNames: colNames,
782
- colModel: colModel,
783
- width: 800,
784
- height: 330,
785
- pager: $('#pager-' + tabId),
786
- rowNum: 1000,
787
- rowList: [1000],
788
- ondblClickRow: function (rowid) {
789
- var id = rowid.substring((tabId + '_').length);
790
- $('.' + tabId + '-edit-submit').hide();
791
- $('.' + tabId + '-delete-submit').hide();
792
- $('.' + tabId + '-ok-submit[data-' + tabId + '-id="' + id + '"]').show();
793
- $('.' + tabId + '-cancel-submit[data-' + tabId + '-id="' + id + '"]').show();
794
- $grid.jqGrid('editRow', rowid, {url: 'clientArray'});
795
- if ($grid[0]._edited.indexOf(id) === -1) $grid[0]._edited.push(id);
796
-
797
- changed = true;
798
- $('#save').button('enable');
799
- $('#saveclose').button('enable');
800
- if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
801
- },
802
- sortname: 'id',
803
- sortorder: 'desc',
804
- viewrecords: false,
805
- pgbuttons: false,
806
- pginput: false,
807
- pgtext: false,
808
- caption: _(title),
809
- ignoreCase: true,
810
- loadComplete: function () {
811
- _editInitButtons($grid, tabId);
812
- },
813
- onSortCol: function () {
814
- changed = true;
815
- $('#save').button('enable');
816
- $('#saveclose').button('enable');
817
- if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
818
- }
819
- }).jqGrid('filterToolbar', {
820
- defaultSearch: 'cn',
821
- autosearch: true,
822
- searchOnEnter: false,
823
- enableClear: false,
824
- afterSearch: function () {
825
- _editInitButtons($grid, tabId);
826
- }
827
- });
828
-
829
- $('#pager-' + tabId + '_center').hide();
830
-
831
- if ($('#pager-' + tabId).length) {
832
- $grid.navGrid('#pager-' + tabId, {
833
- search: false,
834
- edit: false,
835
- add: false,
836
- del: false,
837
- refresh: false
838
- }).jqGrid('navButtonAdd', '#pager-' + tabId, {
839
- caption: '',
840
- buttonicon: 'ui-icon-plus',
841
- onClickButton: function () {
842
- // Find new unique name
843
- var found;
844
- var newText = _('New');
845
- var ids = $grid.jqGrid('getDataIDs');
846
- var idx = 1;
847
- var obj;
848
- do {
849
- found = true;
850
- for (var _id = 0; _id < ids.length; _id++) {
851
- obj = $grid.jqGrid('getRowData', ids[_id]);
852
- if (obj && obj[$grid[0]._cols[0]] === newText + idx) {
853
- idx++;
854
- found = false;
855
- break;
856
- }
857
- }
858
- } while (!found);
859
-
860
- obj = {};
861
- for (var t = 0; t < $grid[0]._cols.length; t++) {
862
- obj[$grid[0]._cols[t]] = '';
863
- }
864
- obj[$grid[0]._cols[0]] = newText + idx;
865
-
866
- changed = true;
867
- $('#save').button('enable');
868
- $('#saveclose').button('enable');
869
- if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
870
-
871
- addToTable(tabId, obj, $grid);
872
- },
873
- position: 'first',
874
- id: 'add-cert',
875
- title: _('new device'),
876
- cursor: 'pointer'
877
- });
878
- }
879
-
880
- if (values) {
881
- for (var u = 0; u < values.length; u++) {
882
- addToTable(tabId, values[u], $grid, true);
883
- }
884
- }
885
- $(window).resize(function () {
886
- $grid.setGridHeight($(this).height() - top).setGridWidth($(this).width() - 10);
887
- });
888
- $(window).trigger('resize');
889
-
890
- // hide scrollbar
891
- $('.ui-jqgrid-bdiv').css({'overflow-x': 'hidden'});
892
-
893
- return $grid;
894
- }
895
-
896
- // converts "enum.room.Sleeping_room" to "Sleeping room"
897
- // As input gets the list from getEnum
898
- function enumName2Id(enums, name) {
899
- for (var enumId in enums) {
900
- if (enums[enumId].common.name === name) return enumId;
901
- if (enums[enumId].name && enums[enumId].name === name) return enumId;
902
- }
903
- return '';
904
- }
905
-
906
- // Creates edit table for any configuration array
907
- // tabId - is id of table where the jqGrid must be created. E.g: <table id="devices"></table><div id="pager-devices"></div>
908
- // cols - array with names of the properties of entry. E.g: ['ip', 'room', 'desc']
909
- // if column has name room, for that will be automatically the room enums loaded and shown
910
- // values - array with values in form [{ip: '1.1.1.1', room: 'enum.room.bla1', desc: 'Bla1'}, {ip: '2.2.2.2', room: 'enum.room.bla2', desc: 'Bla2'}
911
- // top - top position of the table to set the height of the table automatically. Table must be always as last on the page.
912
- // onChange - callback called if something is changed in the table
913
- //
914
- // returns the jquery object of $('#tabId')
915
- // To extract data from table
916
- function editTable(tabId, cols, values, top, onChange) {
917
- if (typeof tabId === 'object') {
918
- cols = tabId.cols;
919
- }
920
-
921
- if (cols.indexOf('room') !== -1) {
922
- getEnums('rooms', function (err, list) {
923
- return _editTable(tabId, cols, values, list, top, onChange);
924
- });
925
- } else {
926
- return _editTable(tabId, cols, values, null, top, onChange);
927
- }
928
- }
929
-
930
- // Extract edited array from table
931
- // tabId - is id of table where the jqGrid must be created. E.g: <table id="devices"></table><div id="pager-devices"></div>
932
- // cols - array with names of the properties of entry. E.g: ['ip', 'room', 'desc']
933
- //
934
- // Returns array with values
935
- function getTableResult(tabId, cols) {
936
- var $grid = $('#' + tabId);
937
- for (var j = 0; j < $grid[0]._edited.length; j++) {
938
- $grid.jqGrid('saveRow', tabId + '_' + $grid[0]._edited[j], {url: 'clientArray'});
939
- }
940
-
941
- $('.' + tabId + '-edit-submit').show();
942
- $('.' + tabId + '-delete-submit').show();
943
- $('.' + tabId + '-ok-submit').hide();
944
- $('.' + tabId + '-cancel-submit').hide();
945
-
946
- var data = $grid.jqGrid('getRowData');
947
- var res = [];
948
- for (var i = 0; i < data.length; i++) {
949
- var obj = {};
950
- for (var z = 0; z < cols.length; z++) {
951
- if (cols[z] === 'room') {
952
- obj[cols[z]] = enumName2Id($grid[0]._rooms, data[i][cols[z]]);
953
- } else {
954
- obj[cols[z]] = data[i][cols[z]];
955
- }
956
- }
957
- res.push(obj);
958
- }
959
- return res;
960
- }
961
-
962
- /*
963
- <div id="values">
964
- <button class="table-button-add" style="margin-left: 10px"></button>
965
- <table class="table-values" style="width: 100%; calc(100% - 200px)">
966
- <thead>
967
- <tr>
968
- <th data-name="regex" style="width: 30%" class="translate">Context</th>
969
- <th data-name="room" class="translate" data-type="select">Room</th>
970
- <th data-name="aaa" class="translate" data-options="1/A;2/B;3/C;4" data-type="select">Room</th>
971
- <th data-name="enabled" class="translate" data-type="checkbox">Enabled</th>
972
- <th data-buttons="delete up down" style="width: 32px"></th>
973
- </tr>
974
- </thead>
975
- </table>
976
- </td>
977
- */
978
-
979
- /**
980
- * Create edit table from javascript array.
981
- *
982
- * This function creates a html edit table.
983
- *
984
- * <pre><code>
985
- * <div id="values" style="width: 100%; height: calc(100% - 205px)">
986
- * <button class="table-button-add" style="margin-left: 10px"></button>
987
- * <div style="width: 100%; height: calc(100% - 30px); overflow: auto;">
988
- * <table class="table-values" style="width: 100%;">
989
- * <thead>
990
- * <tr>
991
- * <th data-name="_index" style="30px" data-style="width: 100%; text-align: right">Context</th>
992
- * <th data-name="regex" class="translate" style="width: 30%" data-style="text-align: right">Context</th>
993
- * <th data-name="room" class="translate" data-type="select">Room</th>
994
- * <th data-name="aaa" class="translate" data-options="1/A;2/B;3/C;4" data-type="select">Room</th>
995
- * <th data-name="enabled" class="translate" data-type="checkbox">Enabled</th>
996
- * <th data-buttons="delete up down" style="width: 32px"></th>
997
- * </tr>
998
- * </thead>
999
- * </table>
1000
- * </div>
1001
- * </div>
1002
- * <pre><code>
1003
- *
1004
- * @param {string} id name of the html element (or empty).
1005
- * @param {string} values data array
1006
- * @param {function} onChange this function will be called if something changed
1007
- * @param {function} onReady called, when the table is ready (may be to modify some elements of it)
1008
- * @return {object} array with values
1009
- */
1010
- function values2table(divId, values, onChange, onReady) {
1011
- if (typeof values === 'function') {
1012
- onChange = values;
1013
- values = divId;
1014
- divId = '';
1015
- }
1016
-
1017
- values = values || [];
1018
- var names = [];
1019
- var $div;
1020
- if (!divId) {
1021
- $div = $('body');
1022
- } else {
1023
- $div = $('#' + divId);
1024
- }
1025
- var $add = $div.find('.table-button-add');
1026
-
1027
- if (!$add.data('inited')) {
1028
- $add.data('inited', true);
1029
-
1030
- $add.button({
1031
- icons: {primary: 'ui-icon-plus'},
1032
- text: false
1033
- })
1034
- //.css({width: '1em', height: '1em'})
1035
- .click(function () {
1036
- var $table = $div.find('.table-values');
1037
- var values = $table.data('values');
1038
- var names = $table.data('names');
1039
- var obj = {};
1040
- for (var i = 0; i < names.length; i++) {
1041
- if (!names[i]) continue;
1042
- obj[names[i].name] = names[i].def;
1043
- }
1044
- values.push(obj);
1045
- onChange && onChange();
1046
- setTimeout(function () {
1047
- values2table(divId, values, onChange, onReady);
1048
- }, 100);
1049
- });
1050
- }
1051
-
1052
- if (values) {
1053
- var buttons = [];
1054
- var $table = $div.find('.table-values');
1055
- $table.data('values', values);
1056
-
1057
- // load rooms
1058
- if (!$table.data('rooms') && $table.find('th[data-name="room"]').length) {
1059
- getEnums('rooms', function (err, list) {
1060
- var result = {};
1061
- result[_('none')] = '';
1062
- var nnames = [];
1063
- for (var n in list) {
1064
- nnames.push(n);
1065
- }
1066
- nnames.sort(function (a, b) {
1067
- a = a.toLowerCase();
1068
- b = b.toLowerCase();
1069
- if (a > b) return 1;
1070
- if (a < b) return -1;
1071
- return 0;
1072
- });
1073
-
1074
- for (var l = 0; l < nnames.length; l++) {
1075
- result[nnames[l]] = list[nnames[l]].common.name || l;
1076
- }
1077
- $table.data('rooms', result);
1078
- values2table(divId, values, onChange, onReady);
1079
- });
1080
- return;
1081
- }
1082
- // load functions
1083
- if (!$table.data('functions') && $table.find('th[data-name="func"]').length) {
1084
- getEnums('functions', function (err, list) {
1085
- var result = {};
1086
- result[_('none')] = '';
1087
- var nnames = [];
1088
- for (var n in list) {
1089
- nnames.push(n);
1090
- }
1091
- nnames.sort(function (a, b) {
1092
- a = a.toLowerCase();
1093
- b = b.toLowerCase();
1094
- if (a > b) return 1;
1095
- if (a < b) return -1;
1096
- return 0;
1097
- });
1098
-
1099
- for (var l = 0; l < nnames.length; l++) {
1100
- result[nnames[l]] = list[nnames[l]].common.name || l;
1101
- }
1102
- $table.data('functions', result);
1103
- values2table(divId, values, onChange, onReady);
1104
- });
1105
- return;
1106
- }
1107
- $table.find('th').each(function () {
1108
- var name = $(this).data('name');
1109
- if (name) {
1110
- var obj = {
1111
- name: name,
1112
- type: $(this).data('type') || 'text',
1113
- def: $(this).data('default'),
1114
- style: $(this).data('style')
1115
- };
1116
- if (obj.type === 'checkbox') {
1117
- if (obj.def === 'false') obj.def = false;
1118
- if (obj.def === 'true') obj.def = true;
1119
- obj.def = !!obj.def;
1120
- } else if (obj.type === 'select') {
1121
- var vals = ($(this).data('options') || '').split(';');
1122
- obj.options = {};
1123
- for (var v = 0; v < vals.length; v++) {
1124
- var parts = vals[v].split('/');
1125
- obj.options[parts[0]] = _(parts[1] || parts[0]);
1126
- if (v === 0) obj.def = (obj.def === undefined) ? parts[0] : obj.def;
1127
- }
1128
- } else {
1129
- obj.def = obj.def || '';
1130
- }
1131
- names.push(obj);
1132
- } else {
1133
- names.push(null);
1134
- }
1135
-
1136
- name = $(this).data('buttons');
1137
-
1138
- if (name) {
1139
- var bs = name.split(' ');
1140
- buttons.push(bs);
1141
- } else {
1142
- buttons.push(null);
1143
- }
1144
- });
1145
-
1146
- $table.data('names', names);
1147
-
1148
- var text = '';
1149
- for (var v = 0; v < values.length; v++) {
1150
- text += '<tr>';
1151
-
1152
- for (var i = 0; i < names.length; i++) {
1153
- text += '<td';
1154
- var line = '';
1155
- var style = '';
1156
- if (names[i]) {
1157
- if (names[i].name === '_index') {
1158
- style = (names[i].style ? names[i].style : 'text-align: right;');
1159
- line += (v + 1);
1160
- } else
1161
- if (names[i].type === 'checkbox') {
1162
- line += '<input ' + (names[i].style || '') + '" class="values-input" type="checkbox" data-index="' + v + '" data-name="' + names[i].name + '" ' + (values[v][names[i].name] ? 'checked' : '') + '" data-old-value="' + (values[v][names[i].name] === undefined ? '' : values[v][names[i].name]) + '"/>';
1163
- } else if (names[i].type === 'select') {
1164
- line += '<select style="' + (names[i].style ? names[i].style : 'width: 100%') + '" class="values-input" data-index="' + v + '" data-name="' + names[i].name + '">';
1165
- var options;
1166
- if (names[i].name === 'room') {
1167
- options = $table.data('rooms');
1168
- } else if (names[i].name === 'func') {
1169
- options = $table.data('functions');
1170
- } else {
1171
- options = names[i].options;
1172
- }
1173
- var val = (values[v][names[i].name] === undefined ? '' : values[v][names[i].name]);
1174
- for (var p in options) {
1175
- line += '<option value="' + p + '" ' + (p === val ? ' selected' : '') + '>' + options[p] + '</option>';
1176
- }
1177
- line += '</select>';
1178
- } else {
1179
- line += '<input class="values-input" style="' + (names[i].style ? names[i].style : 'width: 100%') + '" type="' + names[i].type + '" data-index="' + v + '" data-name="' + names[i].name + '"/>';
1180
- }
1181
- }
1182
-
1183
- if (buttons[i]) {
1184
- style = 'text-align: center;';
1185
- for (var b = 0; b < buttons[i].length; b++) {
1186
- if ((!v && buttons[i][b] === 'up') || v === values.length - 1 && buttons[i][b] === 'down') {
1187
- line += '<button data-command="' + buttons[i][b] + '" class="values-buttons" disabled>&nbsp;</button>';
1188
- continue;
1189
- }
1190
- line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons"></button>';
1191
- }
1192
- }
1193
- text += ' style="' + style + '">' + line + '</td>';
1194
- }
1195
-
1196
- text += '</tr>';
1197
- }
1198
- var $lines = $('.table-lines');
1199
- if (!$lines.length) {
1200
- $table.append('<tbody class="table-lines"></tbody>');
1201
- $lines = $('.table-lines');
1202
- }
1203
-
1204
- $lines.html(text);
1205
-
1206
- $lines.find('.values-input').each(function () {
1207
- var $this = $(this);
1208
- var type = $this.attr('type');
1209
- var name = $this.data('name');
1210
- var id = $this.data('index');
1211
- $this.data('old-value', values[id][name]);
1212
- if (type === 'checkbox') {
1213
- $this.prop('checked', values[id][name]);
1214
- } else {
1215
- $this.val(values[id][name]);
1216
- }
1217
- });
1218
- $lines.find('.values-buttons').each(function () {
1219
- var command = $(this).data('command');
1220
- if (command === 'delete') {
1221
- $(this).button({
1222
- icons: {primary: 'ui-icon-trash'},
1223
- text: false
1224
- })
1225
- .css({width: '1em', height: '1em'})
1226
- .click(function () {
1227
- var id = $(this).data('index');
1228
- values.splice(id, 1);
1229
- onChange && onChange();
1230
- setTimeout(function () {
1231
- values2table(divId, values, onChange, onReady);
1232
- }, 100);
1233
- });
1234
- } else if (command === 'up') {
1235
- $(this).button({
1236
- icons: {primary: 'ui-icon-triangle-1-n'},
1237
- text: false
1238
- })
1239
- .css({width: '1em', height: '1em'})
1240
- .click(function () {
1241
- var id = $(this).data('index');
1242
- var elem = values[id];
1243
- values.splice(id, 1);
1244
- values.splice(id - 1, 0, elem);
1245
- onChange && onChange();
1246
- setTimeout(function () {
1247
- values2table(id, values, onChange, onReady);
1248
- }, 100);
1249
- });
1250
- } else if (command === 'down') {
1251
- $(this).button({
1252
- icons: {primary: 'ui-icon-triangle-1-s'},
1253
- text: false
1254
- })
1255
- .css({width: '1em', height: '1em'})
1256
- .click(function () {
1257
- var id = $(this).data('index');
1258
- var elem = values[id];
1259
- values.splice(id, 1);
1260
- values.splice(id + 1, 0, elem);
1261
- onChange && onChange();
1262
- setTimeout(function () {
1263
- values2table(id, values, onChange, onReady);
1264
- }, 100);
1265
- });
1266
- } else if (command === 'edit') {
1267
- $(this).button({
1268
- icons: {primary: 'ui-icon-pencil'},
1269
- text: false
1270
- })
1271
- .css({width: '1em', height: '1em'})
1272
- .click(function () {
1273
- var id = $(this).data('index');
1274
- var elem = values[id];
1275
- if (typeof editLine === 'function') {
1276
- setTimeout(function () {
1277
- editLine(id, JSON.parse(JSON.stringify(values[id])), function (err, id, newValues) {
1278
- if (!err) {
1279
- if (JSON.stringify(values[id]) !== JSON.stringify(newValues)) {
1280
- onChange && onChange();
1281
- values[id] = newValues;
1282
- _values2table(id, values, onChange, onReady);
1283
- }
1284
- }
1285
- });
1286
- }, 100);
1287
- }
1288
- });
1289
- }
1290
- });
1291
-
1292
- $lines.find('.values-input').change(function () {
1293
- if ($(this).attr('type') === 'checkbox') {
1294
- if ($(this).prop('checked').toString() !== $(this).data('old-value')) onChange();
1295
- values[$(this).data('index')][$(this).data('name')] = $(this).prop('checked');
1296
- } else {
1297
- if ($(this).val() !== $(this).data('old-value')) onChange();
1298
- values[$(this).data('index')][$(this).data('name')] = $(this).val();
1299
-
1300
- }
1301
- }).keyup(function () {
1302
- $(this).trigger('change');
1303
- });
1304
- }
1305
- if (typeof onReady === 'function') onReady();
1306
- }
1307
-
1308
- /**
1309
- * Extract the values from table.
1310
- *
1311
- * This function extracts the values from edit table, that was generated with values2table function.
1312
- *
1313
- * @param {string} id name of the html element (or nothing).
1314
- * @return {object} array with values
1315
- */
1316
- function table2values(divId) {
1317
- var $div;
1318
- if (!divId) {
1319
- $div = $('body');
1320
- } else {
1321
- $div = $('#' + divId);
1322
- }
1323
- var names = [];
1324
- $div.find('.table-values th').each(function () {
1325
- var name = $(this).data('name');
1326
- if (name) {
1327
- names.push(name);
1328
- } else {
1329
- names.push('___ignore___');
1330
- }
1331
- });
1332
-
1333
- var values = [];
1334
- var j = 0;
1335
- $div.find('.table-lines tr').each(function () {
1336
- values[j] = {};
1337
-
1338
- $(this).find('td').each(function () {
1339
- var $input = $(this).find('input');
1340
- if ($input.length) {
1341
- var name = $input.data('name');
1342
- if ($input.attr('type') === 'checkbox') {
1343
- values[j][name] = $input.prop('checked');
1344
- } else {
1345
- values[j][name] = $input.val();
1346
- }
1347
- }
1348
- var $select = $(this).find('select');
1349
- if ($select.length) {
1350
- var name = $select.data('name');
1351
- values[j][name] = $select.val() || '';
1352
- }
1353
- });
1354
- j++;
1355
- });
1356
-
1357
- return values;
1358
- }
1
+ var path = location.pathname;
2
+ var parts = path.split('/');
3
+ parts.splice(-3);
4
+
5
+ var socket = io.connect('/', {path: parts.join('/') + '/socket.io'});
6
+ var query = (window.location.search || '').replace(/^\?/, '').replace(/#.*$/, '');
7
+ var args = {};
8
+ var common = null; // common information of adapter
9
+ var host = null; // host object on which the adapter runs
10
+ var changed = false;
11
+ var systemConfig;
12
+ var certs = [];
13
+ var adapter = '';
14
+ var onChangeSupported = false;
15
+
16
+ var tmp = window.location.pathname.split('/');
17
+ adapter = tmp[tmp.length - 2];
18
+
19
+ // parse parameters
20
+ query.trim().split('&').filter(function (t) {return t.trim();}).forEach(function (b, i) {
21
+ const parts = b.split('=');
22
+ if (!i && parts.length === 1 && !isNaN(parseInt(b, 10))) {
23
+ args.instance = parseInt(b, 10);
24
+ }
25
+ var name = parts[0];
26
+ args[name] = parts.length === 2 ? parts[1] : true;
27
+
28
+ if (name === 'instance') {
29
+ args.instance = parseInt( args.instance, 10) || 0;
30
+ }
31
+
32
+ if (args[name] === 'true') {
33
+ args[name] = true;
34
+ } else if (args[name] === 'false') {
35
+ args[name] = false;
36
+ }
37
+ });
38
+ var instance = args.instance;
39
+
40
+ var _adapterInstance = 'system.adapter.' + adapter + '.' + instance;
41
+
42
+ $(document).ready(function () {
43
+ 'use strict';
44
+
45
+ if (typeof noConfigDialog === 'undefined') {
46
+ // Extend dictionary with standard words for adapter
47
+ if (typeof systemDictionary === 'undefined') systemDictionary = {};
48
+
49
+ systemDictionary.save = {"en": "Save", "de": "Speichern", "ru": "Сохранить"};
50
+ systemDictionary.saveclose = {"en": "Save and close", "de": "Speichern und schließen", "ru": "Сохранить и выйти"};
51
+ systemDictionary.none = {"en": "none", "de": "keins", "ru": ""};
52
+ systemDictionary.all = {"en": "all", "de": "alle", "ru": "все"};
53
+ systemDictionary['Device list'] = {"en": "Device list", "de": "Geräteliste", "ru": "Список устройств"};
54
+ systemDictionary['new device'] = {"en": "new device", "de": "Neues Gerät", "ru": "Новое устройство"};
55
+ systemDictionary.edit = {"en": "edit", "de": "Ändern", "ru": "Изменить"};
56
+ systemDictionary.delete = {"en": "delete", "de": "Löschen", "ru": "Удалить"};
57
+ systemDictionary.ok = {"en": "Ok", "de": "Ok", "ru": "Ok"};
58
+ systemDictionary.cancel = {"en": "Cancel", "de": "Abbrechen", "ru": "Отмена"};
59
+ systemDictionary.Message = {"en": "Message", "de": "Mitteilung", "ru": "Сообщение"};
60
+ systemDictionary.close = {"en": "Close", "de": "Schließen", "ru": "Закрыть"};
61
+ systemDictionary.htooltip = {"en": "Click for help", "de": "Anclicken", "ru": "Перейти по ссылке"};
62
+
63
+ //socket.on('connection', function () {
64
+ loadSystemConfig(function () {
65
+ if (typeof translateAll === 'function') translateAll();
66
+ loadSettings(prepareTooltips);
67
+ });
68
+ //});
69
+ var $body = $('body');
70
+ $body.wrapInner('<div style="height: calc(100% - 44px); width: 100%; overflow:auto"></div>');
71
+ $body.prepend('<div class="header ui-tabs-nav ui-widget ui-widget-header ui-corner-all" style="padding: 2px" >' +
72
+ '<button id="save" class="translateB">save</button>&nbsp;' +
73
+ '<button id="saveclose" class="translateB">saveclose</button>&nbsp;' +
74
+ '<button id="close" class="translateB" style="float: right;">cancel</button>&nbsp;' +
75
+ '</div>');
76
+
77
+ $('button#save').button({icons: {primary: 'ui-icon-disk'}}).click(function () {
78
+ if (typeof save === 'undefined') {
79
+ alert('Please implement save function in your admin/index.html');
80
+ return;
81
+ }
82
+ save(function (obj, common, redirect) {
83
+ if (redirect && parent && parent.adapterRedirect) {
84
+ parent.adapterRedirect(redirect);
85
+ }
86
+ saveSettings(obj, common);
87
+ });
88
+ });
89
+ $('button#saveclose').button({icons: {
90
+ primary: 'ui-icon-disk',
91
+ secondary: 'ui-icon-close'
92
+ }}).click(function () {
93
+ if (typeof save === 'undefined') {
94
+ alert('Please implement save function in your admin/index.html');
95
+ return;
96
+ }
97
+ save(function (obj, common, redirect) {
98
+ if (redirect && parent && parent.adapterRedirect) {
99
+ parent.adapterRedirect(redirect);
100
+ }
101
+ saveSettings(obj, common, function () {
102
+ window.close();
103
+ if (parent && parent.$iframeDialog) {
104
+ parent.$iframeDialog.dialog('close');
105
+ }
106
+ });
107
+ });
108
+ });
109
+ $('button#close').button({icons: {primary: 'ui-icon-close'}}).click(function () {
110
+ window.close();
111
+ if (typeof parent !== 'undefined' && parent && parent.$iframeDialog) {
112
+ parent.$iframeDialog.dialog('close');
113
+ }
114
+ });
115
+ }
116
+
117
+ // callback if something changed
118
+ function onChange(isChanged) {
119
+ onChangeSupported = true;
120
+ if (typeof isChanged === 'boolean' && isChanged === false) {
121
+ changed = false;
122
+ $('#save').button('disable');
123
+ $('#saveclose').button('disable');
124
+ $('#close .ui-button-text').html(_('close'));
125
+ } else {
126
+ changed = true;
127
+ $('#save').button('enable');
128
+ $('#saveclose').button('enable');
129
+ $('#close .ui-button-text').html(_('cancel'));
130
+ }
131
+ }
132
+ });
133
+
134
+ function saveSettings(native, common, callback) {
135
+ if (typeof common === 'function') {
136
+ callback = common;
137
+ common = null;
138
+ }
139
+
140
+ socket.emit('getObject', _adapterInstance, function (err, oldObj) {
141
+ if (!oldObj) oldObj = {};
142
+
143
+ for (var a in native) {
144
+ oldObj.native[a] = native[a];
145
+ }
146
+
147
+ if (common) {
148
+ for (var b in common) {
149
+ oldObj.common[b] = common[b];
150
+ }
151
+ }
152
+ socket.emit('setObject', _adapterInstance, oldObj, function (err) {
153
+ if (err) {
154
+ showMessage(err, _('Error'), 'alert');
155
+ return;
156
+ }
157
+ changed = false;
158
+ if (onChangeSupported) {
159
+ $('#save').button('disable');
160
+ $('#saveclose').button('disable');
161
+ $('#close .ui-button-text').html(_('close'));
162
+ }
163
+ if (callback) callback();
164
+ });
165
+ });
166
+ }
167
+
168
+ // Read language settings
169
+ function loadSystemConfig(callback) {
170
+ socket.emit('getObject', 'system.config', function (err, res) {
171
+ if (!err && res && res.common) {
172
+ systemLang = res.common.language || systemLang;
173
+ systemConfig = res;
174
+ }
175
+ socket.emit('getObject', 'system.certificates', function (err, res) {
176
+ if (!err && res) {
177
+ if (res.native && res.native.certificates) {
178
+ certs = [];
179
+ for (var c in res.native.certificates) {
180
+ if (res.native.certificates.hasOwnProperty(c) && !res.native.certificates[c]) continue;
181
+ var _cert = {
182
+ name: c,
183
+ type: (res.native.certificates[c].substring(0, '-----BEGIN RSA PRIVATE KEY'.length) === '-----BEGIN RSA PRIVATE KEY' || res.native.certificates[c].substring(0, '-----BEGIN PRIVATE KEY'.length) === '-----BEGIN PRIVATE KEY') ? 'private' : 'public'
184
+ };
185
+ if (_cert.type === 'public') {
186
+ var m = res.native.certificates[c].split('-----END CERTIFICATE-----');
187
+ var count = 0;
188
+ for (var _m = 0; _m < m.length; _m++) {
189
+ if (m[_m].replace(/[\r\n|\r|\n]+/, '').trim()) count++;
190
+ }
191
+ if (count > 1) _cert.type = 'chained';
192
+ }
193
+
194
+ certs.push(_cert);
195
+ }
196
+ }
197
+ }
198
+ if (callback) callback();
199
+ });
200
+ });
201
+ }
202
+
203
+ function loadSettings(callback) {
204
+ socket.emit('getObject', _adapterInstance, function (err, res) {
205
+ if (!err && res && res.native) {
206
+ $('.adapter-instance').html(adapter + '.' + instance);
207
+ $('.adapter-config').html('system.adapter.' + adapter + '.' + instance);
208
+ common = res.common;
209
+ if (res.common && res.common.name) $('.adapter-name').html(res.common.name);
210
+ if (typeof load === 'undefined') {
211
+ alert('Please implement save function in your admin/index.html');
212
+ } else {
213
+ load(res.native, onChange);
214
+ }
215
+ if (typeof callback === 'function') {
216
+ callback();
217
+ }
218
+ } else {
219
+ if (typeof callback === 'function') {
220
+ callback();
221
+ }
222
+ alert('error loading settings for ' + _adapterInstance + '\n\n' + err);
223
+ }
224
+ });
225
+ }
226
+
227
+ function prepareTooltips() {
228
+ $('.admin-icon').each(function () {
229
+ var id = $(this).data('id');
230
+ if (!id) {
231
+ var $prev = $(this).prev();
232
+ var $input = $prev.find('input');
233
+ if (!$input.length) $input = $prev.find('select');
234
+ if (!$input.length) $input = $prev.find('textarea');
235
+
236
+ if (!$input.length) {
237
+ $prev = $prev.parent();
238
+ $input = $prev.find('input');
239
+ if (!$input.length) $input = $prev.find('select');
240
+ if (!$input.length) $input = $prev.find('textarea');
241
+ }
242
+ if ($input.length) id = $input.attr('id');
243
+ }
244
+
245
+ if (!id) return;
246
+
247
+ var tooltip = '';
248
+ if (systemDictionary['tooltip_' + id]) {
249
+ tooltip = systemDictionary['tooltip_' + id][systemLang] || systemDictionary['tooltip_' + id].en;
250
+ }
251
+
252
+ var icon = '';
253
+ var link = $(this).data('link');
254
+ if (link) {
255
+ if (link === true) {
256
+ if (common.readme) {
257
+ link = common.readme + '#' + id;
258
+ } else {
259
+ link = 'https://github.com/ioBroker/ioBroker.' + common.name + '#' + id;
260
+ }
261
+ }
262
+ if (!link.match('^https?:\/\/')) {
263
+ if (common.readme) {
264
+ link = common.readme + '#' + link;
265
+ } else {
266
+ link = 'https://github.com/ioBroker/ioBroker.' + common.name + '#' + link;
267
+ }
268
+ }
269
+ icon += '<a class="admin-tooltip-link" target="config_help" href="' + link + '" title="' + (tooltip || systemDictionary.htooltip[systemLang]) + '"><img class="admin-tooltip-icon" src="../../img/info.png" /></a>';
270
+ } else if (tooltip) {
271
+ icon += '<img class="admin-tooltip-icon" title="' + tooltip + '" src="../../img/info.png"/>';
272
+ }
273
+
274
+ if (icon) {
275
+ $(this).html(icon);
276
+ }
277
+ });
278
+ $('.admin-text').each(function () {
279
+ var id = $(this).data('id');
280
+ if (!id) {
281
+ var $prev = $(this).prev();
282
+ var $input = $prev.find('input');
283
+ if (!$input.length) $input = $prev.find('select');
284
+ if (!$input.length) $input = $prev.find('textarea');
285
+ if (!$input.length) {
286
+ $prev = $prev.parent();
287
+ $input = $prev.find('input');
288
+ if (!$input.length) $input = $prev.find('select');
289
+ if (!$input.length) $input = $prev.find('textarea');
290
+ }
291
+ if ($input.length) id = $input.attr('id');
292
+ }
293
+
294
+ if (!id) return;
295
+
296
+ // check if translation for this exist
297
+ if (systemDictionary['info_' + id]) {
298
+ $(this).html('<span class="admin-tooltip-text">' + (systemDictionary['info_' + id][systemLang] || systemDictionary['info_' + id].en) + '</span>');
299
+ }
300
+ });
301
+ }
302
+
303
+ function showMessage(message, title, icon, width) {
304
+ var $dialogMessage = $('#dialog-message-settings');
305
+ if (!$dialogMessage.length) {
306
+ $('body').append('<div id="dialog-message-settings" title="Message" style="display: none">\n' +
307
+ '<p>' +
308
+ '<span id="dialog-message-icon-settings" class="ui-icon ui-icon-circle-check" style="float :left; margin: 0 7px 50px 0;"></span>\n' +
309
+ '<span id="dialog-message-text-settings"></span>\n' +
310
+ '</p>\n' +
311
+ '</div>');
312
+ $dialogMessage = $('#dialog-message-settings');
313
+ $dialogMessage.dialog({
314
+ autoOpen: false,
315
+ modal: true,
316
+ buttons: [
317
+ {
318
+ text: _('Ok'),
319
+ click: function () {
320
+ $(this).dialog('close');
321
+ }
322
+ }
323
+ ]
324
+ });
325
+ }
326
+ $dialogMessage.dialog('option', 'width', width + 500);
327
+
328
+ if (typeof _ != 'undefined') {
329
+ $dialogMessage.dialog('option', 'title', title || _('Message'));
330
+ } else {
331
+ $dialogMessage.dialog('option', 'title', title || 'Message');
332
+ }
333
+ $('#dialog-message-text-settings').html(message);
334
+ if (icon) {
335
+ $('#dialog-message-icon-settings').show();
336
+ $('#dialog-message-icon-settings').attr('class', '');
337
+ $('#dialog-message-icon-settings').addClass('ui-icon ui-icon-' + icon);
338
+ } else {
339
+ $('#dialog-message-icon-settings').hide();
340
+ }
341
+ $dialogMessage.dialog('open');
342
+ }
343
+
344
+ function confirmMessage(message, title, icon, buttons, callback) {
345
+ var $dialogConfirm = $('#dialog-confirm-settings');
346
+ if (!$dialogConfirm.length) {
347
+ $('body').append('<div id="dialog-confirm-settings" title="Message" style="display: none">\n' +
348
+ '<p>' +
349
+ '<span id="dialog-confirm-icon-settings" class="ui-icon ui-icon-circle-check" style="float :left; margin: 0 7px 50px 0;"></span>\n' +
350
+ '<span id="dialog-confirm-text-settings"></span>\n' +
351
+ '</p>\n' +
352
+ '</div>');
353
+ $dialogConfirm = $('#dialog-confirm-settings');
354
+ $dialogConfirm.dialog({
355
+ autoOpen: false,
356
+ modal: true
357
+ });
358
+ }
359
+ if (typeof buttons === 'function') {
360
+ callback = buttons;
361
+ $dialogConfirm.dialog('option', 'buttons', [
362
+ {
363
+ text: _('Ok'),
364
+ click: function () {
365
+ var cb = $(this).data('callback');
366
+ $(this).data('callback', null);
367
+ $(this).dialog('close');
368
+ if (cb) cb(true);
369
+ }
370
+ },
371
+ {
372
+ text: _('Cancel'),
373
+ click: function () {
374
+ var cb = $(this).data('callback');
375
+ $(this).data('callback', null);
376
+ $(this).dialog('close');
377
+ if (cb) cb(false);
378
+ }
379
+ }
380
+
381
+ ]);
382
+ } else if (typeof buttons === 'object') {
383
+ for (var b = 0; b < buttons.length; b++) {
384
+ buttons[b] = {
385
+ text: buttons[b],
386
+ id: 'dialog-confirm-button-' + b,
387
+ click: function (e) {
388
+ var id = parseInt(e.currentTarget.id.substring('dialog-confirm-button-'.length), 10);
389
+ var cb = $(this).data('callback');
390
+ $(this).dialog('close');
391
+ if (cb) cb(id);
392
+ }
393
+ }
394
+ }
395
+ $dialogConfirm.dialog('option', 'buttons', buttons);
396
+ }
397
+
398
+ $dialogConfirm.dialog('option', 'title', title || _('Message'));
399
+ $('#dialog-confirm-text-settings').html(message);
400
+ if (icon) {
401
+ $('#dialog-confirm-icon-settings')
402
+ .show()
403
+ .attr('class', '')
404
+ .addClass('ui-icon ui-icon-' + icon);
405
+ } else {
406
+ $('#dialog-confirm-icon-settings').hide();
407
+ }
408
+ $dialogConfirm.data('callback', callback);
409
+ $dialogConfirm.dialog('open');
410
+ }
411
+
412
+ function getObject(id, callback) {
413
+ socket.emit('getObject', id, function (err, res) {
414
+ if (!err && res) {
415
+ if (callback) callback(err, res);
416
+ } else {
417
+ if (callback) callback(null);
418
+ }
419
+ });
420
+ }
421
+
422
+ function getState(id, callback) {
423
+ socket.emit('getState', id, function (err, res) {
424
+ if (!err && res) {
425
+ if (callback) callback(err, res);
426
+ } else {
427
+ if (callback) callback(null);
428
+ }
429
+ });
430
+ }
431
+
432
+ function getEnums(_enum, callback) {
433
+ socket.emit('getObjectView', 'system', 'enum', {startkey: 'enum.' + _enum, endkey: 'enum.' + _enum + '.\u9999'}, function (err, res) {
434
+ if (!err && res) {
435
+ var _res = {};
436
+ for (var i = 0; i < res.rows.length; i++) {
437
+ if (res.rows[i].id === 'enum.' + _enum) continue;
438
+ _res[res.rows[i].id] = res.rows[i].value;
439
+ }
440
+ if (callback) callback(null, _res);
441
+ } else {
442
+ if (callback) callback(err, []);
443
+ }
444
+ });
445
+ }
446
+
447
+ function getGroups(callback) {
448
+ socket.emit('getObjectView', 'system', 'group', {startkey: 'system.group.', endkey: 'system.group.\u9999'}, function (err, res) {
449
+ if (!err && res) {
450
+ var _res = {};
451
+ for (var i = 0; i < res.rows.length; i++) {
452
+ _res[res.rows[i].id] = res.rows[i].value;
453
+ }
454
+ if (callback) callback(null, _res);
455
+ } else {
456
+ if (callback) callback(err, []);
457
+ }
458
+ });
459
+ }
460
+
461
+ function getUsers(callback) {
462
+ socket.emit('getObjectView', 'system', 'user', {startkey: 'system.user.', endkey: 'system.user.\u9999'}, function (err, res) {
463
+ if (!err && res) {
464
+ var _res = {};
465
+ for (var i = 0; i < res.rows.length; i++) {
466
+ _res[res.rows[i].id] = res.rows[i].value;
467
+ }
468
+ if (callback) callback(null, _res);
469
+ } else {
470
+ if (callback) callback(err, []);
471
+ }
472
+ });
473
+ }
474
+
475
+ function fillUsers(elemId, current, callback) {
476
+ getUsers(function (err, users) {
477
+ var text = '';
478
+ for (var u in users) {
479
+ text += '<option value="' + u + '" ' + ((current === u) ? 'selected' : '') + ' >' + users[u].common.name[0].toUpperCase() + users[u].common.name.substring(1) + '</option>\n';
480
+ }
481
+ $(elemId).html(text);
482
+ });
483
+ }
484
+
485
+ function getIPs(host, callback) {
486
+ if (typeof host === 'function') {
487
+ callback = host;
488
+ host = null;
489
+ }
490
+
491
+ socket.emit('getHostByIp', host || common.host, function (ip, _host) {
492
+ if (_host) {
493
+ host = _host;
494
+ var IPs4 = [{name: '[IPv4] 0.0.0.0 - ' + _('Listen on all IPs'), address: '0.0.0.0', family: 'ipv4'}];
495
+ var IPs6 = [{name: '[IPv6] ::', address: '::', family: 'ipv6'}];
496
+ if (host.native.hardware && host.native.hardware.networkInterfaces) {
497
+ for (var eth in host.native.hardware.networkInterfaces) {
498
+ for (var num = 0; num < host.native.hardware.networkInterfaces[eth].length; num++) {
499
+ if (host.native.hardware.networkInterfaces[eth][num].family != 'IPv6') {
500
+ IPs4.push({name: '[' + host.native.hardware.networkInterfaces[eth][num].family + '] ' + host.native.hardware.networkInterfaces[eth][num].address + ' - ' + eth, address: host.native.hardware.networkInterfaces[eth][num].address, family: 'ipv4'});
501
+ } else {
502
+ IPs6.push({name: '[' + host.native.hardware.networkInterfaces[eth][num].family + '] ' + host.native.hardware.networkInterfaces[eth][num].address + ' - ' + eth, address: host.native.hardware.networkInterfaces[eth][num].address, family: 'ipv6'});
503
+ }
504
+ }
505
+ }
506
+ }
507
+ for (var i = 0; i < IPs6.length; i++) {
508
+ IPs4.push(IPs6[i]);
509
+ }
510
+ callback(IPs4);
511
+ }
512
+ });
513
+ }
514
+
515
+ function fillSelectIPs(id, actualAddr, noIPv4, noIPv6, callback) {
516
+ getIPs(function (ips) {
517
+ var str = '';
518
+ for (var i = 0; i < ips.length; i++) {
519
+ if (noIPv4 && ips[i].family === 'ipv4') continue;
520
+ if (noIPv6 && ips[i].family === 'ipv6') continue;
521
+ str += '<option value="' + ips[i].address + '" ' + ((ips[i].address === actualAddr) ? 'selected' : '') + '>' + ips[i].name + '</option>';
522
+ }
523
+
524
+ $(id).html(str);
525
+ if (typeof callback === 'function') callback();
526
+ });
527
+ }
528
+
529
+ function sendTo(_adapter_instance, command, message, callback) {
530
+ socket.emit('sendTo', (_adapter_instance || adapter + '.' + instance), command, message, callback);
531
+ }
532
+
533
+ function sendToHost(host, command, message, callback) {
534
+ socket.emit('sendToHost', host || common.host, command, message, callback);
535
+ }
536
+
537
+ // fills select with names of the certificates and preselect it
538
+ function fillSelectCertificates(id, type, actualValued) {
539
+ var str = '<option value="">' + _('none') + '</option>';
540
+ for (var i = 0; i < certs.length; i++) {
541
+ if (certs[i].type != type) {
542
+ continue;
543
+ }
544
+ str += '<option value="' + certs[i].name + '" ' + ((certs[i].name === actualValued) ? 'selected' : '') + '>' + certs[i].name + '</option>';
545
+ }
546
+
547
+ $(id).html(str);
548
+ }
549
+
550
+ function getAdapterInstances(_adapter, callback) {
551
+ if (typeof _adapter === 'function') {
552
+ callback = _adapter;
553
+ _adapter = null;
554
+ }
555
+
556
+ socket.emit('getObjectView', 'system', 'instance', {startkey: 'system.adapter.' + (_adapter || adapter), endkey: 'system.adapter.' + (_adapter || adapter) + '.\u9999'}, function (err, doc) {
557
+ if (err) {
558
+ if (callback) callback ([]);
559
+ } else {
560
+ if (doc.rows.length === 0) {
561
+ if (callback) callback ([]);
562
+ } else {
563
+ var res = [];
564
+ for (var i = 0; i < doc.rows.length; i++) {
565
+ res.push(doc.rows[i].value);
566
+ }
567
+ if (callback) callback (res);
568
+ }
569
+ }
570
+
571
+ });
572
+ }
573
+
574
+ function getExtendableInstances(_adapter, callback) {
575
+ if (typeof _adapter === 'function') {
576
+ callback = _adapter;
577
+ _adapter = null;
578
+ }
579
+
580
+ socket.emit('getObjectView', 'system', 'instance', null, function (err, doc) {
581
+ if (err) {
582
+ if (callback) callback ([]);
583
+ } else {
584
+ if (doc.rows.length === 0) {
585
+ if (callback) callback ([]);
586
+ } else {
587
+ var res = [];
588
+ for (var i = 0; i < doc.rows.length; i++) {
589
+ if (doc.rows[i].value.common.webExtendable) {
590
+ res.push(doc.rows[i].value);
591
+ }
592
+ }
593
+ if (callback) callback (res);
594
+ }
595
+ }
596
+ });
597
+ }
598
+
599
+ function getIsAdapterAlive(_adapter, callback) {
600
+ if (typeof _adapter === 'function') {
601
+ callback = _adapter;
602
+ _adapter = null;
603
+ }
604
+ getState('system.adapter.' + (_adapter || adapter) + '.' + instance + '.alive', function (err, obj) {
605
+ if (!obj || !obj.val) {
606
+ callback(false);
607
+ } else {
608
+ callback(true);
609
+ }
610
+ });
611
+ }
612
+
613
+ // Adds one entry to the created with editTable table
614
+ // tabId - the id of the table used by editTable
615
+ // value - is one object to add in form {ip: '3.3.3.3', room: 'enum.room.bla3', desc: 'Bla3'}
616
+ // $grid - [optional] - object returned by editTable to speed up the addition
617
+ // _isInitial - [optional] - if it is initial fill of the table. To not trigger the onChange
618
+ function addToTable(tabId, value, $grid, _isInitial) {
619
+ $grid = $grid || $('#' + tabId);
620
+ var obj = {_id: $grid[0]._maxIdx++};
621
+ var cols = $grid[0]._cols;
622
+
623
+ for (var i = 0; i < cols.length; i++) {
624
+ if (cols[i] === 'room') {
625
+ obj[cols[i]] = ($grid[0]._rooms[value[cols[i]]]) ? $grid[0]._rooms[value[cols[i]]].common.name : value[cols[i]];
626
+ } else {
627
+ obj[cols[i]] = value[cols[i]];
628
+ }
629
+ }
630
+ obj._commands =
631
+ '<button data-' + tabId + '-id="' + obj._id + '" class="' + tabId + '-edit-submit">' + _('edit') + '</button>' +
632
+ '<button data-' + tabId + '-id="' + obj._id + '" class="' + tabId + '-delete-submit">' + _('delete') + '</button>' +
633
+ '<button data-' + tabId + '-id="' + obj._id + '" class="' + tabId + '-ok-submit" style="display:none">' + _('ok') + '</button>' +
634
+ '<button data-' + tabId + '-id="' + obj._id + '" class="' + tabId + '-cancel-submit" style="display:none">' + _('cancel') + '</button>';
635
+
636
+ $grid.jqGrid('addRowData', tabId + '_' + obj._id, obj);
637
+
638
+ _editInitButtons($grid, tabId, obj._id);
639
+
640
+ if (!_isInitial && $grid[0]._onChange) $grid[0]._onChange('add', value);
641
+ }
642
+
643
+ function _editInitButtons($grid, tabId, objId) {
644
+ var search = objId ? '[data-' + tabId + '-id="' + objId + '"]' : '';
645
+
646
+ $('.' + tabId + '-edit-submit' + search).unbind('click').button({
647
+ icons: {primary: 'ui-icon-pencil'},
648
+ text: false
649
+ }).click(function () {
650
+ var id = $(this).attr('data-' + tabId + '-id');
651
+
652
+ $('.' + tabId + '-edit-submit').hide();
653
+ $('.' + tabId + '-delete-submit').hide();
654
+ $('.' + tabId + '-ok-submit[data-' + tabId + '-id="' + id + '"]').show();
655
+ $('.' + tabId + '-cancel-submit[data-' + tabId + '-id="' + id + '"]').show();
656
+
657
+ $grid.jqGrid('editRow', tabId + '_' + id, {url: 'clientArray'});
658
+ if ($grid[0]._edited.indexOf(id) === -1) {
659
+ $grid[0]._edited.push(id);
660
+ }
661
+ changed = true;
662
+ $('#save').button('enable');
663
+ $('#saveclose').button('enable');
664
+ if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
665
+ }).css({'height': '18px', width: '22px'});
666
+
667
+ $('.' + tabId + '-delete-submit' + search).unbind('click').button({
668
+ icons: {primary: 'ui-icon-trash'},
669
+ text: false
670
+ }).click(function () {
671
+ var id = $(this).attr('data-' + tabId + '-id');
672
+ $grid.jqGrid('delRowData', tabId + '_' + id);
673
+
674
+ changed = true;
675
+ $('#save').button('enable');
676
+ $('#saveclose').button('enable');
677
+ if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
678
+
679
+ var pos = $grid[0]._edited.indexOf(id);
680
+ if (pos !== -1) {
681
+ $grid[0]._edited.splice(pos, 1);
682
+ }
683
+ if ($grid[0]._onChange) $grid[0]._onChange('del', id);
684
+ }).css({'height': '18px', width: '22px'});
685
+
686
+ $('.' + tabId + '-ok-submit' + search).unbind('click').button({
687
+ icons: {primary: 'ui-icon-check'},
688
+ text: false
689
+ }).click(function () {
690
+ var id = $(this).attr('data-' + tabId + '-id');
691
+
692
+ $('.' + tabId + '-edit-submit').show();
693
+ $('.' + tabId + '-delete-submit').show();
694
+ $('.' + tabId + '-ok-submit').hide();
695
+ $('.' + tabId + '-cancel-submit').hide();
696
+
697
+ $grid.jqGrid('saveRow', tabId + '_' + id, {url: 'clientArray'});
698
+
699
+ changed = true;
700
+ $('#save').button('enable');
701
+ $('#saveclose').button('enable');
702
+ if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
703
+
704
+ var pos = $grid[0]._edited.indexOf(id);
705
+ if (pos !== -1) {
706
+ $grid[0]._edited.splice(pos, 1);
707
+ }
708
+ if ($grid[0]._onChange) $grid[0]._onChange('changed', $grid.jqGrid('getRowData', tabId + '_' + id));
709
+ }).css({'height': '18px', width: '22px'});
710
+
711
+ $('.' + tabId + '-cancel-submit' + search).unbind('click').button({
712
+ icons: {primary: 'ui-icon-close'},
713
+ text: false
714
+ }).click(function () {
715
+ var id = $(this).attr('data-' + tabId + '-id');
716
+
717
+ $('.' + tabId + '-edit-submit').show();
718
+ $('.' + tabId + '-delete-submit').show();
719
+ $('.' + tabId + '-ok-submit').hide();
720
+ $('.' + tabId + '-cancel-submit').hide();
721
+
722
+ $grid.jqGrid('restoreRow', tabId + '_' + id, false);
723
+ var pos = $grid[0]._edited.indexOf(id);
724
+ if (pos !== -1) {
725
+ $grid[0]._edited.splice(pos, 1);
726
+ }
727
+ }).css({'height': '18px', width: '22px'});
728
+ }
729
+
730
+ function _editTable(tabId, cols, values, rooms, top, onChange) {
731
+ var title = 'Device list';
732
+ if (typeof tabId === 'object') {
733
+ cols = tabId.cols;
734
+ values = tabId.values;
735
+ rooms = tabId.rooms;
736
+ top = tabId.top;
737
+ onChange = tabId.onChange;
738
+ if (tabId.title) title = tabId.title;
739
+ tabId = tabId.tabId;
740
+ }
741
+
742
+ initGridLanguage(systemLang);
743
+ var colNames = [];
744
+ var colModel = [];
745
+ var $grid = $('#' + tabId);
746
+ var room;
747
+
748
+ colNames.push('id');
749
+ colModel.push({
750
+ name: '_id',
751
+ index: '_id',
752
+ hidden: true
753
+ });
754
+ for (var i = 0; i < cols.length; i++) {
755
+ var width = null;
756
+ var checkbox = null;
757
+
758
+ if (typeof cols[i] === 'object') {
759
+ width = cols[i].width;
760
+ if (cols[i].checkbox) checkbox = true;
761
+ cols[i] = cols[i].name;
762
+ }
763
+ colNames.push(_(cols[i]));
764
+ var _obj = {
765
+ name: cols[i],
766
+ index: cols[i],
767
+ // width: 160,
768
+ editable: true
769
+ };
770
+ if (width) _obj.width = width;
771
+ if (checkbox) {
772
+ _obj.edittype = 'checkbox';
773
+ _obj.editoptions = {value: 'true:false'};
774
+ }
775
+
776
+ if (cols[i] === 'room') {
777
+ var list = {'': _('none')};
778
+ for (room in rooms) {
779
+ list[room] = _(rooms[room].common.name);
780
+ }
781
+ _obj.stype = 'select';
782
+ _obj.edittype = 'select';
783
+ _obj.editoptions = {value: list};
784
+ _obj.searchoptions = {
785
+ sopt: ['eq'],
786
+ value: ':' + _('all')
787
+ };
788
+ for (room in rooms) {
789
+ _obj.searchoptions.value += ';' + _(rooms[room].common.name) + ':' + _(rooms[room].common.name);
790
+ }
791
+ }
792
+ colModel.push(_obj);
793
+ }
794
+ colNames.push('');
795
+ colModel.push({name: '_commands', index: '_commands', width: 50, editable: false, align: 'center', search: false});
796
+
797
+ $grid[0]._cols = cols;
798
+ $grid[0]._rooms = rooms;
799
+ $grid[0]._maxIdx = 0;
800
+ $grid[0]._top = top;
801
+ $grid[0]._edited = [];
802
+ $grid[0]._onChange = onChange;
803
+
804
+ $grid.jqGrid({
805
+ datatype: 'local',
806
+ colNames: colNames,
807
+ colModel: colModel,
808
+ width: 800,
809
+ height: 330,
810
+ pager: $('#pager-' + tabId),
811
+ rowNum: 1000,
812
+ rowList: [1000],
813
+ ondblClickRow: function (rowid) {
814
+ var id = rowid.substring((tabId + '_').length);
815
+ $('.' + tabId + '-edit-submit').hide();
816
+ $('.' + tabId + '-delete-submit').hide();
817
+ $('.' + tabId + '-ok-submit[data-' + tabId + '-id="' + id + '"]').show();
818
+ $('.' + tabId + '-cancel-submit[data-' + tabId + '-id="' + id + '"]').show();
819
+ $grid.jqGrid('editRow', rowid, {url: 'clientArray'});
820
+ if ($grid[0]._edited.indexOf(id) === -1) $grid[0]._edited.push(id);
821
+
822
+ changed = true;
823
+ $('#save').button('enable');
824
+ $('#saveclose').button('enable');
825
+ if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
826
+ },
827
+ sortname: 'id',
828
+ sortorder: 'desc',
829
+ viewrecords: false,
830
+ pgbuttons: false,
831
+ pginput: false,
832
+ pgtext: false,
833
+ caption: _(title),
834
+ ignoreCase: true,
835
+ loadComplete: function () {
836
+ _editInitButtons($grid, tabId);
837
+ },
838
+ onSortCol: function () {
839
+ changed = true;
840
+ $('#save').button('enable');
841
+ $('#saveclose').button('enable');
842
+ if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
843
+ }
844
+ }).jqGrid('filterToolbar', {
845
+ defaultSearch: 'cn',
846
+ autosearch: true,
847
+ searchOnEnter: false,
848
+ enableClear: false,
849
+ afterSearch: function () {
850
+ _editInitButtons($grid, tabId);
851
+ }
852
+ });
853
+
854
+ $('#pager-' + tabId + '_center').hide();
855
+
856
+ if ($('#pager-' + tabId).length) {
857
+ $grid.navGrid('#pager-' + tabId, {
858
+ search: false,
859
+ edit: false,
860
+ add: false,
861
+ del: false,
862
+ refresh: false
863
+ }).jqGrid('navButtonAdd', '#pager-' + tabId, {
864
+ caption: '',
865
+ buttonicon: 'ui-icon-plus',
866
+ onClickButton: function () {
867
+ // Find new unique name
868
+ var found;
869
+ var newText = _('New');
870
+ var ids = $grid.jqGrid('getDataIDs');
871
+ var idx = 1;
872
+ var obj;
873
+ do {
874
+ found = true;
875
+ for (var _id = 0; _id < ids.length; _id++) {
876
+ obj = $grid.jqGrid('getRowData', ids[_id]);
877
+ if (obj && obj[$grid[0]._cols[0]] === newText + idx) {
878
+ idx++;
879
+ found = false;
880
+ break;
881
+ }
882
+ }
883
+ } while (!found);
884
+
885
+ obj = {};
886
+ for (var t = 0; t < $grid[0]._cols.length; t++) {
887
+ obj[$grid[0]._cols[t]] = '';
888
+ }
889
+ obj[$grid[0]._cols[0]] = newText + idx;
890
+
891
+ changed = true;
892
+ $('#save').button('enable');
893
+ $('#saveclose').button('enable');
894
+ if (onChangeSupported) $('#close .ui-button-text').html(_('cancel'));
895
+
896
+ addToTable(tabId, obj, $grid);
897
+ },
898
+ position: 'first',
899
+ id: 'add-cert',
900
+ title: _('new device'),
901
+ cursor: 'pointer'
902
+ });
903
+ }
904
+
905
+ if (values) {
906
+ for (var u = 0; u < values.length; u++) {
907
+ addToTable(tabId, values[u], $grid, true);
908
+ }
909
+ }
910
+ $(window).resize(function () {
911
+ $grid.setGridHeight($(this).height() - top).setGridWidth($(this).width() - 10);
912
+ });
913
+ $(window).trigger('resize');
914
+
915
+ // hide scrollbar
916
+ $('.ui-jqgrid-bdiv').css({'overflow-x': 'hidden'});
917
+
918
+ return $grid;
919
+ }
920
+
921
+ // converts "enum.room.Sleeping_room" to "Sleeping room"
922
+ // As input gets the list from getEnum
923
+ function enumName2Id(enums, name) {
924
+ for (var enumId in enums) {
925
+ if (enums[enumId].common.name === name) return enumId;
926
+ if (enums[enumId].name && enums[enumId].name === name) return enumId;
927
+ }
928
+ return '';
929
+ }
930
+
931
+ // Creates edit table for any configuration array
932
+ // tabId - is id of table where the jqGrid must be created. E.g: <table id="devices"></table><div id="pager-devices"></div>
933
+ // cols - array with names of the properties of entry. E.g: ['ip', 'room', 'desc']
934
+ // if column has name room, for that will be automatically the room enums loaded and shown
935
+ // values - array with values in form [{ip: '1.1.1.1', room: 'enum.room.bla1', desc: 'Bla1'}, {ip: '2.2.2.2', room: 'enum.room.bla2', desc: 'Bla2'}
936
+ // top - top position of the table to set the height of the table automatically. Table must be always as last on the page.
937
+ // onChange - callback called if something is changed in the table
938
+ //
939
+ // returns the jquery object of $('#tabId')
940
+ // To extract data from table
941
+ function editTable(tabId, cols, values, top, onChange) {
942
+ if (typeof tabId === 'object') {
943
+ cols = tabId.cols;
944
+ }
945
+
946
+ if (cols.indexOf('room') !== -1) {
947
+ getEnums('rooms', function (err, list) {
948
+ return _editTable(tabId, cols, values, list, top, onChange);
949
+ });
950
+ } else {
951
+ return _editTable(tabId, cols, values, null, top, onChange);
952
+ }
953
+ }
954
+
955
+ // Extract edited array from table
956
+ // tabId - is id of table where the jqGrid must be created. E.g: <table id="devices"></table><div id="pager-devices"></div>
957
+ // cols - array with names of the properties of entry. E.g: ['ip', 'room', 'desc']
958
+ //
959
+ // Returns array with values
960
+ function getTableResult(tabId, cols) {
961
+ var $grid = $('#' + tabId);
962
+ for (var j = 0; j < $grid[0]._edited.length; j++) {
963
+ $grid.jqGrid('saveRow', tabId + '_' + $grid[0]._edited[j], {url: 'clientArray'});
964
+ }
965
+
966
+ $('.' + tabId + '-edit-submit').show();
967
+ $('.' + tabId + '-delete-submit').show();
968
+ $('.' + tabId + '-ok-submit').hide();
969
+ $('.' + tabId + '-cancel-submit').hide();
970
+
971
+ var data = $grid.jqGrid('getRowData');
972
+ var res = [];
973
+ for (var i = 0; i < data.length; i++) {
974
+ var obj = {};
975
+ for (var z = 0; z < cols.length; z++) {
976
+ if (cols[z] === 'room') {
977
+ obj[cols[z]] = enumName2Id($grid[0]._rooms, data[i][cols[z]]);
978
+ } else {
979
+ obj[cols[z]] = data[i][cols[z]];
980
+ }
981
+ }
982
+ res.push(obj);
983
+ }
984
+ return res;
985
+ }
986
+
987
+ /*
988
+ <div id="values">
989
+ <button class="table-button-add" style="margin-left: 10px"></button>
990
+ <table class="table-values" style="width: 100%; calc(100% - 200px)">
991
+ <thead>
992
+ <tr>
993
+ <th data-name="regex" style="width: 30%" class="translate">Context</th>
994
+ <th data-name="room" class="translate" data-type="select">Room</th>
995
+ <th data-name="aaa" class="translate" data-options="1/A;2/B;3/C;4" data-type="select">Room</th>
996
+ <th data-name="enabled" class="translate" data-type="checkbox">Enabled</th>
997
+ <th data-buttons="delete up down" style="width: 32px"></th>
998
+ </tr>
999
+ </thead>
1000
+ </table>
1001
+ </td>
1002
+ */
1003
+
1004
+ /**
1005
+ * Create edit table from javascript array.
1006
+ *
1007
+ * This function creates a html edit table.
1008
+ *
1009
+ * <pre><code>
1010
+ * <div id="values" style="width: 100%; height: calc(100% - 205px)">
1011
+ * <button class="table-button-add" style="margin-left: 10px"></button>
1012
+ * <div style="width: 100%; height: calc(100% - 30px); overflow: auto;">
1013
+ * <table class="table-values" style="width: 100%;">
1014
+ * <thead>
1015
+ * <tr>
1016
+ * <th data-name="_index" style="30px" data-style="width: 100%; text-align: right">Context</th>
1017
+ * <th data-name="regex" class="translate" style="width: 30%" data-style="text-align: right">Context</th>
1018
+ * <th data-name="room" class="translate" data-type="select">Room</th>
1019
+ * <th data-name="aaa" class="translate" data-options="1/A;2/B;3/C;4" data-type="select">Room</th>
1020
+ * <th data-name="enabled" class="translate" data-type="checkbox">Enabled</th>
1021
+ * <th data-buttons="delete up down" style="width: 32px"></th>
1022
+ * </tr>
1023
+ * </thead>
1024
+ * </table>
1025
+ * </div>
1026
+ * </div>
1027
+ * <pre><code>
1028
+ *
1029
+ * @param {string} id name of the html element (or empty).
1030
+ * @param {string} values data array
1031
+ * @param {function} onChange this function will be called if something changed
1032
+ * @param {function} onReady called, when the table is ready (may be to modify some elements of it)
1033
+ * @return {object} array with values
1034
+ */
1035
+ function values2table(divId, values, onChange, onReady) {
1036
+ if (typeof values === 'function') {
1037
+ onChange = values;
1038
+ values = divId;
1039
+ divId = '';
1040
+ }
1041
+
1042
+ values = values || [];
1043
+ var names = [];
1044
+ var $div;
1045
+ if (!divId) {
1046
+ $div = $('body');
1047
+ } else {
1048
+ $div = $('#' + divId);
1049
+ }
1050
+ var $add = $div.find('.table-button-add');
1051
+
1052
+ if (!$add.data('inited')) {
1053
+ $add.data('inited', true);
1054
+
1055
+ $add.button({
1056
+ icons: {primary: 'ui-icon-plus'},
1057
+ text: false
1058
+ })
1059
+ //.css({width: '1em', height: '1em'})
1060
+ .click(function () {
1061
+ var $table = $div.find('.table-values');
1062
+ var values = $table.data('values');
1063
+ var names = $table.data('names');
1064
+ var obj = {};
1065
+ for (var i = 0; i < names.length; i++) {
1066
+ if (!names[i]) continue;
1067
+ obj[names[i].name] = names[i].def;
1068
+ }
1069
+ values.push(obj);
1070
+ onChange && onChange();
1071
+ setTimeout(function () {
1072
+ values2table(divId, values, onChange, onReady);
1073
+ }, 100);
1074
+ });
1075
+ }
1076
+
1077
+ if (values) {
1078
+ var buttons = [];
1079
+ var $table = $div.find('.table-values');
1080
+ $table.data('values', values);
1081
+
1082
+ // load rooms
1083
+ if (!$table.data('rooms') && $table.find('th[data-name="room"]').length) {
1084
+ getEnums('rooms', function (err, list) {
1085
+ var result = {};
1086
+ result[_('none')] = '';
1087
+ var nnames = [];
1088
+ for (var n in list) {
1089
+ nnames.push(n);
1090
+ }
1091
+ nnames.sort(function (a, b) {
1092
+ a = a.toLowerCase();
1093
+ b = b.toLowerCase();
1094
+ if (a > b) return 1;
1095
+ if (a < b) return -1;
1096
+ return 0;
1097
+ });
1098
+
1099
+ for (var l = 0; l < nnames.length; l++) {
1100
+ result[nnames[l]] = list[nnames[l]].common.name || l;
1101
+ }
1102
+ $table.data('rooms', result);
1103
+ values2table(divId, values, onChange, onReady);
1104
+ });
1105
+ return;
1106
+ }
1107
+ // load functions
1108
+ if (!$table.data('functions') && $table.find('th[data-name="func"]').length) {
1109
+ getEnums('functions', function (err, list) {
1110
+ var result = {};
1111
+ result[_('none')] = '';
1112
+ var nnames = [];
1113
+ for (var n in list) {
1114
+ nnames.push(n);
1115
+ }
1116
+ nnames.sort(function (a, b) {
1117
+ a = a.toLowerCase();
1118
+ b = b.toLowerCase();
1119
+ if (a > b) return 1;
1120
+ if (a < b) return -1;
1121
+ return 0;
1122
+ });
1123
+
1124
+ for (var l = 0; l < nnames.length; l++) {
1125
+ result[nnames[l]] = list[nnames[l]].common.name || l;
1126
+ }
1127
+ $table.data('functions', result);
1128
+ values2table(divId, values, onChange, onReady);
1129
+ });
1130
+ return;
1131
+ }
1132
+ $table.find('th').each(function () {
1133
+ var name = $(this).data('name');
1134
+ if (name) {
1135
+ var obj = {
1136
+ name: name,
1137
+ type: $(this).data('type') || 'text',
1138
+ def: $(this).data('default'),
1139
+ style: $(this).data('style')
1140
+ };
1141
+ if (obj.type === 'checkbox') {
1142
+ if (obj.def === 'false') obj.def = false;
1143
+ if (obj.def === 'true') obj.def = true;
1144
+ obj.def = !!obj.def;
1145
+ } else if (obj.type === 'select') {
1146
+ var vals = ($(this).data('options') || '').split(';');
1147
+ obj.options = {};
1148
+ for (var v = 0; v < vals.length; v++) {
1149
+ var parts = vals[v].split('/');
1150
+ obj.options[parts[0]] = _(parts[1] || parts[0]);
1151
+ if (v === 0) obj.def = (obj.def === undefined) ? parts[0] : obj.def;
1152
+ }
1153
+ } else {
1154
+ obj.def = obj.def || '';
1155
+ }
1156
+ names.push(obj);
1157
+ } else {
1158
+ names.push(null);
1159
+ }
1160
+
1161
+ name = $(this).data('buttons');
1162
+
1163
+ if (name) {
1164
+ var bs = name.split(' ');
1165
+ buttons.push(bs);
1166
+ } else {
1167
+ buttons.push(null);
1168
+ }
1169
+ });
1170
+
1171
+ $table.data('names', names);
1172
+
1173
+ var text = '';
1174
+ for (var v = 0; v < values.length; v++) {
1175
+ text += '<tr>';
1176
+
1177
+ for (var i = 0; i < names.length; i++) {
1178
+ text += '<td';
1179
+ var line = '';
1180
+ var style = '';
1181
+ if (names[i]) {
1182
+ if (names[i].name === '_index') {
1183
+ style = (names[i].style ? names[i].style : 'text-align: right;');
1184
+ line += (v + 1);
1185
+ } else
1186
+ if (names[i].type === 'checkbox') {
1187
+ line += '<input ' + (names[i].style || '') + '" class="values-input" type="checkbox" data-index="' + v + '" data-name="' + names[i].name + '" ' + (values[v][names[i].name] ? 'checked' : '') + '" data-old-value="' + (values[v][names[i].name] === undefined ? '' : values[v][names[i].name]) + '"/>';
1188
+ } else if (names[i].type === 'select') {
1189
+ line += '<select style="' + (names[i].style ? names[i].style : 'width: 100%') + '" class="values-input" data-index="' + v + '" data-name="' + names[i].name + '">';
1190
+ var options;
1191
+ if (names[i].name === 'room') {
1192
+ options = $table.data('rooms');
1193
+ } else if (names[i].name === 'func') {
1194
+ options = $table.data('functions');
1195
+ } else {
1196
+ options = names[i].options;
1197
+ }
1198
+ var val = (values[v][names[i].name] === undefined ? '' : values[v][names[i].name]);
1199
+ for (var p in options) {
1200
+ line += '<option value="' + p + '" ' + (p === val ? ' selected' : '') + '>' + options[p] + '</option>';
1201
+ }
1202
+ line += '</select>';
1203
+ } else {
1204
+ line += '<input class="values-input" style="' + (names[i].style ? names[i].style : 'width: 100%') + '" type="' + names[i].type + '" data-index="' + v + '" data-name="' + names[i].name + '"/>';
1205
+ }
1206
+ }
1207
+
1208
+ if (buttons[i]) {
1209
+ style = 'text-align: center;';
1210
+ for (var b = 0; b < buttons[i].length; b++) {
1211
+ if ((!v && buttons[i][b] === 'up') || v === values.length - 1 && buttons[i][b] === 'down') {
1212
+ line += '<button data-command="' + buttons[i][b] + '" class="values-buttons" disabled>&nbsp;</button>';
1213
+ continue;
1214
+ }
1215
+ line += '<button data-index="' + v + '" data-command="' + buttons[i][b] + '" class="values-buttons"></button>';
1216
+ }
1217
+ }
1218
+ text += ' style="' + style + '">' + line + '</td>';
1219
+ }
1220
+
1221
+ text += '</tr>';
1222
+ }
1223
+ var $lines = $('.table-lines');
1224
+ if (!$lines.length) {
1225
+ $table.append('<tbody class="table-lines"></tbody>');
1226
+ $lines = $('.table-lines');
1227
+ }
1228
+
1229
+ $lines.html(text);
1230
+
1231
+ $lines.find('.values-input').each(function () {
1232
+ var $this = $(this);
1233
+ var type = $this.attr('type');
1234
+ var name = $this.data('name');
1235
+ var id = $this.data('index');
1236
+ $this.data('old-value', values[id][name]);
1237
+ if (type === 'checkbox') {
1238
+ $this.prop('checked', values[id][name]);
1239
+ } else {
1240
+ $this.val(values[id][name]);
1241
+ }
1242
+ });
1243
+ $lines.find('.values-buttons').each(function () {
1244
+ var command = $(this).data('command');
1245
+ if (command === 'delete') {
1246
+ $(this).button({
1247
+ icons: {primary: 'ui-icon-trash'},
1248
+ text: false
1249
+ })
1250
+ .css({width: '1em', height: '1em'})
1251
+ .click(function () {
1252
+ var id = $(this).data('index');
1253
+ values.splice(id, 1);
1254
+ onChange && onChange();
1255
+ setTimeout(function () {
1256
+ values2table(divId, values, onChange, onReady);
1257
+ }, 100);
1258
+ });
1259
+ } else if (command === 'up') {
1260
+ $(this).button({
1261
+ icons: {primary: 'ui-icon-triangle-1-n'},
1262
+ text: false
1263
+ })
1264
+ .css({width: '1em', height: '1em'})
1265
+ .click(function () {
1266
+ var id = $(this).data('index');
1267
+ var elem = values[id];
1268
+ values.splice(id, 1);
1269
+ values.splice(id - 1, 0, elem);
1270
+ onChange && onChange();
1271
+ setTimeout(function () {
1272
+ values2table(id, values, onChange, onReady);
1273
+ }, 100);
1274
+ });
1275
+ } else if (command === 'down') {
1276
+ $(this).button({
1277
+ icons: {primary: 'ui-icon-triangle-1-s'},
1278
+ text: false
1279
+ })
1280
+ .css({width: '1em', height: '1em'})
1281
+ .click(function () {
1282
+ var id = $(this).data('index');
1283
+ var elem = values[id];
1284
+ values.splice(id, 1);
1285
+ values.splice(id + 1, 0, elem);
1286
+ onChange && onChange();
1287
+ setTimeout(function () {
1288
+ values2table(id, values, onChange, onReady);
1289
+ }, 100);
1290
+ });
1291
+ } else if (command === 'edit') {
1292
+ $(this).button({
1293
+ icons: {primary: 'ui-icon-pencil'},
1294
+ text: false
1295
+ })
1296
+ .css({width: '1em', height: '1em'})
1297
+ .click(function () {
1298
+ var id = $(this).data('index');
1299
+ var elem = values[id];
1300
+ if (typeof editLine === 'function') {
1301
+ setTimeout(function () {
1302
+ editLine(id, JSON.parse(JSON.stringify(values[id])), function (err, id, newValues) {
1303
+ if (!err) {
1304
+ if (JSON.stringify(values[id]) !== JSON.stringify(newValues)) {
1305
+ onChange && onChange();
1306
+ values[id] = newValues;
1307
+ _values2table(id, values, onChange, onReady);
1308
+ }
1309
+ }
1310
+ });
1311
+ }, 100);
1312
+ }
1313
+ });
1314
+ }
1315
+ });
1316
+
1317
+ $lines.find('.values-input').change(function () {
1318
+ if ($(this).attr('type') === 'checkbox') {
1319
+ if ($(this).prop('checked').toString() !== $(this).data('old-value')) onChange();
1320
+ values[$(this).data('index')][$(this).data('name')] = $(this).prop('checked');
1321
+ } else {
1322
+ if ($(this).val() !== $(this).data('old-value')) onChange();
1323
+ values[$(this).data('index')][$(this).data('name')] = $(this).val();
1324
+
1325
+ }
1326
+ }).keyup(function () {
1327
+ $(this).trigger('change');
1328
+ });
1329
+ }
1330
+ if (typeof onReady === 'function') onReady();
1331
+ }
1332
+
1333
+ /**
1334
+ * Extract the values from table.
1335
+ *
1336
+ * This function extracts the values from edit table, that was generated with values2table function.
1337
+ *
1338
+ * @param {string} id name of the html element (or nothing).
1339
+ * @return {object} array with values
1340
+ */
1341
+ function table2values(divId) {
1342
+ var $div;
1343
+ if (!divId) {
1344
+ $div = $('body');
1345
+ } else {
1346
+ $div = $('#' + divId);
1347
+ }
1348
+ var names = [];
1349
+ $div.find('.table-values th').each(function () {
1350
+ var name = $(this).data('name');
1351
+ if (name) {
1352
+ names.push(name);
1353
+ } else {
1354
+ names.push('___ignore___');
1355
+ }
1356
+ });
1357
+
1358
+ var values = [];
1359
+ var j = 0;
1360
+ $div.find('.table-lines tr').each(function () {
1361
+ values[j] = {};
1362
+
1363
+ $(this).find('td').each(function () {
1364
+ var $input = $(this).find('input');
1365
+ if ($input.length) {
1366
+ var name = $input.data('name');
1367
+ if ($input.attr('type') === 'checkbox') {
1368
+ values[j][name] = $input.prop('checked');
1369
+ } else {
1370
+ values[j][name] = $input.val();
1371
+ }
1372
+ }
1373
+ var $select = $(this).find('select');
1374
+ if ($select.length) {
1375
+ var name = $select.data('name');
1376
+ values[j][name] = $select.val() || '';
1377
+ }
1378
+ });
1379
+ j++;
1380
+ });
1381
+
1382
+ return values;
1383
+ }