Skip to content

Commit 7d64cdf

Browse files
committed
feat(tree): add ability to hide root node (refs #25)
In some cases root node should not be shown - now it is accomplished via special options TreeViewOptions, that affect tree visual representation - in contrast for every TreeModel exists special class - TreeModelOptions
1 parent ff5dc1f commit 7d64cdf

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

demo/app.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component } from '@angular/core';
2-
import { NodeEvent, TreeModel, RenamableNode } from '../index';
2+
import { NodeEvent, TreeModel, RenamableNode, TreeViewOptions } from '../index';
33

44
require('../src/styles.css');
55

@@ -12,7 +12,7 @@ declare const alertify: any;
1212
<div class="tree-container">
1313
<p>Fonts tree</p>
1414
<tree
15-
[tree]="fonts"
15+
[tree]="fonts"
1616
(nodeRemoved)="onNodeRemoved($event)"
1717
(nodeRenamed)="onNodeRenamed($event)"
1818
(nodeSelected)="onNodeSelected($event)"
@@ -24,6 +24,7 @@ declare const alertify: any;
2424
<p>Programming languages tree</p>
2525
<tree
2626
[tree]="pls"
27+
[viewOptions]="treeViewOptions"
2728
(nodeRemoved)="onNodeRemoved($event)"
2829
(nodeRenamed)="onNodeRenamed($event)"
2930
(nodeSelected)="onNodeSelected($event)"
@@ -52,6 +53,10 @@ declare const alertify: any;
5253
`]
5354
})
5455
export class AppComponent {
56+
public treeViewOptions: TreeViewOptions = {
57+
rootIsVisible: false
58+
};
59+
5560
public fonts: TreeModel = {
5661
value: 'Fonts',
5762
children: [

index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
TreeModel,
3+
TreeModelOptions,
4+
TreeViewOptions,
35
RenamableNode,
46
NodeEvent,
57
NodeCreatedEvent,
@@ -15,6 +17,8 @@ import { TreeModule } from './src/tree.module';
1517

1618
export {
1719
TreeModel,
20+
TreeModelOptions,
21+
TreeViewOptions,
1822
RenamableNode,
1923
NodeEvent,
2024
NodeCreatedEvent,

src/styles.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,12 @@ tree-internal .tree .folding.node-leaf {
142142
tree-internal .tree .folding.node-leaf:before {
143143
content: '\25CF';
144144
color: #757575;
145+
}
146+
147+
tree-internal ul.rootless {
148+
padding: 0;
149+
}
150+
151+
tree-internal div.rootless {
152+
display: none !important;
145153
}

src/tree.component.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Input, Component, OnInit, EventEmitter, Output, ElementRef, Inject } from '@angular/core';
2-
import { TreeStatus, TreeModel, TreeModelOptions, FoldingType, NodeEvent, RenamableNode, NodeSelectedEvent } from './tree.types';
2+
import { TreeStatus, TreeModel, TreeModelOptions, TreeViewOptions, FoldingType, NodeEvent, RenamableNode, NodeSelectedEvent } from './tree.types';
33
import { NodeDraggableService } from './draggable/node-draggable.service';
44
import { NodeMenuService } from './menu/node-menu.service';
55
import { NodeDraggableEventAction, NodeDraggableEvent } from './draggable/draggable.types';
@@ -13,9 +13,9 @@ import { applyNewValueToRenamable, isRenamable, isValueEmpty } from './utils/typ
1313
@Component({
1414
selector: 'tree-internal',
1515
template: `
16-
<ul class="tree" *ngIf="tree">
16+
<ul class="tree" *ngIf="tree" [ngClass]="{rootless: !viewOptions.rootIsVisible}">
1717
<li>
18-
<div (contextmenu)="showMenu($event)" [nodeDraggable]="element" [tree]="tree">
18+
<div [ngClass]="{rootless: !viewOptions.rootIsVisible}" (contextmenu)="showMenu($event)" [nodeDraggable]="element" [tree]="tree">
1919
<div class="folding" (click)="switchFoldingType($event, tree)" [ngClass]="getFoldingTypeCssClass(tree)"></div>
2020
<div href="#" class="node-value" *ngIf="!isEditInProgress()" [class.node-selected]="isSelected" (click)="onNodeSelected($event)">{{tree.value}}</div>
2121
@@ -41,6 +41,9 @@ export class TreeInternalComponent implements OnInit {
4141
@Input()
4242
public tree: TreeModel;
4343

44+
@Input()
45+
public viewOptions: TreeViewOptions;
46+
4447
@Input()
4548
public parentTree: TreeModel;
4649

@@ -65,7 +68,9 @@ export class TreeInternalComponent implements OnInit {
6568
this.tree._indexInParent = this.indexInParent;
6669

6770
this.isLeaf = !Array.isArray(this.tree.children);
71+
6872
this.tree.options = TreeModelOptions.merge(this.tree, this.parentTree);
73+
this.viewOptions = this.viewOptions || new TreeViewOptions();
6974

7075
this.setUpNodeSelectedEventHandler();
7176
this.setUpMenuEventHandler();
@@ -320,13 +325,16 @@ export class TreeInternalComponent implements OnInit {
320325

321326
@Component({
322327
selector: 'tree',
323-
template: `<tree-internal [tree]="tree"></tree-internal>`,
328+
template: `<tree-internal [tree]="tree" [viewOptions]="viewOptions"></tree-internal>`,
324329
providers: [TreeService]
325330
})
326331
export class TreeComponent implements OnInit {
327332
@Input()
328333
public tree: TreeModel;
329334

335+
@Input()
336+
public viewOptions: TreeViewOptions;
337+
330338
@Output()
331339
public nodeCreated: EventEmitter<any> = new EventEmitter();
332340

src/tree.types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@ export interface TreeModel {
2323
}
2424

2525
export class TreeModelOptions {
26-
static: boolean = false;
26+
public static: boolean = false;
2727

2828
static merge(sourceA: TreeModel, sourceB: TreeModel): TreeModelOptions {
2929
return _.defaults({}, _.get(sourceA, 'options'), _.get(sourceB, 'options'), {static: false});
3030
}
3131
}
3232

33+
export class TreeViewOptions {
34+
public rootIsVisible: boolean = true;
35+
}
36+
3337
export enum TreeStatus {
3438
New,
3539
Modified,

0 commit comments

Comments
 (0)