123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- // Licensed Materials - Property of IBM
- //
- // IBM Cognos Products: pps
- //
- // (C) Copyright IBM Corp. 2005, 2017
- //
- // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- //This file defines an JavaScript class that replicates the attributeTree used in PPES
- function ppesAdminATreeSortAscendingByIndex(a,b) {
- //Provides a comparison method for sorting an array of children based on the sortIndex
- if (a.type == "folder" && b.type == "folder" && a.getChild("sortIndex") && b.getChild("sortIndex")) {
- if (parseInt(a.getChild("sortIndex").value) > parseInt(b.getChild("sortIndex").value))
- return 1;
- else
- return -1;
- }
- else
- return 0;
- };
- function AttributeTreeNodeFolder(name) {
- var children = new Array();
- var childrenNames = new Array();
-
- this.name = name;
-
- this.addChild = function(name, child) {
- var idx = children.length;
- if (childrenNames[name]) {
- idx = childrenNames[name];
- } else {
- childrenNames[name] = idx;
- }
-
- children[idx] = child;
- }
-
- this.getChild = function(name) {
- return children[childrenNames[name]];
- }
-
- this.getChildren = function() {
- return children;
- }
-
- this.toString = function() {
- return "Folder(" + name + ")";
- }
-
- this.sortChildren = function() {
- children.sort(ppesAdminATreeSortAscendingByIndex);
- for (var i = 0; i < children.length; i++ ) {
- childrenNames[children[i].name] = i;
- }
- }
- }
- AttributeTreeNodeFolder.prototype.type = "folder";
- function AttributeTreeNodeInt(name, value, subType, specialized) {
- this.name = name;
- this.value = value;
- this.subType = subType;
- this.specialized = (specialized == true);
-
- this.toString = function() {
- return "Int(" + name + ") + value:" + value + " subType:" + subType + " specialized:" + specialized;
- }
- }
- AttributeTreeNodeInt.prototype.type="int";
- function AttributeTreeNodeString(name, value, subType, specialized) {
- this.name = name;
- this.value = value;
- this.subType = subType;
- this.specialized = (specialized == true);
-
- this.toString = function() {
- return "Int(" + name + ") + value:" + value + " subType:" + subType + " specialized:" + specialized;
- }
- }
- AttributeTreeNodeString.prototype.type="string";
- function AttributeTree() {
- var root = new AttributeTreeNodeFolder("TreeRoot");
-
- //public methods
-
- this.getRoot = function() {
- return root;
- }
-
- this.getNode = function(path) {
- return findNode(path);
- }
-
- this.getInt = function(path, defaultValue) {
- var node = findNode(path);
-
- if (node && node.type == "int")
- return parseInt(node.value);
- else
- return defaultValue;
- }
-
- this.getString = function(path, defaultValue) {
- var node = findNode(path);
-
- if (node && node.type == "string")
- return node.value;
- else
- return defaultValue;
- }
-
- this.getNode = function(path) {
- return findNode(path);
- }
-
- this.addInt = function(path, value) {
- var name = path.substr(path.lastIndexOf('.') + 1);
- var node = new AttributeTreeNodeInt(name, value, "", false);
-
- this.insertNode(path,node);
- }
-
- this.addString = function(path, value) {
- var name = path.substr(path.lastIndexOf('.') + 1);
- var node = new AttributeTreeNodeString(name, value, "", false);
-
- this.insertNode(path,node);
- }
-
- this.insertNode = function(path, node) {
- var name = path.substr(path.lastIndexOf('.') + 1);
- var path = path.substr(0,path.lastIndexOf('.'));
-
- var parentNode = createPath(path);
- if (parentNode.type == "folder") {
- parentNode.addChild(node.name,node);
- }
- }
-
- this.mergeIntoTree = function(path, node) {
- var name = path.substr(path.lastIndexOf('.') + 1);
- var path = path.substr(0,path.lastIndexOf('.'));
-
- var parentNode = createPath(path);
- if (parentNode.type == "folder") {
- mergeNode(parentNode, node);
- }
- }
-
- this.getSpecializedTreeCopy = function() {
- var attTree = new AttributeTree();
-
- for (var i = 0; i < root.getChildren().length; i++) {
- copySpecializedNodes(root.getChildren()[i],"",attTree);
- }
-
- return attTree;
- }
- //private methods
-
- function copySpecializedNodes(node,curPath,sTree) {
-
- var path = curPath;
- if (path)
- path += "."
- path += node.name;
-
- if (node.type == "folder") {
-
- var includedChild = false;
- for (var i = 0; i < node.getChildren().length; i++) {
- copySpecializedNodes(node.getChildren()[i],path,sTree);
- }
-
- } else if (node.specialized) {
-
- var nodeCopy;
- if (node.type == "int")
- nodeCopy = new AttributeTreeNodeInt(node.name, node.value, node.subType, node.specialized);
- else if (node.type == "string")
- nodeCopy = new AttributeTreeNodeString(node.name, node.value, node.subType, node.specialized);
-
- sTree.insertNode(path,nodeCopy);
- }
- }
-
- function mergeNode(parentNode, node) {
-
- if (parentNode.getChild(node.name)) {
- var twin = parentNode.getChild(node.name);
- if (twin.type == "folder" && node.type == "folder") {
- for (var i = 0; i < node.getChildren().length; i++)
- mergeNode(twin, node.getChildren()[i]);
- } else {
- parentNode.addChild(node.name,node);
- }
- } else {
- parentNode.addChild(node.name,node);
- }
-
- }
-
- function findNode(path) {
-
- var currentNode = root;
-
- if (path.length) {
- var nodes = path.split('.');
- for (var i = 0; i < nodes.length; i++) {
- if (currentNode && (currentNode.type == "folder"))
- currentNode = currentNode.getChild(nodes[i]);
- else
- return currentNode;
- }
- }
-
- return currentNode;
- }
-
- function createPath(path) {
-
- var currentNode = root;
-
- if (path.length) {
- var nodes = path.split('.');
- for (var i = 0; i < nodes.length; i++) {
- if (currentNode && (currentNode.type == "folder")) {
- if (!currentNode.getChild(nodes[i]))
- currentNode.addChild(nodes[i],new AttributeTreeNodeFolder(nodes[i]));
- currentNode = currentNode.getChild(nodes[i]);
- } else
- return currentNode;
- }
- }
-
- return currentNode;
- }
-
- //Debug stuff, can be removed later:
-
- function dumpNode(prefix, folderNode) {
- var str = prefix;
-
- if (folderNode.type == "folder") {
- str += "-" + folderNode.name + "\n";
-
- var children = folderNode.getChildren();
- for (var i = 0; i < children.length; i++) {
- str += dumpNode(prefix + " ", children[i]);
- }
- } else {
- str += "-" + folderNode.name + "(" + folderNode.value + ")\n";
- }
-
- return str;
- }
-
- this.toString = function() {
- //Debug helper, dumps the whole tree to a string
- return dumpNode("", root);
- }
- }
|