summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-09-05 21:46:40 +0700
committerShulhan <ms@kilabit.info>2021-09-05 21:46:40 +0700
commit903561a9074da0632e1e001db3879cc45f974493 (patch)
treeae0fa5cb0d44dbb086eb37a8d174ebb1cf3bc65a
parentcaf0f69ed09d01ff6053cf095b720606077e442c (diff)
downloadpakakeh.ts-903561a9074da0632e1e001db3879cc45f974493.tar.xz
vfs: refactor example to use TypeScript loaded with type="module"
-rw-r--r--index.html1
-rw-r--r--vfs/example.d.ts1
-rw-r--r--vfs/example.html144
-rw-r--r--vfs/example.ts159
-rw-r--r--vfs/index.html11
-rw-r--r--vfs/vfs.d.ts10
-rw-r--r--vfs/vfs.ts10
7 files changed, 182 insertions, 154 deletions
diff --git a/index.html b/index.html
index da342af..f0be1a4 100644
--- a/index.html
+++ b/index.html
@@ -7,5 +7,6 @@
<body>
<h3><a href="/editor"> Editor </a></h3>
<h3><a href="/input"> Input </a></h3>
+ <h3><a href="/vfs"> VFS </a></h3>
</body>
</html>
diff --git a/vfs/example.d.ts b/vfs/example.d.ts
new file mode 100644
index 0000000..cb0ff5c
--- /dev/null
+++ b/vfs/example.d.ts
@@ -0,0 +1 @@
+export {};
diff --git a/vfs/example.html b/vfs/example.html
deleted file mode 100644
index 4e28bf8..0000000
--- a/vfs/example.html
+++ /dev/null
@@ -1,144 +0,0 @@
-<!DOCTYPE html>
-<html>
- <head>
- <meta charset="UTF-8" />
- <title>WUI - Virtual File System (vfs)</title>
- </head>
- <body onload="main()">
- <div id="vfs"></div>
-
- <script>
- var exports = {}
- var wui_vfs
- </script>
- <script src="vfs.js"></script>
- <script>
- let dummyfs = {
- "/": {
- name: "/",
- path: "/",
- is_dir: true,
- childs: [
- {
- name: "Dir 1",
- path: "/Dir 1",
- is_dir: true,
- childs: [
- {
- name: "File 1.1",
- path: "/Dir 1/File 1.1",
- },
- {
- name: "File 1.2",
- path: "/Dir 1/File 1.2",
- },
- ],
- },
- {
- name: "Dir 2",
- path: "/Dir 2",
- is_dir: true,
- childs: [
- {
- name: "File 2.1",
- path: "/Dir 2/File 2.1",
- },
- {
- name: "File 2.2",
- path: "/Dir 2/File 2.2",
- },
- ],
- },
- ],
- },
- "/Dir 1": {
- name: "Dir 1",
- path: "/Dir 1",
- is_dir: true,
- childs: [
- {
- name: "File 1.1",
- path: "/Dir 1/File 1.1",
- },
- {
- name: "File 1.2",
- path: "/Dir 1/File 1.2",
- },
- ],
- },
- "/Dir 2": {
- name: "Dir 2",
- path: "/Dir 2",
- is_dir: true,
- childs: [
- {
- name: "File 2.1",
- path: "/Dir 2/File 2.1",
- },
- {
- name: "File 2.2",
- path: "/Dir 2/File 2.2",
- },
- ],
- },
- }
-
- async function main() {
- let opts = {
- id: "vfs",
- Open: Open,
- OpenNode: OpenNode,
- }
-
- wui_vfs = new WuiVfs(opts)
- wui_vfs.OpenDir("/")
- }
-
- function Open(path, is_dir) {
- console.log("Open:", path, is_dir)
- let res = {
- code: 200,
- }
-
- if (is_dir) {
- res.data = dummyfs[path]
- return res
- }
-
- res.data = {
- name: "",
- path: path,
- content: "",
- }
-
- switch (path) {
- case "/Dir 1/File 1.1":
- res.data.name = "File 1.1"
- res.data.content = "This is the content of " + res.data.name
- break
- case "/Dir 1/File 1.2":
- res.data.name = "File 1.2"
- res.data.content = "This is the content of " + res.data.name
- break
- case "/Dir 2/File 2.1":
- res.data.name = "File 2.1"
- res.data.content = "This is the content of " + res.data.name
- break
- case "/Dir 2/File 2.2":
- res.data.name = "File 2.1"
- res.data.content = "This is the content of " + res.data.name
- break
- default:
- res.code = 404
- res.message = "path not found"
- }
-
- console.log("Open:", res)
- }
-
- function OpenNode(node) {
- Open(node.path, node.is_dir)
- }
- </script>
- </body>
-</html>
diff --git a/vfs/example.ts b/vfs/example.ts
new file mode 100644
index 0000000..bc8d37d
--- /dev/null
+++ b/vfs/example.ts
@@ -0,0 +1,159 @@
+import { WuiVfs, WuiVfsNodeInterface } from "./vfs.js"
+import { WuiResponseInterface } from "../response.js"
+
+interface PathNodeInterface {
+ [key: string]: WuiVfsNodeInterface
+}
+
+let dummyfs: PathNodeInterface = {
+ "/": {
+ name: "/",
+ path: "/",
+ is_dir: true,
+ content: "",
+ childs: [
+ {
+ name: "Dir 1",
+ path: "/Dir 1",
+ is_dir: true,
+ content: "",
+ childs: [
+ {
+ name: "File 1.1",
+ path: "/Dir 1/File 1.1",
+ is_dir: false,
+ content: "This is the content of File 1.1",
+ },
+ {
+ name: `File 1.2`,
+ path: "/Dir 1/File 1.2",
+ is_dir: false,
+ content: "This is the content of File 1.2",
+ },
+ ],
+ },
+ {
+ name: "Dir 2",
+ path: "/Dir 2",
+ is_dir: true,
+ content: "",
+ childs: [
+ {
+ name: "File 2.1",
+ path: "/Dir 2/File 2.1",
+ is_dir: false,
+ content: "This is the content of File 2.1",
+ },
+ {
+ name: "File 2.2",
+ path: "/Dir 2/File 2.2",
+ is_dir: false,
+ content: "This is the content of File 2.2",
+ },
+ ],
+ },
+ ],
+ },
+ "/Dir 1": {
+ name: "Dir 1",
+ path: "/Dir 1",
+ is_dir: true,
+ content: "",
+ childs: [
+ {
+ name: "File 1.1",
+ path: "/Dir 1/File 1.1",
+ is_dir: false,
+ content: "This is the content of File 1.1",
+ },
+ {
+ name: "File 1.2",
+ path: "/Dir 1/File 1.2",
+ is_dir: false,
+ content: "This is the content of File 1.2",
+ },
+ ],
+ },
+ "/Dir 2": {
+ name: "Dir 2",
+ path: "/Dir 2",
+ is_dir: true,
+ content: "",
+ childs: [
+ {
+ name: "File 2.1",
+ path: "/Dir 2/File 2.1",
+ is_dir: false,
+ content: "This is the content of File 2.1",
+ },
+ {
+ name: "File 2.2",
+ path: "/Dir 2/File 2.2",
+ is_dir: false,
+ content: "This is the content of File 2.2",
+ },
+ ],
+ }
+}
+
+async function main() {
+ let opts = {
+ id: "vfs",
+ Open: Open,
+ OpenNode: OpenNode,
+ }
+
+ let wui_vfs = new WuiVfs(opts)
+ wui_vfs.OpenDir("/")
+}
+
+async function Open(path: string, is_dir: boolean): Promise<WuiResponseInterface> {
+ console.log("Open:", path, is_dir)
+ let res: WuiResponseInterface = {
+ code: 200,
+ message: "",
+ }
+
+ if (is_dir) {
+ res.data = dummyfs[path]
+ return res
+ }
+
+ res.data = {
+ name: "",
+ path: path,
+ content: "",
+ }
+
+ switch (path) {
+ case "/Dir 1/File 1.1":
+ res.data.name = "File 1.1"
+ res.data.content = "This is the content of " + res.data.name
+ break
+ case "/Dir 1/File 1.2":
+ res.data.name = "File 1.2"
+ res.data.content = "This is the content of " + res.data.name
+ break
+ case "/Dir 2/File 2.1":
+ res.data.name = "File 2.1"
+ res.data.content = "This is the content of " + res.data.name
+ break
+ case "/Dir 2/File 2.2":
+ res.data.name = "File 2.1"
+ res.data.content = "This is the content of " + res.data.name
+ break
+ default:
+ res.code = 404
+ res.message = "path not found"
+ }
+
+ console.log("Open:", res)
+
+ return res
+}
+
+async function OpenNode(node: WuiVfsNodeInterface): Promise<WuiResponseInterface> {
+ return await Open(node.path, node.is_dir)
+}
+
+main()
diff --git a/vfs/index.html b/vfs/index.html
new file mode 100644
index 0000000..10a69f2
--- /dev/null
+++ b/vfs/index.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta charset="UTF-8" />
+ <title>WUI - Virtual File System (vfs)</title>
+ </head>
+ <body>
+ <div id="vfs"></div>
+ <script type="module" src="/vfs/example.js"></script>
+ </body>
+</html>
diff --git a/vfs/vfs.d.ts b/vfs/vfs.d.ts
index 177fcfd..5e89b33 100644
--- a/vfs/vfs.d.ts
+++ b/vfs/vfs.d.ts
@@ -3,11 +3,11 @@ export interface WuiVfsNodeInterface {
name: string;
path: string;
is_dir: boolean;
- content_type: string;
- mod_time: number;
- size: number;
- mode: string;
- childs: WuiVfsNodeInterface[];
+ content_type?: string;
+ mod_time?: number;
+ size?: number;
+ mode?: string;
+ childs?: WuiVfsNodeInterface[];
content: string;
}
export interface WuiVfsOptions {
diff --git a/vfs/vfs.ts b/vfs/vfs.ts
index b1b0881..c6a5a8d 100644
--- a/vfs/vfs.ts
+++ b/vfs/vfs.ts
@@ -8,11 +8,11 @@ export interface WuiVfsNodeInterface {
name: string
path: string
is_dir: boolean
- content_type: string
- mod_time: number
- size: number
- mode: string
- childs: WuiVfsNodeInterface[]
+ content_type?: string
+ mod_time?: number
+ size?: number
+ mode?: string
+ childs?: WuiVfsNodeInterface[]
content: string // If its not empty, it MUST be encoded in base64.
}