From 627e3219d11a7689e96683a2dac605cd5d0cf4b4 Mon Sep 17 00:00:00 2001 From: James Lyne Date: Thu, 23 Jun 2022 21:27:50 +0100 Subject: [PATCH] Migrate LoginControl to vue --- src/components/MapUI.vue | 2 +- src/components/map/control/LoginControl.vue | 66 ++++++++---- src/leaflet/control/LoginControl.ts | 109 -------------------- 3 files changed, 47 insertions(+), 130 deletions(-) delete mode 100644 src/leaflet/control/LoginControl.ts diff --git a/src/components/MapUI.vue b/src/components/MapUI.vue index 525be58..bebeeef 100644 --- a/src/components/MapUI.vue +++ b/src/components/MapUI.vue @@ -26,7 +26,7 @@
- +
diff --git a/src/components/map/control/LoginControl.vue b/src/components/map/control/LoginControl.vue index 7965a69..3d1578b 100644 --- a/src/components/map/control/LoginControl.vue +++ b/src/components/map/control/LoginControl.vue @@ -14,30 +14,56 @@ - limitations under the License. --> + + diff --git a/src/leaflet/control/LoginControl.ts b/src/leaflet/control/LoginControl.ts deleted file mode 100644 index 6ce6c07..0000000 --- a/src/leaflet/control/LoginControl.ts +++ /dev/null @@ -1,109 +0,0 @@ -/* - * Copyright 2022 James Lyne - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import {Control, ControlOptions, DomEvent, DomUtil} from 'leaflet'; -import {useStore} from "@/store"; -import {watch} from "@vue/runtime-core"; - -import "@/assets/icons/login.svg"; -import "@/assets/icons/logout.svg"; -import {computed} from "vue"; -import {ActionTypes} from "@/store/action-types"; -import {notify} from "@kyvg/vue3-notification"; -import LiveAtlasLeafletMap from "@/leaflet/LiveAtlasLeafletMap"; - -/** - * Leaflet map control providing a login/logout button which opens the login modal/logs out on click - */ -export class LoginControl extends Control { - declare _map: LiveAtlasLeafletMap; - declare options: ControlOptions; - - private readonly store = useStore(); - private readonly loggedIn = computed(() => this.store.state.loggedIn); - private readonly _button: HTMLButtonElement; - - constructor(options: ControlOptions) { - super(options); - - this._button = DomUtil.create('button', - 'leaflet-control-bottom leaflet-control-button leaflet-control-login') as HTMLButtonElement; - - this._button.type = 'button'; - - this._button.addEventListener('click', async e => { - e.stopPropagation(); - e.preventDefault(); - - await this.handleClick(); - }); - - //Open login on ArrowRight from button - DomEvent.on(this._button,'keydown', async (e: Event) => { - if ((e as KeyboardEvent).key === 'ArrowRight') { - e.stopPropagation(); - e.preventDefault(); - - await this.handleClick(); - } - }); - - watch(this.loggedIn, () => { - this.update(); - }); - - const visibleModal = computed(() => this.store.state.ui.visibleModal); - - watch(visibleModal, (newValue, oldValue) => { - this._button.setAttribute('aria-expanded', (newValue === 'login').toString()); - - if(this._map && !newValue && oldValue === 'login') { - this._button.focus(); - } - }); - - this.update(); - } - - onAdd() { - return this._button; - } - - private update() { - this._button.title = this.loggedIn.value - ? this.store.state.messages.logoutTitle : this.store.state.messages.loginTitle; - this._button.innerHTML = ` - - - `; - } - - private async handleClick() { - const logoutSuccess = computed(() => this.store.state.messages.logoutSuccess), - logoutError = computed(() => this.store.state.messages.logoutErrorUnknown); - - if (this.loggedIn.value) { - try { - await this.store.dispatch(ActionTypes.LOGOUT, undefined); - notify(logoutSuccess.value); - } catch(e) { - notify(logoutError.value); - } - } else { - await this.store.dispatch(ActionTypes.LOGIN, null) - } - } -}