From d5a0e441868149578cada4f9b9a11a6a1fcf2e9c Mon Sep 17 00:00:00 2001 From: maxazan Date: Wed, 13 Nov 2013 15:08:01 +0200 Subject: [PATCH] Add isNode method. Add getAllNodes method Fix isLast method. Throw error if method is not node of tree. --- CHANGELOG.txt | 5 ++ index.html | 26 +++++++ js/jquery.treegrid.js | 43 ++++++++--- test.html | 8 +- tests/tests.js | 168 +++++++++++++++++++++++------------------- treegrid.jquery.json | 2 +- 6 files changed, 165 insertions(+), 87 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 2ca315f..87dfd06 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,8 @@ +== 0.1.6 +* Add isNode method +* Add getAllNodes method +* Fix isLast method. Throw error if method is not node of tree. + == 0.1.5 * Add support alphanumeric id diff --git a/index.html b/index.html index 355518b..7890931 100644 --- a/index.html +++ b/index.html @@ -209,6 +209,19 @@

Public methods

}; + + getAllNodes + Return the all nodes of tree + +
// Find all nodes
+  $('#tree-1').treegrid('getAllNodes').each(function() {
+    if ($(this).treegrid("isLast")) {
+      //Do something with all last nodes
+    }
+  });
+
+ + getParentNode Return the parent node or null if node is root @@ -238,6 +251,19 @@

Public methods

} }); + + isNode + return true if element is node + +

+  $('#tree-1').find('tr').each(function() {
+    if ($(this).treegrid("isNode")) {
+      //Do something
+    }
+  });
+
+ + isLeaf Is there a node leaf diff --git a/js/jquery.treegrid.js b/js/jquery.treegrid.js index 42ae650..8cfcaca 100644 --- a/js/jquery.treegrid.js +++ b/js/jquery.treegrid.js @@ -149,6 +149,9 @@ * @returns {unresolved} */ getSetting: function(name) { + if (!$(this).treegrid('getTreeContainer')) { + return null; + } return $(this).treegrid('getTreeContainer').data('settings')[name]; }, /** @@ -183,16 +186,35 @@ * @returns {Array} */ getRootNodes: function() { - return $(this).treegrid('getSetting', 'getRootNodes').apply(this, [$(this).treegrid('getTreeContainer')]); }, + /** + * Method return all nodes of tree. + * + * @returns {Array} + */ + getAllNodes: function() { + return $(this).treegrid('getSetting', 'getAllNodes').apply(this, [$(this).treegrid('getTreeContainer')]); + }, + /** + * Mthod return true if element is Node + * + * @returns {String} + */ + isNode: function() { + return $(this).treegrid('getNodeId') !== null; + }, /** * Mthod return id of node * * @returns {String} */ getNodeId: function() { - return $(this).treegrid('getSetting', 'getNodeId').apply(this); + if ($(this).treegrid('getSetting', 'getNodeId') == null) { + return null; + } else { + return $(this).treegrid('getSetting', 'getNodeId').apply(this); + } }, /** * Method return parent id of node or null if root node @@ -257,13 +279,8 @@ * @returns {Boolean} */ isLast: function() { - var current_parent_id = $(this).treegrid('getParentNodeId'); - if ($(this).next()) { - if ($(this).next().treegrid('getParentNodeId') === current_parent_id) { - return false; - } else { - return true; - } + if ($(this).next() && $(this).next().treegrid('isNode') && $(this).next().treegrid('getParentNodeId') === $(this).treegrid('getParentNodeId')) { + return false; } else { return true; } @@ -503,6 +520,14 @@ return templateClass.test(classNames) && !templateParentClass.test(classNames); }); return $(result); + }, + getAllNodes: function(treegridContainer) { + var result = $.grep(treegridContainer.find('tr'), function(element) { + var classNames = $(element).attr('class'); + var templateClass = /treegrid-(\w+)/; + return templateClass.test(classNames); + }); + return $(result); } }; })(jQuery); \ No newline at end of file diff --git a/test.html b/test.html index 4d7ffc0..648c8f4 100644 --- a/test.html +++ b/test.html @@ -12,7 +12,7 @@ - + @@ -48,7 +48,11 @@ -
12
2Simple text of ...
+ + Total343 + + +
diff --git a/tests/tests.js b/tests/tests.js index 6fe81b4..b42d8f9 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,123 +1,141 @@ test("getRootNodes()", function() { - var rootNodes = $('#tree-1').treegrid('getRootNodes'); - ok(rootNodes.length === 2, "Length need to be 2"); - equal($(rootNodes.get(0)).attr('id'), 'node-1', 'Test node 1'); - equal($(rootNodes.get(1)).attr('id'), 'node-2', 'Test node 2'); - var rootNodes2 = $('#node-1-1-2-1').treegrid('getRootNodes'); - equal(rootNodes.length, rootNodes2.length, "Length need to be equal"); + var rootNodes = $('#tree-1').treegrid('getRootNodes'); + ok(rootNodes.length === 2, "Length need to be 2"); + equal($(rootNodes.get(0)).attr('id'), 'node-1', 'Test node 1'); + equal($(rootNodes.get(1)).attr('id'), 'node-2', 'Test node 2'); + var rootNodes2 = $('#node-1-1-2-1').treegrid('getRootNodes'); + equal(rootNodes.length, rootNodes2.length, "Length need to be equal"); }); test("isolated options", function() { - ok($('#node-1-1-2-1').treegrid('getSetting', 'initialState') !== $('#tnode-1-1-2-1').treegrid('getSetting', 'initialState'), "need to be not equal"); + ok($('#node-1-1-2-1').treegrid('getSetting', 'initialState') !== $('#tnode-1-1-2-1').treegrid('getSetting', 'initialState'), "need to be not equal"); }); test("treeColumn test", function() { - ok($($('#node-1-1-2').find('td').get(1)).find('.treegrid-expander').length===0, "0"); - ok($($('#node-1-1-2').find('td').get(0)).find('.treegrid-expander').length===1, "1"); - - ok($($('#tnode-1-1-2').find('td').get(0)).find('.treegrid-expander').length===0, "0"); - ok($($('#tnode-1-1-2').find('td').get(1)).find('.treegrid-expander').length===1, "1"); + ok($($('#node-1-1-2').find('td').get(1)).find('.treegrid-expander').length === 0, "0"); + ok($($('#node-1-1-2').find('td').get(0)).find('.treegrid-expander').length === 1, "1"); + ok($($('#tnode-1-1-2').find('td').get(0)).find('.treegrid-expander').length === 0, "0"); + ok($($('#tnode-1-1-2').find('td').get(1)).find('.treegrid-expander').length === 1, "1"); }); test("getNodeId()", function() { - equal($('#node-1-1-2-1').treegrid('getNodeId'), 10, "Return 10"); - equal($('#node-1-1-2').treegrid('getNodeId'), 9, "Return 9"); + equal($('#node-1-1-2-1').treegrid('getNodeId'), 10, "Return 10"); + equal($('#node-1-1-2').treegrid('getNodeId'), 9, "Return 9"); }); test("getParentNodeId()", function() { - equal($('#node-1-1-2-1').treegrid('getParentNodeId'), 9, "Return 9"); - equal($('#node-1-1-2').treegrid('getParentNodeId'), 2, "Return 2"); - equal($('#node-1-1-2').treegrid('getParentNodeId'), $('#node-1-1').treegrid('getNodeId'), "Equal id"); + equal($('#node-1-1-2-1').treegrid('getParentNodeId'), 9, "Return 9"); + equal($('#node-1-1-2').treegrid('getParentNodeId'), 2, "Return 2"); + equal($('#node-1-1-2').treegrid('getParentNodeId'), $('#node-1-1').treegrid('getNodeId'), "Equal id"); }); test("getParentNode()", function() { - equal($('#node-1-1-2-1').treegrid('getParentNode').treegrid('getNodeId'), 9, "Return 9"); - equal($('#node-1').treegrid('getParentNode'), null, "Return null"); + equal($('#node-1-1-2-1').treegrid('getParentNode').treegrid('getNodeId'), 9, "Return 9"); + equal($('#node-1').treegrid('getParentNode'), null, "Return null"); }); test("getChildNodes()", function() { - equal($('#node-1').treegrid('getChildNodes').length, 4, "Return 4"); - equal($('#node-1-1-2-1').treegrid('getChildNodes').length, [], "Return []"); + equal($('#node-1').treegrid('getChildNodes').length, 4, "Return 4"); + equal($('#node-1-1-2-1').treegrid('getChildNodes').length, [], "Return []"); }); + test("getDepth()", function() { - equal($('#node-1').treegrid('getDepth'), 0, "Return 0"); - equal($('#node-1-1-2-1').treegrid('getDepth'), 3, "Return 3"); + equal($('#node-1').treegrid('getDepth'), 0, "Return 0"); + equal($('#node-1-1-2-1').treegrid('getDepth'), 3, "Return 3"); }); test("isLeaf()", function() { - ok($('#node-1-1-2-1').treegrid('isLeaf') === true, "true"); - ok($('#node-1').treegrid('isLeaf') === false, "false"); + ok($('#node-1-1-2-1').treegrid('isLeaf') === true, "true"); + ok($('#node-1').treegrid('isLeaf') === false, "false"); }); test("isLast()", function() { - ok($('#node-1-1-2-1').treegrid('isLast') === true, "Last test!"); - ok($('#node-1-2').treegrid('isLast') === false, "Not Last test!"); + ok($('#node-1-1-2-1').treegrid('isLast') === true, "Last test!"); + ok($('#node-1-2').treegrid('isLast') === false, "Not Last test!"); }); + test("isRoot()", function() { - ok($('#node-1').treegrid('isRoot') === true, "Root test!"); - ok($('#node-1-2').treegrid('isRoot') === false, "Not Root test!"); - ok($('#node-2').treegrid('isRoot') === true, "Other root test!"); + ok($('#node-1').treegrid('isRoot') === true, "Root test!"); + ok($('#node-1-2').treegrid('isRoot') === false, "Not Root test!"); + ok($('#node-2').treegrid('isRoot') === true, "Other root test!"); }); test("expand(), collapse(), isExpanded(), isCollapsed()", function() { - $('#node-1').treegrid('expand'); - ok($('#node-1').treegrid('isExpanded') === true, "Expanded"); - ok($('#node-1').hasClass('treegrid-expanded'), "Expanded class"); - $('#node-1').treegrid('collapse'); - ok($('#node-1').treegrid('isCollapsed') === true, "Collapsed"); - ok($('#node-1').hasClass('treegrid-collapsed'), "Collapsed class"); - $('#node-1').find('.treegrid-expander').click(); - ok($('#node-1').treegrid('isExpanded') === true, "Expanded after click simulate"); - $('#node-1').treegrid('collapse'); - ok($('#node-1-1').is(':visible') === false, "hidden child node 1-1"); - ok($('#node-1-1-2').is(':visible') === false, "hidden child node 1-1-2"); - ok($('#node-1-1-2-1').is(':visible') === false, "hidden child node 1-1-2-1"); - + $('#node-1').treegrid('expand'); + ok($('#node-1').treegrid('isExpanded') === true, "Expanded"); + ok($('#node-1').hasClass('treegrid-expanded'), "Expanded class"); + $('#node-1').treegrid('collapse'); + ok($('#node-1').treegrid('isCollapsed') === true, "Collapsed"); + ok($('#node-1').hasClass('treegrid-collapsed'), "Collapsed class"); + $('#node-1').find('.treegrid-expander').click(); + ok($('#node-1').treegrid('isExpanded') === true, "Expanded after click simulate"); + $('#node-1').treegrid('collapse'); + ok($('#node-1-1').is(':visible') === false, "hidden child node 1-1"); + ok($('#node-1-1-2').is(':visible') === false, "hidden child node 1-1-2"); + ok($('#node-1-1-2-1').is(':visible') === false, "hidden child node 1-1-2-1"); }); - test("expandAll()", function() { - $('#tree-1').treegrid('expandAll'); - ok($('#node-1').treegrid('isExpanded') === true, "Expanded"); - ok($('#node-1-1').treegrid('isExpanded') === true, "Expanded 1-1"); - ok($('#node-1-1-2').treegrid('isExpanded') === true, "Expanded 1-1-2"); + $('#tree-1').treegrid('expandAll'); + ok($('#node-1').treegrid('isExpanded') === true, "Expanded"); + ok($('#node-1-1').treegrid('isExpanded') === true, "Expanded 1-1"); + ok($('#node-1-1-2').treegrid('isExpanded') === true, "Expanded 1-1-2"); }); - test("collapseAll()", function() { - $('#tree-1').treegrid('collapseAll'); - ok($('#node-1').treegrid('isExpanded') === false, "Collapsed"); - ok($('#node-1-1').treegrid('isExpanded') === false, "Collapsed 1-1"); - ok($('#node-1-1-2').treegrid('isExpanded') === false, "Collapsed 1-1-2"); - $('#node-1-1-2').treegrid('expandAll'); - ok($('#node-1-1').treegrid('isExpanded') === true, "Expanded 1-1"); + $('#tree-1').treegrid('collapseAll'); + ok($('#node-1').treegrid('isExpanded') === false, "Collapsed"); + ok($('#node-1-1').treegrid('isExpanded') === false, "Collapsed 1-1"); + ok($('#node-1-1-2').treegrid('isExpanded') === false, "Collapsed 1-1-2"); + $('#node-1-1-2').treegrid('expandAll'); + ok($('#node-1-1').treegrid('isExpanded') === true, "Expanded 1-1"); }); test("Save state (cookie method)", function() { - - ok($.cookie(saveStateName) !== undefined, "Cookie set"); - $.cookie(saveStateName, '1,5'); - $('#tnode-1').treegrid('restoreState'); - $('#tnode-1-3').treegrid('restoreState'); - ok($('#tnode-1').treegrid('isExpanded'), "tnode-1 expanded"); - ok($('#tnode-1-3').treegrid('isExpanded'), "tnode-1-3 expanded"); - $.cookie(saveStateName, '2'); - $('#tnode-1').treegrid('restoreState'); - $('#tnode-1-3').treegrid('restoreState'); - $('#tnode-1-1').treegrid('restoreState'); - ok($('#tnode-1').treegrid('isCollapsed'), "tnode-1 collapsed"); - ok($('#tnode-1-3').treegrid('isCollapsed'), "tnode-1-3 collpased"); - ok($('#tnode-1-1').treegrid('isExpanded'), "tnode-1-1 expanded"); - + ok($.cookie(saveStateName) !== undefined, "Cookie set"); + $.cookie(saveStateName, '1,5'); + $('#tnode-1').treegrid('restoreState'); + $('#tnode-1-3').treegrid('restoreState'); + ok($('#tnode-1').treegrid('isExpanded'), "tnode-1 expanded"); + ok($('#tnode-1-3').treegrid('isExpanded'), "tnode-1-3 expanded"); + $.cookie(saveStateName, '2'); + $('#tnode-1').treegrid('restoreState'); + $('#tnode-1-3').treegrid('restoreState'); + $('#tnode-1-1').treegrid('restoreState'); + ok($('#tnode-1').treegrid('isCollapsed'), "tnode-1 collapsed"); + ok($('#tnode-1-3').treegrid('isCollapsed'), "tnode-1-3 collpased"); + ok($('#tnode-1-1').treegrid('isExpanded'), "tnode-1-1 expanded"); }); - test("Alphanumeric id", function() { + equal($('#anode-1').treegrid('getDepth'), 0, "Return 0"); + equal($('#anode-1-1-2-1').treegrid('getDepth'), 3, "Return 3"); + equal($('#anode-1').treegrid('getChildNodes').length, 4, "Return 4"); + equal($('#anode-1-1-2-1').treegrid('getChildNodes').length, [], "Return []"); +}); - equal($('#anode-1').treegrid('getDepth'), 0, "Return 0"); - equal($('#anode-1-1-2-1').treegrid('getDepth'), 3, "Return 3"); - equal($('#anode-1').treegrid('getChildNodes').length, 4, "Return 4"); - equal($('#anode-1-1-2-1').treegrid('getChildNodes').length, [], "Return []"); +test("getAllNodes", function() { + equal($('#tree-1').treegrid('getAllNodes').length, 11, "11"); +}); +test("isNode", function() { + ok($('#tree-head-1').treegrid("isNode") === false, 'Head is not node'); + ok($('#tree-summary-1').treegrid("isNode") === false, 'Summary is not node'); + ok($('#node-1-1').treegrid("isNode") === true, 'Node is node'); +}); +test("getLast from getAllNodes", function() { + $('#tree-1').treegrid('getAllNodes').each(function() { + if ($(this).treegrid("isLast")) { + ok($.inArray($(this).treegrid('getNodeId'), [11, 7, 9, 10, 6, 8])); + } + }); }); + +test("getLast from tr.each", function() { + $('#tree-1').find('tr').each(function() { + if ($(this).treegrid("isNode") && $(this).treegrid("isLast")) { + ok($.inArray($(this).treegrid('getNodeId'), [11, 7, 9, 10, 6, 8])); + } + }); +}); \ No newline at end of file diff --git a/treegrid.jquery.json b/treegrid.jquery.json index 259dea3..2d86d1d 100644 --- a/treegrid.jquery.json +++ b/treegrid.jquery.json @@ -11,7 +11,7 @@ "tree", "ui" ], - "version": "0.1.5", + "version": "0.1.6", "title": "TreeGrid plugin for jQuery", "author": { "name": "Pomazan Max"