forked from maplibre/maplibre-gl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
165 additions
and
4 deletions.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,65 @@ | ||
import {GlobeControl} from './globe_control'; | ||
import {createMap as globalCreateMap, beforeMapTest} from '../../util/test/util'; | ||
|
||
function createMap() { | ||
return globalCreateMap({ | ||
attributionControl: false, | ||
style: { | ||
version: 8, | ||
sources: {}, | ||
layers: [], | ||
owner: 'maplibre', | ||
id: 'basic' | ||
}, | ||
hash: true | ||
}, undefined); | ||
} | ||
|
||
let map; | ||
|
||
beforeEach(() => { | ||
beforeMapTest(); | ||
map = createMap(); | ||
}); | ||
|
||
afterEach(() => { | ||
map.remove(); | ||
}); | ||
|
||
describe('GlobeControl', () => { | ||
test('appears in top-right by default', () => { | ||
map.addControl(new GlobeControl()); | ||
|
||
expect( | ||
map.getContainer().querySelectorAll('.maplibregl-ctrl-top-right .maplibregl-ctrl-globe') | ||
).toHaveLength(1); | ||
}); | ||
|
||
test('appears in the position specified by the position option', () => { | ||
map.addControl(new GlobeControl(), 'bottom-right'); | ||
|
||
expect( | ||
map.getContainer().querySelectorAll('.maplibregl-ctrl-bottom-right .maplibregl-ctrl-globe') | ||
).toHaveLength(1); | ||
|
||
expect( | ||
map.getContainer().querySelectorAll('.maplibregl-ctrl-top-right .maplibregl-ctrl-globe') | ||
).toHaveLength(0); | ||
}); | ||
|
||
test('toggles projection when clicked', async () => { | ||
await new Promise(resolve => map.on('load', resolve)); | ||
|
||
map.addControl(new GlobeControl()); | ||
expect(map.style.projection.name).toBe('mercator'); | ||
const button = map.getContainer().querySelector('.maplibregl-ctrl-globe'); | ||
|
||
button.click(); | ||
await new Promise(resolve => setTimeout(resolve, 0)); | ||
expect(map.style.projection.name).toBe('globe'); | ||
|
||
button.click(); | ||
await new Promise(resolve => setTimeout(resolve, 0)); | ||
expect(map.style.projection.name).toBe('mercator'); | ||
}); | ||
}); |
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,67 @@ | ||
import {DOM} from '../../util/dom'; | ||
|
||
import type {Map} from '../map'; | ||
import type {IControl} from './control'; | ||
|
||
/** | ||
* A `GlobeControl` control contains a button for toggling the map projection between "mercator" and "globe". | ||
* | ||
* @group Markers and Controls | ||
* | ||
* @example | ||
* ```ts | ||
* let map = new Map() | ||
* .addControl(new GlobeControl()); | ||
* ``` | ||
* | ||
* @see [Display a globe with a fill extrusion layer](https://maplibre.org/maplibre-gl-js/docs/examples/globe-fill-extrusion/) | ||
*/ | ||
export class GlobeControl implements IControl { | ||
_map: Map; | ||
_container: HTMLElement; | ||
_globeButton: HTMLButtonElement; | ||
|
||
/** {@inheritDoc IControl.onAdd} */ | ||
onAdd(map: Map) { | ||
this._map = map; | ||
this._container = DOM.create('div', 'maplibregl-ctrl maplibregl-ctrl-group'); | ||
this._globeButton = DOM.create('button', 'maplibregl-ctrl-globe', this._container); | ||
DOM.create('span', 'maplibregl-ctrl-icon', this._globeButton).setAttribute('aria-hidden', 'true'); | ||
this._globeButton.type = 'button'; | ||
this._globeButton.addEventListener('click', this._toggleProjection); | ||
|
||
this._updateGlobeIcon(); | ||
this._map.on('styledata', this._updateGlobeIcon); | ||
return this._container; | ||
} | ||
|
||
/** {@inheritDoc IControl.onRemove} */ | ||
onRemove() { | ||
DOM.remove(this._container); | ||
this._map.off('styledata', this._updateGlobeIcon); | ||
this._globeButton.removeEventListener('click', this._toggleProjection); | ||
this._map = undefined; | ||
} | ||
|
||
_toggleProjection = () => { | ||
const currentProjection = this._map.getProjection()?.type; | ||
if (currentProjection === 'mercator' || !currentProjection) { | ||
this._map.setProjection({type: 'globe'}); | ||
} else { | ||
this._map.setProjection({type: 'mercator'}); | ||
} | ||
this._updateGlobeIcon(); | ||
}; | ||
|
||
_updateGlobeIcon = () => { | ||
this._globeButton.classList.remove('maplibregl-ctrl-globe'); | ||
this._globeButton.classList.remove('maplibregl-ctrl-globe-enabled'); | ||
if (this._map.getProjection()?.type === 'globe') { | ||
this._globeButton.classList.add('maplibregl-ctrl-globe-enabled'); | ||
this._globeButton.title = this._map._getUIString('GlobeControl.Disable'); | ||
} else { | ||
this._globeButton.classList.add('maplibregl-ctrl-globe'); | ||
this._globeButton.title = this._map._getUIString('GlobeControl.Enable'); | ||
} | ||
}; | ||
} |
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