TooltipHTMLContent.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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;z-index:-1;", 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._alwaysShowContent = false;
  187. this._firstShow = true;
  188. this._longHide = true;
  189. if (env.wxa) {
  190. return null;
  191. }
  192. var el = document.createElement('div'); // TODO: TYPE
  193. el.domBelongToZr = true;
  194. this.el = el;
  195. var zr = this._zr = api.getZr();
  196. var appendToBody = this._appendToBody = opt && opt.appendToBody;
  197. makeStyleCoord(this._styleCoord, zr, appendToBody, api.getWidth() / 2, api.getHeight() / 2);
  198. if (appendToBody) {
  199. document.body.appendChild(el);
  200. } else {
  201. container.appendChild(el);
  202. }
  203. this._container = container; // FIXME
  204. // Is it needed to trigger zr event manually if
  205. // the browser do not support `pointer-events: none`.
  206. var self = this;
  207. el.onmouseenter = function () {
  208. // clear the timeout in hideLater and keep showing tooltip
  209. if (self._enterable) {
  210. clearTimeout(self._hideTimeout);
  211. self._show = true;
  212. }
  213. self._inContent = true;
  214. };
  215. el.onmousemove = function (e) {
  216. e = e || window.event;
  217. if (!self._enterable) {
  218. // `pointer-events: none` is set to tooltip content div
  219. // if `enterable` is set as `false`, and `el.onmousemove`
  220. // can not be triggered. But in browser that do not
  221. // support `pointer-events`, we need to do this:
  222. // Try trigger zrender event to avoid mouse
  223. // in and out shape too frequently
  224. var handler = zr.handler;
  225. var zrViewportRoot = zr.painter.getViewportRoot();
  226. normalizeEvent(zrViewportRoot, e, true);
  227. handler.dispatch('mousemove', e);
  228. }
  229. };
  230. el.onmouseleave = function () {
  231. // set `_inContent` to `false` before `hideLater`
  232. self._inContent = false;
  233. if (self._enterable) {
  234. if (self._show) {
  235. self.hideLater(self._hideDelay);
  236. }
  237. }
  238. };
  239. }
  240. /**
  241. * Update when tooltip is rendered
  242. */
  243. TooltipHTMLContent.prototype.update = function (tooltipModel) {
  244. // FIXME
  245. // Move this logic to ec main?
  246. var container = this._container;
  247. var position = getComputedStyle(container, 'position');
  248. var domStyle = container.style;
  249. if (domStyle.position !== 'absolute' && position !== 'absolute') {
  250. domStyle.position = 'relative';
  251. } // move tooltip if chart resized
  252. var alwaysShowContent = tooltipModel.get('alwaysShowContent');
  253. alwaysShowContent && this._moveIfResized(); // update alwaysShowContent
  254. this._alwaysShowContent = alwaysShowContent; // update className
  255. this.el.className = tooltipModel.get('className') || ''; // Hide the tooltip
  256. // PENDING
  257. // this.hide();
  258. };
  259. TooltipHTMLContent.prototype.show = function (tooltipModel, nearPointColor) {
  260. clearTimeout(this._hideTimeout);
  261. clearTimeout(this._longHideTimeout);
  262. var el = this.el;
  263. var style = el.style;
  264. var styleCoord = this._styleCoord;
  265. if (!el.innerHTML) {
  266. style.display = 'none';
  267. } else {
  268. style.cssText = gCssText + assembleCssText(tooltipModel, !this._firstShow, this._longHide) // initial transform
  269. + 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
  270. // triggered by canvas, and cause some unexpectable result like dragging
  271. // stop, "unfocusAdjacency". Here `pointer-events: none` is used to solve
  272. // it. Although it is not supported by IE8~IE10, fortunately it is a rare
  273. // scenario.
  274. + (";pointer-events:" + (this._enterable ? 'auto' : 'none'));
  275. }
  276. this._show = true;
  277. this._firstShow = false;
  278. this._longHide = false;
  279. };
  280. TooltipHTMLContent.prototype.setContent = function (content, markers, tooltipModel, borderColor, arrowPosition) {
  281. var el = this.el;
  282. if (content == null) {
  283. el.innerHTML = '';
  284. return;
  285. }
  286. var arrow = '';
  287. if (isString(arrowPosition) && tooltipModel.get('trigger') === 'item' && !shouldTooltipConfine(tooltipModel)) {
  288. arrow = assembleArrow(tooltipModel, borderColor, arrowPosition);
  289. }
  290. if (isString(content)) {
  291. el.innerHTML = content + arrow;
  292. } else if (content) {
  293. // Clear previous
  294. el.innerHTML = '';
  295. if (!isArray(content)) {
  296. content = [content];
  297. }
  298. for (var i = 0; i < content.length; i++) {
  299. if (isDom(content[i]) && content[i].parentNode !== el) {
  300. el.appendChild(content[i]);
  301. }
  302. } // no arrow if empty
  303. if (arrow && el.childNodes.length) {
  304. // no need to create a new parent element, but it's not supported by IE 10 and older.
  305. // const arrowEl = document.createRange().createContextualFragment(arrow);
  306. var arrowEl = document.createElement('div');
  307. arrowEl.innerHTML = arrow;
  308. el.appendChild(arrowEl);
  309. }
  310. }
  311. };
  312. TooltipHTMLContent.prototype.setEnterable = function (enterable) {
  313. this._enterable = enterable;
  314. };
  315. TooltipHTMLContent.prototype.getSize = function () {
  316. var el = this.el;
  317. return [el.offsetWidth, el.offsetHeight];
  318. };
  319. TooltipHTMLContent.prototype.moveTo = function (zrX, zrY) {
  320. var styleCoord = this._styleCoord;
  321. makeStyleCoord(styleCoord, this._zr, this._appendToBody, zrX, zrY);
  322. if (styleCoord[0] != null && styleCoord[1] != null) {
  323. var style_1 = this.el.style;
  324. var transforms = assembleTransform(styleCoord[0], styleCoord[1]);
  325. each(transforms, function (transform) {
  326. style_1[transform[0]] = transform[1];
  327. });
  328. }
  329. };
  330. /**
  331. * when `alwaysShowContent` is true,
  332. * move the tooltip after chart resized
  333. */
  334. TooltipHTMLContent.prototype._moveIfResized = function () {
  335. // The ratio of left to width
  336. var ratioX = this._styleCoord[2]; // The ratio of top to height
  337. var ratioY = this._styleCoord[3];
  338. this.moveTo(ratioX * this._zr.getWidth(), ratioY * this._zr.getHeight());
  339. };
  340. TooltipHTMLContent.prototype.hide = function () {
  341. var _this = this;
  342. var style = this.el.style;
  343. style.visibility = 'hidden';
  344. style.opacity = '0';
  345. env.transform3dSupported && (style.willChange = '');
  346. this._show = false;
  347. this._longHideTimeout = setTimeout(function () {
  348. return _this._longHide = true;
  349. }, 500);
  350. };
  351. TooltipHTMLContent.prototype.hideLater = function (time) {
  352. if (this._show && !(this._inContent && this._enterable) && !this._alwaysShowContent) {
  353. if (time) {
  354. this._hideDelay = time; // Set show false to avoid invoke hideLater multiple times
  355. this._show = false;
  356. this._hideTimeout = setTimeout(bind(this.hide, this), time);
  357. } else {
  358. this.hide();
  359. }
  360. }
  361. };
  362. TooltipHTMLContent.prototype.isShow = function () {
  363. return this._show;
  364. };
  365. TooltipHTMLContent.prototype.dispose = function () {
  366. this.el.parentNode.removeChild(this.el);
  367. };
  368. return TooltipHTMLContent;
  369. }();
  370. export default TooltipHTMLContent;