Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
fix: types
Browse files Browse the repository at this point in the history
  • Loading branch information
artemnih committed Oct 4, 2021
1 parent b00029e commit 3909c62
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
23 changes: 11 additions & 12 deletions projects/ngx-explorer/src/lib/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Observable } from "rxjs";

export type TNode = any;
export type NodeContent = { leafs: TNode[], nodes: TNode[] };
export type NodeContent<T> = { leafs: T[], nodes: T[] };

export interface Dictionary<T> {
[Key: string]: T;
Expand All @@ -10,18 +9,18 @@ export interface Dictionary<T> {
export interface XNode {
id: string;
parentId: string;
data: TNode;
data: any;
isLeaf: boolean;
children: XNode[];
}

export interface IDataService {
getNodeChildren(node: TNode): Observable<NodeContent>;
createNode(parentNode: TNode, node: TNode) : Observable<TNode>;
renameNode(node: TNode, newName: string): Observable<TNode>;
renameLeaf(node: TNode, newName: string): Observable<TNode>;
deleteNodes(nodes: TNode[]): Observable<any>;
deleteLeafs(nodes: TNode[]): Observable<any>;
uploadFiles(node: TNode, files: File[]): Observable<TNode>;
download(node: TNode): Observable<any>;
export interface IDataService<T> {
getNodeChildren(node: T): Observable<NodeContent<T>>;
createNode(parentNode: T, name: string) : Observable<any>;
renameNode(node: T, newName: string): Observable<any>;
renameLeaf(node: T, newName: string): Observable<any>;
deleteNodes(nodes: T[]): Observable<any>;
deleteLeafs(nodes: T[]): Observable<any>;
uploadFiles(node: T, files: File[]): Observable<any>;
download(node: T): Observable<any>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { IDataService } from "ngx-explorer";
@Injectable({
providedIn: 'root'
})
export abstract class DataService implements IDataService {
export abstract class DataService implements IDataService<any> {
abstract getNodeChildren(node: any);
abstract createNode(parentNode: any, node: any)
abstract createNode(parentNode: any, name: any)
abstract renameNode(node: any, newName: string)
abstract renameLeaf(node: any, newName: string)
abstract deleteNodes(nodes: any[])
abstract deleteLeafs(nodes: any[])
abstract uploadFiles(node: any, files: File[])
abstract download(node: any)
abstract download(node: any) // TODO multple download. should be configurable in settings
// move(from to) // TODO: on/off in settings
// copyPaste(from to) // TODO: on/off in settings
// cutPaste(from to) // TODO: on/off in settings
}
4 changes: 2 additions & 2 deletions projects/ngx-explorer/src/lib/services/explorer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { BehaviorSubject, forkJoin, of } from 'rxjs';
import { XNode, Dictionary, NodeContent } from '../common/types';
import { Utils } from '../shared/utils';
import { DataService } from './data-provider.service';
import { DataService } from './data.service';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -37,7 +37,7 @@ export class ExplorerService {

this.dataService
.getNodeChildren(parent.data)
.subscribe(({ leafs, nodes }: NodeContent) => {
.subscribe(({ leafs, nodes }: NodeContent<any>) => {
const childrenNodes = nodes.map(data => Utils.createNode(nodeId, false, data));
const childrenLeafs = leafs.map(data => Utils.createNode(nodeId, true, data));
parent.children = childrenNodes.concat(childrenLeafs);
Expand Down
2 changes: 1 addition & 1 deletion projects/ngx-explorer/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

export * from './lib/common/types';
export * from './lib/ngx-explorer.module';
export * from './lib/services/data-provider.service';
export * from './lib/services/data.service';
export * from './lib/services/explorer.service';
export * from './lib/components/icons/icons.component';
export * from './lib/components/explorer/explorer.component';
Expand Down
32 changes: 19 additions & 13 deletions src/app/data.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { forkJoin, Observable, of, Subscriber } from 'rxjs';
import { IDataService, NodeContent, TNode } from 'ngx-explorer'
import { IDataService, NodeContent } from 'ngx-explorer'
import { v4 as uuid } from 'uuid';

let mock_folders = [
Expand All @@ -26,9 +26,16 @@ let mock_files = [
{ id: 32, name: 'Rock And Roll All Nite.txt', path: 'music/rock/ledzeppelin/rockandrollallnight', content: 'hi, this is an example' },
]

export class ExampleDataService implements IDataService {
interface ExampleNode {
name: string;
path: string;
content?: string;
id: number | string;
}

export class ExampleDataService implements IDataService<ExampleNode> {

download(node: TNode): Observable<any> {
download(node: ExampleNode): Observable<any> {
const file = mock_files.find(f => f.id === node.id);

const myblob = new Blob([file.content], {
Expand All @@ -47,7 +54,7 @@ export class ExampleDataService implements IDataService {
return of(null);
}

uploadFiles(node: TNode, files: File[]): Observable<TNode[]> {
uploadFiles(node: ExampleNode, files: File[]): Observable<any> {
const results = [];

for (let i = 0; i < files.length; i++) {
Expand All @@ -70,7 +77,7 @@ export class ExampleDataService implements IDataService {
return forkJoin(results);
}

deleteNodes(nodes: TNode[]): Observable<any> {
deleteNodes(nodes: ExampleNode[]): Observable<any> {
const results = nodes.map(node => {
const path = node.path + '/';
mock_files = mock_files.filter(f => !f.path.startsWith(path))
Expand All @@ -81,7 +88,7 @@ export class ExampleDataService implements IDataService {
return forkJoin(results)
}

deleteLeafs(nodes: TNode[]): Observable<any> {
deleteLeafs(nodes: ExampleNode[]): Observable<any> {
const results = nodes.map(node => {
const leaf = mock_files.find(f => f.id === node.id);
const index = mock_files.indexOf(leaf);
Expand All @@ -91,14 +98,14 @@ export class ExampleDataService implements IDataService {
return forkJoin(results)
}

createNode(node: TNode, data: TNode): Observable<TNode> {
const path = (node?.path ? node.path + '/' : '') + data.replace(/[\W_]+/g, " ");
const newFolder = { path, id: uuid(), name: data };
createNode(node: ExampleNode, name: string): Observable<any> {
const path = (node?.path ? node.path + '/' : '') + name.replace(/[\W_]+/g, " ");
const newFolder = { path, id: uuid(), name: name };
mock_folders.push(newFolder);
return of(newFolder);
}

getNodeChildren(node: TNode): Observable<NodeContent> {
getNodeChildren(node: ExampleNode): Observable<NodeContent<ExampleNode>> {
const folderPath = node?.path || '';

const nodes = mock_folders.filter(f => {
Expand All @@ -118,16 +125,15 @@ export class ExampleDataService implements IDataService {
return of({ leafs, nodes });
}

renameNode(nodeInfo: TNode, newName: string): Observable<TNode> {
renameNode(nodeInfo: ExampleNode, newName: string): Observable<ExampleNode> {
const node = mock_folders.find(f => f.id === nodeInfo.id);
node.name = newName;
return of(node);
}

renameLeaf(leafInfo: TNode, newName: string): Observable<TNode> {
renameLeaf(leafInfo: ExampleNode, newName: string): Observable<ExampleNode> {
const leaf = mock_files.find(f => f.id === leafInfo.id);
leaf.name = newName;
return of(leaf);

}
}

0 comments on commit 3909c62

Please sign in to comment.