gridviz 2.0.34 → 2.0.36

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.34",
3
+ "version": "2.0.36",
4
4
  "description": "Visualization tool for gridded statistics",
5
5
  "keywords": [
6
6
  "statistics",
package/src/App.js CHANGED
@@ -186,7 +186,7 @@ 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('body')
189
+ this.legend = select('#' + container.id)
190
190
  .append('div')
191
191
  .attr('id', this.legendDivId)
192
192
  .style('position', 'absolute')
@@ -206,6 +206,10 @@ export class App {
206
206
 
207
207
  //tooltip
208
208
 
209
+ // set App container as default parent element for tooltip
210
+ if (!opts.tooltip) opts.tooltip = {}
211
+ if (!opts.tooltip.parentElement) opts.tooltip.parentElement = container
212
+
209
213
  /**
210
214
  * @private
211
215
  * @type {Tooltip} */
package/src/Tooltip.js CHANGED
@@ -44,12 +44,17 @@ export class Tooltip {
44
44
  this.yMouseOffset = opts.yMouseOffset || 0
45
45
  /** @type {number} */
46
46
  this.xMouseOffset = opts.xMouseOffset || 0
47
+ /** @type {HTMLElement} */
48
+ this.parentElement = opts.parentElement || document.body
47
49
 
48
50
  /**
49
51
  * @private
50
52
  * @type {import("d3-selection").Selection} */
51
53
  this.tooltip = select('#' + this.div)
52
- if (this.tooltip.empty()) this.tooltip = select('body').append('div').attr('id', this.div)
54
+ if (this.tooltip.empty())
55
+ this.tooltip = select('#' + this.parentElement.id)
56
+ .append('div')
57
+ .attr('id', this.div)
53
58
 
54
59
  //initialise
55
60
  this.tooltip.style('max-width', this.maxWidth)
@@ -64,6 +69,9 @@ export class Tooltip {
64
69
  this.tooltip.style('position', 'absolute')
65
70
  this.tooltip.style('pointer-events', 'none')
66
71
  this.tooltip.style('opacity', '0')
72
+
73
+ // aria-labels (thanks to wahlatlas)
74
+ this.tooltip.attr('role', 'tooltip').attr('aria-live', 'polite')
67
75
  }
68
76
 
69
77
  /** Show the tooltip */
@@ -94,6 +102,8 @@ export class Tooltip {
94
102
  this.tooltip
95
103
  .style('left', event.pageX + this.xOffset + 'px')
96
104
  .style('top', event.pageY - this.yOffset + 'px')
105
+
106
+ this.ensureTooltipInsideContainer(event)
97
107
  }
98
108
 
99
109
  /*
@@ -101,12 +111,12 @@ export class Tooltip {
101
111
  if (html) my.html(html);
102
112
  my.setPosition(event);
103
113
  my.show()
104
- //this.ensureTooltipOnScreen();
114
+ //this.ensureTooltipInsideContainer();
105
115
  };
106
116
 
107
117
  my.mousemove = function (event) {
108
118
  my.setPosition(event);
109
- //this.ensureTooltipOnScreen();
119
+ //this.ensureTooltipInsideContainer();
110
120
  };
111
121
 
112
122
  my.mouseout = function () {
@@ -126,25 +136,29 @@ export class Tooltip {
126
136
  }
127
137
 
128
138
  /**
129
- * @function ensureTooltipOnScreen
130
- * @description Prevents the tooltip from overflowing off screen
139
+ * @function ensureTooltipInsideContainer
140
+ * @description Prevents the tooltip from overflowing out of the App container (ensures that the tooltip is inside the gridviz container)
141
+ * @param {MouseEvent} event
131
142
  */
132
- /*my.ensureTooltipOnScreen = function () {
133
- // TODO: parent needs to be the all-encompassing container, not the map SVG id otherwise it just uses the last SVG which will be an inset SVG.
134
- let parent = document.getElementById(config.parentContainerId);
135
- let bbox = parent.getBBox();
136
- let parentWidth = bbox.width;
137
- let parentHeight = bbox.height;
138
- let node = tooltip.node();
139
- //too far right
140
- if (node.offsetLeft > parentWidth - node.clientWidth) {
141
- node.style.left = node.offsetLeft - (node.clientWidth + config.xOffset * 2) + "px";
142
-
143
- }
144
- //too far down
145
- if (node.offsetTop + node.clientHeight > parentHeight) {
146
- node.style.top = node.offsetTop - (node.clientHeight + config.yOffset * 2) + "px";
147
- }
148
-
149
- }*/
143
+ ensureTooltipInsideContainer = function (event) {
144
+ let ttNode = this.tooltip.node()
145
+ let parentRect = this.parentElement.getBoundingClientRect()
146
+
147
+ //too far right
148
+ if (ttNode.offsetLeft > parentRect.left + parentRect.width - ttNode.clientWidth) {
149
+ let left = event.pageX - ttNode.clientWidth - this.xOffset
150
+ ttNode.style.left = left + 'px'
151
+ // check if mouse covers tooltip
152
+ if (ttNode.offsetLeft + ttNode.clientWidth > event.pageX) {
153
+ //move tooltip left so it doesnt cover mouse
154
+ let left2 = event.pageX - (ttNode.clientWidth + this.xOffset)
155
+ ttNode.style.left = left2 + 'px'
156
+ }
157
+ }
158
+
159
+ //too far down
160
+ if (event.pageY + ttNode.clientHeight > parentRect.top + parentRect.height) {
161
+ ttNode.style.top = event.pageY - ttNode.clientHeight + 'px'
162
+ }
163
+ }
150
164
  }