Skip to content

Commit

Permalink
feat: add class when map is rendered and push-analytics helper files …
Browse files Browse the repository at this point in the history
…and classes (#552)

* feat: add class when map is rendered

* chore: code cleaning

* chore: code cleaning

* feat: check if bing layer is loaded

* chore: code cleaning

* fix: rendering timeout

* chore: refactor

* chore: change isLoading to within the trycatch

* fix: clear render timeout when data is fetched or received

* fix: restore check that returns if loading layers are encountered

---------

Co-authored-by: Jen Jones Arnesen <jennifer@dhis2.org>
Co-authored-by: HendrikThePendric <hendrik@dhis2.org>
  • Loading branch information
3 people authored Mar 4, 2024
1 parent 4bf8d59 commit c3830ac
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import Popup from './ui/Popup'
import Label from './ui/Label'
import './Map.css'

const renderedClass = 'dhis2-map-rendered'
const RENDER_TIMEOUT_DURATION = 500

export class MapGL extends Evented {
// Returns true if the layer type is supported
static hasLayerSupport(type) {
Expand Down Expand Up @@ -44,6 +47,7 @@ export class MapGL extends Evented {

this._mapgl = mapgl
this._glyphs = glyphs
this._renderTimeout = null

// Translate strings
if (locale) {
Expand All @@ -55,12 +59,18 @@ export class MapGL extends Evented {
})
}

mapgl.on('render', this.onRender)
mapgl.on('idle', this.onIdle)
mapgl.on('load', this.onLoad)
mapgl.on('click', this.onClick)
mapgl.on('contextmenu', this.onContextMenu)
mapgl.on('mousemove', this.onMouseMove)
mapgl.on('mouseout', this.onMouseOut)
mapgl.on('error', this.onError)
/* Data and dataloading events are an indication that
* the map is not done yet */
mapgl.on('data', this._clearRenderTimeout)
mapgl.on('dataloading', this._clearRenderTimeout)

this._layers = []
this._controls = {}
Expand Down Expand Up @@ -146,12 +156,16 @@ export class MapGL extends Evented {
remove() {
const mapgl = this._mapgl

mapgl.off('render', this.onRender)
mapgl.off('idle', this.onIdle)
mapgl.off('load', this.onLoad)
mapgl.off('click', this.onClick)
mapgl.off('contextmenu', this.onContextMenu)
mapgl.off('mousemove', this.onMouseMove)
mapgl.off('mouseout', this.onMouseOut)
mapgl.off('error', this.onError)
mapgl.off('data', this._clearRenderTimeout)
mapgl.off('dataloading', this._clearRenderTimeout)

mapgl.remove()

Expand Down Expand Up @@ -258,6 +272,21 @@ export class MapGL extends Evented {
this.getMapGL().getCanvas().style.cursor = feature ? 'pointer' : ''
}

// Remove rendered class if rendering is happening
onRender = () => {
this._removeClass(renderedClass)
this._clearRenderTimeout()
}

// Add rendered class if map is idle
onIdle = () => {
if (this.getLayers().some(layer => layer._isLoading)) {
return
}

this._setRenderTimeout()
}

// Set hover state for features
setHoverState(features) {
// Only set hover state when features are changed
Expand Down Expand Up @@ -453,6 +482,45 @@ export class MapGL extends Evented {

return { type, coordinates, position, feature }
}

_setRenderTimeout() {
// Ensure pending timeout is cleared before setting a new one
this._clearRenderTimeout()
// Make sure the map stay rendered for at least 500ms
this._renderTimeout = setTimeout(() => {
this._addClass(renderedClass)
this._renderTimeout = null
}, RENDER_TIMEOUT_DURATION)
}

_clearRenderTimeout() {
if (this._renderTimeout) {
clearTimeout(this._renderTimeout)
this._renderTimeout = null
}
}

// Add class to map container
_addClass(className) {
if (this._mapgl) {
const { classList } = this._mapgl._container

if (!classList.contains(className)) {
classList.add(className)
}
}
}

// Remove class from map container
_removeClass(className) {
if (this._mapgl) {
const { classList } = this._mapgl._container

if (classList.contains(className)) {
classList.remove(className)
}
}
}
}

export default MapGL
4 changes: 4 additions & 0 deletions src/layers/BingLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ class BingLayer extends Layer {
}

async addTo(map) {
this._isLoading = true

await this.createSource()

this.createLayer()

await super.addTo(map)

this._isLoading = false

// Make sure overlays are on top
map.orderOverlays()

Expand Down
8 changes: 7 additions & 1 deletion src/layers/EarthEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class EarthEngine extends Layer {
this._map = map

if (map.styleIsLoaded()) {
this._isLoading = true
this.getWorkerInstance()
.then(async worker => {
this.worker = worker
Expand All @@ -35,6 +36,8 @@ class EarthEngine extends Layer {
super.addTo(map)
this.onLoad()

this._isLoading = false

const { preload, data } = this.options

// Get aggregations if not plugin (preload=false) and org units
Expand All @@ -46,7 +49,10 @@ class EarthEngine extends Layer {

resolve()
})
.catch(reject)
.catch(() => {
this._isLoading = false
reject()
})
} else {
resolve()
}
Expand Down
3 changes: 3 additions & 0 deletions src/layers/ServerCluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class ServerCluster extends Cluster {

if (!this.tileClusters[tileId]) {
this.tileClusters[tileId] = 'pending'
this._isLoading = true
this.options.load(this.getTileParams(tileId), this.onTileLoad)
}
}
Expand Down Expand Up @@ -196,6 +197,8 @@ class ServerCluster extends Cluster {
if (visibleTiles.includes(tileId)) {
this.updateClusters([tileId])
}

this._isLoading = false
}

getBounds = () => this.options.bounds
Expand Down

0 comments on commit c3830ac

Please sign in to comment.