From 7dcf086fe48f7a1690122418b7f7108e7be9c6a2 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 30 Aug 2021 10:07:36 +0700 Subject: vfs: remove WuiVfsNode in favor of WuiVfsNodeInterface Since WuiVfsNode does not have any methods then and the constructor only initialize the value to zero, we remove the type and use the interface instead. --- vfs/vfs.d.ts | 30 +++++++++---------------- vfs/vfs.ts | 71 ++++++++++++++++++++++-------------------------------------- 2 files changed, 36 insertions(+), 65 deletions(-) diff --git a/vfs/vfs.d.ts b/vfs/vfs.d.ts index 2d3a538..177fcfd 100644 --- a/vfs/vfs.d.ts +++ b/vfs/vfs.d.ts @@ -2,18 +2,18 @@ import { WuiResponseInterface } from "../response"; export interface WuiVfsNodeInterface { name: string; path: string; - is_dir?: boolean; - content_type?: string; - mod_time?: number; - size?: number; - mode?: string; - childs?: WuiVfsNodeInterface[]; - content?: string; + is_dir: boolean; + content_type: string; + mod_time: number; + size: number; + mode: string; + childs: WuiVfsNodeInterface[]; + content: string; } export interface WuiVfsOptions { id: string; Open(path: string, is_dir: boolean): Promise; - OpenNode(node: WuiVfsNode): Promise; + OpenNode(node: WuiVfsNodeInterface): Promise; } export declare class WuiVfs { opts: WuiVfsOptions; @@ -21,17 +21,7 @@ export declare class WuiVfs { private com_path; private com_list; constructor(opts: WuiVfsOptions); - OpenNode(node: WuiVfsNode): void; + OpenNode(node: WuiVfsNodeInterface): void; OpenDir(path: string): Promise; -} -export declare class WuiVfsNode implements WuiVfsNodeInterface { - path: string; - name: string; - content_type: string; - mod_time: number; - size: number; - mode: string; - is_dir: boolean; - childs: WuiVfsNode[]; - constructor(opts: WuiVfsNodeInterface); + Set(node: WuiVfsNodeInterface): void; } diff --git a/vfs/vfs.ts b/vfs/vfs.ts index 44c447b..b1b0881 100644 --- a/vfs/vfs.ts +++ b/vfs/vfs.ts @@ -7,15 +7,18 @@ import { WuiResponseInterface } from "../response" export interface WuiVfsNodeInterface { name: string path: string - is_dir?: boolean - content_type?: string - mod_time?: number - size?: number - mode?: string - childs?: WuiVfsNodeInterface[] - content?: string // If its not empty, it MUST be encoded in base64. + is_dir: boolean + content_type: string + mod_time: number + size: number + mode: string + childs: WuiVfsNodeInterface[] + content: string // If its not empty, it MUST be encoded in base64. } +type PathClickHandler = (path: string) => void +type NodeClickHandler = (node: WuiVfsNodeInterface) => void + export interface WuiVfsOptions { id: string @@ -25,7 +28,7 @@ export interface WuiVfsOptions { // OpenNode define an handler that will be called when a file is clicked // from the WuiVfsList. - OpenNode(node: WuiVfsNode): Promise + OpenNode(node: WuiVfsNodeInterface): Promise } export class WuiVfs { @@ -48,7 +51,7 @@ export class WuiVfs { }) this.el.appendChild(this.com_path.el) - this.com_list = new WuiVfsList((node: WuiVfsNode) => { + this.com_list = new WuiVfsList((node: WuiVfsNodeInterface) => { this.OpenNode(node) }) this.el.appendChild(this.com_list.el) @@ -56,7 +59,7 @@ export class WuiVfs { // OpenNode is a handler that will be called when a node is clicked // inside the WuiVfsList. - OpenNode(node: WuiVfsNode): void { + OpenNode(node: WuiVfsNodeInterface): void { if (node.is_dir) { this.OpenDir(node.path) } else { @@ -71,42 +74,20 @@ export class WuiVfs { if (res.code != 200) { return } - let node = new WuiVfsNode(res.data as WuiVfsNodeInterface) - this.com_path.Open(node) - this.com_list.Open(node) + this.Set(res.data as WuiVfsNodeInterface) } -} -export class WuiVfsNode implements WuiVfsNodeInterface { - path: string - name: string - content_type: string - mod_time: number - size: number - mode: string - is_dir: boolean - childs: WuiVfsNode[] - - constructor(opts: WuiVfsNodeInterface) { - this.path = opts.path || "" - this.name = opts.name || "" - this.content_type = opts.content_type || "" - this.mod_time = opts.mod_time || 0 - this.size = opts.size || 0 - this.mode = opts.mode || "" - this.is_dir = opts.is_dir || false - - this.childs = [] - if (opts.childs) { - for (let c of opts.childs) { - this.childs.push(new WuiVfsNode(c)) - } + Set(node: WuiVfsNodeInterface) { + if (node.is_dir) { + this.com_path.Open(node) + this.com_list.Open(node) } } } class WuiVfsList { el: HTMLElement + node: WuiVfsNodeInterface | null = null constructor(public onClick: NodeClickHandler) { this.el = document.createElement("div") @@ -115,10 +96,13 @@ class WuiVfsList { this.el.style.borderColor = "silver" } - Open(node: WuiVfsNode) { + Open(node: WuiVfsNodeInterface) { + this.node = node this.el.innerHTML = "" - - for (let c of node.childs) { + if (!this.node.childs) { + return + } + for (let c of this.node.childs) { let el = document.createElement("div") el.style.padding = "1em" el.style.cursor = "pointer" @@ -161,7 +145,7 @@ class WuiVfsPath { this.onClick = onClick } - Open(node: WuiVfsNode) { + Open(node: WuiVfsNodeInterface) { this.el.innerHTML = "" this.crumbs = [] let paths = [] @@ -204,6 +188,3 @@ class WuiVfsPath { } } } - -type PathClickHandler = (path: string) => void -type NodeClickHandler = (node: WuiVfsNode) => void -- cgit v1.3