Skip to content

Commit

Permalink
docs(tree): add tree examples (#9585)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinayuangao authored Feb 16, 2018
1 parent b519039 commit 631397c
Show file tree
Hide file tree
Showing 15 changed files with 503 additions and 253 deletions.
2 changes: 0 additions & 2 deletions src/demo-app/demo-app/demo-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import {
import {ToolbarDemo} from '../toolbar/toolbar-demo';
import {TooltipDemo} from '../tooltip/tooltip-demo';
import {TreeDemo} from '../tree/tree-demo';
import {JsonDatabase} from '../tree/json-database';
import {TypographyDemo} from '../typography/typography-demo';
import {DemoApp, Home} from './demo-app';
import {DEMO_APP_ROUTES} from './routes';
Expand Down Expand Up @@ -134,7 +133,6 @@ import {BadgeDemo} from '../badge/badge-demo';
],
providers: [
{provide: OverlayContainer, useClass: FullscreenOverlayContainer},
JsonDatabase
],
entryComponents: [
ContentElementDialog,
Expand Down
119 changes: 119 additions & 0 deletions src/demo-app/tree/file-database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Injectable} from '@angular/core';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';

/**
* File node data with nested structure.
* Each node has a filename, and a type or a list of children.
*/
export class FileNode {
children: FileNode[];
filename: string;
type: any;
}

/** Flat node with expandable and level information */
export class FileFlatNode {
filename: string;
type: any;
level: number;
expandable: boolean;
}

/**
* The file structure tree data in string. The data could be parsed into a Json object
*/
const TREE_DATA = `{"Tina":
{
"Documents": {
"angular": {
"src": {
"core": "ts",
"compiler": "ts"
}
},
"material2": {
"src": {
"button": "ts",
"checkbox": "ts",
"input": "ts"
}
}
},
"Downloads": {
"Tutorial": "html",
"November": "pdf",
"October": "pdf"
},
"Pictures": {
"Sun": "png",
"Woods": "jpg",
"Photo Booth Library": {
"Contents": "dir",
"Pictures": "dir"
}
},
"Applications": {
"Chrome": "app",
"Calendar": "app",
"Webstorm": "app"
}
}}`;

/**
* File database, it can build a tree structured Json object from string.
* Each node in Json object represents a file or a directory. For a file, it has filename and type.
* For a directory, it has filename and children (a list of files or directories).
* The input will be a json object string, and the output is a list of `FileNode` with nested
* structure.
*/
@Injectable()
export class FileDatabase {
dataChange: BehaviorSubject<FileNode[]> = new BehaviorSubject<FileNode[]>([]);

get data(): FileNode[] { return this.dataChange.value; }

constructor() {
this.initialize();
}

initialize() {
// Parse the string to json object.
const dataObject = JSON.parse(TREE_DATA);

// Build the tree nodes from Json object. The result is a list of `FileNode` with nested
// file node as children.
const data = this.buildFileTree(dataObject, 0);

// Notify the change.
this.dataChange.next(data);
}

/**
* Build the file structure tree. The `value` is the Json object, or a sub-tree of a Json object.
* The return value is the list of `FileNode`.
*/
buildFileTree(value: any, level: number) {
let data: any[] = [];
for (let k in value) {
let v = value[k];
let node = new FileNode();
node.filename = `${k}`;
if (v === null || v === undefined) {
// no action
} else if (typeof v === 'object') {
node.children = this.buildFileTree(v, level + 1);
} else {
node.type = v;
}
data.push(node);
}
return data;
}
}
102 changes: 0 additions & 102 deletions src/demo-app/tree/flat-data-source.ts

This file was deleted.

88 changes: 0 additions & 88 deletions src/demo-app/tree/json-database.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/demo-app/tree/nested-data-source.ts

This file was deleted.

Loading

0 comments on commit 631397c

Please sign in to comment.