Breadcrumb.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 graphic from '../../util/graphic.js';
  41. import { getECData } from '../../util/innerStore.js';
  42. import * as layout from '../../util/layout.js';
  43. import { wrapTreePathInfo } from '../helper/treeHelper.js';
  44. import { curry, defaults } from 'zrender/lib/core/util.js';
  45. import { convertOptionIdName } from '../../util/model.js';
  46. import { toggleHoverEmphasis, Z2_EMPHASIS_LIFT } from '../../util/states.js';
  47. import { createTextStyle } from '../../label/labelStyle.js';
  48. var TEXT_PADDING = 8;
  49. var ITEM_GAP = 8;
  50. var ARRAY_LENGTH = 5;
  51. var Breadcrumb =
  52. /** @class */
  53. function () {
  54. function Breadcrumb(containerGroup) {
  55. this.group = new graphic.Group();
  56. containerGroup.add(this.group);
  57. }
  58. Breadcrumb.prototype.render = function (seriesModel, api, targetNode, onSelect) {
  59. var model = seriesModel.getModel('breadcrumb');
  60. var thisGroup = this.group;
  61. thisGroup.removeAll();
  62. if (!model.get('show') || !targetNode) {
  63. return;
  64. }
  65. var normalStyleModel = model.getModel('itemStyle');
  66. var emphasisModel = model.getModel('emphasis');
  67. var textStyleModel = normalStyleModel.getModel('textStyle');
  68. var emphasisTextStyleModel = emphasisModel.getModel(['itemStyle', 'textStyle']);
  69. var layoutParam = {
  70. pos: {
  71. left: model.get('left'),
  72. right: model.get('right'),
  73. top: model.get('top'),
  74. bottom: model.get('bottom')
  75. },
  76. box: {
  77. width: api.getWidth(),
  78. height: api.getHeight()
  79. },
  80. emptyItemWidth: model.get('emptyItemWidth'),
  81. totalWidth: 0,
  82. renderList: []
  83. };
  84. this._prepare(targetNode, layoutParam, textStyleModel);
  85. this._renderContent(seriesModel, layoutParam, normalStyleModel, emphasisModel, textStyleModel, emphasisTextStyleModel, onSelect);
  86. layout.positionElement(thisGroup, layoutParam.pos, layoutParam.box);
  87. };
  88. /**
  89. * Prepare render list and total width
  90. * @private
  91. */
  92. Breadcrumb.prototype._prepare = function (targetNode, layoutParam, textStyleModel) {
  93. for (var node = targetNode; node; node = node.parentNode) {
  94. var text = convertOptionIdName(node.getModel().get('name'), '');
  95. var textRect = textStyleModel.getTextRect(text);
  96. var itemWidth = Math.max(textRect.width + TEXT_PADDING * 2, layoutParam.emptyItemWidth);
  97. layoutParam.totalWidth += itemWidth + ITEM_GAP;
  98. layoutParam.renderList.push({
  99. node: node,
  100. text: text,
  101. width: itemWidth
  102. });
  103. }
  104. };
  105. /**
  106. * @private
  107. */
  108. Breadcrumb.prototype._renderContent = function (seriesModel, layoutParam, normalStyleModel, emphasisModel, textStyleModel, emphasisTextStyleModel, onSelect) {
  109. // Start rendering.
  110. var lastX = 0;
  111. var emptyItemWidth = layoutParam.emptyItemWidth;
  112. var height = seriesModel.get(['breadcrumb', 'height']);
  113. var availableSize = layout.getAvailableSize(layoutParam.pos, layoutParam.box);
  114. var totalWidth = layoutParam.totalWidth;
  115. var renderList = layoutParam.renderList;
  116. var emphasisItemStyle = emphasisModel.getModel('itemStyle').getItemStyle();
  117. for (var i = renderList.length - 1; i >= 0; i--) {
  118. var item = renderList[i];
  119. var itemNode = item.node;
  120. var itemWidth = item.width;
  121. var text = item.text; // Hdie text and shorten width if necessary.
  122. if (totalWidth > availableSize.width) {
  123. totalWidth -= itemWidth - emptyItemWidth;
  124. itemWidth = emptyItemWidth;
  125. text = null;
  126. }
  127. var el = new graphic.Polygon({
  128. shape: {
  129. points: makeItemPoints(lastX, 0, itemWidth, height, i === renderList.length - 1, i === 0)
  130. },
  131. style: defaults(normalStyleModel.getItemStyle(), {
  132. lineJoin: 'bevel'
  133. }),
  134. textContent: new graphic.Text({
  135. style: createTextStyle(textStyleModel, {
  136. text: text
  137. })
  138. }),
  139. textConfig: {
  140. position: 'inside'
  141. },
  142. z2: Z2_EMPHASIS_LIFT * 1e4,
  143. onclick: curry(onSelect, itemNode)
  144. });
  145. el.disableLabelAnimation = true;
  146. el.getTextContent().ensureState('emphasis').style = createTextStyle(emphasisTextStyleModel, {
  147. text: text
  148. });
  149. el.ensureState('emphasis').style = emphasisItemStyle;
  150. toggleHoverEmphasis(el, emphasisModel.get('focus'), emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  151. this.group.add(el);
  152. packEventData(el, seriesModel, itemNode);
  153. lastX += itemWidth + ITEM_GAP;
  154. }
  155. };
  156. Breadcrumb.prototype.remove = function () {
  157. this.group.removeAll();
  158. };
  159. return Breadcrumb;
  160. }();
  161. function makeItemPoints(x, y, itemWidth, itemHeight, head, tail) {
  162. var points = [[head ? x : x - ARRAY_LENGTH, y], [x + itemWidth, y], [x + itemWidth, y + itemHeight], [head ? x : x - ARRAY_LENGTH, y + itemHeight]];
  163. !tail && points.splice(2, 0, [x + itemWidth + ARRAY_LENGTH, y + itemHeight / 2]);
  164. !head && points.push([x, y + itemHeight / 2]);
  165. return points;
  166. } // Package custom mouse event.
  167. function packEventData(el, seriesModel, itemNode) {
  168. getECData(el).eventData = {
  169. componentType: 'series',
  170. componentSubType: 'treemap',
  171. componentIndex: seriesModel.componentIndex,
  172. seriesIndex: seriesModel.seriesIndex,
  173. seriesName: seriesModel.name,
  174. seriesType: 'treemap',
  175. selfType: 'breadcrumb',
  176. nodeData: {
  177. dataIndex: itemNode && itemNode.dataIndex,
  178. name: itemNode && itemNode.name
  179. },
  180. treePathInfo: itemNode && wrapTreePathInfo(itemNode, seriesModel)
  181. };
  182. }
  183. export default Breadcrumb;