TooltipRichContent.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 * as zrUtil from 'zrender/lib/core/util.js';
  41. import ZRText from 'zrender/lib/graphic/Text.js';
  42. import { getPaddingFromTooltipModel } from './tooltipMarkup.js';
  43. import { throwError } from '../../util/log.js';
  44. var TooltipRichContent =
  45. /** @class */
  46. function () {
  47. function TooltipRichContent(api) {
  48. this._show = false;
  49. this._styleCoord = [0, 0, 0, 0];
  50. this._enterable = true;
  51. this._zr = api.getZr();
  52. makeStyleCoord(this._styleCoord, this._zr, api.getWidth() / 2, api.getHeight() / 2);
  53. }
  54. /**
  55. * Update when tooltip is rendered
  56. */
  57. TooltipRichContent.prototype.update = function (tooltipModel) {
  58. var alwaysShowContent = tooltipModel.get('alwaysShowContent');
  59. alwaysShowContent && this._moveIfResized();
  60. };
  61. TooltipRichContent.prototype.show = function () {
  62. if (this._hideTimeout) {
  63. clearTimeout(this._hideTimeout);
  64. }
  65. this.el.show();
  66. this._show = true;
  67. };
  68. /**
  69. * Set tooltip content
  70. */
  71. TooltipRichContent.prototype.setContent = function (content, markupStyleCreator, tooltipModel, borderColor, arrowPosition) {
  72. var _this = this;
  73. if (zrUtil.isObject(content)) {
  74. throwError(process.env.NODE_ENV !== 'production' ? 'Passing DOM nodes as content is not supported in richText tooltip!' : '');
  75. }
  76. if (this.el) {
  77. this._zr.remove(this.el);
  78. }
  79. var textStyleModel = tooltipModel.getModel('textStyle');
  80. this.el = new ZRText({
  81. style: {
  82. rich: markupStyleCreator.richTextStyles,
  83. text: content,
  84. lineHeight: 22,
  85. borderWidth: 1,
  86. borderColor: borderColor,
  87. textShadowColor: textStyleModel.get('textShadowColor'),
  88. fill: tooltipModel.get(['textStyle', 'color']),
  89. padding: getPaddingFromTooltipModel(tooltipModel, 'richText'),
  90. verticalAlign: 'top',
  91. align: 'left'
  92. },
  93. z: tooltipModel.get('z')
  94. });
  95. zrUtil.each(['backgroundColor', 'borderRadius', 'shadowColor', 'shadowBlur', 'shadowOffsetX', 'shadowOffsetY'], function (propName) {
  96. _this.el.style[propName] = tooltipModel.get(propName);
  97. });
  98. zrUtil.each(['textShadowBlur', 'textShadowOffsetX', 'textShadowOffsetY'], function (propName) {
  99. _this.el.style[propName] = textStyleModel.get(propName) || 0;
  100. });
  101. this._zr.add(this.el);
  102. var self = this;
  103. this.el.on('mouseover', function () {
  104. // clear the timeout in hideLater and keep showing tooltip
  105. if (self._enterable) {
  106. clearTimeout(self._hideTimeout);
  107. self._show = true;
  108. }
  109. self._inContent = true;
  110. });
  111. this.el.on('mouseout', function () {
  112. if (self._enterable) {
  113. if (self._show) {
  114. self.hideLater(self._hideDelay);
  115. }
  116. }
  117. self._inContent = false;
  118. });
  119. };
  120. TooltipRichContent.prototype.setEnterable = function (enterable) {
  121. this._enterable = enterable;
  122. };
  123. TooltipRichContent.prototype.getSize = function () {
  124. var el = this.el;
  125. var bounding = this.el.getBoundingRect(); // bounding rect does not include shadow. For renderMode richText,
  126. // if overflow, it will be cut. So calculate them accurately.
  127. var shadowOuterSize = calcShadowOuterSize(el.style);
  128. return [bounding.width + shadowOuterSize.left + shadowOuterSize.right, bounding.height + shadowOuterSize.top + shadowOuterSize.bottom];
  129. };
  130. TooltipRichContent.prototype.moveTo = function (x, y) {
  131. var el = this.el;
  132. if (el) {
  133. var styleCoord = this._styleCoord;
  134. makeStyleCoord(styleCoord, this._zr, x, y);
  135. x = styleCoord[0];
  136. y = styleCoord[1];
  137. var style = el.style;
  138. var borderWidth = mathMaxWith0(style.borderWidth || 0);
  139. var shadowOuterSize = calcShadowOuterSize(style); // rich text x, y do not include border.
  140. el.x = x + borderWidth + shadowOuterSize.left;
  141. el.y = y + borderWidth + shadowOuterSize.top;
  142. el.markRedraw();
  143. }
  144. };
  145. /**
  146. * when `alwaysShowContent` is true,
  147. * move the tooltip after chart resized
  148. */
  149. TooltipRichContent.prototype._moveIfResized = function () {
  150. // The ratio of left to width
  151. var ratioX = this._styleCoord[2]; // The ratio of top to height
  152. var ratioY = this._styleCoord[3];
  153. this.moveTo(ratioX * this._zr.getWidth(), ratioY * this._zr.getHeight());
  154. };
  155. TooltipRichContent.prototype.hide = function () {
  156. if (this.el) {
  157. this.el.hide();
  158. }
  159. this._show = false;
  160. };
  161. TooltipRichContent.prototype.hideLater = function (time) {
  162. if (this._show && !(this._inContent && this._enterable)) {
  163. if (time) {
  164. this._hideDelay = time; // Set show false to avoid invoke hideLater multiple times
  165. this._show = false;
  166. this._hideTimeout = setTimeout(zrUtil.bind(this.hide, this), time);
  167. } else {
  168. this.hide();
  169. }
  170. }
  171. };
  172. TooltipRichContent.prototype.isShow = function () {
  173. return this._show;
  174. };
  175. TooltipRichContent.prototype.dispose = function () {
  176. this._zr.remove(this.el);
  177. };
  178. return TooltipRichContent;
  179. }();
  180. function mathMaxWith0(val) {
  181. return Math.max(0, val);
  182. }
  183. function calcShadowOuterSize(style) {
  184. var shadowBlur = mathMaxWith0(style.shadowBlur || 0);
  185. var shadowOffsetX = mathMaxWith0(style.shadowOffsetX || 0);
  186. var shadowOffsetY = mathMaxWith0(style.shadowOffsetY || 0);
  187. return {
  188. left: mathMaxWith0(shadowBlur - shadowOffsetX),
  189. right: mathMaxWith0(shadowBlur + shadowOffsetX),
  190. top: mathMaxWith0(shadowBlur - shadowOffsetY),
  191. bottom: mathMaxWith0(shadowBlur + shadowOffsetY)
  192. };
  193. }
  194. function makeStyleCoord(out, zr, zrX, zrY) {
  195. out[0] = zrX;
  196. out[1] = zrY;
  197. out[2] = out[0] / zr.getWidth();
  198. out[3] = out[1] / zr.getHeight();
  199. }
  200. export default TooltipRichContent;