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