Skip to content

Commit

Permalink
Merge pull request GEOLYTIX#1098 from dbauszus-glx/mapview-attribution
Browse files Browse the repository at this point in the history
Mapview attribution
  • Loading branch information
RobAndrewHurst authored Jan 30, 2024
2 parents 985d641 + edd9e23 commit 594f579
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 53 deletions.
34 changes: 4 additions & 30 deletions lib/mapview/_mapview.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import attribution from './attribution.mjs'

import addLayer from './addLayer.mjs'

import fitView from './fitView.mjs'
Expand Down Expand Up @@ -185,36 +187,8 @@ export default (mapview) => {
units: mapview.locale.ScaleLine === 'imperial' ? 'imperial' : 'metric',
}))

// Attribution
if (mapview.attribution) {

if (typeof mapview.attribution !== 'object') {
mapview.attribution = {}
}

mapview.attribution.target = mapview.attribution.target
|| mapview.Map.getTargetElement()
.appendChild(mapp.utils.html.node`
<div class="attribution-links">`)

mapview.attribution.check = () => {

const o = Object.assign({}, mapview.attribution.links || {})

// Iterate through layers to check whether attribution should be displayed.
Object.values(mapview.layers).forEach(layer => {
layer.display && layer.attribution && Object.assign(
o, layer.attribution)
})

// Render the layer attribution links into the placeholder.
mapp.utils.render(mapview.attribution.target,
mapp.utils.html`${Object.entries(o)
.map(entry => mapp.utils.html`
<a target="_blank" href="${entry[1]}">${entry[0]}`)}`)

}
}
// Create mapview attribution.
attribution(mapview)

// Events

Expand Down
38 changes: 38 additions & 0 deletions lib/mapview/attribution.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export default function attribution(mapview) {

if (typeof mapview.attribution !== 'object') return;

if (!mapview.attribution.target) {

mapview.attribution.node = mapview.target.appendChild(
mapp.utils.html.node`<div class="map-attribution">
${mapview.attribution.logo}`)

mapview.attribution.target = mapview.attribution.node
.appendChild(mapp.utils.html.node`<div class="attribution-links">`)
}

mapview.attribution.links ??= {}

mapview.attribution.check = () => {

// Create clone of mapview.attribution.links which are always displayed.
const links = structuredClone(mapview.attribution.links)

// Assign layer.attribution for visible to layers to links.
Object.values(mapview.layers)
.filter(layer => !!layer.display)
.filter(layer => !!layer.attribution)
.forEach(layer => Object.assign(links, layer.attribution))

const elements = Object.entries(links)
.map(entry => mapp.utils.html`
<a target="_blank" href="${entry[1]}">${entry[0]}`)

// Render all links into mapview.attribution.target.
mapp.utils.render(
mapview.attribution.target,
mapp.utils.html.node`${elements}`)

}
}
12 changes: 12 additions & 0 deletions public/css/_mapp.css
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ a > img {
height: 100%;
}

.map-attribution {
position: absolute;
display: flex;
justify-content: space-between;
align-items: end;
bottom: 0;
right: 0;
width: 100%;
padding: 5px;
pointer-events: none;
}

.attribution-links {
z-index: 999;
text-align: right;
Expand Down
11 changes: 11 additions & 0 deletions public/css/mapp.css
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ body {
a > img {
height: 100%;
}
.map-attribution {
position: absolute;
display: flex;
justify-content: space-between;
align-items: end;
bottom: 0;
right: 0;
width: 100%;
padding: 5px;
pointer-events: none;
}
.attribution-links {
z-index: 999;
text-align: right;
Expand Down
25 changes: 4 additions & 21 deletions public/views/_default.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,7 @@
position: fixed;
}

#Attribution {
position: absolute;
display: flex;
justify-content: space-between;
align-items: end;
bottom: 0;
right: 0;
width: 100%;
padding: 5px;
pointer-events: none;
}

#Attribution > .logo {
.map-attribution > .logo {
pointer-events: auto;
height: 1em;
}
Expand Down Expand Up @@ -155,7 +143,7 @@
position: absolute;
}

#Attribution {
.map-attribution {
padding-left: 4em;
}

Expand Down Expand Up @@ -293,14 +281,9 @@
<body style="grid-template-rows: auto 0 0;">

<div id="Map">

<div id="OL"></div>
<div id="Attribution">
<a class="logo" target="_blank" href="https://geolytix.co.uk">
<img src="https://geolytix.github.io/public/geolytix_mapp.svg">
</a>
<div class="attribution-links"></div>
</div>

</div>

<div id="mapButton" class="btn-column"></div>
Expand Down
6 changes: 4 additions & 2 deletions public/views/_default.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,14 @@ window.onload = async () => {
hooks: true,
scrollWheelZoom: true,
attribution: {
target: document.querySelector('#Attribution > .attribution-links'),
logo: mapp.utils.html.node`
<a class="logo" target="_blank" href="https://geolytix.co.uk">
<img src="https://geolytix.github.io/public/geolytix_mapp.svg">`,
links: {
[`XYZ v${mapp.version}`]: 'https://github.com/GEOLYTIX/xyz',
['SHA']: `https://github.com/GEOLYTIX/xyz/commit/${mapp.hash}`,
Openlayers: 'https://openlayers.org',
},
}
},

// mapp.Mapview must be awaited.
Expand Down

0 comments on commit 594f579

Please sign in to comment.