-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
264 lines (237 loc) · 12 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE html>
<html>
<head>
<title>yeet</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
</head>
<body>
<main class="container">
<h2 class="mt-3 mb-3">Go!</h2>
<div class="row mb-3">
<go-link-form action="/api/golink" source="{{.Current}}"></go-link-form>
</div>
<hr>
<ul class="list-group">
{{range .Links}}
<li class="list-group-item d-flex go-link-item">
<span class="flex-fill">{{.Source}} <i class="bi bi-arrow-right"></i> {{.Destination}} ({{.Total}} views)</span>
<go-link-edit class="me-2" link-id="{{.Source}}" source="{{.Source}}" destination="{{.Destination}}" description="{{.Description.String}}"></go-link-edit>
<form is="go-form" action="/api/golink{{.Source}}" method="delete">
<button class="btn btn-sm btn-outline-danger">
<i class="bi bi-trash3"></i>
</button>
</form>
</li>
{{else}}
<li class="list-group-item">no links</li>
{{end}}
</ul>
</main>
<template id="go-form-template">
<slot></slot>
</template>
<template id="go-link-form-template">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<form is="go-form" action="/api/golink" method="post">
<div class="row">
<div class="col">
<div class="row mb-3">
<div class="col d-flex align-items-center">
<div class="form-floating flex-fill">
<input name="source" type="text" class="form-control" placeholder="/something"/>
<label>From</label>
</div>
<i class="bi bi-arrow-right ms-4 d-none d-sm-block"></i>
</div>
<div class="col">
<div class="form-floating">
<input name="destination" type="text" placeholder="https://somewhere.cool" class="form-control"/>
<label>To</label>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-floating col">
<textarea name="description" placeholder="What's cool about this link?" class="form-control"></textarea>
<label>Description</label>
</div>
</div>
<div class="col d-flex align-items-center d-block d-sm-none">
<button class="btn btn-primary">Go!</button>
</div>
</div>
</div>
<div class="col-2 d-flex align-items-center d-none d-sm-block">
<button class="btn btn-primary">Go!</button>
</div>
</div>
</form>
</template>
<template id="go-link-edit-template">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">
<style>
dialog {
border: var(--bs-border-width) solid var(--bs-border-color);
border-radius: var(--bs-border-radius-lg);
}
</style>
<button type="button" class="open-dialog btn btn-sm btn-outline-primary">Edit</button>
<dialog>
<div class="d-flex align-items-center">
<h5 class="flex-fill">Edit link</h5>
<button class="btn btn-ghost close-dialog" type="button">
<i class="bi bi-x-lg"></i>
</button>
</div>
<hr>
<go-link-form method="put"></go-link-form>
</dialog>
</template>
<script>
const adaptedForms = new WeakSet();
const adaptForms = (elementRoot = document) => {
let nodes = [...elementRoot.querySelectorAll("form[data-method]")];
if (elementRoot.tagName === "FORM") {
nodes.push(elementRoot);
}
nodes.forEach(form => {
if (!["delete", "put"].includes(form.dataset?.method?.toLowerCase())) {
console.error(`cannot adapt method: ${form.dataset?.method}`);
return;
}
if (adaptedForms.has(form)) {
return;
}
adaptedForms.add(form);
form.addEventListener("submit", event => {
event.preventDefault();
event.stopImmediatePropagation();
event.stopPropagation();
console.log(form.action, form.dataset.method);
fetch(form.action, {
method: form.dataset.method,
body: new FormData(form)
}).catch(reason => {
console.error(reason);
}).then(res => {
if (res.status === 200) {
if (res.headers.has("Redirect")) {
window.location.pathname = res.headers.get("Redirect");
}
}
});
});
});
};
adaptForms();
class GoForm extends HTMLFormElement {
static get observedAttributes() {
return ["method"];
}
set method(value) {
if (["delete", "put"].includes(value?.toLowerCase())) {
this.setAttribute("data-method", value);
this.removeAttribute("method");
adaptForms(this);
}
}
get method() {
return this.getAttribute("method") ?? this.getAttribute("data-method");
}
connectedCallback() {
this.method = this.getAttribute("method");
}
attributeChangedCallback(name, oldValue, newValue) {
this[name] = newValue;
}
}
customElements.define("go-form", GoForm, { extends: 'form' });
class GoLinkForm extends HTMLElement {
static get observedAttributes() {
return ["source", "destination", "description", "action", "method"];
}
constructor() {
super();
this.attachShadow({mode: "open"});
const template = document.querySelector("#go-link-form-template");
this.shadowRoot.append(template.content.cloneNode(true));
}
set source(value) {
this.shadowRoot.querySelector("[name='source']").value = value;
}
get source() {
return this.shadowRoot.querySelector("[name='source']").value;
}
set destination(value) {
this.shadowRoot.querySelector("[name='destination']").value = value;
}
get destination() {
this.shadowRoot.querySelector("[name='destination']").value;
}
set description(value) {
if (!value) {
return;
}
this.shadowRoot.querySelector("[name='description']").value = value;
}
get description() {
this.shadowRoot.querySelector("[name='description']").value;
}
set action(value) {
this.shadowRoot.querySelector("[is='go-form']").action = this.getAttribute("action");
}
get action() {
this.shadowRoot.querySelector("[is='go-form']").action;
}
set method(value) {
this.shadowRoot.querySelector("[is='go-form']").method = value;
}
get method() {
return this.shadowRoot.querySelector("[is='go-form']").method;
}
connectedCallback() {
this.source = this.getAttribute("source");
this.destination = this.getAttribute("destination");
this.description = this.getAttribute("description");
this.action = this.getAttribute("action");
this.method = this.getAttribute("method") ?? "post";
}
attributeChangedCallback(name, oldValue, newValue) {
this[name] = newValue;
}
}
customElements.define("go-link-form", GoLinkForm);
class GoLinkEdit extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: "open"});
const template = document.querySelector("#go-link-edit-template");
this.shadowRoot.append(template.content.cloneNode(true));
}
connectedCallback() {
const goLinkForm = this.shadowRoot.querySelector("go-link-form");
const openDialogButton = this.shadowRoot.querySelector(".open-dialog");
const dialog = this.shadowRoot.querySelector("dialog");
const closeDialogButton = this.shadowRoot.querySelector(".close-dialog");
goLinkForm.setAttribute("source", this.getAttribute("source"));
goLinkForm.setAttribute("destination", this.getAttribute("destination"));
goLinkForm.setAttribute("description", this.getAttribute("description") ?? "");
goLinkForm.setAttribute("action", `/api/golink${this.getAttribute("link-id")}`);
openDialogButton.addEventListener("click", () => {
dialog.showModal();
});
closeDialogButton.addEventListener("click", () => {
dialog.close();
});
}
}
customElements.define("go-link-edit", GoLinkEdit);
</script>
</body>
</html>