Skip to content

Commit

Permalink
Add isNode method.
Browse files Browse the repository at this point in the history
Add getAllNodes method
Fix isLast method. Throw error if method is not node of tree.
  • Loading branch information
maxazan committed Nov 13, 2013
1 parent a70b0ed commit d5a0e44
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 87 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
26 changes: 26 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ <h3>Public methods</h3>
};</code></pre>
</td>
</tr>
<tr>
<td>getAllNodes</td>
<td>Return the all nodes of tree</td>
<td>
<pre><code class="javascript">// Find all nodes
$('#tree-1').treegrid('getAllNodes').each(function() {
if ($(this).treegrid("isLast")) {
//Do something with all last nodes
}
});
</code></pre>
</td>
</tr>
<tr>
<td>getParentNode</td>
<td>Return the parent node or null if node is root</td>
Expand Down Expand Up @@ -238,6 +251,19 @@ <h3>Public methods</h3>
}
});</code></pre>
</td>
<tr>
<td>isNode</td>
<td>return true if element is node</td>
<td>
<pre><code class="javascript">
$('#tree-1').find('tr').each(function() {
if ($(this).treegrid("isNode")) {
//Do something
}
});
</code></pre>
</td>
</tr>
<tr>
<td>isLeaf</td>
<td>Is there a node leaf</td>
Expand Down
43 changes: 34 additions & 9 deletions js/jquery.treegrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
* @returns {unresolved}
*/
getSetting: function(name) {
if (!$(this).treegrid('getTreeContainer')) {
return null;
}
return $(this).treegrid('getTreeContainer').data('settings')[name];
},
/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
8 changes: 6 additions & 2 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<script src="tests/qunit-1.12.0.js"></script>
<script src="tests/tests.js"></script>
<table id='tree-1' border="1" cellpadding="0" cellspacing="0">
<tr>
<tr id="tree-head-1">
<th>1</th><th>2</th>
</tr>
<tr class="treegrid-1" id="node-1">
Expand Down Expand Up @@ -48,7 +48,11 @@
<tr class="treegrid-11" id="node-2">
<td>2</td><td>Simple text of ...</td>
</tr>
</table>
<tr id="tree-summary-1">
<td>Total</td><td>343</td>
</tr>

</table>
<hr>
<table id='tree-2' border="1" cellpadding="0" cellspacing="0">
<tr>
Expand Down
168 changes: 93 additions & 75 deletions tests/tests.js
Original file line number Diff line number Diff line change
@@ -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]));
}
});
});
2 changes: 1 addition & 1 deletion treegrid.jquery.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"tree",
"ui"
],
"version": "0.1.5",
"version": "0.1.6",
"title": "TreeGrid plugin for jQuery",
"author": {
"name": "Pomazan Max"
Expand Down

0 comments on commit d5a0e44

Please sign in to comment.