TooltipHTMLContent.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { isString, indexOf, each, bind, isArray, isDom } from 'zrender/lib/core/util.js';
  41. import { normalizeEvent } from 'zrender/lib/core/event.js';
  42. import { transformLocalCoord } from 'zrender/lib/core/dom.js';
  43. import env from 'zrender/lib/core/env.js';
  44. import { convertToColorString, toCamelCase, normalizeCssArray } from '../../util/format.js';
  45. import { shouldTooltipConfine, toCSSVendorPrefix, getComputedStyle, TRANSFORM_VENDOR, TRANSITION_VENDOR } from './helper.js';
  46. import { getPaddingFromTooltipModel } from './tooltipMarkup.js';
  47. /* global document, window */
  48. var CSS_TRANSITION_VENDOR = toCSSVendorPrefix(TRANSITION_VENDOR, 'transition');
  49. var CSS_TRANSFORM_VENDOR = toCSSVendorPrefix(TRANSFORM_VENDOR, 'transform'); // eslint-disable-next-line
  50. var gCssText = "position:absolute;display:block;border-style:solid;white-space:nowrap;z-index:9999999;" + (env.transform3dSupported ? 'will-change:transform;' : '');
  51. function mirrorPos(pos) {
  52. pos = pos === 'left' ? 'right' : pos === 'right' ? 'left' : pos === 'top' ? 'bottom' : 'top';
  53. return pos;
  54. }
  55. function assembleArrow(tooltipModel, borderColor, arrowPosition) {
  56. if (!isString(arrowPosition) || arrowPosition === 'inside') {
  57. return '';
  58. }
  59. var backgroundColor = tooltipModel.get('backgroundColor');
  60. var borderWidth = tooltipModel.get('borderWidth');
  61. borderColor = convertToColorString(borderColor);
  62. var arrowPos = mirrorPos(arrowPosition);
  63. var arrowSize = Math.max(Math.round(borderWidth) * 1.5, 6);
  64. var positionStyle = '';
  65. var transformStyle = CSS_TRANSFORM_VENDOR + ':';
  66. var rotateDeg;
  67. if (indexOf(['left', 'right'], arrowPos) > -1) {
  68. positionStyle += 'top:50%';
  69. transformStyle += "translateY(-50%) rotate(" + (rotateDeg = arrowPos === 'left' ? -225 : -45) + "deg)";
  70. } else {
  71. positionStyle += 'left:50%';
  72. transformStyle += "translateX(-50%) rotate(" + (rotateDeg = arrowPos === 'top' ? 225 : 45) + "deg)";
  73. }
  74. var rotateRadian = rotateDeg * Math.PI / 180;
  75. var arrowWH = arrowSize + borderWidth;
  76. var rotatedWH = arrowWH * Math.abs(Math.cos(rotateRadian)) + arrowWH * Math.abs(Math.sin(rotateRadian));
  77. var arrowOffset = Math.round(((rotatedWH - Math.SQRT2 * borderWidth) / 2 + Math.SQRT2 * borderWidth - (rotatedWH - arrowWH) / 2) * 100) / 100;
  78. positionStyle += ";" + arrowPos + ":-" + arrowOffset + "px";
  79. var borderStyle = borderColor + " solid " + borderWidth + "px;";
  80. var styleCss = ["position:absolute;width:" + arrowSize + "px;height:" + arrowSize + "px;", positionStyle + ";" + transformStyle + ";", "border-bottom:" + borderStyle, "border-right:" + borderStyle, "background-color:" + backgroundColor + ";"];
  81. return "<div style=\"" + styleCss.join('') + "\"></div>";
  82. }
  83. function assembleTransition(duration, onlyFade) {
  84. var transitionCurve = 'cubic-bezier(0.23,1,0.32,1)';
  85. var transitionOption = " " + duration / 2 + "s " + transitionCurve;
  86. var transitionText = "opacity" + transitionOption + ",visibility" + transitionOption;
  87. if (!onlyFade) {
  88. transitionOption = " " + duration + "s " + transitionCurve;
  89. transitionText += env.transformSupported ? "," + CSS_TRANSFORM_VENDOR + transitionOption : ",left" + transitionOption + ",top" + transitionOption;
  90. }
  91. return CSS_TRANSITION_VENDOR + ':' + transitionText;
  92. }
  93. function assembleTransform(x, y, toString) {
  94. // If using float on style, the final width of the dom might
  95. // keep changing slightly while mouse move. So `toFixed(0)` them.
  96. var x0 = x.toFixed(0) + 'px';
  97. var y0 = y.toFixed(0) + 'px'; // not support transform, use `left` and `top` instead.
  98. if (!env.transformSupported) {
  99. return toString ? "top:" + y0 + ";left:" + x0 + ";" : [['top', y0], ['left', x0]];
  100. } // support transform
  101. var is3d = env.transform3dSupported;
  102. var translate = "translate" + (is3d ? '3d' : '') + "(" + x0 + "," + y0 + (is3d ? ',0' : '') + ")";
  103. return toString ? 'top:0;left:0;' + CSS_TRANSFORM_VENDOR + ':' + translate + ';' : [['top', 0], ['left', 0], [TRANSFORM_VENDOR, translate]];
  104. }
  105. /**
  106. * @param {Object} textStyle
  107. * @return {string}
  108. * @inner
  109. */
  110. function assembleFont(textStyleModel) {
  111. var cssText = [];
  112. var fontSize = textStyleModel.get('fontSize');
  113. var color = textStyleModel.getTextColor();
  114. color && cssText.push('color:' + color);
  115. cssText.push('font:' + textStyleModel.getFont());
  116. fontSize // @ts-ignore, leave it to the tooltip refactor.
  117. && cssText.push('line-height:' + Math.round(fontSize * 3 / 2) + 'px');
  118. var shadowColor = textStyleModel.get('textShadowColor');
  119. var shadowBlur = textStyleModel.get('textShadowBlur') || 0;
  120. var shadowOffsetX = textStyleModel.get('textShadowOffsetX') || 0;
  121. var shadowOffsetY = textStyleModel.get('textShadowOffsetY') || 0;
  122. shadowColor && shadowBlur && cssText.push('text-shadow:' + shadowOffsetX + 'px ' + shadowOffsetY + 'px ' + shadowBlur + 'px ' + shadowColor);
  123. each(['decoration', 'align'], function (name) {
  124. var val = textStyleModel.get(name);
  125. val && cssText.push('text-' + name + ':' + val);
  126. });
  127. return cssText.join(';');
  128. }
  129. function assembleCssText(tooltipModel, enableTransition, onlyFade) {
  130. var cssText = [];
  131. var transitionDuration = tooltipModel.get('transitionDuration');
  132. var backgroundColor = tooltipModel.get('backgroundColor');
  133. var shadowBlur = tooltipModel.get('shadowBlur');
  134. var shadowColor = tooltipModel.get('shadowColor');
  135. var shadowOffsetX = tooltipModel.get('shadowOffsetX');
  136. var shadowOffsetY = tooltipModel.get('shadowOffsetY');
  137. var textStyleModel = tooltipModel.getModel('textStyle');
  138. var padding = getPaddingFromTooltipModel(tooltipModel, 'html');
  139. var boxShadow = shadowOffsetX + "px " + shadowOffsetY + "px " + shadowBlur + "px " + shadowColor;
  140. cssText.push('box-shadow:' + boxShadow); // Animation transition. Do not animate when transitionDuration is 0.
  141. enableTransition && transitionDuration && cssText.push(assembleTransition(transitionDuration, onlyFade));
  142. if (backgroundColor) {
  143. cssText.push('background-color:' + backgroundColor);
  144. } // Border style
  145. each(['width', 'color', 'radius'], function (name) {
  146. var borderName = 'border-' + name;
  147. var camelCase = toCamelCase(borderName);
  148. var val = tooltipModel.get(camelCase);
  149. val != null && cssText.push(borderName + ':' + val + (name === 'color' ? '' : 'px'));
  150. }); // Text style
  151. cssText.push(assembleFont(textStyleModel)); // Padding
  152. if (padding != null) {
  153. cssText.push('padding:' + normalizeCssArray(padding).join('px ') + 'px');
  154. }
  155. return cssText.join(';') + ';';
  156. } // If not able to make, do not modify the input `out`.
  157. function makeStyleCoord(out, zr, appendToBody, zrX, zrY) {
  158. var zrPainter = zr && zr.painter;
  159. if (appendToBody) {
  160. var zrViewportRoot = zrPainter && zrPainter.getViewportRoot();
  161. if (zrViewportRoot) {
  162. // Some APPs might use scale on body, so we support CSS transform here.
  163. transformLocalCoord(out, zrViewportRoot, document.body, zrX, zrY);
  164. }
  165. } else {
  166. out[0] = zrX;
  167. out[1] = zrY; // xy should be based on canvas root. But tooltipContent is
  168. // the sibling of canvas root. So padding of ec container
  169. // should be considered here.
  170. var viewportRootOffset = zrPainter && zrPainter.getViewportRootOffset();
  171. if (viewportRootOffset) {
  172. out[0] += viewportRootOffset.offsetLeft;
  173. out[1] += viewportRootOffset.offsetTop;
  174. }
  175. }
  176. out[2] = out[0] / zr.getWidth();
  177. out[3] = out[1] / zr.getHeight();
  178. }
  179. var TooltipHTMLContent =
  180. /** @class */
  181. function () {
  182. function TooltipHTMLContent(container, api, opt) {
  183. this._show = false;
  184. this._styleCoord = [0, 0, 0, 0];
  185. this._enterable = true;
  186. this._firstShow = true;
  187. this._longHide = true;
  188. if (env.wxa) {
  189. return null;
  190. }
  191. var el = document.createElement('div'); // TODO: TYPE
  192. el.domBelongToZr = true;
  193. this.el = el;
  194. var zr = this._zr = api.getZr();
  195. var appendToBody = this._appendToBody = opt && opt.appendToBody;
  196. makeStyleCoord(this._styleCoord, zr, appendToBody, api.getWidth() / 2, api.getHeight() / 2);
  197. if (appendToBody) {
  198. document.body.appendChild(el);
  199. } else {
  200. container.appendChild(el);
  201. }
  202. this._container = container; // FIXME
  203. // Is it needed to trigger zr event manually if
  204. // the browser do not support `pointer-events: none`.
  205. var self = this;
  206. el.onmouseenter = function () {
  207. // clear the timeout in hideLater and keep showing tooltip
  208. if (self._enterable) {
  209. clearTimeout(self._hideTimeout);
  210. self._show = true;
  211. }
  212. self._inContent = true;
  213. };
  214. el.onmousemove = function (e) {
  215. e = e || window.event;
  216. if (!self._enterable) {
  217. // `pointer-events: none` is set to tooltip content div
  218. // if `enterable` is set as `false`, and `el.onmousemove`
  219. // can not be triggered. But in browser that do not
  220. // support `pointer-events`, we need to do this:
  221. // Try trigger zrender event to avoid mouse
  222. // in and out shape too frequently
  223. var handler = zr.handler;
  224. var zrViewportRoot = zr.painter.getViewportRoot();
  225. normalizeEvent(zrViewportRoot, e, true);
  226. handler.dispatch('mousemove', e);
  227. }
  228. };
  229. el.onmouseleave = function () {
  230. // set `_inContent` to `false` before `hideLater`
  231. self._inContent = false;
  232. if (self._enterable) {
  233. if (self._show) {
  234. self.hideLater(self._hideDelay);
  235. }
  236. }
  237. };
  238. }
  239. /**
  240. * Update when tooltip is rendered
  241. */
  242. TooltipHTMLContent.prototype.update = function (tooltipModel) {
  243. // FIXME
  244. // Move this logic to ec main?
  245. var container = this._container;
  246. var position = getComputedStyle(container, 'position');
  247. var domStyle = container.style;
  248. if (domStyle.position !== 'absolute' && position !== 'absolute') {
  249. domStyle.position = 'relative';
  250. } // move tooltip if chart resized
  251. var alwaysShowContent = tooltipModel.get('alwaysShowContent');
  252. alwaysShowContent && this._moveIfResized(); // update className
  253. this.el.className = tooltipModel.get('className') || ''; // Hide the tooltip
  254. // PENDING
  255. // this.hide();
  256. };
  257. TooltipHTMLContent.prototype.show = function (tooltipModel, nearPointColor) {
  258. clearTimeout(this._hideTimeout);
  259. clearTimeout(this._longHideTimeout);
  260. var el = this.el;
  261. var style = el.style;
  262. var styleCoord = this._styleCoord;
  263. if (!el.innerHTML) {
  264. style.display = 'none';
  265. } else {
  266. style.cssText = gCssText + assembleCssText(tooltipModel, !this._firstShow, this._longHide) // initial transform
  267. + assembleTransform(styleCoord[0], styleCoord[1], true) + ("border-color:" + convertToColorString(nearPointColor) + ";") + (tooltipModel.get('extraCssText') || '') // If mouse occasionally move over the tooltip, a mouseout event will be
  268. // triggered by canvas, and cause some unexpectable result like dragging
  269. // stop, "unfocusAdjacency". Here `pointer-events: none` is used to solve
  270. // it. Although it is not supported by IE8~IE10, fortunately it is a rare
  271. // scenario.
  272. + (";pointer-events:" + (this._enterable ? 'auto' : 'none'));
  273. }
  274. this._show = true;
  275. this._firstShow = false;
  276. this._longHide = false;
  277. };
  278. TooltipHTMLContent.prototype.setContent = function (content, markers, tooltipModel, borderColor, arrowPosition) {
  279. var el = this.el;
  280. if (content == null) {
  281. el.innerHTML = '';
  282. return;
  283. }
  284. var arrow = '';
  285. if (isString(arrowPosition) && tooltipModel.get('trigger') === 'item' && !shouldTooltipConfine(tooltipModel)) {
  286. arrow = assembleArrow(tooltipModel, borderColor, arrowPosition);
  287. }
  288. if (isString(content)) {
  289. el.innerHTML = content + arrow;
  290. } else if (content) {
  291. // Clear previous
  292. el.innerHTML = '';
  293. if (!isArray(content)) {
  294. content = [content];
  295. }
  296. for (var i = 0; i < content.length; i++) {
  297. if (isDom(content[i]) && content[i].parentNode !== el) {
  298. el.appendChild(content[i]);
  299. }
  300. } // no arrow if empty
  301. if (arrow && el.childNodes.length) {
  302. // no need to create a new parent element, but it's not supported by IE 10 and older.
  303. // const arrowEl = document.createRange().createContextualFragment(arrow);
  304. var arrowEl = document.createElement('div');
  305. arrowEl.innerHTML = arrow;
  306. el.appendChild(arrowEl);
  307. }
  308. }
  309. };
  310. TooltipHTMLContent.prototype.setEnterable = function (enterable) {
  311. this._enterable = enterable;
  312. };
  313. TooltipHTMLContent.prototype.getSize = function () {
  314. var el = this.el;
  315. return [el.offsetWidth, el.offsetHeight];
  316. };
  317. TooltipHTMLContent.prototype.moveTo = function (zrX, zrY) {
  318. var styleCoord = this._styleCoord;
  319. makeStyleCoord(styleCoord, this._zr, this._appendToBody, zrX, zrY);
  320. if (styleCoord[0] != null && styleCoord[1] != null) {
  321. var style_1 = this.el.style;
  322. var transforms = assembleTransform(styleCoord[0], styleCoord[1]);
  323. each(transforms, function (transform) {
  324. style_1[transform[0]] = transform[1];
  325. });
  326. }
  327. };
  328. /**
  329. * when `alwaysShowContent` is true,
  330. * move the tooltip after chart resized
  331. */
  332. TooltipHTMLContent.prototype._moveIfResized = function () {
  333. // The ratio of left to width
  334. var ratioX = this._styleCoord[2]; // The ratio of top to height
  335. var ratioY = this._styleCoord[3];
  336. this.moveTo(ratioX * this._zr.getWidth(), ratioY * this._zr.getHeight());
  337. };
  338. TooltipHTMLContent.prototype.hide = function () {
  339. var _this = this;
  340. var style = this.el.style;
  341. style.visibility = 'hidden';
  342. style.opacity = '0';
  343. env.transform3dSupported && (style.willChange = '');
  344. this._show = false;
  345. this._longHideTimeout = setTimeout(function () {
  346. return _this._longHide = true;
  347. }, 500);
  348. };
  349. TooltipHTMLContent.prototype.hideLater = function (time) {
  350. if (this._show && !(this._inContent && this._enterable)) {
  351. if (time) {
  352. this._hideDelay = time; // Set show false to avoid invoke hideLater multiple times
  353. this._show = false;
  354. this._hideTimeout = setTimeout(bind(this.hide, this), time);
  355. } else {
  356. this.hide();
  357. }
  358. }
  359. };
  360. TooltipHTMLContent.prototype.isShow = function () {
  361. return this._show;
  362. };
  363. TooltipHTMLContent.prototype.dispose = function () {
  364. this.el.parentNode.removeChild(this.el);
  365. };
  366. return TooltipHTMLContent;
  367. }();
  368. export default TooltipHTMLContent;