Skip to content

Commit

Permalink
feat(landing): adding debug information to map when ?debug=true (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha authored Jun 24, 2020
1 parent cd66b4c commit 0e526ce
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 54 deletions.
6 changes: 5 additions & 1 deletion packages/landing/scripts/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ const BUILD_CMD = [
* List of CSS files to bundle
* TODO it would be good to read these from the `index.html`
*/
const CSS_SOURCE = ['../../node_modules/ol/ol.css', '../../node_modules/@philals/lui/dist/lui.cjs.development.css'];
const CSS_SOURCE = [
'static/index.css',
'../../node_modules/ol/ol.css',
'../../node_modules/@philals/lui/dist/lui.cjs.development.css',
];

/**
* Display a number in KB with a few decimal places
Expand Down
12 changes: 12 additions & 0 deletions packages/landing/src/__test__/hash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,43 @@ o.spec('WindowUrl', () => {
projection: Epsg.Google,
imageId: 'aerial',
tag: 'production',
debug: false,
});
o(WindowUrl.fromUrl('?p=2193')).deepEquals({
projection: Epsg.Nztm2000,
imageId: 'aerial',
tag: 'production',
debug: false,
});
o(WindowUrl.fromUrl('?i=abc123')).deepEquals({
projection: Epsg.Google,
imageId: 'abc123',
tag: 'production',
debug: false,
});
o(WindowUrl.fromUrl('?v=beta')).deepEquals({
projection: Epsg.Google,
imageId: 'aerial',
tag: 'beta',
debug: false,
});
o(WindowUrl.fromUrl('?v=beta&i=abc123&p=2193')).deepEquals({
projection: Epsg.Nztm2000,
imageId: 'abc123',
tag: 'beta',
debug: false,
});
o(WindowUrl.fromUrl('?v=beta&i=abc123&p=2193&d=true')).deepEquals({
projection: Epsg.Nztm2000,
imageId: 'abc123',
tag: 'beta',
debug: false,
});
o(WindowUrl.fromUrl('?v=beta&i=abc123&p=2193&d=true&debug=yes')).deepEquals({
projection: Epsg.Nztm2000,
imageId: 'abc123',
tag: 'beta',
debug: true,
});
});

Expand Down
102 changes: 102 additions & 0 deletions packages/landing/src/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Basemaps } from './map';
import OSM from 'ol/source/OSM';
import TileLayer from 'ol/layer/Tile';

/**
* VDom style document.createElement
* @param name
*/
function e(name: string): HTMLElement;
function e(name: string, attrs: Record<string, unknown>, value?: string | HTMLElement | HTMLElement[]): HTMLElement;
function e(name: string, attrs?: Record<string, unknown>, value?: string | HTMLElement | HTMLElement[]): HTMLElement {
const el = document.createElement(name);
if (value == null) {
// noop
} else if (Array.isArray(value)) {
value.forEach((v) => el.appendChild(v));
} else if (typeof value == 'object') {
el.appendChild(value);
} else {
el.innerHTML = value;
}

if (attrs) {
Object.entries(attrs).forEach(([key, value]) => {
(el as any)[key] = value;
});
}
return el;
}

function kv(key: string, value: string | HTMLElement): { value: HTMLElement; container: HTMLElement } {
const valueEl: HTMLElement = typeof value == 'string' ? e('div', { className: 'debug__value' }, value) : value;
const containerEl = e('div', { className: 'debug__info' }, [
e('label', { className: 'debug__label' }, key),
valueEl,
]);
return { value: valueEl, container: containerEl };
}

export function addDebugLayer(bm: Basemaps): void {
// Hide header/footer
Array.from(document.querySelectorAll('header')).forEach((f) => (f.style.display = 'none'));
Array.from(document.querySelectorAll('footer')).forEach((f) => (f.style.display = 'none'));
bm.el.style.height = '100vh';
bm.map.updateSize();

const debugEl = e('div', { id: 'debug', className: 'debug' });
const imageEl = kv('ImageId', bm.config.imageId);
debugEl.appendChild(imageEl.container);

const projectionEl = kv('Projection', bm.config.projection.toEpsgString());
debugEl.appendChild(projectionEl.container);

const zoomEl = kv('Zoom', '');
debugEl.appendChild(zoomEl.container);

// Change the background colors
const purpleCheck = e('input', { type: 'checkbox' }) as HTMLInputElement;
const colorButton = kv('Purple', purpleCheck);
purpleCheck.onclick = (): void => {
if (purpleCheck.checked) {
document.body.style.backgroundColor = 'magenta';
} else {
document.body.style.backgroundColor = '';
}
};
debugEl.appendChild(colorButton.container);
// Add a debug OSM layer
const osmLayer = new TileLayer({ source: new OSM({}), className: 'osm' });
const osmRange = e('input', {
type: 'range',
min: 0,
max: 1,
value: 0,
step: 0.05,
className: 'osm__slider',
}) as HTMLInputElement;
const osmButton = kv('OSM', osmRange);
osmRange.oninput = (): void => {
const range = Number(osmRange.value);
console.log('Range', { range });
if (range == 0) {
bm.map.removeLayer(osmLayer);
return;
}
const hasLayer = bm.map
.getLayers()
.getArray()
.find((f) => f == osmLayer);
if (hasLayer == null) bm.map.addLayer(osmLayer);
osmLayer.setOpacity(range);
};
debugEl.appendChild(osmButton.container);

bm.map.addEventListener('postrender', () => {
const view = bm.map.getView();
const zoom = Math.floor(view.getZoom() * 1e4) / 1e4;
zoomEl.value.innerHTML = String(zoom);
});

document.body.appendChild(debugEl);
}
6 changes: 5 additions & 1 deletion packages/landing/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import './global';

import { Basemaps } from './map';
import { addDebugLayer } from './debug';

document.addEventListener('DOMContentLoaded', () => {
const mapEl = document.getElementById('map');
if (mapEl == null) throw new Error('Cannot find #map element');
window.basemaps = new Basemaps(mapEl);
const basemaps = new Basemaps(mapEl);
window.basemaps = basemaps;

if (basemaps.config.debug) addDebugLayer(basemaps);
});
5 changes: 4 additions & 1 deletion packages/landing/src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface MapOptions {
projection: Epsg;
tag: string;
imageId: string;
/** Is the debug layer enabled @default false */
debug: boolean;
}
export const enum MapOptionType {
Tile = 'tile',
Expand Down Expand Up @@ -54,8 +56,9 @@ export const WindowUrl = {
const tag = urlParams.get('v') ?? 'production';
const imageId = urlParams.get('i') ?? 'aerial';
const projection = Epsg.parse(urlParams.get('p') ?? `${Epsg.Google.code}`) ?? Epsg.Google;
const debug = urlParams.get('debug') != null;

return { tag, imageId, projection };
return { tag, imageId, projection, debug };
},

toTileUrl(opts: MapOptions, urlType: MapOptionType): string {
Expand Down
75 changes: 75 additions & 0 deletions packages/landing/static/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
body {
margin: 0;
}

html {
overflow-y: auto !important;
}

#map {
width: 100%;
height: calc(100% - 58px);
position: fixed;
}

.g-footer-wrapper {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
}

#map .ol-control {
background-color: inherit;
}

#map .ol-zoom button {
background-color: white;
color: #0f4c65;
margin: 0;
width: 28px;
height: 32px;
}

#map .ol-zoom-in {
border-radius: 4px 4px 0 0;
}

#map .ol-zoom-out {
border-radius: 0 0 4px 4px;
}

.checker-board {
background-image: linear-gradient(45deg, #00000010 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #00000010 75%),
linear-gradient(45deg, transparent 75%, #00000010 75%),
linear-gradient(45deg, #00000010 25%, transparent 25%);
background-size: 100px 100px;
background-position: 0 0, 0 0, -50px -50px, 50px 50px;
}


.debug {
font-size: 12px;
position: absolute;
top: 8px;
right: 8px;
width: 200px;
background-color: rgba(255, 255, 255, 0.87);
padding: 16px;
}

.debug__info {
display: flex;
justify-content: space-between;
margin-bottom: 4px;
}

.debug__label {
font-weight: 700;
}


.osm__slider {
width: 75%;
}
51 changes: 0 additions & 51 deletions packages/landing/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,6 @@
<head>
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<style>
body {
margin: 0;
}

html {
overflow-y: auto !important;
}

#map {
width: 100%;
height: calc(100% - 58px);
position: fixed;
}

.g-footer-wrapper {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
}

#map .ol-control {
background-color: inherit;
}

#map .ol-zoom button {
background-color: white;
color: #0f4c65;
margin: 0;
width: 28px;
height: 32px;
}

#map .ol-zoom-in {
border-radius: 4px 4px 0 0;
}

#map .ol-zoom-out {
border-radius: 0 0 4px 4px;
}

.checker-board {
background-image: linear-gradient(45deg, #00000010 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #00000010 75%),
linear-gradient(45deg, transparent 75%, #00000010 75%),
linear-gradient(45deg, #00000010 25%, transparent 25%);
background-size: 100px 100px;
background-position: 0 0, 0 0, -50px -50px, 50px 50px;
}
</style>
$CSS_FILE
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,500,600,700&display=swap" rel="stylesheet" />
<title>Basemaps | Land Information New Zealand (LINZ)</title>
Expand Down

0 comments on commit 0e526ce

Please sign in to comment.