From 92c303ef5f9b48803f78d63e72e53a77d410b77f Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Fri, 21 Jul 2017 16:47:53 +0200 Subject: [PATCH 01/28] Refactor search(), demo added --- .gitignore | 1 + cosmoz-default-tree.html | 18 +++++------ cosmoz-tree.html | 65 ++++++++++++++++++++++++++++++++-------- demo/index.html | 37 ++++++++++++++++++++++- 4 files changed, 98 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index e0241d9..dc7f924 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bower_components/ node_modules/ debug.log +.DS_Store diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index 7458c72..ee37119 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -72,16 +72,14 @@ return node[propertyName]; }; - Cosmoz.DefaultTree.prototype._searchAllRoots = function (propertyName, propertyValue) { - var i, - node; - - for (i = 0; i < this._roots.length; i+=1) { - node = this.search(this._roots[i], propertyName, propertyValue); - if (node !== undefined) { - return node; - } - } + Cosmoz.DefaultTree.prototype._searchAllRoots = function (propertyName, propertyValue, exact = true, all = false) { + var results = []; + + this._roots.forEach(function (rootObj) { + results = results.concat(this.search(rootObj, propertyName, propertyValue, exact, all)); + }, this); + + return results; }; Cosmoz.DefaultTree.prototype._getNodeByPathLocator = function (pathLocator) { diff --git a/cosmoz-tree.html b/cosmoz-tree.html index f685429..a17f5e6 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -52,26 +52,67 @@ } }, + nodeConformsSearch: function (node, propertyName, propertyValue, exact) { + var property = this.getProperty(node, propertyName); + + switch (exact) { + case true: + if (property === propertyValue) { + return true; + } + break; + + case false: + if (property.toLowerCase().indexOf(propertyValue.toLowerCase()) > -1) { + return true; + } + break; + + default: + return false; + break; + } + + return false; + }, + + /** + * After node found and before pushed to results array. + */ + afterFound: function (node) { + return node; + }, + /** * Basic search for a node by one of its property */ - search: function (node, propertyName, propertyValue) { - if (this.getProperty(node, propertyName) === propertyValue) { - return node; + search: function (node, propertyName, propertyValue, exact = true, all = false, results = []) { + var nodeConforms = this.nodeConformsSearch(node, propertyName, propertyValue, exact), + children, + result, + i; + + if (nodeConforms) { + if (all) { + results.push(this.afterFound(node)); + } else { + return node; + } } - var children = this.getChildren(node), - i = 0, - n; + children = this.getChildren(node); - if (children) { - for (i = 0; i < children.length; i+=1) { - n = this.search(children[i], propertyName, propertyValue); - if (n !== undefined) { - return n; + if (children && Array.isArray(children)) { + for (i = 0; i < children.length; i++) { + var child = children[i]; + result = this.search(children[i], propertyName, propertyValue, exact, all, results); + if (!Array.isArray(result)) { + return result; } - } + } } + + return results; } }; diff --git a/demo/index.html b/demo/index.html index 79323d2..8158178 100644 --- a/demo/index.html +++ b/demo/index.html @@ -10,6 +10,7 @@ + @@ -21,7 +22,41 @@ From 9dca38a4bd54bd9136c71281b36afead8dbea02f Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Fri, 21 Jul 2017 17:05:16 +0200 Subject: [PATCH 02/28] Cleanup & Demo via iron-component-page --- cosmoz-default-tree.html | 9 +++++++++ cosmoz-tree.html | 7 +++++++ demo/index.html | 20 +------------------- index.html | 1 + 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index ee37119..ca809fa 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -1,7 +1,16 @@ + + + diff --git a/cosmoz-tree.html b/cosmoz-tree.html index 0db74ae..239a78f 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -29,15 +29,11 @@ throw new Error('Must be implemented in derived object'); }, - getPath: function (node) { + getPathString: function (node) { throw new Error('Must be implemented in derived object'); }, - getPathByProperty: function (propertyName, propertyValue) { - throw new Error('Must be implemented in derived object'); - }, - - getChildren: function (node) { + getChildren: function (node, asList) { throw new Error('Must be implemented in derived object'); }, @@ -45,20 +41,9 @@ throw new Error('Must be implemented in derived object'); }, - getPathString: function (node, textPropertyName, separator) { - var path = this.getPath(node); - return path.map(function (n) { - return this.getProperty(n, textPropertyName); - }, this).join(separator); - }, - - getPathStringByProperty: function (propertyName, propertyValue, textPropertyName, separator) { - var node = this.getNodeByProperty(propertyName, propertyValue); - if (node !== undefined) { - return this.getPathString(node, textPropertyName, separator); - } - }, - + /** + * Returns True if a node fullfills the search criteria. + */ nodeConformsSearch: function (node, propertyName, propertyValue, exact) { var property = this.getProperty(node, propertyName); @@ -84,14 +69,15 @@ }, /** - * After node found and before pushed to results array. + * Gets called after a node has been found + * and before pushed to results array. */ afterFound: function (node) { return node; }, /** - * Basic search for a node by one of its property + * Basic search for a node by one of its properties. */ search: function (node, propertyName, propertyValue, exact = true, all = false, results = []) { var nodeConforms = this.nodeConformsSearch(node, propertyName, propertyValue, exact), @@ -107,7 +93,7 @@ } } - children = this.getChildren(node); + children = this.getChildren(node, true); if (children && Array.isArray(children)) { for (i = 0; i < children.length; i++) { From b9fb2ffdd8427045c36c774c268ae6170ddaa9c1 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Sun, 23 Jul 2017 14:39:08 +0200 Subject: [PATCH 04/28] added cosmoz-tree-behavior --- cosmoz-tree-behavior.html | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 cosmoz-tree-behavior.html diff --git a/cosmoz-tree-behavior.html b/cosmoz-tree-behavior.html new file mode 100644 index 0000000..722681c --- /dev/null +++ b/cosmoz-tree-behavior.html @@ -0,0 +1,52 @@ + + + \ No newline at end of file From f7d2fdb448901112ebd7ad7c0510e736b4b6e926 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Sun, 23 Jul 2017 18:19:10 +0200 Subject: [PATCH 05/28] added hook for tree class; added notify tree changes --- cosmoz-tree-behavior.html | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cosmoz-tree-behavior.html b/cosmoz-tree-behavior.html index 722681c..ed6f8af 100644 --- a/cosmoz-tree-behavior.html +++ b/cosmoz-tree-behavior.html @@ -6,6 +6,7 @@ /** * Use `Cosmoz.TreeBehavior` to use your properties with generic cosmoz-tree classes. + * @demo demo/index.html * @polymerBehavior Cosmoz.TreeBehavior */ Cosmoz.TreeBehavior = { @@ -35,18 +36,27 @@ } var treeClass = eval(this.properties[treePropertyKey].treeClass); + treeClass = this._getTreeClass(treeClass); + if (!treeClass) { console.error('The provided treeClass ' + this.properties[treePropertyKey].treeClass + ' is invalid or not imported.') } - Object.setPrototypeOf(this[treePropertyKey], { tree: function() { return new treeClass(this); }.call(this[treePropertyKey]) }); + + this.notifyPath(treePropertyKey + '.tree', this[treePropertyKey].tree); }; this._addObserverEffect(treePropertyKey, observer); }, this); }, + /** + * A hook to manipulate the default tree class. + */ + _getTreeClass: function (treeClass) { + return treeClass; + } }; \ No newline at end of file From dea0b9accb351cc9f464d4ba9f1e64c59139d7b5 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Sun, 23 Jul 2017 18:19:30 +0200 Subject: [PATCH 06/28] add properties to afterFound Hook --- cosmoz-tree.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cosmoz-tree.html b/cosmoz-tree.html index 239a78f..d289377 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -72,7 +72,7 @@ * Gets called after a node has been found * and before pushed to results array. */ - afterFound: function (node) { + afterFound: function (node, propertyName, propertyValue) { return node; }, @@ -87,7 +87,7 @@ if (nodeConforms) { if (all) { - results.push(this.afterFound(node)); + results.push(this.afterFound(node, propertyName, propertyValue)); } else { return node; } From 760b426569bb3455230ae7c817e4601b26802589 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Sun, 23 Jul 2017 18:19:59 +0200 Subject: [PATCH 07/28] refaktor to make it renderLevel sensitive --- cosmoz-default-tree.html | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index 2c88ac4..3b9b7a5 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -36,7 +36,7 @@ * Returns an Array with all found Nodes. * Returns an Object with the first found Node. */ - Cosmoz.DefaultTree.prototype.getNodeByProperty = function (propertyName, propertyValue, exact = true, all = false) { + Cosmoz.DefaultTree.prototype.getNodeByProperty = function (propertyName, propertyValue, exact = true, all = false, nodeList) { if (propertyName === undefined || propertyValue === undefined) { return; } @@ -45,17 +45,18 @@ return this.getNodeByPathLocator(propertyValue); } - return this._searchAllRoots(propertyName, propertyValue, exact, all); + return this._searchNodeList(propertyName, propertyValue, exact, all, nodeList); }; /** - * Loops over the _roots Array and returns the concatenated results of search(). + * Loops over a node list (or roots if not defined) and returns the concatenated results of search(). */ - Cosmoz.DefaultTree.prototype._searchAllRoots = function (propertyName, propertyValue, exact = true, all = false) { + Cosmoz.DefaultTree.prototype._searchNodeList = function (propertyName, propertyValue, exact = true, all = false, nodeList) { + nodeList = nodeList ? nodeList : this._roots; var results = []; - this._roots.forEach(function (rootObj) { - results = results.concat(this.search(rootObj, propertyName, propertyValue, exact, all)); + nodeList.forEach(function (node) { + results = results.concat(this.search(node, propertyName, propertyValue, exact, all)); }, this); return results; @@ -65,17 +66,23 @@ * Returns the Node of a given pathLocator. */ Cosmoz.DefaultTree.prototype.getNodeByPathLocator = function (pathLocator) { - var nodesOnPath = this.nodesOnPath(this._treeData, pathLocator); + if (!pathLocator) { + return this._treeData; + } + var nodesOnPath = this.getNodesOnPath(pathLocator, this._treeData); return nodesOnPath[nodesOnPath.length - 1]; }; /** * Returns an Array of nodes on a given path. + * If pathLocator is empty or not defined, the Root objects get returned. */ - Cosmoz.DefaultTree.prototype.getNodesOnPath = function (nodes, pathLocator, separatorSign = '.') { + Cosmoz.DefaultTree.prototype.getNodesOnPath = function (pathLocator, nodes, separatorSign = '.') { + if (!pathLocator) return this._roots; + var path = pathLocator.split(separatorSign), - pathSegment = nodes; - + pathSegment = nodes ? nodes : this._treeData; + return path.map(function (nodeKey) { var node = pathSegment[nodeKey], children = this.getChildren(node); @@ -90,8 +97,8 @@ /** * Returns a String (separated node comparison properties) representing the path. */ - Cosmoz.DefaultTree.prototype.getPathString = function (nodeList, pathLocator, comparisonProperty) { - var nodesOnPath = this.getNodesOnPath(nodeList, pathLocator), + Cosmoz.DefaultTree.prototype.getPathString = function (pathLocator, comparisonProperty) { + var nodesOnPath = this.getNodesOnPath(pathLocator, this._treeData), path = ''; nodesOnPath.forEach(function (node) { path += node[comparisonProperty] + '/'; @@ -119,4 +126,4 @@ return node[propertyName]; }; - + \ No newline at end of file From 1c88582beb01048b55a3cfde921f89871e36754a Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Sun, 23 Jul 2017 18:39:39 +0200 Subject: [PATCH 08/28] Defaults to ES5 --- cosmoz-default-tree.html | 19 +++++++++++++++---- cosmoz-tree.html | 7 ++++++- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index 3b9b7a5..efa0164 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -36,11 +36,15 @@ * Returns an Array with all found Nodes. * Returns an Object with the first found Node. */ - Cosmoz.DefaultTree.prototype.getNodeByProperty = function (propertyName, propertyValue, exact = true, all = false, nodeList) { + Cosmoz.DefaultTree.prototype.getNodeByProperty = function (propertyName, propertyValue, exact, all, nodeList) { if (propertyName === undefined || propertyValue === undefined) { return; } + // Defaults + exact = exact !== undefined ? exact : true; + all = all !== undefined ? all : false; + if (propertyName === 'pathLocator') { return this.getNodeByPathLocator(propertyValue); } @@ -51,10 +55,14 @@ /** * Loops over a node list (or roots if not defined) and returns the concatenated results of search(). */ - Cosmoz.DefaultTree.prototype._searchNodeList = function (propertyName, propertyValue, exact = true, all = false, nodeList) { - nodeList = nodeList ? nodeList : this._roots; + Cosmoz.DefaultTree.prototype._searchNodeList = function (propertyName, propertyValue, exact, all, nodeList) { var results = []; + // Defaults + exact = exact !== undefined ? exact : true; + all = all !== undefined ? all : false; + nodeList = nodeList ? nodeList : this._roots; + nodeList.forEach(function (node) { results = results.concat(this.search(node, propertyName, propertyValue, exact, all)); }, this); @@ -77,9 +85,12 @@ * Returns an Array of nodes on a given path. * If pathLocator is empty or not defined, the Root objects get returned. */ - Cosmoz.DefaultTree.prototype.getNodesOnPath = function (pathLocator, nodes, separatorSign = '.') { + Cosmoz.DefaultTree.prototype.getNodesOnPath = function (pathLocator, nodes, separatorSign) { if (!pathLocator) return this._roots; + // Defaults + separatorSign = separatorSign ? separatorSign : '.' + var path = pathLocator.split(separatorSign), pathSegment = nodes ? nodes : this._treeData; diff --git a/cosmoz-tree.html b/cosmoz-tree.html index d289377..04cc579 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -79,11 +79,16 @@ /** * Basic search for a node by one of its properties. */ - search: function (node, propertyName, propertyValue, exact = true, all = false, results = []) { + search: function (node, propertyName, propertyValue, exact, all, results) { var nodeConforms = this.nodeConformsSearch(node, propertyName, propertyValue, exact), children, result, i; + + // Defaults + exact = exact !== undefined ? exact : true; + all = all !== undefined ? all : false; + results = results ? results : []; if (nodeConforms) { if (all) { From cd64e21cea30756ab61104c792ee9398bee22f51 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Sun, 23 Jul 2017 19:06:46 +0200 Subject: [PATCH 09/28] fixes of PR comments --- cosmoz-default-tree.html | 26 +++++++++++++++++++------- cosmoz-tree.html | 4 ++-- demo/index.html | 16 ++++++++-------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index efa0164..9e336ac 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -52,6 +52,14 @@ return this._searchNodeList(propertyName, propertyValue, exact, all, nodeList); }; + /** + * Returns the path String of the found Node(s). + */ + Cosmoz.DefaultTree.prototype.getPathByProperty = function (propertyName, propertyValue, exact, all, nodeList) { + var node = this.getNodeByProperty(propertyName, propertyValue, exact, all, nodeList); + return this.getPath(node.path, propertyName); + }, + /** * Loops over a node list (or roots if not defined) and returns the concatenated results of search(). */ @@ -108,13 +116,17 @@ /** * Returns a String (separated node comparison properties) representing the path. */ - Cosmoz.DefaultTree.prototype.getPathString = function (pathLocator, comparisonProperty) { - var nodesOnPath = this.getNodesOnPath(pathLocator, this._treeData), - path = ''; - nodesOnPath.forEach(function (node) { - path += node[comparisonProperty] + '/'; - }, this); - return path; + Cosmoz.DefaultTree.prototype.getPath = function (pathLocator, comparisonProperty, pathSeparator) { + var nodesOnPath = this.getNodesOnPath(pathLocator, this._treeData), + path = ''; + + // Defaults + pathSeparator = pathSeparator ? pathSeparator : '/'; + + nodesOnPath.forEach(function (node) { + path += node[comparisonProperty] + pathSeparator; + }, this); + return path; }; /** diff --git a/cosmoz-tree.html b/cosmoz-tree.html index 04cc579..cab657f 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -100,14 +100,14 @@ children = this.getChildren(node, true); - if (children && Array.isArray(children)) { + if (Array.isArray(children)) { for (i = 0; i < children.length; i++) { var child = children[i]; result = this.search(children[i], propertyName, propertyValue, exact, all, results); if (!Array.isArray(result)) { return result; } - } + } } return results; diff --git a/demo/index.html b/demo/index.html index fb0b459..73ea1ee 100644 --- a/demo/index.html +++ b/demo/index.html @@ -18,25 +18,25 @@ From 5a2064e1f23f97198d3b5dc3c6f48d45818721bb Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Wed, 26 Jul 2017 15:58:34 +0200 Subject: [PATCH 10/28] PR comments (without rename) --- cosmoz-default-tree.html | 22 +++++++++++++--------- cosmoz-tree.html | 7 +++---- demo/index.html | 14 ++++++-------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index 9e336ac..3bf4ef8 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -57,6 +57,9 @@ */ Cosmoz.DefaultTree.prototype.getPathByProperty = function (propertyName, propertyValue, exact, all, nodeList) { var node = this.getNodeByProperty(propertyName, propertyValue, exact, all, nodeList); + if (!node) { + return; + } return this.getPath(node.path, propertyName); }, @@ -94,7 +97,9 @@ * If pathLocator is empty or not defined, the Root objects get returned. */ Cosmoz.DefaultTree.prototype.getNodesOnPath = function (pathLocator, nodes, separatorSign) { - if (!pathLocator) return this._roots; + if (!pathLocator) { + return this._roots + }; // Defaults separatorSign = separatorSign ? separatorSign : '.' @@ -118,22 +123,21 @@ */ Cosmoz.DefaultTree.prototype.getPath = function (pathLocator, comparisonProperty, pathSeparator) { var nodesOnPath = this.getNodesOnPath(pathLocator, this._treeData), - path = ''; + path; // Defaults pathSeparator = pathSeparator ? pathSeparator : '/'; - - nodesOnPath.forEach(function (node) { - path += node[comparisonProperty] + pathSeparator; - }, this); - return path; + + return nodesOnPath.map(function (node) { + return node[comparisonProperty]; + }).join(pathSeparator); }; /** * Returns an Object or an Array representing the children of a node. */ Cosmoz.DefaultTree.prototype.getChildren = function (node, asList) { - if (node === undefined) { + if (!node) { return; } return asList ? _objectValues(node.children) : node.children; @@ -143,7 +147,7 @@ * Returns the property of a Node based on a given property name. */ Cosmoz.DefaultTree.prototype.getProperty = function (node, propertyName) { - if (node === undefined || propertyName === undefined) { + if (!node || !propertyName) { return; } return node[propertyName]; diff --git a/cosmoz-tree.html b/cosmoz-tree.html index cab657f..2493311 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -91,18 +91,17 @@ results = results ? results : []; if (nodeConforms) { - if (all) { - results.push(this.afterFound(node, propertyName, propertyValue)); - } else { + if (!all) { return node; } + + results.push(this.afterFound(node, propertyName, propertyValue)); } children = this.getChildren(node, true); if (Array.isArray(children)) { for (i = 0; i < children.length; i++) { - var child = children[i]; result = this.search(children[i], propertyName, propertyValue, exact, all, results); if (!Array.isArray(result)) { return result; diff --git a/demo/index.html b/demo/index.html index 73ea1ee..2dea5c6 100644 --- a/demo/index.html +++ b/demo/index.html @@ -23,16 +23,14 @@

See console

\ No newline at end of file diff --git a/cosmoz-tree.html b/cosmoz-tree.html index 2493311..3b6fce2 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -29,6 +29,10 @@ throw new Error('Must be implemented in derived object'); }, + getPath: function (node) { + throw new Error('Must be implemented in derived object'); + }, + getPathString: function (node) { throw new Error('Must be implemented in derived object'); }, From 5af285ea11e3ebb2f5dbc768a1d456a80bb4b2bb Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Sat, 29 Jul 2017 16:12:03 +0200 Subject: [PATCH 12/28] Removed & Exposed Methods, fixed issues Removed: getNodeByProperty(), getPathByProperty(), Exposed: searchNodes() fixed some issues --- cosmoz-default-tree.html | 59 ++++++++++------------------------------ cosmoz-tree.html | 23 ++++------------ 2 files changed, 19 insertions(+), 63 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index a299daa..22733ad 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -14,6 +14,9 @@ window.Cosmoz = window.Cosmoz || {}; function _objectValues(obj) { + if (!obj) { + return; + } return Object.keys(obj).map(function (key) { return obj[key]; }); @@ -32,50 +35,18 @@ Cosmoz.DefaultTree.prototype = Object.create(Cosmoz.Tree.prototype); - /** - * Returns an Array with all found Nodes. - * Returns an Object with the first found Node. - */ - Cosmoz.DefaultTree.prototype.getNodeByProperty = function (propertyName, propertyValue, exact, all, nodeList) { - if (propertyName === undefined || propertyValue === undefined) { - return; - } - - // Defaults - exact = exact !== undefined ? exact : true; - all = all !== undefined ? all : false; - - if (propertyName === 'pathLocator') { - return this.getNodeByPathLocator(propertyValue); - } - - return this._searchNodeList(propertyName, propertyValue, exact, all, nodeList); - }; - - /** - * Returns the path String of the found Node(s). - */ - Cosmoz.DefaultTree.prototype.getPathByProperty = function (propertyName, propertyValue, exact, all, nodeList) { - var node = this.getNodeByProperty(propertyName, propertyValue, exact, all, nodeList); - if (!node) { - return; - } - return this.getPathString(node.path, propertyName); - }, - /** * Loops over a node list (or roots if not defined) and returns the concatenated results of search(). */ - Cosmoz.DefaultTree.prototype._searchNodeList = function (propertyName, propertyValue, exact, all, nodeList) { + Cosmoz.DefaultTree.prototype.searchNodes = function (propertyName, propertyValue, exact, nodes) { var results = []; // Defaults exact = exact !== undefined ? exact : true; - all = all !== undefined ? all : false; - nodeList = nodeList ? nodeList : this._roots; + nodes = nodes ? nodes : this._roots; - nodeList.forEach(function (node) { - results = results.concat(this.search(node, propertyName, propertyValue, exact, all)); + nodes.forEach(function (node) { + results = results.concat(this.search(node, propertyName, propertyValue, exact)); }, this); return results; @@ -98,11 +69,11 @@ */ Cosmoz.DefaultTree.prototype.getPath = function (pathLocator, nodes, separatorSign) { if (!pathLocator) { - return this._roots - }; + return this._roots; + } // Defaults - separatorSign = separatorSign ? separatorSign : '.' + separatorSign = separatorSign ? separatorSign : '.'; var path = pathLocator.split(separatorSign), pathSegment = nodes ? nodes : this._treeData; @@ -110,7 +81,6 @@ return path.map(function (nodeKey) { var node = pathSegment[nodeKey], children = this.getChildren(node); - node['key'] = nodeKey; if (node && children !== undefined && Object.keys(children).length > 0) { pathSegment = children; } @@ -122,9 +92,8 @@ * Returns a String (separated node comparison properties) representing the path. */ Cosmoz.DefaultTree.prototype.getPathString = function (pathLocator, comparisonProperty, pathSeparator) { - var nodesOnPath = this.getPath(pathLocator, this._treeData), - path; - + var nodesOnPath = this.getPath(pathLocator, this._treeData); + // Defaults pathSeparator = pathSeparator ? pathSeparator : '/'; @@ -136,11 +105,11 @@ /** * Returns an Object or an Array representing the children of a node. */ - Cosmoz.DefaultTree.prototype.getChildren = function (node, asList) { + Cosmoz.DefaultTree.prototype.getChildren = function (node) { if (!node) { return; } - return asList ? _objectValues(node.children) : node.children; + return node.children; }; /** diff --git a/cosmoz-tree.html b/cosmoz-tree.html index 3b6fce2..4050b2a 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -72,18 +72,10 @@ return false; }, - /** - * Gets called after a node has been found - * and before pushed to results array. - */ - afterFound: function (node, propertyName, propertyValue) { - return node; - }, - /** * Basic search for a node by one of its properties. */ - search: function (node, propertyName, propertyValue, exact, all, results) { + search: function (node, propertyName, propertyValue, exact, results) { var nodeConforms = this.nodeConformsSearch(node, propertyName, propertyValue, exact), children, result, @@ -91,22 +83,17 @@ // Defaults exact = exact !== undefined ? exact : true; - all = all !== undefined ? all : false; results = results ? results : []; if (nodeConforms) { - if (!all) { - return node; - } - - results.push(this.afterFound(node, propertyName, propertyValue)); + results.push(node); } - children = this.getChildren(node, true); + children = _objectValues(this.getChildren(node)); if (Array.isArray(children)) { for (i = 0; i < children.length; i++) { - result = this.search(children[i], propertyName, propertyValue, exact, all, results); + result = this.search(children[i], propertyName, propertyValue, exact, results); if (!Array.isArray(result)) { return result; } @@ -116,4 +103,4 @@ return results; } }; - + \ No newline at end of file From d8e9ffdb2ddb32daab4e5ed844f1062dd497ef35 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Sat, 29 Jul 2017 16:19:23 +0200 Subject: [PATCH 13/28] Rename getPath() => getPathNodes() --- cosmoz-default-tree.html | 8 ++++---- cosmoz-tree.html | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index 22733ad..c824277 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -59,15 +59,15 @@ if (!pathLocator) { return this._treeData; } - var nodesOnPath = this.getPath(pathLocator, this._treeData); - return nodesOnPath[nodesOnPath.length - 1]; + var pathNodes = this.getPathNodes(pathLocator, this._treeData); + return pathNodes[pathNodes.length - 1]; }; /** * Returns an Array of nodes on a given path. * If pathLocator is empty or not defined, the Root objects get returned. */ - Cosmoz.DefaultTree.prototype.getPath = function (pathLocator, nodes, separatorSign) { + Cosmoz.DefaultTree.prototype.getPathNodes = function (pathLocator, nodes, separatorSign) { if (!pathLocator) { return this._roots; } @@ -92,7 +92,7 @@ * Returns a String (separated node comparison properties) representing the path. */ Cosmoz.DefaultTree.prototype.getPathString = function (pathLocator, comparisonProperty, pathSeparator) { - var nodesOnPath = this.getPath(pathLocator, this._treeData); + var nodesOnPath = this.getPathNodes(pathLocator, this._treeData); // Defaults pathSeparator = pathSeparator ? pathSeparator : '/'; diff --git a/cosmoz-tree.html b/cosmoz-tree.html index 4050b2a..e5335e3 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -29,7 +29,7 @@ throw new Error('Must be implemented in derived object'); }, - getPath: function (node) { + getPathNodes: function (node) { throw new Error('Must be implemented in derived object'); }, From e878ec1951bdd699cb88d9a2bb47157612779204 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Mon, 31 Jul 2017 17:43:35 +0200 Subject: [PATCH 14/28] re-added getNodeByProperty() getPathStringByProperty() --- cosmoz-default-tree.html | 73 ++++++++++++++++++++++++++++++++++------ cosmoz-tree.html | 22 +++--------- 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index c824277..c52cd6f 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -35,6 +35,24 @@ Cosmoz.DefaultTree.prototype = Object.create(Cosmoz.Tree.prototype); + /** + * Returns the first found Node. + */ + Cosmoz.DefaultTree.prototype.getNodeByProperty = function (propertyName, propertyValue, exact, nodes) { + if (propertyName === undefined || propertyValue === undefined) { + return; + } + // Defaults + exact = exact !== undefined ? exact : true; + + if (propertyName === 'pathLocator') { + return this.getNodeByPathLocator(propertyValue); + } + + var results = this.searchNodes(propertyName, propertyValue, exact, nodes); + return results && results.length > 0 ? results[0] : null; + }; + /** * Loops over a node list (or roots if not defined) and returns the concatenated results of search(). */ @@ -59,49 +77,82 @@ if (!pathLocator) { return this._treeData; } - var pathNodes = this.getPathNodes(pathLocator, this._treeData); - return pathNodes[pathNodes.length - 1]; + var pathNodes = this.getPathNodes(pathLocator, this._treeData), + lastNodeOnPath = pathNodes.pop(); + return lastNodeOnPath !== undefined ? lastNodeOnPath : null; }; /** * Returns an Array of nodes on a given path. * If pathLocator is empty or not defined, the Root objects get returned. */ - Cosmoz.DefaultTree.prototype.getPathNodes = function (pathLocator, nodes, separatorSign) { + Cosmoz.DefaultTree.prototype.getPathNodes = function (pathLocator, nodes, separatorSign, childProperty) { if (!pathLocator) { return this._roots; } // Defaults separatorSign = separatorSign ? separatorSign : '.'; + childProperty = childProperty ? childProperty : 'children'; var path = pathLocator.split(separatorSign), - pathSegment = nodes ? nodes : this._treeData; + pathSegment = nodes ? nodes : this._treeData, + pathHasUndefinedSegments, + nodes; - return path.map(function (nodeKey) { + // Get the nodes on the path. + nodes = path.map(function (nodeKey) { var node = pathSegment[nodeKey], - children = this.getChildren(node); + children = node[childProperty]; if (node && children !== undefined && Object.keys(children).length > 0) { pathSegment = children; } return node; }, this); + + // Check if all nodes on the path are valid. + pathHasUndefinedSegments = nodes.some(function (n) { + if (n === undefined) { + return true; + } + }); + + return pathHasUndefinedSegments ? null : nodes; }, /** - * Returns a String (separated node comparison properties) representing the path. + * Returns a String (separated node properties) representing the path of a given path locator. */ - Cosmoz.DefaultTree.prototype.getPathString = function (pathLocator, comparisonProperty, pathSeparator) { - var nodesOnPath = this.getPathNodes(pathLocator, this._treeData); + Cosmoz.DefaultTree.prototype.getPathString = function (pathLocator, pathProperty, pathSeparator) { + var pathNodes = this.getPathNodes(pathLocator, this._treeData); // Defaults pathSeparator = pathSeparator ? pathSeparator : '/'; - return nodesOnPath.map(function (node) { - return node[comparisonProperty]; + return pathNodes.map(function (node) { + return node[pathProperty]; }).join(pathSeparator); }; + Cosmoz.DefaultTree.prototype.getPathStringByProperty = function (propertyName, propertyValue, pathProperty, pathSeparator) { + if (propertyName === undefined || propertyValue === undefined) { + return; + } + + // Defaults + pathProperty = pathProperty ? pathProperty : 'name'; + + if (propertyName === 'pathLocator') { + return this.getPathString(propertyValue, pathProperty); + } + + var node = this.getNodeByProperty(propertyName, propertyValue); + + if (node) { + return this.getPathString(node.pathLocator || node.path, pathProperty); + } + }; + /** * Returns an Object or an Array representing the children of a node. */ diff --git a/cosmoz-tree.html b/cosmoz-tree.html index e5335e3..8c72d25 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -51,25 +51,11 @@ nodeConformsSearch: function (node, propertyName, propertyValue, exact) { var property = this.getProperty(node, propertyName); - switch (exact) { - case true: - if (property === propertyValue) { - return true; - } - break; - - case false: - if (property.toLowerCase().indexOf(propertyValue.toLowerCase()) > -1) { - return true; - } - break; - - default: - return false; - break; + if (exact) { + return property && property === propertyValue; } - - return false; + + return property && property.toLowerCase().indexOf(propertyValue.toLowerCase()) > -1; }, /** From 00bb3de8e67ec6d38aec63805f0d12221c096cd3 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Mon, 31 Jul 2017 17:57:03 +0200 Subject: [PATCH 15/28] getChildren() refactor --- cosmoz-default-tree.html | 8 ++++++-- cosmoz-tree.html | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index c52cd6f..84735c8 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -156,11 +156,15 @@ /** * Returns an Object or an Array representing the children of a node. */ - Cosmoz.DefaultTree.prototype.getChildren = function (node) { + Cosmoz.DefaultTree.prototype.getChildren = function (node, childProperty) { if (!node) { return; } - return node.children; + + // Defaults + childProperty = childProperty ? childProperty : 'children'; + + return _objectValues(node[childProperty]); }; /** diff --git a/cosmoz-tree.html b/cosmoz-tree.html index 8c72d25..698d218 100644 --- a/cosmoz-tree.html +++ b/cosmoz-tree.html @@ -37,6 +37,10 @@ throw new Error('Must be implemented in derived object'); }, + getPathStringByProperty: function (node) { + throw new Error('Must be implemented in derived object'); + }, + getChildren: function (node, asList) { throw new Error('Must be implemented in derived object'); }, @@ -75,7 +79,7 @@ results.push(node); } - children = _objectValues(this.getChildren(node)); + children = this.getChildren(node); if (Array.isArray(children)) { for (i = 0; i < children.length; i++) { From 44ca085b0be341c914be2ae43d7ec3de9d402533 Mon Sep 17 00:00:00 2001 From: JaySunSyn Date: Mon, 31 Jul 2017 21:58:03 +0200 Subject: [PATCH 16/28] Added comments and some (default) properties --- cosmoz-default-tree.html | 97 +++++++++++++++++++++++++++++----------- cosmoz-tree.html | 83 +++++++++++++++++++++++++++++----- 2 files changed, 142 insertions(+), 38 deletions(-) diff --git a/cosmoz-default-tree.html b/cosmoz-default-tree.html index 84735c8..e27be35 100644 --- a/cosmoz-default-tree.html +++ b/cosmoz-default-tree.html @@ -5,7 +5,6 @@ @demo demo/index.html --> -