-
-
Notifications
You must be signed in to change notification settings - Fork 730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Style Switcher for Examples #4813
Draft
ianthetechie
wants to merge
1
commit into
maplibre:main
Choose a base branch
from
ianthetechie:style-switcher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Style switcher for embedding into example pages. | ||
// Note that there are several uses of `window.parent` throughout this file. | ||
// This is because the code is executing from an example | ||
// that is embedded into the page via an iframe. | ||
// As these are served from the same origin, this is allowed by JavaScript. | ||
|
||
/** | ||
* Gets a list of nodes whose text content includes the given string. | ||
* | ||
* @param searchText The text to look for in the element text node. | ||
* @param root The root node to start traversing from. | ||
* @returns A list of DOM nodes matching the search. | ||
*/ | ||
function getNodesByTextContent(searchText, root = window.parent.document.body) { | ||
const matchingNodes = []; | ||
|
||
function traverse(node) { | ||
if (node.nodeType === Node.ELEMENT_NODE) { | ||
node.childNodes.forEach(traverse); | ||
} else if (node.nodeType === Node.TEXT_NODE) { | ||
if (node.nodeValue.includes(searchText)) { | ||
matchingNodes.push(node); | ||
} | ||
} | ||
} | ||
|
||
traverse(root); | ||
|
||
return matchingNodes.map(node => node.parentNode); // Return parent nodes of the matching text nodes | ||
} | ||
|
||
/** | ||
* Gets the current map style slug from the query string. | ||
* @returns {string} | ||
*/ | ||
function getMapStyleQueryParam() { | ||
const url = new URL(window.parent.location.href); | ||
return url.searchParams.get('mapStyle'); | ||
} | ||
|
||
/** | ||
* Sets the map style slug in the browser's query string | ||
* (ex: when the user selects a new style). | ||
* @param styleKey | ||
*/ | ||
function setMapStyleQueryParam(styleKey) { | ||
const url = new URL(window.parent.location.href); | ||
if (url.searchParams.get('mapStyle') !== styleKey) { | ||
url.searchParams.set('mapStyle', styleKey); | ||
// TODO: Observe URL changes ex: forward and back | ||
// Manipulates the window history so that the page doesn't reload. | ||
window.parent.history.pushState(null, '', url); | ||
} | ||
} | ||
|
||
class StyleSwitcherControl { | ||
constructor () { | ||
this.el = document.createElement('div'); | ||
} | ||
|
||
onAdd (_) { | ||
this.el.className = 'maplibregl-ctrl'; | ||
|
||
const select = document.createElement('select'); | ||
select.oninput = (event) => { | ||
const styleKey = event.target.value; | ||
const style = availableMapStyles[styleKey]; | ||
this.setStyle(styleKey, style); | ||
}; | ||
|
||
const mapStyleKey = getMapStyleQueryParam(); | ||
|
||
for (const key in availableMapStyles) { | ||
if (availableMapStyles.hasOwnProperty(key)) { | ||
const style = availableMapStyles[key]; | ||
let selected = ''; | ||
|
||
// As we go through the styles, look for it in the rendered example. | ||
if (this.styleURLNode === undefined && getNodesByTextContent(style.styleUrl)) { | ||
this.styleURLNode = getNodesByTextContent(style.styleUrl)[0]; | ||
} | ||
|
||
if (key === mapStyleKey) { | ||
selected = ' selected'; | ||
this.setStyle(key, style); | ||
} | ||
|
||
select.insertAdjacentHTML('beforeend', `<option value="${key}"${selected}>${style.name}</option>`); | ||
} | ||
} | ||
|
||
// Add the select to the element | ||
this.el.append(select); | ||
|
||
return this.el; | ||
} | ||
|
||
onRemove (_) { | ||
// Remove all children | ||
this.el.replaceChildren() | ||
} | ||
|
||
setStyle(styleKey, style) { | ||
// Change the map style | ||
map.setStyle(style.styleUrl) | ||
|
||
// Update the example | ||
this.styleURLNode.innerText = `'${style.styleUrl}'`; | ||
|
||
// Update the URL | ||
setMapStyleQueryParam(styleKey); | ||
} | ||
} | ||
|
||
map.addControl(new StyleSwitcherControl(), 'top-left'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Incomplete multi-character sanitization High