Skip to content

Commit

Permalink
fix(treeBase): Change calculation of number of levels in tree
Browse files Browse the repository at this point in the history
Change the treeBase.numberLevels variable to consider a single level as 1 instead of 0
Previously there was no way to use this variable to differentiate between a single tree level
and no grouping at all. This does not impact row.treeLevel, which is still zero-based.

Reset treeBase.numberLevels each time the tree is created.

Remove unneeded dependency for groupingService in treeBase.uiGridViewport directive

Add treeRowHeaderAlwaysVisible=false option to complex grouping tutorial (320).

Fixes: #3572
  • Loading branch information
AgDude committed May 23, 2015
1 parent 363e4a5 commit 26ca621
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions misc/tutorial/320_complex_grouping.ngdoc
Expand Up @@ -43,6 +43,7 @@ children of the selected rowHeader.
$scope.gridOptions = {
enableFiltering: true,
enableGroupHeaderSelection: true,
treeRowHeaderAlwaysVisible: false,
columnDefs: [
{ name: 'name', width: '30%' },
{ name: 'gender', grouping: { groupPriority: 1 }, sort: { priority: 1, direction: 'asc' }, editableCellTemplate: 'ui-grid/dropdownEditor', width: '20%',
Expand Down
11 changes: 6 additions & 5 deletions src/features/tree-base/js/tree-base.js
Expand Up @@ -924,7 +924,7 @@
updateRowHeaderWidth: function( grid ){
var rowHeader = grid.getColumn(uiGridTreeBaseConstants.rowHeaderColName);

var newWidth = grid.options.treeRowHeaderBaseWidth + grid.options.treeIndent * grid.treeBase.numberLevels;
var newWidth = grid.options.treeRowHeaderBaseWidth + grid.options.treeIndent * Math.max(grid.treeBase.numberLevels - 1, 0);
if ( rowHeader && newWidth !== rowHeader.width ){
rowHeader.width = newWidth;
grid.queueRefresh();
Expand Down Expand Up @@ -985,6 +985,7 @@
var parents = [];
var currentState;
grid.treeBase.tree = [];
grid.treeBase.numberLevels = 0;
var aggregations = service.getAggregations( grid );

var createNode = function( row ){
Expand Down Expand Up @@ -1023,8 +1024,8 @@
}

// update the tree number of levels, so we can set header width if we need to
if ( grid.treeBase.numberLevels < row.treeLevel ){
grid.treeBase.numberLevels = row.treeLevel;
if ( grid.treeBase.numberLevels < row.treeLevel + 1){
grid.treeBase.numberLevels = row.treeLevel + 1;
}
};

Expand Down Expand Up @@ -1454,8 +1455,8 @@
* @description Stacks on top of ui.grid.uiGridViewport to set formatting on a tree header row
*/
module.directive('uiGridViewport',
['$compile', 'uiGridConstants', 'gridUtil', '$parse', 'uiGridGroupingService',
function ($compile, uiGridConstants, gridUtil, $parse, uiGridGroupingService) {
['$compile', 'uiGridConstants', 'gridUtil', '$parse',
function ($compile, uiGridConstants, gridUtil, $parse) {
return {
priority: -200, // run after default directive
scope: false,
Expand Down
8 changes: 4 additions & 4 deletions src/features/tree-base/test/tree-base.spec.js
Expand Up @@ -172,7 +172,7 @@ describe('ui.grid.treeBase uiGridTreeBaseService', function () {
it( 'tree the rows', function() {
var treeRows = uiGridTreeBaseService.treeRows.call( grid, grid.rows.slice(0) );
expect( treeRows.length ).toEqual( 2, 'only the level 0 rows are visible' );
expect( grid.treeBase.numberLevels).toEqual(2, 'two levels in the tree');
expect( grid.treeBase.numberLevels).toEqual(3, 'three levels in the tree');
});
});

Expand Down Expand Up @@ -244,7 +244,7 @@ describe('ui.grid.treeBase uiGridTreeBaseService', function () {
expect( rows[0].treeNode ).toEqual( grid.treeBase.tree[0], 'treeNode is the first node in the tree' );
delete rows[0].treeNode;
expect( rows[0] ).toEqual( { uid: 1, treeLevel: 0, entity: { $$treeLevel: 0 }, visible: true }, 'treeLevel copied down' );
expect( grid.treeBase.numberLevels).toEqual(0, 'one level in the tree (is zero based like an array)');
expect( grid.treeBase.numberLevels).toEqual(1, 'one level in the tree');
expect( grid.treeBase.tree.length).toEqual( 1, 'one node at root level of tree');
expect( grid.treeBase.tree[0].row).toEqual( rows[0], 'node is for row 0');
delete grid.treeBase.tree[0].row;
Expand All @@ -263,7 +263,7 @@ describe('ui.grid.treeBase uiGridTreeBaseService', function () {
var tree = uiGridTreeBaseService.createTree( grid, rows );

// overall settings
expect( grid.treeBase.numberLevels).toEqual(1, 'two levels in the tree (is zero based like an array)');
expect( grid.treeBase.numberLevels).toEqual(2, 'two levels in the tree');

// rows
expect( rows.length ).toEqual( 5, 'still only 5 rows' );
Expand Down Expand Up @@ -356,7 +356,7 @@ describe('ui.grid.treeBase uiGridTreeBaseService', function () {
var tree = uiGridTreeBaseService.createTree( grid, rows );

// overall settings
expect( grid.treeBase.numberLevels).toEqual(1, 'two levels in the tree (is zero based like an array)');
expect( grid.treeBase.numberLevels).toEqual(2, 'two levels in the tree');

// rows
expect( rows.length ).toEqual( 10, 'still only 10 rows' );
Expand Down

0 comments on commit 26ca621

Please sign in to comment.