From 6991257cd67631efae78e141b8ae7794129483c1 Mon Sep 17 00:00:00 2001 From: Matthias Andreas Benkard Date: Thu, 13 Feb 2020 04:41:32 +0100 Subject: Bookmark submission: Make bookmark submission form a reusable WebComponent. Change-Id: I723d98208040db5b0d2d56d8c5e727c68399f843 --- .../bookmarks/MlkBookmarkSubmissionForm.js | 97 ++++++++++++++++++++++ .../META-INF/resources/bookmarks/bookmarkList.js | 25 +----- .../templates/benki/bookmarks/bookmarkList.html | 38 +-------- 3 files changed, 102 insertions(+), 58 deletions(-) create mode 100644 src/main/resources/META-INF/resources/bookmarks/MlkBookmarkSubmissionForm.js (limited to 'src') diff --git a/src/main/resources/META-INF/resources/bookmarks/MlkBookmarkSubmissionForm.js b/src/main/resources/META-INF/resources/bookmarks/MlkBookmarkSubmissionForm.js new file mode 100644 index 0000000..25de54b --- /dev/null +++ b/src/main/resources/META-INF/resources/bookmarks/MlkBookmarkSubmissionForm.js @@ -0,0 +1,97 @@ +import {html, render} from "../../web_modules/lit-html.js"; +import ProgressSpinner from "../web_modules/elix/define/ProgressSpinner.js"; + +export class MlkBookmarkSubmissionForm extends HTMLElement { + constructor() { + super(); + this.attachShadow({mode: "open"}); + } + + static get observedAttributes() { + return ["greetee"]; + } + + connectedCallback () { + this.render(); + } + + attributeChangedCallback(name, oldValue, newValue) { + this.render(); + } + + focus() { + let uriInput = this.shadowRoot.getElementById('uri-input'); + uriInput.focus(); + } + + async onUriBlur() { + let titleInput = this.shadowRoot.getElementById('title-input'); + let uriInput = this.shadowRoot.getElementById('uri-input'); + let uriSpinner = this.shadowRoot.getElementById('uri-spinner'); + + if (!uriInput.value) { + return; + } + + uriSpinner.hidden = false; + uriSpinner.playing = true; + let searchParams = new URLSearchParams({'uri': uriInput.value}); + console.log(`/bookmarks/page-info?${searchParams}`); + let fetchUrl = new URL(`/bookmarks/page-info?${searchParams}`, document.URL); + let r = await fetch(fetchUrl); + uriSpinner.hidden = true; + uriSpinner.playing = false; + + if (!r.ok) { + return; + } + + let pageInfo = await r.json(); + titleInput.value = pageInfo.title; + } + + render() { + const template = html` + + +
+
+ New Bookmark + +
+ + + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+
`; + + render(template, this.shadowRoot); + } +} + +customElements.define("mlk-bookmark-submission-form", MlkBookmarkSubmissionForm); diff --git a/src/main/resources/META-INF/resources/bookmarks/bookmarkList.js b/src/main/resources/META-INF/resources/bookmarks/bookmarkList.js index 4c8287a..a6f78e6 100644 --- a/src/main/resources/META-INF/resources/bookmarks/bookmarkList.js +++ b/src/main/resources/META-INF/resources/bookmarks/bookmarkList.js @@ -1,28 +1,9 @@ document.addEventListener('DOMContentLoaded', () => { let bookmarkSubmissionPane = document.getElementById( 'bookmark-submission-pane'); - let titleInput = document.getElementById('title-input'); - let uriInput = document.getElementById('uri-input'); - let uriSpinner = document.getElementById('uri-spinner'); + let bookmarkSubmissionForm = document.getElementById( + 'bookmark-submission-form'); - bookmarkSubmissionPane.addEventListener('opened', () => uriInput.focus()); - - uriInput.addEventListener('blur', async () => { - uriSpinner.hidden = false; - uriSpinner.playing = true; - let searchParams = new URLSearchParams({'uri': uriInput.value}); - console.log(`/bookmarks/page-info?${searchParams}`); - let fetchUrl = new URL(`/bookmarks/page-info?${searchParams}`, document.URL); - let r = await fetch(fetchUrl); - uriSpinner.hidden = true; - uriSpinner.playing = false; - - if (!r.ok) { - return; - } - - let pageInfo = await r.json(); - titleInput.value = pageInfo.title; - }); + bookmarkSubmissionPane.addEventListener('opened', () => bookmarkSubmissionForm.focus()); }); diff --git a/src/main/resources/templates/benki/bookmarks/bookmarkList.html b/src/main/resources/templates/benki/bookmarks/bookmarkList.html index 2c35249..566bfb2 100644 --- a/src/main/resources/templates/benki/bookmarks/bookmarkList.html +++ b/src/main/resources/templates/benki/bookmarks/bookmarkList.html @@ -13,47 +13,13 @@ {! #if authenticated !} - - +

-
-
- New Bookmark - -
- - - -
- -
- - -
- -
- - -
- -
- - -
- -
- -
-
-
+
{! /if !} -- cgit v1.2.3