Skip to content

Commit adc3c78

Browse files
Zhivka Dimovageo
authored andcommitted
feat(*): add cssClasses setting for folding elements and html templates for node and left menu activation elements
1 parent 8a6bb18 commit adc3c78

17 files changed

Lines changed: 1089 additions & 691 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ factories
1010
node_modules
1111

1212
.DS_Store
13+
*.swp
1314
npm-debug.log
1415
yarn-error.log

.vimrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set tabstop=2
2+
set shiftwidth=2
3+
set softtabstop=2
4+
set expandtab
5+
set nosmarttab
6+
7+
set textwidth=80

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,18 @@ Here is an example of its usage:
271271
value: 'Prototype-based programming',
272272
settings: {
273273
'static': true,
274-
'rightMenu': true,
275-
'leftMenu': true
274+
'rightMenu': true,
275+
'leftMenu': true,
276+
'cssClasses': {
277+
'expanded': 'fa fa-caret-down fa-lg',
278+
'collapsed': 'fa fa-caret-right fa-lg',
279+
'leaf:': 'fa fa-lg'
280+
},
281+
'templates': {
282+
'node': '<i class="fa fa-folder-o fa-lg"></i>',
283+
'leaf': '<i class="fa fa-file-o fa-lg"></i>',
284+
'leftMenu': '<i class="fa fa-navicon fa-lg"></i>'
285+
}
276286
},
277287
children: [
278288
{value: 'JavaScript'},
@@ -282,9 +292,17 @@ Here is an example of its usage:
282292
}
283293
```
284294

285-
* `static` - This option makes it impossible to drag a tree or modify it in a some way, though you still can select nodes in the static tree and appropriate events will be generated.
286-
* `rightMenu` - This option allows you to activate (true, by default) or deactivate (false) right menu when clicking with right button of a mouse.
287-
* `leftMenu` - This option allows you to activate (true) or deactivate (false, by default) left menu.
295+
* `static` - Boolean - This option makes it impossible to drag a tree or modify it in a some way, though you still can select nodes in the static tree and appropriate events will be generated.
296+
* `rightMenu` - Boolan - This option allows you to activate (true, by default) or deactivate (false) right menu when clicking with right button of a mouse.
297+
* `leftMenu` - Boolean - This option allows you to activate (true) or deactivate (false, by default) left menu.
298+
* `cssClasses` - Object:
299+
* `expanded` - String - It specifies a css class (or classes) for an item which represents expanded state of a node. The item is clickable and it transitions the node to the collapsed state
300+
* `collapsed` - String - It specifies a css class (or classes) for an item which represents collapsed state of a node. The item is clickable and it transitions the node to the expanded state
301+
* `leaf` - String - It specifies a css class (or classes) for an item which represents a node without an option to expand or collapse - in other words: a leaf node.
302+
* `templates` - Object:
303+
* `node` - String - It specifies a html template which will be included to the left of the node's value.
304+
* `leaf` - String - It specifies a html template which will be included to the left of the leaf's value.
305+
* `leftMenu` - String - It specifies a html template to the right of the node's value. This template becomes clickable and shows a menu on node's click.
288306

289307
All options that's defined on a `parent` are automatically applied to children. If you want you can override them by `settings` of the child node.
290308

demo/app.component.ts

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

44
require('../src/styles.css');
5+
require('../node_modules/font-awesome/css/font-awesome.min.css');
56

67
declare const alertify: any;
78

@@ -140,6 +141,17 @@ export class AppComponent implements OnInit {
140141
public dfs: TreeModel = {
141142
value: '/',
142143
id: 1,
144+
settings: {
145+
cssClasses: {
146+
expanded: 'fa fa-caret-down',
147+
collapsed: 'fa fa-caret-right',
148+
leaf: 'fa'
149+
},
150+
templates: {
151+
node: '<i class="fa fa-folder-o"></i>',
152+
leaf: '<i class="fa fa-file-o"></i>'
153+
}
154+
},
143155
children: [
144156
{
145157
value: 'bin',
@@ -265,13 +277,21 @@ export class AppComponent implements OnInit {
265277
]
266278
},
267279
{
268-
value: 'secondUser',
280+
value: 'secondUser - left menu templates',
269281
id: 54,
282+
settings: {
283+
leftMenu: true
284+
},
270285
children: [
271286
{value: 'Documents', id: 55, children: []},
272287
{
273-
value: 'Downloads',
288+
value: 'Downloads - custom left menu template',
274289
id: 56,
290+
settings: {
291+
templates: {
292+
leftMenu: '<i class="fa fa-navicon"></i>'
293+
}
294+
},
275295
children: [
276296
{value: 'Actobat3', id: 57},
277297
{value: 'Complib', id: 58},

demo/vendor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ import '@angular/common';
33
import '@angular/core';
44
import '@angular/platform-browser';
55
import '@angular/platform-browser-dynamic';
6+
import 'font-awesome/css/font-awesome.css';

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@
7373
"conventional-github-releaser": "1.1.3",
7474
"core-js": "2.4.1",
7575
"css-loader": "0.26.1",
76+
"font-awesome": "4.7.0",
7677
"gulp": "3.9.1",
7778
"gulp-gh-pages": "0.5.4",
7879
"istanbul-instrumenter-loader": "2.0.0",
7980
"jasmine-core": "2.5.2",
81+
"jasmine-data-provider": "2.2.0",
8082
"jasmine-spec-reporter": "~3.2.0",
81-
"karma": "1.4.0",
83+
"karma-cli": "1.0.1",
8284
"karma-coverage-istanbul-reporter": "0.2.0",
8385
"karma-jasmine": "1.1.0",
8486
"karma-phantomjs-launcher": "github:karma-runner/karma-phantomjs-launcher",
@@ -100,6 +102,7 @@
100102
"tslint": "4.4.2",
101103
"tslint-config-valorsoft": "1.2.0",
102104
"typescript": "2.0.10",
105+
"url-loader": "0.5.8",
103106
"webpack": "1.14.0",
104107
"webpack-dev-server": "1.16.2",
105108
"zone.js": "0.7.6"

src/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ tree-internal .tree .node-left-menu {
109109
width: auto;
110110
}
111111

112-
tree-internal .tree .node-left-menu:before {
112+
tree-internal .tree .node-left-menu span:before {
113113
content: '\2026';
114114
color: #757575;
115115
}

src/tree-internal.component.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@ import { Observable } from 'rxjs';
2020
[nodeDraggable]="element"
2121
[tree]="tree">
2222
23-
<div class="folding" (click)="onSwitchFoldingType()" [ngClass]="tree.foldingType.cssClass"></div>
23+
<div class="folding" (click)="onSwitchFoldingType()" [ngClass]="tree.foldingCssClass"></div>
2424
<div class="node-value"
2525
*ngIf="!shouldShowInputForTreeValue()"
2626
[class.node-selected]="isSelected"
2727
(click)="onNodeSelected($event)">
28-
{{tree.value}}<span class="loading-children" *ngIf="tree.childrenAreBeingLoaded()"></span>
28+
<div *ngIf="tree.nodeTemplate" class="node-template" [innerHTML]="tree.nodeTemplate"></div>
29+
{{ tree.value }}
30+
<span class="loading-children" *ngIf="tree.childrenAreBeingLoaded()"></span>
2931
</div>
3032
3133
<input type="text" class="node-value"
3234
*ngIf="shouldShowInputForTreeValue()"
3335
[nodeEditable]="tree.value"
3436
(valueChanged)="applyNewValue($event)"/>
3537
36-
<div class="node-left-menu" #leftMenuButton
37-
*ngIf="tree.hasLeftMenu()" (click)="showLeftMenu($event)">
38+
<div class="node-left-menu" *ngIf="tree.hasLeftMenu()" (click)="showLeftMenu($event)" [innerHTML]="tree.leftMenuTemplate">
3839
</div>
3940
<node-menu *ngIf="tree.hasLeftMenu() && isLeftMenuVisible"
4041
(menuItemSelected)="onMenuItemSelected($event)">
@@ -175,9 +176,9 @@ export class TreeInternalComponent implements OnInit {
175176
this.treeService.fireNodeRemoved(this.tree);
176177
}
177178

178-
private onSwitchFoldingType(): void {
179+
public onSwitchFoldingType(): void {
179180
this.tree.switchFoldingType();
180-
this.treeService.fireNodeSwitchFoldingType(this.tree);
181+
this.treeService.fireNodeSwitchFoldingType(this.tree);
181182
}
182183

183184
public applyNewValue(e: NodeEditableEvent): void {

src/tree.ts

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,15 @@ export class Tree {
206206

207207
/**
208208
* Check whether or not this tree has a left menu.
209-
* @returns {boolean} A flag indicating whether or not this has a left menu.
209+
* @returns {boolean} A flag indicating whether or not this tree has a left menu.
210210
*/
211211
public hasLeftMenu(): boolean {
212212
return !_.get(this.node.settings, 'static', false) && _.get(this.node.settings, 'leftMenu', false);
213213
}
214214

215215
/**
216216
* Check whether or not this tree has a right menu.
217-
* @returns {boolean} A flag indicating whether or not this has a right menu.
217+
* @returns {boolean} A flag indicating whether or not this tree has a right menu.
218218
*/
219219
public hasRightMenu(): boolean {
220220
return !_.get(this.node.settings, 'static', false) && _.get(this.node.settings, 'rightMenu', false);
@@ -306,23 +306,79 @@ export class Tree {
306306
return this.foldingType === FoldingType.Expanded;
307307
}
308308

309+
/**
310+
* Set a current folding type: expanded, collapsed or leaf.
311+
*/
312+
private _setFoldingType(): void {
313+
if (this.childrenShouldBeLoaded()) {
314+
this.node._foldingType = FoldingType.Collapsed;
315+
} else if (this._children) {
316+
this.node._foldingType = FoldingType.Expanded;
317+
} else {
318+
this.node._foldingType = FoldingType.Leaf;
319+
}
320+
}
321+
309322
/**
310323
* Get a current folding type: expanded, collapsed or leaf.
311324
* @returns {FoldingType} A folding type of the current tree.
312325
*/
313326
public get foldingType(): FoldingType {
314327
if (!this.node._foldingType) {
315-
if (this.childrenShouldBeLoaded()) {
316-
this.node._foldingType = FoldingType.Collapsed;
317-
} else if (this._children) {
318-
this.node._foldingType = FoldingType.Expanded;
319-
} else {
320-
this.node._foldingType = FoldingType.Leaf;
321-
}
328+
this._setFoldingType();
322329
}
323330
return this.node._foldingType;
324331
}
325332

333+
/**
334+
* Get a css class for element which displayes folding state - expanded, collapsed or leaf
335+
* @returns {string} A string icontaining css class (classes)
336+
*/
337+
public get foldingCssClass(): string {
338+
return this.getCssClassesFromSettings() || this.foldingType.cssClass;
339+
}
340+
341+
private getCssClassesFromSettings(): string {
342+
if (!this.node._foldingType) {
343+
this._setFoldingType();
344+
}
345+
346+
if (this.node._foldingType === FoldingType.Collapsed) {
347+
return _.get(this.node.settings, 'cssClasses.collapsed', null);
348+
} else if (this.node._foldingType === FoldingType.Expanded) {
349+
return _.get(this.node.settings, 'cssClasses.expanded', null);
350+
}
351+
352+
return _.get(this.node.settings, 'cssClasses.leaf', null);
353+
}
354+
355+
/**
356+
* Get a html template to render before every node's name.
357+
* @returns {string} A string representing a html template.
358+
*/
359+
public get nodeTemplate(): string {
360+
return this.getTemplateFromSettings();
361+
}
362+
363+
private getTemplateFromSettings(): string {
364+
if (this.isLeaf()) {
365+
return _.get(this.node.settings, 'templates.leaf', '');
366+
} else {
367+
return _.get(this.node.settings, 'templates.node', '');
368+
}
369+
}
370+
371+
/**
372+
* Get a html template to render for an element activatin left menu of a node.
373+
* @returns {string} A string representing a html template.
374+
*/
375+
public get leftMenuTemplate(): string {
376+
if (this.hasLeftMenu()) {
377+
return _.get(this.node.settings, 'templates.leftMenu', '<span></span>');
378+
}
379+
return '';
380+
}
381+
326382
/**
327383
* Check that current tree is newly created (added by user via menu for example). Tree that was built from the TreeModel is not marked as new.
328384
* @returns {boolean} A flag whether the tree is new.

src/tree.types.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,35 @@ export interface TreeModel {
2525
_foldingType?: FoldingType;
2626
}
2727

28+
export interface CssClasses {
29+
/* The class or classes that should be added to the expanded node */
30+
expanded?: string;
31+
32+
/* The class or classes that should be added to the collapsed node */
33+
collapsed?: string;
34+
35+
/* The class or classes that should be added to the expanded to the leaf */
36+
leaf?: string;
37+
}
38+
39+
export interface Templates {
40+
/* A template for a node */
41+
node?: string;
42+
43+
/* A template for a leaf node */
44+
leaf?: string;
45+
46+
/* A template for left menu html element */
47+
leftMenu?: string;
48+
}
49+
2850
export class TreeModelSettings {
51+
/* cssClasses - set custom css classes which will be used for a tree */
52+
public cssClasses?: CssClasses;
53+
54+
/* Templates - set custom html templates to be used in a tree */
55+
public templates?: Templates;
56+
2957
/**
3058
* "leftMenu" property when set to true makes left menu available.
3159
* @name TreeModelSettings#leftMenu
@@ -51,7 +79,7 @@ export class TreeModelSettings {
5179
public static?: boolean;
5280

5381
public static merge(sourceA: TreeModel, sourceB: TreeModel): TreeModelSettings {
54-
return _.defaults({}, _.get(sourceA, 'settings'), _.get(sourceB, 'settings'), {static: false, leftMenu: false, rightMenu: true});
82+
return _.defaultsDeep({}, _.get(sourceA, 'settings'), _.get(sourceB, 'settings'), {static: false, leftMenu: false, rightMenu: true});
5583
}
5684
}
5785

0 commit comments

Comments
 (0)