Breadcrumb.js 7.0 KB

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