Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { TodoItemNode } from './classes';
- import { BehaviorSubject } from 'rxjs';
- import { Injectable } from '@angular/core';
- /**
- * Checklist database, it can build a tree structured Json object.
- * Each node in Json object represents a to-do item or a category.
- * If a node is a category, it has children items and new items can be added under the category.
- */
- @Injectable()
- export class ChecklistDatabase {
- dataChange = new BehaviorSubject<TodoItemNode[]>([]);
- get data(): TodoItemNode[] {
- return this.dataChange.value;
- }
- constructor() {
- this.initialize();
- }
- initialize() {
- // Build the tree nodes from Json object. The result is a list of `TodoItemNode` with nested
- // file node as children.
- // const data = this.buildFileTree(tree_data, 0);
- // Notify the change.
- this.dataChange.next([]);
- }
- /**
- * 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 `TodoItemNode`.
- */
- buildFileTree(obj: object, level: number): TodoItemNode[] {
- return Object.keys(obj).reduce<TodoItemNode[]>((accumulator, key) => {
- const value = obj[key];
- const node = new TodoItemNode();
- node.item = key;
- if (value != null) {
- if (typeof value === 'object') {
- node.children = this.buildFileTree(value, level + 1);
- } else {
- node.item = value;
- }
- }
- return accumulator.concat(node);
- }, []);
- }
- /** Add an item to to-do list */
- insertItem(parent: TodoItemNode, name: string): TodoItemNode {
- if (!parent.children) {
- parent.children = [];
- }
- const newItem = { item: name } as TodoItemNode;
- parent.children.push(newItem);
- this.dataChange.next(this.data);
- return newItem;
- }
- insertItemAbove(node: TodoItemNode, name: string): TodoItemNode {
- const parentNode = this.getParentFromNodes(node);
- const newItem = { item: name } as TodoItemNode;
- if (parentNode != null) {
- parentNode.children.splice(parentNode.children.indexOf(node), 0, newItem);
- } else {
- this.data.splice(this.data.indexOf(node), 0, newItem);
- }
- this.dataChange.next(this.data);
- return newItem;
- }
- insertItemBelow(node: TodoItemNode, name: string): TodoItemNode {
- const parentNode = this.getParentFromNodes(node);
- const newItem = { item: name } as TodoItemNode;
- if (parentNode != null) {
- parentNode.children.splice(
- parentNode.children.indexOf(node) + 1,
- 0,
- newItem
- );
- } else {
- this.data.splice(this.data.indexOf(node) + 1, 0, newItem);
- }
- this.dataChange.next(this.data);
- return newItem;
- }
- getParentFromNodes(node: TodoItemNode): TodoItemNode {
- for (let i = 0; i < this.data.length; ++i) {
- const currentRoot = this.data[i];
- const parent = this.getParent(currentRoot, node);
- if (parent != null) {
- return parent;
- }
- }
- return null;
- }
- getParent(currentRoot: TodoItemNode, node: TodoItemNode): TodoItemNode {
- if (currentRoot.children && currentRoot.children.length > 0) {
- for (let i = 0; i < currentRoot.children.length; ++i) {
- const child = currentRoot.children[i];
- if (child === node) {
- return currentRoot;
- } else if (child.children && child.children.length > 0) {
- const parent = this.getParent(child, node);
- if (parent != null) {
- return parent;
- }
- }
- }
- }
- return null;
- }
- updateItem(node: TodoItemNode, name: string) {
- node.item = name;
- this.dataChange.next(this.data);
- }
- deleteItem(node: TodoItemNode) {
- this.deleteNode(this.data, node);
- this.dataChange.next(this.data);
- }
- copyPasteItem(from: TodoItemNode, to: TodoItemNode): TodoItemNode {
- const newItem = this.insertItem(to, from.item);
- if (from.children) {
- from.children.forEach(child => {
- this.copyPasteItem(child, newItem);
- });
- }
- return newItem;
- }
- copyPasteItemAbove(from: TodoItemNode, to: TodoItemNode): TodoItemNode {
- const newItem = this.insertItemAbove(to, from.item);
- if (from.children) {
- from.children.forEach(child => {
- this.copyPasteItem(child, newItem);
- });
- }
- return newItem;
- }
- copyPasteItemBelow(from: TodoItemNode, to: TodoItemNode): TodoItemNode {
- const newItem = this.insertItemBelow(to, from.item);
- if (from.children) {
- from.children.forEach(child => {
- this.copyPasteItem(child, newItem);
- });
- }
- return newItem;
- }
- deleteNode(nodes: TodoItemNode[], nodeToDelete: TodoItemNode) {
- const index = nodes.indexOf(nodeToDelete, 0);
- if (index > -1) {
- nodes.splice(index, 1);
- } else {
- nodes.forEach(node => {
- if (node.children && node.children.length > 0) {
- this.deleteNode(node.children, nodeToDelete);
- }
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement