Skip to content

Commit beb641a

Browse files
committed
address comments
1 parent 9be93a3 commit beb641a

13 files changed

+254
-323
lines changed

src/demo-app/demo-app/demo-module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ import {FoggyTabContent, RainyTabContent, SunnyTabContent, TabsDemo} from '../ta
5656
import {ToolbarDemo} from '../toolbar/toolbar-demo';
5757
import {TooltipDemo} from '../tooltip/tooltip-demo';
5858
import {TreeDemo} from '../tree/tree-demo';
59-
import {JsonDatabase} from '../tree/json-database';
59+
import {FileDatabase} from '../tree/file-database';
6060
import {TypographyDemo} from '../typography/typography-demo';
6161
import {DemoApp, Home} from './demo-app';
6262
import {DEMO_APP_ROUTES} from './routes';
@@ -129,7 +129,7 @@ import {TableDemoModule} from '../table/table-demo-module';
129129
],
130130
providers: [
131131
{provide: OverlayContainer, useClass: FullscreenOverlayContainer},
132-
JsonDatabase
132+
FileDatabase
133133
],
134134
entryComponents: [
135135
ContentElementDialog,

src/demo-app/tree/file-database.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import {Component, Injectable} from '@angular/core';
2+
import {FlatTreeControl} from '@angular/cdk-experimental/tree';
3+
import {MatTreeFlattener, MatTreeFlatDataSource} from '@angular/material-experimental/tree';
4+
import {of} from 'rxjs/observable/of';
5+
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
6+
7+
/**
8+
* File node data with nested structure.
9+
* Each node has a filename, and a type or a list of children.
10+
*/
11+
export class FileNode {
12+
children: FileNode[];
13+
filename: string;
14+
type: any;
15+
}
16+
17+
/** Flat node with expandable and level information */
18+
export class FileFlatNode {
19+
filename: string;
20+
type: any;
21+
level: number;
22+
expandable: boolean;
23+
}
24+
25+
/**
26+
* The file structure tree data in string. The data could be parsed into a Json object
27+
*/
28+
const TREE_DATA = `{"Tina":
29+
{
30+
"Documents": {
31+
"angular": {
32+
"src": {
33+
"core": "ts",
34+
"compiler": "ts"
35+
}
36+
},
37+
"material2": {
38+
"src": {
39+
"button": "ts",
40+
"checkbox": "ts",
41+
"input": "ts"
42+
}
43+
}
44+
},
45+
"Downloads": {
46+
"Tutorial": "html",
47+
"November": "pdf",
48+
"October": "pdf"
49+
},
50+
"Pictures": {
51+
"Sun": "png",
52+
"Woods": "jpg",
53+
"Photo Booth Library": {
54+
"Contents": "dir",
55+
"Pictures": "dir"
56+
}
57+
},
58+
"Applications": {
59+
"Chrome": "app",
60+
"Calendar": "app",
61+
"Webstorm": "app"
62+
}
63+
}}`;
64+
65+
/**
66+
* File database, it can build a tree structured Json object from string.
67+
* Each node in Json object represents a file or a directory. For a file, it has filename and type.
68+
* For a directory, it has filename and children (a list of files or directories).
69+
* The input will be a json object string, and the output is a list of `FileNode` with nested
70+
* structure.
71+
*/
72+
@Injectable()
73+
export class FileDatabase {
74+
dataChange: BehaviorSubject<FileNode[]> = new BehaviorSubject<FileNode[]>([]);
75+
76+
get data(): FileNode[] { return this.dataChange.value; }
77+
78+
constructor() {
79+
this.initialize();
80+
}
81+
82+
initialize() {
83+
// Parse the string to json object.
84+
const dataObject = JSON.parse(TREE_DATA);
85+
86+
// Build the tree nodes from Json object. The result is a list of `FileNode` with nested
87+
// file node as children.
88+
const data = this.buildFileTree(dataObject, 0);
89+
90+
// Notify the change.
91+
this.dataChange.next(data);
92+
}
93+
94+
/**
95+
* Build the file structure tree. The `value` is the Json object, or a sub-tree of a Json object.
96+
* The return value is the list of `FileNode`.
97+
*/
98+
buildFileTree(value: any, level: number) {
99+
let data: any[] = [];
100+
for (let k in value) {
101+
let v = value[k];
102+
let node = new FileNode();
103+
node.filename = `${k}`;
104+
if (v === null || v === undefined) {
105+
// no action
106+
} else if (typeof v === 'object') {
107+
node.children = this.buildFileTree(v, level + 1);
108+
} else {
109+
node.type = v;
110+
}
111+
data.push(node);
112+
}
113+
return data;
114+
}
115+
}

src/demo-app/tree/flat-data-source.ts

Lines changed: 0 additions & 102 deletions
This file was deleted.

src/demo-app/tree/json-database.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/demo-app/tree/nested-data-source.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

0 commit comments

Comments
 (0)