TooltipContent.js 9.2 KB

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