Add tile loading indicator

This commit is contained in:
James Lyne 2020-12-15 01:51:19 +00:00
parent 4b389d26b1
commit 37c1766e6f
5 changed files with 319 additions and 2 deletions

View File

@ -0,0 +1,29 @@
<!-- By Sam Herbert (@sherb), for everyone. More @ http://goo.gl/7AJzbL -->
<!--
The MIT License (MIT)
Copyright (c) 2014 Sam Herbert
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<svg width="38" height="38" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg" stroke="#fff">
<g fill="none" fill-rule="evenodd">
<g transform="translate(1 1)" stroke-width="2">
<circle stroke-opacity=".5" cx="18" cy="18" r="18"/>
<path d="M36 18c0-9.94-8.06-18-18-18">
<animateTransform
attributeName="transform"
type="rotate"
from="0 18 18"
to="360 18 18"
dur="1s"
repeatCount="indefinite"/>
</path>
</g>
</g>
</svg>

View File

@ -26,6 +26,7 @@ import {MutationTypes} from "@/store/mutation-types";
import {Coordinate, DynmapPlayer} from "@/dynmap"; import {Coordinate, DynmapPlayer} from "@/dynmap";
import {ActionTypes} from "@/store/action-types"; import {ActionTypes} from "@/store/action-types";
import DynmapMap from "@/leaflet/DynmapMap"; import DynmapMap from "@/leaflet/DynmapMap";
import {LoadingControl} from "@/leaflet/control/LoadingControl";
export default defineComponent({ export default defineComponent({
components: { components: {
@ -145,6 +146,11 @@ export default defineComponent({
// markerZoomAnimation: false, // markerZoomAnimation: false,
})); }));
this.leaflet.addControl(new LoadingControl({
position: 'topleft',
delayIndicator: 500,
}));
this.leaflet.on('moveend', () => { this.leaflet.on('moveend', () => {
useStore().commit(MutationTypes.SET_CURRENT_LOCATION, this.currentProjection.latLngToLocation(this.leaflet!.getCenter(), 64)); useStore().commit(MutationTypes.SET_CURRENT_LOCATION, this.currentProjection.latLngToLocation(this.leaflet!.getCenter(), 64));
}); });

View File

@ -0,0 +1,244 @@
/*
Portions of this file are taken from Leaflet.loading:
Copyright (c) 2013 Eric Brelsford
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import {
Control,
ControlOptions,
DomUtil,
Layer,
LeafletEvent,
Map, TileLayer,
} from 'leaflet';
import loadingIcon from '@/assets/icons/loading.svg';
export interface LoadingControlOptions extends ControlOptions {
delayIndicator?: number;
}
export class LoadingControl extends Control {
// @ts-ignore
options: LoadingControlOptions;
_dataLoaders: Set<number> = new Set();
_loadingIndicator: HTMLDivElement;
_delayIndicatorTimeout?: number;
constructor(options: LoadingControlOptions) {
super(options);
this._loadingIndicator = DomUtil.create('div', 'leaflet-control-loading') as HTMLDivElement;
}
onAdd(map: Map) {
this._loadingIndicator.title = 'Loading...';
this._loadingIndicator.hidden = true;
this._loadingIndicator.innerHTML = `
<svg class="svg-icon" viewBox="${loadingIcon.viewBox}">
<use xlink:href="${loadingIcon.url}" />
</svg>`;
this._addLayerListeners(map);
this._addMapListeners(map);
return this._loadingIndicator;
}
onRemove(map: Map) {
this._removeLayerListeners(map);
this._removeMapListeners(map);
}
addLoader(id: number) {
this._dataLoaders.add(id);
if (this.options.delayIndicator && !this._delayIndicatorTimeout) {
// If we are delaying showing the indicator and we're not
// already waiting for that delay, set up a timeout.
this._delayIndicatorTimeout = setTimeout(() => {
this.updateIndicator();
this._delayIndicatorTimeout = undefined;
}, this.options.delayIndicator);
} else {
// Otherwise show the indicator immediately
this.updateIndicator();
}
}
removeLoader(id: number) {
this._dataLoaders.delete(id);
this.updateIndicator();
// If removing this loader means we're in no danger of loading,
// clear the timeout. This prevents old delays from instantly
// triggering the indicator.
if (this.options.delayIndicator && this._delayIndicatorTimeout && !this.isLoading()) {
clearTimeout(this._delayIndicatorTimeout);
this._delayIndicatorTimeout = undefined;
}
}
updateIndicator() {
if (this.isLoading()) {
this._showIndicator();
}
else {
this._hideIndicator();
}
}
isLoading() {
return this._countLoaders() > 0;
}
_countLoaders() {
return this._dataLoaders.size;
}
_showIndicator() {
this._loadingIndicator.hidden = false;
}
_hideIndicator() {
this._loadingIndicator.hidden = true;
}
_handleLoading(e: LeafletEvent) {
this.addLoader(this.getEventId(e));
}
_handleBaseLayerChange (e: LeafletEvent) {
// Check for a target 'layer' that contains multiple layers, such as
// L.LayerGroup. This will happen if you have an L.LayerGroup in an
// L.Control.Layers.
if (e.layer && e.layer.eachLayer && typeof e.layer.eachLayer === 'function') {
e.layer.eachLayer((layer: Layer) => {
this._handleBaseLayerChange({ layer: layer } as LeafletEvent);
});
}
}
_handleLoad(e: LeafletEvent) {
this.removeLoader(this.getEventId(e));
}
getEventId(e: any) {
if (e.id) {
return e.id;
} else if (e.layer) {
return e.layer._leaflet_id;
}
return e.target._leaflet_id;
}
_layerAdd(e: LeafletEvent) {
if(!(e.layer instanceof TileLayer)) {
return;
}
try {
if(e.layer.isLoading()) {
this.addLoader((e.layer as any)._leaflet_id);
}
e.layer.on('loading', this._handleLoading, this);
e.layer.on('load', this._handleLoad, this);
} catch (exception) {
console.warn('L.Control.Loading: Tried and failed to add ' +
' event handlers to layer', e.layer);
console.warn('L.Control.Loading: Full details', exception);
}
}
_layerRemove(e: LeafletEvent) {
if(!(e.layer instanceof TileLayer)) {
return;
}
try {
e.layer.off('loading', this._handleLoading, this);
e.layer.off('load', this._handleLoad, this);
} catch (exception) {
console.warn('L.Control.Loading: Tried and failed to remove ' +
'event handlers from layer', e.layer);
console.warn('L.Control.Loading: Full details', exception);
}
}
_addLayerListeners(map: Map) {
// Add listeners for begin and end of load to any layers already
// on the map
map.eachLayer((layer: Layer) => {
if(!(layer instanceof TileLayer)) {
return;
}
if(layer.isLoading()) {
this.addLoader((layer as any)._leaflet_id);
}
layer.on('loading', this._handleLoading, this);
layer.on('load', this._handleLoad, this);
});
// When a layer is added to the map, add listeners for begin and
// end of load
map.on('layeradd', this._layerAdd, this);
map.on('layerremove', this._layerRemove, this);
}
_removeLayerListeners(map: Map) {
// Remove listeners for begin and end of load from all layers
map.eachLayer((layer: Layer) => {
if(!(layer instanceof TileLayer)) {
return;
}
this.removeLoader((layer as any)._leaflet_id);
layer.off('loading', this._handleLoading, this);
layer.off('load', this._handleLoad, this);
});
// Remove layeradd/layerremove listener from map
map.off('layeradd', this._layerAdd, this);
map.off('layerremove', this._layerRemove, this);
}
_addMapListeners(map: Map) {
// Add listeners to the map for (custom) dataloading and dataload
// events, eg, for AJAX calls that affect the map but will not be
// reflected in the above layer events.
map.on('baselayerchange', this._handleBaseLayerChange, this);
map.on('dataloading', this._handleLoading, this);
map.on('dataload', this._handleLoad, this);
map.on('layerremove', this._handleLoad, this);
}
_removeMapListeners(map: Map) {
map.off('baselayerchange', this._handleBaseLayerChange, this);
map.off('dataloading', this._handleLoading, this);
map.off('dataload', this._handleLoad, this);
map.off('layerremove', this._handleLoad, this);
}
}

View File

@ -52,11 +52,11 @@
} }
} }
.leaflet-control-link { .leaflet-control-link,
.leaflet-control-loading {
@extend %button; @extend %button;
width: 5rem; width: 5rem;
height: 5rem; height: 5rem;
background-size: 3.2rem 3.2rem;
} }
.leaflet-control-coordinates { .leaflet-control-coordinates {
@ -173,3 +173,21 @@
width: 5rem; width: 5rem;
height: 5rem; height: 5rem;
} }
.leaflet-control-loading {
cursor: wait;
animation: fade 0.3s linear;
animation-fill-mode: forwards;
&:focus, &:hover, &:active {
background-color: $global-background;
}
&[hidden] {
display: none;
}
svg {
animation: spin 1s linear infinite;
}
}

View File

@ -8,6 +8,26 @@
@import "leaflet/controls"; @import "leaflet/controls";
@import "leaflet/popups"; @import "leaflet/popups";
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
@keyframes fade {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@font-face { @font-face {
font-family: 'Raleway'; font-family: 'Raleway';
font-style: normal; font-style: normal;