gridviz 2.0.44 → 2.0.45

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gridviz",
3
- "version": "2.0.44",
3
+ "version": "2.0.45",
4
4
  "description": "Visualization tool for gridded statistics",
5
5
  "keywords": [
6
6
  "statistics",
package/src/App.js CHANGED
@@ -37,17 +37,17 @@ export class App {
37
37
  this.layers = []
38
38
 
39
39
  //get container element
40
- container = container || document.getElementById('gridviz')
41
- if (!container) {
40
+ this.container = container || document.getElementById('gridviz')
41
+ if (!this.container) {
42
42
  console.error('Cannot find gridviz container element.')
43
43
  return
44
44
  }
45
45
 
46
46
  //set dimensions
47
47
  /** @type {number} */
48
- this.w = opts.w || container.offsetWidth
48
+ this.w = opts.w || this.container.offsetWidth
49
49
  /** @type {number} */
50
- this.h = opts.h || container.offsetHeight
50
+ this.h = opts.h || this.container.offsetHeight
51
51
 
52
52
  //create canvas element if user doesnt specify one
53
53
  /** @type {HTMLCanvasElement} */
@@ -56,7 +56,7 @@ export class App {
56
56
  canvas = document.createElement('canvas')
57
57
  canvas.setAttribute('width', '' + this.w)
58
58
  canvas.setAttribute('height', '' + this.h)
59
- container.appendChild(canvas)
59
+ this.container.appendChild(canvas)
60
60
  }
61
61
 
62
62
  /** Make geo canvas
@@ -161,7 +161,7 @@ export class App {
161
161
  if (monitor) monitorDuration('End redraw')
162
162
 
163
163
  // listen for resize events on the App's container and handle them
164
- this.defineResizeObserver(container, canvas)
164
+ this.defineResizeObserver(this.container, canvas)
165
165
 
166
166
  return this
167
167
  }
@@ -186,7 +186,9 @@ export class App {
186
186
  this.legendDivId = opts.legendDivId || 'gvizLegend'
187
187
  this.legend = select('#' + this.legendDivId)
188
188
  if (this.legend.empty()) {
189
- this.legend = select(container.id && container.id != '' ? '#' + container.id : 'body')
189
+ this.legend = select(
190
+ this.container.id && this.container.id != '' ? '#' + this.container.id : 'body'
191
+ )
190
192
  .append('div')
191
193
  .attr('id', this.legendDivId)
192
194
  .style('position', 'absolute')
@@ -208,7 +210,7 @@ export class App {
208
210
 
209
211
  // set App container as default parent element for tooltip
210
212
  if (!opts.tooltip) opts.tooltip = {}
211
- if (!opts.tooltip.parentElement) opts.tooltip.parentElement = container
213
+ if (!opts.tooltip.parentElement) opts.tooltip.parentElement = this.container
212
214
 
213
215
  /**
214
216
  * @private
@@ -278,15 +280,14 @@ export class App {
278
280
  if (this.canvasSave) this.cg.ctx.drawImage(this.canvasSave, 0, 0)
279
281
  }
280
282
  }
281
- container.addEventListener('mouseover', (e) => {
282
- focusCell(e)
283
- })
284
- container.addEventListener('mousemove', (e) => {
285
- focusCell(e)
286
- })
287
- container.addEventListener('mouseout', () => {
288
- this.tooltip.hide()
289
- })
283
+
284
+ // add event listeners to container
285
+ this.mouseOverHandler = (e) => focusCell(e)
286
+ this.mouseMoveHandler = (e) => focusCell(e)
287
+ this.mouseOutHandler = (e) => this.tooltip.hide()
288
+ this.container.addEventListener('mouseover', this.mouseOverHandler)
289
+ this.container.addEventListener('mousemove', this.mouseMoveHandler)
290
+ this.container.addEventListener('mouseout', this.mouseOutHandler)
290
291
 
291
292
  // add extra logic to onZoomStartFun
292
293
  this.cg.onZoomStartFun = (e) => {
@@ -639,4 +640,29 @@ export class App {
639
640
 
640
641
  resizeObserver.observe(container)
641
642
  }
643
+
644
+ /**
645
+ * @description Destroy the app and it's event listeners
646
+ * This should significantly reduce the memory used when creating and destroying gridviz app instances (for example in leaflet-gridviz)
647
+ * @memberof App
648
+ */
649
+ destroy() {
650
+ // clear layers
651
+ this.layers = []
652
+ this.bgLayers = []
653
+
654
+ // remove event listeners from container
655
+ this.container.removeEventListener('mouseover', this.mouseOverHandler)
656
+ this.container.removeEventListener('mousemove', this.mouseMoveHandler)
657
+ this.container.removeEventListener('mouseout', this.mouseOutHandler)
658
+
659
+ // remove canvas
660
+ this.cg.canvas.remove()
661
+
662
+ // remove legend
663
+ this.legend?.remove()
664
+
665
+ // remove tooltip
666
+ this.tooltip.tooltip?.remove()
667
+ }
642
668
  }
package/src/Tooltip.js CHANGED
@@ -48,7 +48,7 @@ export class Tooltip {
48
48
  this.parentElement = opts.parentElement || document.body
49
49
 
50
50
  /**
51
- * @private
51
+ * @public
52
52
  * @type {import("d3-selection").Selection} */
53
53
  this.tooltip = select('#' + this.div)
54
54
  if (this.tooltip.empty())