TooltipContent.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. var zrUtil = require("zrender/lib/core/util");
  20. var zrColor = require("zrender/lib/tool/color");
  21. var eventUtil = require("zrender/lib/core/event");
  22. var domUtil = require("zrender/lib/core/dom");
  23. var env = require("zrender/lib/core/env");
  24. var formatUtil = require("../../util/format");
  25. /*
  26. * Licensed to the Apache Software Foundation (ASF) under one
  27. * or more contributor license agreements. See the NOTICE file
  28. * distributed with this work for additional information
  29. * regarding copyright ownership. The ASF licenses this file
  30. * to you under the Apache License, Version 2.0 (the
  31. * "License"); you may not use this file except in compliance
  32. * with the License. You may obtain a copy of the License at
  33. *
  34. * http://www.apache.org/licenses/LICENSE-2.0
  35. *
  36. * Unless required by applicable law or agreed to in writing,
  37. * software distributed under the License is distributed on an
  38. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  39. * KIND, either express or implied. See the License for the
  40. * specific language governing permissions and limitations
  41. * under the License.
  42. */
  43. var each = zrUtil.each;
  44. var toCamelCase = formatUtil.toCamelCase;
  45. var vendors = ['', '-webkit-', '-moz-', '-o-'];
  46. var gCssText = 'position:absolute;display:block;border-style:solid;white-space:nowrap;z-index:9999999;';
  47. /**
  48. * @param {number} duration
  49. * @return {string}
  50. * @inner
  51. */
  52. function assembleTransition(duration) {
  53. var transitionCurve = 'cubic-bezier(0.23, 1, 0.32, 1)';
  54. var transitionText = 'left ' + duration + 's ' + transitionCurve + ',' + 'top ' + duration + 's ' + transitionCurve;
  55. return zrUtil.map(vendors, function (vendorPrefix) {
  56. return vendorPrefix + 'transition:' + transitionText;
  57. }).join(';');
  58. }
  59. /**
  60. * @param {Object} textStyle
  61. * @return {string}
  62. * @inner
  63. */
  64. function assembleFont(textStyleModel) {
  65. var cssText = [];
  66. var fontSize = textStyleModel.get('fontSize');
  67. var color = textStyleModel.getTextColor();
  68. color && cssText.push('color:' + color);
  69. cssText.push('font:' + textStyleModel.getFont());
  70. fontSize && cssText.push('line-height:' + Math.round(fontSize * 3 / 2) + 'px');
  71. each(['decoration', 'align'], function (name) {
  72. var val = textStyleModel.get(name);
  73. val && cssText.push('text-' + name + ':' + val);
  74. });
  75. return cssText.join(';');
  76. }
  77. /**
  78. * @param {Object} tooltipModel
  79. * @return {string}
  80. * @inner
  81. */
  82. function assembleCssText(tooltipModel) {
  83. var cssText = [];
  84. var transitionDuration = tooltipModel.get('transitionDuration');
  85. var backgroundColor = tooltipModel.get('backgroundColor');
  86. var textStyleModel = tooltipModel.getModel('textStyle');
  87. var padding = tooltipModel.get('padding'); // Animation transition. Do not animate when transitionDuration is 0.
  88. transitionDuration && cssText.push(assembleTransition(transitionDuration));
  89. if (backgroundColor) {
  90. if (env.canvasSupported) {
  91. cssText.push('background-Color:' + backgroundColor);
  92. } else {
  93. // for ie
  94. cssText.push('background-Color:#' + zrColor.toHex(backgroundColor));
  95. cssText.push('filter:alpha(opacity=70)');
  96. }
  97. } // Border style
  98. each(['width', 'color', 'radius'], function (name) {
  99. var borderName = 'border-' + name;
  100. var camelCase = toCamelCase(borderName);
  101. var val = tooltipModel.get(camelCase);
  102. val != null && cssText.push(borderName + ':' + val + (name === 'color' ? '' : 'px'));
  103. }); // Text style
  104. cssText.push(assembleFont(textStyleModel)); // Padding
  105. if (padding != null) {
  106. cssText.push('padding:' + formatUtil.normalizeCssArray(padding).join('px ') + 'px');
  107. }
  108. return cssText.join(';') + ';';
  109. } // If not able to make, do not modify the input `out`.
  110. function makeStyleCoord(out, zr, appendToBody, zrX, zrY) {
  111. var zrPainter = zr && zr.painter;
  112. if (appendToBody) {
  113. var zrViewportRoot = zrPainter && zrPainter.getViewportRoot();
  114. if (zrViewportRoot) {
  115. // Some APPs might use scale on body, so we support CSS transform here.
  116. domUtil.transformLocalCoord(out, zrViewportRoot, document.body, zrX, zrY);
  117. }
  118. } else {
  119. out[0] = zrX;
  120. out[1] = zrY; // xy should be based on canvas root. But tooltipContent is
  121. // the sibling of canvas root. So padding of ec container
  122. // should be considered here.
  123. var viewportRootOffset = zrPainter && zrPainter.getViewportRootOffset();
  124. if (viewportRootOffset) {
  125. out[0] += viewportRootOffset.offsetLeft;
  126. out[1] += viewportRootOffset.offsetTop;
  127. }
  128. }
  129. }
  130. /**
  131. * @alias module:echarts/component/tooltip/TooltipContent
  132. * @param {HTMLElement} container
  133. * @param {ExtensionAPI} api
  134. * @param {Object} [opt]
  135. * @param {boolean} [opt.appendToBody]
  136. * `false`: the DOM element will be inside the container. Default value.
  137. * `true`: the DOM element will be appended to HTML body, which avoid
  138. * some overflow clip but intrude outside of the container.
  139. * @constructor
  140. */
  141. function TooltipContent(container, api, opt) {
  142. if (env.wxa) {
  143. return null;
  144. }
  145. var el = document.createElement('div');
  146. el.domBelongToZr = true;
  147. this.el = el;
  148. var zr = this._zr = api.getZr();
  149. var appendToBody = this._appendToBody = opt && opt.appendToBody;
  150. this._styleCoord = [0, 0];
  151. makeStyleCoord(this._styleCoord, zr, appendToBody, api.getWidth() / 2, api.getHeight() / 2);
  152. if (appendToBody) {
  153. document.body.appendChild(el);
  154. } else {
  155. container.appendChild(el);
  156. }
  157. this._container = container;
  158. this._show = false;
  159. /**
  160. * @private
  161. */
  162. this._hideTimeout; // FIXME
  163. // Is it needed to trigger zr event manually if
  164. // the browser do not support `pointer-events: none`.
  165. var self = this;
  166. el.onmouseenter = function () {
  167. // clear the timeout in hideLater and keep showing tooltip
  168. if (self._enterable) {
  169. clearTimeout(self._hideTimeout);
  170. self._show = true;
  171. }
  172. self._inContent = true;
  173. };
  174. el.onmousemove = function (e) {
  175. e = e || window.event;
  176. if (!self._enterable) {
  177. // `pointer-events: none` is set to tooltip content div
  178. // if `enterable` is set as `false`, and `el.onmousemove`
  179. // can not be triggered. But in browser that do not
  180. // support `pointer-events`, we need to do this:
  181. // Try trigger zrender event to avoid mouse
  182. // in and out shape too frequently
  183. var handler = zr.handler;
  184. var zrViewportRoot = zr.painter.getViewportRoot();
  185. eventUtil.normalizeEvent(zrViewportRoot, e, true);
  186. handler.dispatch('mousemove', e);
  187. }
  188. };
  189. el.onmouseleave = function () {
  190. if (self._enterable) {
  191. if (self._show) {
  192. self.hideLater(self._hideDelay);
  193. }
  194. }
  195. self._inContent = false;
  196. };
  197. }
  198. TooltipContent.prototype = {
  199. constructor: TooltipContent,
  200. /**
  201. * @private
  202. * @type {boolean}
  203. */
  204. _enterable: true,
  205. /**
  206. * Update when tooltip is rendered
  207. */
  208. update: function () {
  209. // FIXME
  210. // Move this logic to ec main?
  211. var container = this._container;
  212. var stl = container.currentStyle || document.defaultView.getComputedStyle(container);
  213. var domStyle = container.style;
  214. if (domStyle.position !== 'absolute' && stl.position !== 'absolute') {
  215. domStyle.position = 'relative';
  216. } // Hide the tooltip
  217. // PENDING
  218. // this.hide();
  219. },
  220. show: function (tooltipModel) {
  221. clearTimeout(this._hideTimeout);
  222. var el = this.el;
  223. var styleCoord = this._styleCoord;
  224. el.style.cssText = gCssText + assembleCssText(tooltipModel) // Because of the reason described in:
  225. // http://stackoverflow.com/questions/21125587/css3-transition-not-working-in-chrome-anymore
  226. // we should set initial value to `left` and `top`.
  227. + ';left:' + styleCoord[0] + 'px;top:' + styleCoord[1] + 'px;' + (tooltipModel.get('extraCssText') || '');
  228. el.style.display = el.innerHTML ? 'block' : 'none'; // If mouse occsionally move over the tooltip, a mouseout event will be
  229. // triggered by canvas, and cuase some unexpectable result like dragging
  230. // stop, "unfocusAdjacency". Here `pointer-events: none` is used to solve
  231. // it. Although it is not suppored by IE8~IE10, fortunately it is a rare
  232. // scenario.
  233. el.style.pointerEvents = this._enterable ? 'auto' : 'none';
  234. this._show = true;
  235. },
  236. setContent: function (content) {
  237. this.el.innerHTML = content == null ? '' : content;
  238. },
  239. setEnterable: function (enterable) {
  240. this._enterable = enterable;
  241. },
  242. getSize: function () {
  243. var el = this.el;
  244. return [el.clientWidth, el.clientHeight];
  245. },
  246. moveTo: function (zrX, zrY) {
  247. var styleCoord = this._styleCoord;
  248. makeStyleCoord(styleCoord, this._zr, this._appendToBody, zrX, zrY);
  249. var style = this.el.style;
  250. style.left = styleCoord[0] + 'px';
  251. style.top = styleCoord[1] + 'px';
  252. },
  253. hide: function () {
  254. this.el.style.display = 'none';
  255. this._show = false;
  256. },
  257. hideLater: function (time) {
  258. if (this._show && !(this._inContent && this._enterable)) {
  259. if (time) {
  260. this._hideDelay = time; // Set show false to avoid invoke hideLater mutiple times
  261. this._show = false;
  262. this._hideTimeout = setTimeout(zrUtil.bind(this.hide, this), time);
  263. } else {
  264. this.hide();
  265. }
  266. }
  267. },
  268. isShow: function () {
  269. return this._show;
  270. },
  271. dispose: function () {
  272. this.el.parentNode.removeChild(this.el);
  273. },
  274. getOuterSize: function () {
  275. var width = this.el.clientWidth;
  276. var height = this.el.clientHeight; // Consider browser compatibility.
  277. // IE8 does not support getComputedStyle.
  278. if (document.defaultView && document.defaultView.getComputedStyle) {
  279. var stl = document.defaultView.getComputedStyle(this.el);
  280. if (stl) {
  281. width += parseInt(stl.borderLeftWidth, 10) + parseInt(stl.borderRightWidth, 10);
  282. height += parseInt(stl.borderTopWidth, 10) + parseInt(stl.borderBottomWidth, 10);
  283. }
  284. }
  285. return {
  286. width: width,
  287. height: height
  288. };
  289. }
  290. };
  291. var _default = TooltipContent;
  292. module.exports = _default;