annotate web/src/main.ts @ 6:a4659594f2e5

add web page
author drewp@bigasterisk.com
date Sat, 06 Jul 2024 16:44:47 -0700
parents
children f23b21bd0fce
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
1 import { SlTextarea, setBasePath } from "@shoelace-style/shoelace";
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
2 import "@shoelace-style/shoelace/dist/themes/light.css";
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
3 import { LitElement, PropertyValueMap, css, html } from "lit";
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
4 import { customElement, queryAsync, state } from "lit/decorators.js";
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
5 import { unsafeHTML } from "lit/directives/unsafe-html.js";
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
6 setBasePath("@fs/opt/node_modules/@shoelace-style/shoelace/dist");
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
7
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
8 @customElement("sco-search-page")
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
9 export class ScoSearchPage extends LitElement {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
10 static styles = [
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
11 css`
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
12 :host {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
13 display: flex;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
14 flex-direction: column;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
15 height: 100vh;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
16 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
17 form {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
18 display: flex;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
19 flex-direction: row;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
20 align-items: flex-end;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
21
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
22 margin: 0 10% 0 100px;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
23 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
24 sl-textarea {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
25 width: 100%;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
26 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
27 :host > * {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
28 padding: 5px;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
29 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
30 section#results {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
31 background-color: #ffffffde;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
32 flex-grow: 1;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
33 display: flex;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
34 flex-direction: column;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
35 overflow: hidden;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
36 margin-bottom: 15px;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
37 margin-left: 110px;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
38 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
39 img.bot {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
40 width: 237px;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
41 position: absolute;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
42 left: -33px;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
43 top: -22px;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
44 z-index: -1;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
45 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
46 `,
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
47 ];
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
48 @state() query: string = "climate";
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
49 @state() results: Object[] = [];
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
50 @state() queryError: string = "";
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
51 @queryAsync("sl-textarea") queryEl?: Promise<SlTextarea>;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
52 protected async firstUpdated(_changedProperties: PropertyValueMap<any> | Map<PropertyKey, unknown>) {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
53 super.firstUpdated(_changedProperties);
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
54
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
55 await this.pressingEnterSubmitsForm();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
56
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
57 this.queryParamToProperty();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
58
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
59 window.addEventListener("popstate", (event) => {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
60 this.queryParamToProperty();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
61 });
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
62
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
63 this.submit().catch(console.error);
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
64 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
65
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
66 private async pressingEnterSubmitsForm() {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
67 const ta = (await this.queryEl)?.shadowRoot?.querySelector("textarea");
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
68 ta?.addEventListener("keydown", (event: KeyboardEvent) => {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
69 if (event.code == "Enter" && !event.shiftKey) {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
70 event.preventDefault();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
71 this.submit();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
72 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
73 });
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
74 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
75
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
76 private queryParamToProperty() {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
77 const urlParams = new URLSearchParams(window.location.search);
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
78 const qParam = urlParams.get("q");
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
79
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
80 if (qParam) {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
81 this.query = qParam;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
82 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
83 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
84
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
85 private async propertyToQueryParam() {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
86 const currentUrl = new URL(window.location.href);
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
87
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
88 const q = await this.getCurrentQuery();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
89 if (currentUrl.searchParams.get("q") !== q) {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
90 currentUrl.searchParams.set("q", q);
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
91
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
92 const newUrl = currentUrl.toString();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
93
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
94 history.pushState({}, document.title, newUrl);
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
95 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
96 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
97
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
98 render() {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
99 return html`
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
100 <section id="query">
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
101 <img class="bot" src="sco-bot.jpg" />
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
102 <form>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
103 <sl-textarea .value=${this.query} rows="1" resize="auto" label="Query" enterkeyhint="search" autocapitalize="off" autofocus="true"></sl-textarea>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
104 <sl-button variant="primary" @click=${this.submit}>Submit</sl-button>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
105 <div>${this.queryError}</div>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
106 </form>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
107 </section>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
108 <section id="results">
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
109 ${this.results.map(
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
110 (r) =>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
111 html`<div>${r.title}</div>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
112 <div>${unsafeHTML(r.snippetHtml)}</div>`
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
113 )}
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
114 <div>Matching results: ${this.results.length}</div>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
115 </section>
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
116 `;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
117 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
118
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
119 async getCurrentQuery(): Promise<string> {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
120 return (await this.queryEl)!.value || "";
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
121 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
122
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
123 async submit() {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
124 await this.propertyToQueryParam();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
125
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
126 this.results = [];
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
127
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
128 const sentQ = await this.getCurrentQuery();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
129
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
130 // const resp = await fetch("query", {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
131 // method: "POST",
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
132 // body: "query=" + encodeURIComponent(sentQ),
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
133 // headers: { "Content-Type": "application/x-www-form-urlencoded" },
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
134 // });
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
135 // if (sentQ != (await this.getCurrentQuery())) {
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
136 // // old result- ignore
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
137 // return;
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
138 // }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
139 this.results.push({ title: "doc1", snippetHtml: "<h1>hello</h1>" });
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
140 console.log("ScoSearchPage ~ submit ~ this.results:", this.results);
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
141 this.requestUpdate();
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
142 }
a4659594f2e5 add web page
drewp@bigasterisk.com
parents:
diff changeset
143 }