attributeTree.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: pps
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2017
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //This file defines an JavaScript class that replicates the attributeTree used in PPES
  9. function ppesAdminATreeSortAscendingByIndex(a,b) {
  10. //Provides a comparison method for sorting an array of children based on the sortIndex
  11. if (a.type == "folder" && b.type == "folder" && a.getChild("sortIndex") && b.getChild("sortIndex")) {
  12. if (parseInt(a.getChild("sortIndex").value) > parseInt(b.getChild("sortIndex").value))
  13. return 1;
  14. else
  15. return -1;
  16. }
  17. else
  18. return 0;
  19. };
  20. function AttributeTreeNodeFolder(name) {
  21. var children = new Array();
  22. var childrenNames = new Array();
  23. this.name = name;
  24. this.addChild = function(name, child) {
  25. var idx = children.length;
  26. if (childrenNames[name]) {
  27. idx = childrenNames[name];
  28. } else {
  29. childrenNames[name] = idx;
  30. }
  31. children[idx] = child;
  32. }
  33. this.getChild = function(name) {
  34. return children[childrenNames[name]];
  35. }
  36. this.getChildren = function() {
  37. return children;
  38. }
  39. this.toString = function() {
  40. return "Folder(" + name + ")";
  41. }
  42. this.sortChildren = function() {
  43. children.sort(ppesAdminATreeSortAscendingByIndex);
  44. for (var i = 0; i < children.length; i++ ) {
  45. childrenNames[children[i].name] = i;
  46. }
  47. }
  48. }
  49. AttributeTreeNodeFolder.prototype.type = "folder";
  50. function AttributeTreeNodeInt(name, value, subType, specialized) {
  51. this.name = name;
  52. this.value = value;
  53. this.subType = subType;
  54. this.specialized = (specialized == true);
  55. this.toString = function() {
  56. return "Int(" + name + ") + value:" + value + " subType:" + subType + " specialized:" + specialized;
  57. }
  58. }
  59. AttributeTreeNodeInt.prototype.type="int";
  60. function AttributeTreeNodeString(name, value, subType, specialized) {
  61. this.name = name;
  62. this.value = value;
  63. this.subType = subType;
  64. this.specialized = (specialized == true);
  65. this.toString = function() {
  66. return "Int(" + name + ") + value:" + value + " subType:" + subType + " specialized:" + specialized;
  67. }
  68. }
  69. AttributeTreeNodeString.prototype.type="string";
  70. function AttributeTree() {
  71. var root = new AttributeTreeNodeFolder("TreeRoot");
  72. //public methods
  73. this.getRoot = function() {
  74. return root;
  75. }
  76. this.getNode = function(path) {
  77. return findNode(path);
  78. }
  79. this.getInt = function(path, defaultValue) {
  80. var node = findNode(path);
  81. if (node && node.type == "int")
  82. return parseInt(node.value);
  83. else
  84. return defaultValue;
  85. }
  86. this.getString = function(path, defaultValue) {
  87. var node = findNode(path);
  88. if (node && node.type == "string")
  89. return node.value;
  90. else
  91. return defaultValue;
  92. }
  93. this.getNode = function(path) {
  94. return findNode(path);
  95. }
  96. this.addInt = function(path, value) {
  97. var name = path.substr(path.lastIndexOf('.') + 1);
  98. var node = new AttributeTreeNodeInt(name, value, "", false);
  99. this.insertNode(path,node);
  100. }
  101. this.addString = function(path, value) {
  102. var name = path.substr(path.lastIndexOf('.') + 1);
  103. var node = new AttributeTreeNodeString(name, value, "", false);
  104. this.insertNode(path,node);
  105. }
  106. this.insertNode = function(path, node) {
  107. var name = path.substr(path.lastIndexOf('.') + 1);
  108. var path = path.substr(0,path.lastIndexOf('.'));
  109. var parentNode = createPath(path);
  110. if (parentNode.type == "folder") {
  111. parentNode.addChild(node.name,node);
  112. }
  113. }
  114. this.mergeIntoTree = function(path, node) {
  115. var name = path.substr(path.lastIndexOf('.') + 1);
  116. var path = path.substr(0,path.lastIndexOf('.'));
  117. var parentNode = createPath(path);
  118. if (parentNode.type == "folder") {
  119. mergeNode(parentNode, node);
  120. }
  121. }
  122. this.getSpecializedTreeCopy = function() {
  123. var attTree = new AttributeTree();
  124. for (var i = 0; i < root.getChildren().length; i++) {
  125. copySpecializedNodes(root.getChildren()[i],"",attTree);
  126. }
  127. return attTree;
  128. }
  129. //private methods
  130. function copySpecializedNodes(node,curPath,sTree) {
  131. var path = curPath;
  132. if (path)
  133. path += "."
  134. path += node.name;
  135. if (node.type == "folder") {
  136. var includedChild = false;
  137. for (var i = 0; i < node.getChildren().length; i++) {
  138. copySpecializedNodes(node.getChildren()[i],path,sTree);
  139. }
  140. } else if (node.specialized) {
  141. var nodeCopy;
  142. if (node.type == "int")
  143. nodeCopy = new AttributeTreeNodeInt(node.name, node.value, node.subType, node.specialized);
  144. else if (node.type == "string")
  145. nodeCopy = new AttributeTreeNodeString(node.name, node.value, node.subType, node.specialized);
  146. sTree.insertNode(path,nodeCopy);
  147. }
  148. }
  149. function mergeNode(parentNode, node) {
  150. if (parentNode.getChild(node.name)) {
  151. var twin = parentNode.getChild(node.name);
  152. if (twin.type == "folder" && node.type == "folder") {
  153. for (var i = 0; i < node.getChildren().length; i++)
  154. mergeNode(twin, node.getChildren()[i]);
  155. } else {
  156. parentNode.addChild(node.name,node);
  157. }
  158. } else {
  159. parentNode.addChild(node.name,node);
  160. }
  161. }
  162. function findNode(path) {
  163. var currentNode = root;
  164. if (path.length) {
  165. var nodes = path.split('.');
  166. for (var i = 0; i < nodes.length; i++) {
  167. if (currentNode && (currentNode.type == "folder"))
  168. currentNode = currentNode.getChild(nodes[i]);
  169. else
  170. return currentNode;
  171. }
  172. }
  173. return currentNode;
  174. }
  175. function createPath(path) {
  176. var currentNode = root;
  177. if (path.length) {
  178. var nodes = path.split('.');
  179. for (var i = 0; i < nodes.length; i++) {
  180. if (currentNode && (currentNode.type == "folder")) {
  181. if (!currentNode.getChild(nodes[i]))
  182. currentNode.addChild(nodes[i],new AttributeTreeNodeFolder(nodes[i]));
  183. currentNode = currentNode.getChild(nodes[i]);
  184. } else
  185. return currentNode;
  186. }
  187. }
  188. return currentNode;
  189. }
  190. //Debug stuff, can be removed later:
  191. function dumpNode(prefix, folderNode) {
  192. var str = prefix;
  193. if (folderNode.type == "folder") {
  194. str += "-" + folderNode.name + "\n";
  195. var children = folderNode.getChildren();
  196. for (var i = 0; i < children.length; i++) {
  197. str += dumpNode(prefix + " ", children[i]);
  198. }
  199. } else {
  200. str += "-" + folderNode.name + "(" + folderNode.value + ")\n";
  201. }
  202. return str;
  203. }
  204. this.toString = function() {
  205. //Debug helper, dumps the whole tree to a string
  206. return dumpNode("", root);
  207. }
  208. }