Skip to content

Commit

Permalink
feat(tree): add ability to hide root node (refs #25)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rychkog committed Jan 5, 2017
1 parent ff5dc1f commit 7d64cdf
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
9 changes: 7 additions & 2 deletions demo/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@angular/core';
import { NodeEvent, TreeModel, RenamableNode } from '../index';
import { NodeEvent, TreeModel, RenamableNode, TreeViewOptions } from '../index';

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

Expand All @@ -12,7 +12,7 @@ declare const alertify: any;
<div class="tree-container">
<p>Fonts tree</p>
<tree
[tree]="fonts"
[tree]="fonts"
(nodeRemoved)="onNodeRemoved($event)"
(nodeRenamed)="onNodeRenamed($event)"
(nodeSelected)="onNodeSelected($event)"
Expand All @@ -24,6 +24,7 @@ declare const alertify: any;
<p>Programming languages tree</p>
<tree
[tree]="pls"
[viewOptions]="treeViewOptions"
(nodeRemoved)="onNodeRemoved($event)"
(nodeRenamed)="onNodeRenamed($event)"
(nodeSelected)="onNodeSelected($event)"
Expand Down Expand Up @@ -52,6 +53,10 @@ declare const alertify: any;
`]
})
export class AppComponent {
public treeViewOptions: TreeViewOptions = {
rootIsVisible: false
};

public fonts: TreeModel = {
value: 'Fonts',
children: [
Expand Down
4 changes: 4 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
TreeModel,
TreeModelOptions,
TreeViewOptions,
RenamableNode,
NodeEvent,
NodeCreatedEvent,
Expand All @@ -15,6 +17,8 @@ import { TreeModule } from './src/tree.module';

export {
TreeModel,
TreeModelOptions,
TreeViewOptions,
RenamableNode,
NodeEvent,
NodeCreatedEvent,
Expand Down
8 changes: 8 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,12 @@ tree-internal .tree .folding.node-leaf {
tree-internal .tree .folding.node-leaf:before {
content: '\25CF';
color: #757575;
}

tree-internal ul.rootless {
padding: 0;
}

tree-internal div.rootless {
display: none !important;
}
16 changes: 12 additions & 4 deletions src/tree.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Input, Component, OnInit, EventEmitter, Output, ElementRef, Inject } from '@angular/core';
import { TreeStatus, TreeModel, TreeModelOptions, FoldingType, NodeEvent, RenamableNode, NodeSelectedEvent } from './tree.types';
import { TreeStatus, TreeModel, TreeModelOptions, TreeViewOptions, FoldingType, NodeEvent, RenamableNode, NodeSelectedEvent } from './tree.types';
import { NodeDraggableService } from './draggable/node-draggable.service';
import { NodeMenuService } from './menu/node-menu.service';
import { NodeDraggableEventAction, NodeDraggableEvent } from './draggable/draggable.types';
Expand All @@ -13,9 +13,9 @@ import { applyNewValueToRenamable, isRenamable, isValueEmpty } from './utils/typ
@Component({
selector: 'tree-internal',
template: `
<ul class="tree" *ngIf="tree">
<ul class="tree" *ngIf="tree" [ngClass]="{rootless: !viewOptions.rootIsVisible}">
<li>
<div (contextmenu)="showMenu($event)" [nodeDraggable]="element" [tree]="tree">
<div [ngClass]="{rootless: !viewOptions.rootIsVisible}" (contextmenu)="showMenu($event)" [nodeDraggable]="element" [tree]="tree">
<div class="folding" (click)="switchFoldingType($event, tree)" [ngClass]="getFoldingTypeCssClass(tree)"></div>
<div href="#" class="node-value" *ngIf="!isEditInProgress()" [class.node-selected]="isSelected" (click)="onNodeSelected($event)">{{tree.value}}</div>
Expand All @@ -41,6 +41,9 @@ export class TreeInternalComponent implements OnInit {
@Input()
public tree: TreeModel;

@Input()
public viewOptions: TreeViewOptions;

@Input()
public parentTree: TreeModel;

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

this.isLeaf = !Array.isArray(this.tree.children);

this.tree.options = TreeModelOptions.merge(this.tree, this.parentTree);
this.viewOptions = this.viewOptions || new TreeViewOptions();

this.setUpNodeSelectedEventHandler();
this.setUpMenuEventHandler();
Expand Down Expand Up @@ -320,13 +325,16 @@ export class TreeInternalComponent implements OnInit {

@Component({
selector: 'tree',
template: `<tree-internal [tree]="tree"></tree-internal>`,
template: `<tree-internal [tree]="tree" [viewOptions]="viewOptions"></tree-internal>`,
providers: [TreeService]
})
export class TreeComponent implements OnInit {
@Input()
public tree: TreeModel;

@Input()
public viewOptions: TreeViewOptions;

@Output()
public nodeCreated: EventEmitter<any> = new EventEmitter();

Expand Down
6 changes: 5 additions & 1 deletion src/tree.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ export interface TreeModel {
}

export class TreeModelOptions {
static: boolean = false;
public static: boolean = false;

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

export class TreeViewOptions {
public rootIsVisible: boolean = true;
}

export enum TreeStatus {
New,
Modified,
Expand Down

0 comments on commit 7d64cdf

Please sign in to comment.