(function ($) {
'use strict';
if ($.fn.treeTable) return;
function nodeExpand() {
var id = this.id;
var $table = this.row.parent().parent(); // table > tbody > tr
var options = $table.data('options');
if (options.name) {
options.expanded = options.expanded || [];
if (options.expanded.indexOf(id) === -1) {
options.expanded.push(id);
if (typeof Storage !== 'undefined') {
window.localStorage.setItem(options.name + '-treetable', JSON.stringify(options.expanded));
}
}
}
var parentNode = $table.treetable('node', id);
}
function nodeCollapse() {
var id = this.id;
var $table = this.row.parent().parent(); // table > tbody > tr
var options = $table.data('options');
if (options.name && options.expanded) {
var pos = options.expanded.indexOf(id);
if (pos !== -1) {
options.expanded.splice(pos, 1);
if (typeof Storage !== 'undefined') {
window.localStorage.setItem(options.name + '-treetable', JSON.stringify(options.expanded));
}
}
}
var parentNode = $table.treetable('node', id);
}
function filter($table, word) {
if (word) {
word = word.toLowerCase();
var options = $table.data('options');
$table.find('tr').each(function () {
if ($(this).hasClass('tree-table-main-header')) return;
if (!$(this).data('tt-branch') && $(this).find('td:first-child').text().indexOf(word) === -1) {
$(this).addClass('filtered-out');
} else {
$(this).removeClass('filtered-out');
}
});
var branch = '';
var isOneVisible = false;
// hide branches without children
$table.find('tr').each(function () {
if ($(this).data('tt-branch')) {
if (branch) {
if (!isOneVisible) {
$table.find('tr[data-tt-id="' + branch + '"]').addClass('filtered-out');
} else {
$table.find('tr[data-tt-id="' + branch + '"]').removeClass('filtered-out');
}
}
isOneVisible = false;
branch = $(this).data('tt-id');
} else if (branch) {
if (!$(this).hasClass('filtered-out')) isOneVisible = true;
}
});
if (branch) {
if (!isOneVisible) {
$table.find('tr[data-tt-id="' + branch + '"]').addClass('filtered-out');
} else {
$table.find('tr[data-tt-id="' + branch + '"]').removeClass('filtered-out');
}
}
} else {
$table.find('tr').removeClass('filtered-out');
}
}
function processMoveTasks(options, tasks, callback) {
if (!tasks || !tasks.length) {
callback && callback();
return;
}
var task = tasks.shift();
options.moveId && options.moveId(task.oldId, task.newId, function (err) {
setTimeout(function () {
processMoveTasks(options, tasks, callback);
}, 50);
});
}
function buildList(options, noButtons) {
var table = noButtons ? '' : '
';
table += '
';
var rows = options.rows;
for (var i = 0; i < rows.length; i++) {
var parents = 0;
var current = rows[i];
while (current.parent) {
var found = false;
for (var j = 0; j < rows.length; j++) {
if (rows[j].id === current.parent) {
current = rows[j];
found = true;
break;
}
}
if (!found) break;
parents++;
}
var title = rows[i].title;
if (typeof title === 'object') {
title = title[systemLang] || title.en;
}
var isNotFolder = rows[i].instance === undefined ? 0 : 1;
table += '
' +
(!isNotFolder ? '' : '') + '' + title + '
';
}
table += '
';
var $dlg = $(this);
var $table = $(table);
$dlg.find('.treetablelist-buttons').remove();
$dlg.find('.treetable-list').remove();
$dlg.find('.tree-table-buttons').remove();
$dlg.find('.tree-table-main').remove();
$dlg.prepend($table);
var $buttons = $($table[0]);
var $list = $($table[1]);
$list.sortable({
cancel: '.treetable-list-folder',
axis: 'y'
}).data('options', options);
var that = this;
$buttons.find('.treetable-list-btn-ok').button({
icons: {primary: 'ui-icon-check'},
text: false
})
.css({width: 24, height: 24})
.on('click', function () {
// analyse new structure
var currentFolder = '';
var tasks = [];
$list.find('li').each(function () {
var id = $(this).data('id');
if ($(this).hasClass('treetable-list-folder')) {
currentFolder = id;
} else {
var parts = id.split('.');
var name = parts.pop();
if (parts.join('.') !== currentFolder) {
tasks.push({oldId: id, newId: currentFolder + '.' + name});
}
}
});
processMoveTasks(options, tasks, function () {
buildTable.call(that, options);
});
});
$buttons.find('.treetable-list-btn-cancel').button({
icons: {primary: 'ui-icon-cancel'},
text: false
})
.css({width: 24, height: 24})
.on('click', function () {
buildTable.call(that, options);
});
}
function getIconFromObj(obj, imgPath, classes) {
var icon = '';
var alt = '';
var isCommon = obj && obj.common;
if (isCommon) {
if (isCommon.icon) {
if (!isCommon.icon.match(/^data:image\//)) {
if (isCommon.icon.indexOf('.') !== -1) {
var instance;
if (obj.type === 'instance') {
icon = '/adapter/' + obj.common.name + '/' + obj.common.icon;
} else if (obj._id.match(/^system\.adapter\./)) {
instance = obj._id.split('.', 3);
if (obj.common.icon[0] === '/') {
instance[2] += obj.common.icon;
} else {
instance[2] += '/' + obj.common.icon;
}
icon = '/adapter/' + instance[2];
} else {
instance = obj._id.split('.', 2);
if (obj.common.icon[0] === '/') {
instance[0] += obj.common.icon;
} else {
instance[0] += '/' + obj.common.icon;
}
icon = '/adapter/' + instance[0];
}
} else {
return '' + isCommon.icon + '';
}
} else {
icon = isCommon.icon;
}
alt = obj.type;
} else {
imgPath = imgPath || 'lib/css/fancytree/';
if (obj.type === 'device') {
icon = imgPath + 'device.png';
alt = 'device';
} else if (obj.type === 'channel') {
icon = imgPath + 'channel.png';
alt = 'channel';
} else if (obj.type === 'state') {
icon = imgPath + 'state.png';
alt = 'state';
}
}
}
if (icon) return '';
return '';
}
// https://stackoverflow.com/questions/35969656/how-can-i-generate-the-opposite-color-according-to-current-color
/*function invertColor(hex) {
if (hex.indexOf('#') === 0) {
hex = hex.slice(1);
}
// convert 3-digit hex to 6-digits.
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
if (hex.length !== 6) {
return false;
}
var r = parseInt(hex.slice(0, 2), 16),
g = parseInt(hex.slice(2, 4), 16),
b = parseInt(hex.slice(4, 6), 16);
// http://stackoverflow.com/a/3943023/112731
return (r * 0.299 + g * 0.587 + b * 0.114) <= 186;
}*/
function getUsersGroups(objects, groups) {
var usersGroups = {};
for (var g = 0; g < groups.length; g++) {
if (objects[groups[g]] && objects[groups[g]].common && objects[groups[g]].common.members) {
var users = objects[groups[g]].common.members;
for (var u = 0; u < users.length; u++) {
usersGroups[users[u]] = usersGroups[users[u]] || [];
var name = objects[groups[g]].common.name;
if (name && typeof name === 'object') {
name = name[systemLang] || 'en';
}
usersGroups[users[u]].push({id: groups[g], name: name || id.replace('system.group.', '')});
}
}
}
return usersGroups;
}
function buildTable(options) {
var table = '';
// detect materialize
var buttonTag = !options.isMaterial ? 'button' : 'a';
if (options.panelButtons) {
table += '