// Pandora FMS - http://pandorafms.com
// ==================================================
// Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
// Please see http://pandorafms.org for full contribution list
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; version 2
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
/*global $, _*/
var TreeController = {
controllers: [],
getController: function() {
var controller = {
index: -1,
recipient: "",
tree: [],
emptyMessage: "No data found.",
foundMessage: "Groups found",
errorMessage: "Error",
baseURL: "",
ajaxURL: "ajax.php",
ajaxPage: "include/ajax/tree.ajax",
detailRecipient: "",
filter: {},
counterTitles: {},
shouldHaveCounters: true,
reload: function() {
// Bad recipient
if (
typeof this.recipient == "undefined" ||
this.recipient.length == 0
) {
return;
}
function _recursiveGroupsCount(elements, childGroupsLength) {
if (typeof childGroupsLength === "undefined") {
childGroupsLength = 0;
}
_.each(elements, function(element) {
if (typeof element.children !== "undefined") {
childGroupsLength = _recursiveGroupsCount(
element.children,
childGroupsLength
);
childGroupsLength += element.children.length;
}
});
return childGroupsLength;
}
// Load branch
function _processGroup(container, elements, rootGroup) {
var $group = $("
'
);
}
$content.append(
'
' +
element.name +
""
);
break;
case "tag":
if (
typeof element.icon != "undefined" &&
element.icon.length > 0
) {
$content.append(
'
'
);
} else {
$content.append(
'
'
);
}
$content.append(
'
' +
element.name +
""
);
break;
case "services":
// Status image
if (
typeof element.statusImageHTML != "undefined" &&
element.statusImageHTML.length > 0
) {
var $statusImage = $(element.statusImageHTML);
$statusImage.addClass("agent-status");
$content.append($statusImage);
}
$content.append(
'
' +
element.name +
""
);
break;
default:
$content.append(
'
' +
element.name +
""
);
break;
}
// Load the status counters
var hasCounters = _processNodeCounters(
$content,
element.counters,
element.type
);
//Don't show empty groups
if (element.type == "agent") {
if (!hasCounters) {
return;
}
}
// If detail container exists, show the data.
if (
typeof controller.detailRecipient !== "undefined" ||
disabled == false
) {
if (element.type == "agent" || element.type == "module") {
if (typeof element.noAcl === "undefined") {
$content
.click(function(e) {
_getTreeDetailData(
element.type,
element.id,
element.serverID,
function(error, data) {
if (error) {
// console.error(error);
} else {
controller.detailRecipient
.render(element.name, data)
.open();
}
}
);
})
.css("cursor", "pointer");
}
}
}
$node
.addClass("tree-node")
.append($leafIcon)
.append($content);
container.append($node);
$node.addClass("leaf-empty");
if (
(typeof element.children != "undefined" &&
element.children.length > 0) ||
element.disabled == false
) {
// Add children
var $children = _processGroup($node, element.children);
$node.data("children", $children);
/*
if (
typeof element.searchChildren == "undefined" ||
!element.searchChildren
) {
$leafIcon.click(function(e) {
console.log(e);
e.preventDefault();
return;
if ($node.hasClass("leaf-open")) {
$node
.removeClass("leaf-open")
.addClass("leaf-closed")
.data("children")
.slideUp();
} else {
$node
.removeClass("leaf-closed")
.addClass("leaf-open")
.data("children")
.slideDown();
}
});
}
*/
}
if (
typeof element.searchChildren != "undefined" &&
element.searchChildren
) {
if (
element.rootType == "group_edition" &&
typeof element.children == "undefined"
) {
$node.addClass("leaf-empty");
} else {
$node.removeClass("leaf-empty").addClass("leaf-closed");
$leafIcon.click(function(e) {
e.preventDefault();
if (
!$node.hasClass("leaf-loading") &&
!$node.hasClass("children-loaded") &&
!$node.hasClass("leaf-empty")
) {
$node
.removeClass("leaf-closed")
.removeClass("leaf-error")
.addClass("leaf-loading");
$.ajax({
url: controller.ajaxURL,
type: "POST",
dataType: "json",
data: {
page: controller.ajaxPage,
getChildren: 1,
id: element.id,
type: element.type,
rootID: element.rootID,
serverID: element.serverID,
rootType: element.rootType,
metaID: element.metaID,
title: element.title,
filter: controller.filter,
auth_class: controller.auth_class,
id_user: controller.id_user,
auth_hash: controller.auth_hash
},
complete: function(xhr, textStatus) {
$node.removeClass("leaf-loading");
$node.addClass("children-loaded");
},
success: function(data, textStatus, xhr) {
if (data.success) {
var $group = $node.children("ul.tree-group");
if (
(typeof data.tree != "undefined" &&
data.tree.length > 0) ||
$group.length > 0
) {
$node.addClass("leaf-open");
if ($group.length <= 0) {
$group = $("
");
$group.addClass("tree-group").hide();
$node.append($group);
}
// Get the main values of the tree.
var rawTree = Object.values(data.tree);
// Sorting tree by description (services.treeview_services.php).
rawTree.sort(function(a, b) {
// Only the services are ordered since only they have the elementDescription property.
if (a.elementDescription && b.elementDescription) {
var x = a.elementDescription.toLowerCase();
var y = b.elementDescription.toLowerCase();
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
}
return 0;
});
_.each(rawTree, function(element) {
element.jqObject = _processNode($group, element);
});
$group.slideDown();
$node.data("children", $group);
// Add again the hover event to the 'force_callback' elements
forced_title_callback();
} else {
$node.addClass("leaf-empty");
}
} else {
$node.addClass("leaf-error");
}
},
error: function(xhr, textStatus, errorThrown) {
$node.addClass("leaf-error");
}
});
} else if (!$node.hasClass("leaf-empty")) {
if ($node.hasClass("leaf-open")) {
$node
.removeClass("leaf-open")
.addClass("leaf-closed")
.data("children")
.slideUp();
} else {
$node
.removeClass("leaf-closed")
.addClass("leaf-open")
.data("children")
.slideDown();
}
}
});
}
}
return $node;
}
if (controller.recipient.length == 0) {
return;
} else if (controller.tree.length == 0) {
controller.recipient.empty();
controller.recipient.html(
"
" + controller.emptyMessage + "
"
);
return;
}
controller.recipient.empty();
var $children = _processGroup(this.recipient, this.tree, true);
$children.show();
controller.recipient.data("children", $children);
// Add again the hover event to the 'force_callback' elements
forced_title_callback();
},
load: function() {
this.reload();
},
changeTree: function(tree) {
this.tree = tree;
this.reload();
},
init: function(data) {
if (
typeof data.recipient !== "undefined" &&
data.recipient.length > 0
) {
this.recipient = data.recipient;
}
if (typeof data.detailRecipient !== "undefined") {
this.detailRecipient = data.detailRecipient;
}
if (typeof data.tree !== "undefined") {
this.tree = data.tree;
}
if (
typeof data.emptyMessage !== "undefined" &&
data.emptyMessage.length > 0
) {
this.emptyMessage = data.emptyMessage;
}
if (
typeof data.foundMessage !== "undefined" &&
data.foundMessage.length > 0
) {
this.foundMessage = data.foundMessage;
}
if (
typeof data.errorMessage !== "undefined" &&
data.errorMessage.length > 0
) {
this.errorMessage = data.errorMessage;
}
if (typeof data.baseURL !== "undefined" && data.baseURL.length > 0) {
this.baseURL = data.baseURL;
}
if (typeof data.ajaxURL !== "undefined" && data.ajaxURL.length > 0) {
this.ajaxURL = data.ajaxURL;
}
if (typeof data.ajaxPage !== "undefined" && data.ajaxPage.length > 0) {
this.ajaxPage = data.ajaxPage;
}
if (typeof data.filter !== "undefined") {
this.filter = data.filter;
}
if (typeof data.auth_class !== "undefined") {
this.auth_class = data.auth_class;
}
if (typeof data.id_user !== "undefined") {
this.id_user = data.id_user;
}
if (typeof data.auth_hash !== "undefined") {
this.auth_hash = data.auth_hash;
}
if (
typeof data.tree !== "undefined" &&
Array.isArray(data.tree) &&
data.tree.length > 0 &&
data.tree[0]["rootType"] == "services"
) {
this.foundMessage = "";
}
this.load();
},
remove: function() {
if (typeof this.recipient != "undefined" && this.recipient.length > 0) {
this.recipient.empty();
}
if (this.index > -1) {
TreeController.controllers.splice(this.index, 1);
}
}
};
controller.index = TreeController.controllers.push(controller) - 1;
return controller;
}
};