annotate src/ingest/IngestDrop.ts @ 24:1a9a8af1aa19

breadcrumbs and tree view UI
author drewp@bigasterisk.com
date Mon, 17 Apr 2023 13:35:18 -0700
parents ff73b95fc72f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
22
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
1 import { LitElement, html, css, TemplateResult } from "lit";
13
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
2 import { customElement, property } from "lit/decorators.js";
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
3
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
4 interface SubTree {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
5 name: string;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
6 children?: SubTree[];
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
7 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
8
13
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
9 @customElement("ingest-drop")
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
10 export class IngestDrop extends LitElement {
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
11 @property() folder: string = "/";
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
12 @property() tree: SubTree = { name: "TOP" };
13
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
13 static styles = [
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
14 css`
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
15 #drop {
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
16 min-width: 10em;
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
17 min-height: 10em;
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
18 background: gray;
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
19 border: 5px gray dashed;
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
20 margin: 1% 10%;
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
21 }
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
22 #drop.dnd-hover {
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
23 background: yellow;
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
24 }
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
25 .twocol {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
26 display: flex;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
27 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
28 .twocol > div {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
29 background: #262626;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
30 }
13
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
31 `,
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
32 ];
18
1b388ee5dd09 reformat
drewp@bigasterisk.com
parents: 17
diff changeset
33
13
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
34 render() {
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
35 const self = this;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
36 function renderTree(node: SubTree, pathToHere?: string): TemplateResult {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
37 const isTop = pathToHere === undefined;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
38 const toplessPath = isTop ? "" : pathToHere + "/" + node.name;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
39 const internalPath = toplessPath || "/";
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
40 return html`<sl-tree-item expanded ?selected=${isTop} data-path="${internalPath}" @click=${self.setFolder}>
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
41 ${node.name} ${(node.children || []).map((n: SubTree) => renderTree(n, toplessPath))}
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
42 </sl-tree-item>`;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
43 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
44
22
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
45 return html`
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
46 <div class="twocol">
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
47 <div>
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
48 <h3>Select folder</h3>
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
49 <sl-tree>${renderTree(this.tree)}</sl-tree>
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
50 </div>
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
51 <div>
22
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
52 <div id="drop" @dragover=${this.dragover} @dragleave=${this.leave} @drop=${this.drop}>
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
53 Drop video file or url here to upload to <em>TOP${this.folder}</em>
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
54 </div>
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
55 </div>
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
56 </div>
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
57 `;
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
58 }
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
59
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
60 connectedCallback(): void {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
61 super.connectedCallback();
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
62 this.loadFolderTree();
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
63 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
64
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
65 async loadFolderTree() {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
66 this.tree = await (await fetch("../api/folderTree")).json();
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
67 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
68
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
69 setFolder(ev: MouseEvent) {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
70 for (let el of ev.composedPath()) {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
71 const ds = (el as HTMLElement).dataset;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
72 if (ds) {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
73 if (ds.path) {
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
74 this.folder = ds.path;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
75 return;
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
76 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
77 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
78 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
79 console.error("didn't find data-path");
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
80 }
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
81
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
82 dragover(ev: DragEvent) {
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
83 this.shadowRoot?.querySelector("#drop")?.classList.add("dnd-hover");
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
84 ev.preventDefault();
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
85 if (ev.dataTransfer) {
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
86 ev.dataTransfer.dropEffect = "copy";
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
87 }
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
88 }
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
89
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
90 leave() {
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
91 this.shadowRoot?.querySelector("#drop")?.classList.remove("dnd-hover");
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
92 }
24
1a9a8af1aa19 breadcrumbs and tree view UI
drewp@bigasterisk.com
parents: 22
diff changeset
93
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
94 drop(ev: DragEvent) {
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
95 ev.preventDefault();
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
96 this.leave();
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
97
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
98 if (!ev.dataTransfer) {
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
99 return;
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
100 }
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
101
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
102 const url = ev.dataTransfer.getData("text/plain");
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
103 if (url) {
22
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
104 this.queueUrlToFetch(url);
17
071943adf000 dnd a file or a url which we'll queue and fetch
drewp@bigasterisk.com
parents: 15
diff changeset
105 } else {
071943adf000 dnd a file or a url which we'll queue and fetch
drewp@bigasterisk.com
parents: 15
diff changeset
106 for (let i = 0; i < ev.dataTransfer.files.length; i++) {
22
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
107 this.uploadVideoFile(ev.dataTransfer.files[i]);
17
071943adf000 dnd a file or a url which we'll queue and fetch
drewp@bigasterisk.com
parents: 15
diff changeset
108 }
15
53d99454f394 support dropping url or file
drewp@bigasterisk.com
parents: 13
diff changeset
109 }
13
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
110 }
22
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
111 queueUrlToFetch(url: string) {
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
112 fetch("../api/ingest/videoUrl?folder=" + encodeURIComponent(this.folder), {
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
113 method: "POST",
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
114 body: url,
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
115 });
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
116 }
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
117 uploadVideoFile(f: File) {
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
118 fetch("../api/ingest/videoUpload?name=" + encodeURIComponent(this.folder + "/" + f.name), {
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
119 method: "POST",
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
120 body: f.stream(),
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
121 duplex: "half",
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
122 } as any /* types don't include 'duplex' */ );
ff73b95fc72f frontend cleanups and improvements. this commit contains some future work too
drewp@bigasterisk.com
parents: 18
diff changeset
123 }
13
b73941c4dc0a more wip on ingest page; shared styles
drewp@bigasterisk.com
parents:
diff changeset
124 }