ToolboxView.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import * as textContain from 'zrender/lib/contain/text.js';
  43. import * as graphic from '../../util/graphic.js';
  44. import { enterEmphasis, leaveEmphasis } from '../../util/states.js';
  45. import Model from '../../model/Model.js';
  46. import DataDiffer from '../../data/DataDiffer.js';
  47. import * as listComponentHelper from '../helper/listComponent.js';
  48. import ComponentView from '../../view/Component.js';
  49. import { ToolboxFeature, getFeature } from './featureManager.js';
  50. import { getUID } from '../../util/component.js';
  51. import ZRText from 'zrender/lib/graphic/Text.js';
  52. import { getFont } from '../../label/labelStyle.js';
  53. import { box, createBoxLayoutReference, getLayoutRect, positionElement } from '../../util/layout.js';
  54. import tokens from '../../visual/tokens.js';
  55. var ToolboxView = /** @class */function (_super) {
  56. __extends(ToolboxView, _super);
  57. function ToolboxView() {
  58. return _super !== null && _super.apply(this, arguments) || this;
  59. }
  60. ToolboxView.prototype.render = function (toolboxModel, ecModel, api, payload) {
  61. var group = this.group;
  62. group.removeAll();
  63. if (!toolboxModel.get('show')) {
  64. return;
  65. }
  66. var itemSize = +toolboxModel.get('itemSize');
  67. var isVertical = toolboxModel.get('orient') === 'vertical';
  68. var featureOpts = toolboxModel.get('feature') || {};
  69. var features = this._features || (this._features = {});
  70. var featureNames = [];
  71. zrUtil.each(featureOpts, function (opt, name) {
  72. featureNames.push(name);
  73. });
  74. new DataDiffer(this._featureNames || [], featureNames).add(processFeature).update(processFeature).remove(zrUtil.curry(processFeature, null)).execute();
  75. // Keep for diff.
  76. this._featureNames = featureNames;
  77. function processFeature(newIndex, oldIndex) {
  78. var featureName = featureNames[newIndex];
  79. var oldName = featureNames[oldIndex];
  80. var featureOpt = featureOpts[featureName];
  81. var featureModel = new Model(featureOpt, toolboxModel, toolboxModel.ecModel);
  82. var feature;
  83. // FIX#11236, merge feature title from MagicType newOption. TODO: consider seriesIndex ?
  84. if (payload && payload.newTitle != null && payload.featureName === featureName) {
  85. featureOpt.title = payload.newTitle;
  86. }
  87. if (featureName && !oldName) {
  88. // Create
  89. if (isUserFeatureName(featureName)) {
  90. feature = {
  91. onclick: featureModel.option.onclick,
  92. featureName: featureName
  93. };
  94. } else {
  95. var Feature = getFeature(featureName);
  96. if (!Feature) {
  97. return;
  98. }
  99. feature = new Feature();
  100. }
  101. features[featureName] = feature;
  102. } else {
  103. feature = features[oldName];
  104. // If feature does not exist.
  105. if (!feature) {
  106. return;
  107. }
  108. }
  109. feature.uid = getUID('toolbox-feature');
  110. feature.model = featureModel;
  111. feature.ecModel = ecModel;
  112. feature.api = api;
  113. var isToolboxFeature = feature instanceof ToolboxFeature;
  114. if (!featureName && oldName) {
  115. isToolboxFeature && feature.dispose && feature.dispose(ecModel, api);
  116. return;
  117. }
  118. if (!featureModel.get('show') || isToolboxFeature && feature.unusable) {
  119. isToolboxFeature && feature.remove && feature.remove(ecModel, api);
  120. return;
  121. }
  122. createIconPaths(featureModel, feature, featureName);
  123. featureModel.setIconStatus = function (iconName, status) {
  124. var option = this.option;
  125. var iconPaths = this.iconPaths;
  126. option.iconStatus = option.iconStatus || {};
  127. option.iconStatus[iconName] = status;
  128. if (iconPaths[iconName]) {
  129. (status === 'emphasis' ? enterEmphasis : leaveEmphasis)(iconPaths[iconName]);
  130. }
  131. };
  132. if (feature instanceof ToolboxFeature) {
  133. if (feature.render) {
  134. feature.render(featureModel, ecModel, api, payload);
  135. }
  136. }
  137. }
  138. function createIconPaths(featureModel, feature, featureName) {
  139. var iconStyleModel = featureModel.getModel('iconStyle');
  140. var iconStyleEmphasisModel = featureModel.getModel(['emphasis', 'iconStyle']);
  141. // If one feature has multiple icons, they are organized as
  142. // {
  143. // icon: {
  144. // foo: '',
  145. // bar: ''
  146. // },
  147. // title: {
  148. // foo: '',
  149. // bar: ''
  150. // }
  151. // }
  152. var icons = feature instanceof ToolboxFeature && feature.getIcons ? feature.getIcons() : featureModel.get('icon');
  153. var titles = featureModel.get('title') || {};
  154. var iconsMap;
  155. var titlesMap;
  156. if (zrUtil.isString(icons)) {
  157. iconsMap = {};
  158. iconsMap[featureName] = icons;
  159. } else {
  160. iconsMap = icons;
  161. }
  162. if (zrUtil.isString(titles)) {
  163. titlesMap = {};
  164. titlesMap[featureName] = titles;
  165. } else {
  166. titlesMap = titles;
  167. }
  168. var iconPaths = featureModel.iconPaths = {};
  169. zrUtil.each(iconsMap, function (iconStr, iconName) {
  170. var path = graphic.createIcon(iconStr, {}, {
  171. x: -itemSize / 2,
  172. y: -itemSize / 2,
  173. width: itemSize,
  174. height: itemSize
  175. }); // TODO handling image
  176. path.setStyle(iconStyleModel.getItemStyle());
  177. var pathEmphasisState = path.ensureState('emphasis');
  178. pathEmphasisState.style = iconStyleEmphasisModel.getItemStyle();
  179. // Text position calculation
  180. // TODO: extract `textStyle` from `iconStyle` and use `createTextStyle`
  181. var textContent = new ZRText({
  182. style: {
  183. text: titlesMap[iconName],
  184. align: iconStyleEmphasisModel.get('textAlign'),
  185. borderRadius: iconStyleEmphasisModel.get('textBorderRadius'),
  186. padding: iconStyleEmphasisModel.get('textPadding'),
  187. fill: null,
  188. font: getFont({
  189. fontStyle: iconStyleEmphasisModel.get('textFontStyle'),
  190. fontFamily: iconStyleEmphasisModel.get('textFontFamily'),
  191. fontSize: iconStyleEmphasisModel.get('textFontSize'),
  192. fontWeight: iconStyleEmphasisModel.get('textFontWeight')
  193. }, ecModel)
  194. },
  195. ignore: true
  196. });
  197. path.setTextContent(textContent);
  198. graphic.setTooltipConfig({
  199. el: path,
  200. componentModel: toolboxModel,
  201. itemName: iconName,
  202. formatterParamsExtra: {
  203. title: titlesMap[iconName]
  204. }
  205. });
  206. path.__title = titlesMap[iconName];
  207. path.on('mouseover', function () {
  208. // Should not reuse above hoverStyle, which might be modified.
  209. var hoverStyle = iconStyleEmphasisModel.getItemStyle();
  210. var defaultTextPosition = isVertical ? toolboxModel.get('right') == null && toolboxModel.get('left') !== 'right' ? 'right' : 'left' : toolboxModel.get('bottom') == null && toolboxModel.get('top') !== 'bottom' ? 'bottom' : 'top';
  211. textContent.setStyle({
  212. fill: iconStyleEmphasisModel.get('textFill') || hoverStyle.fill || hoverStyle.stroke || tokens.color.neutral99,
  213. backgroundColor: iconStyleEmphasisModel.get('textBackgroundColor')
  214. });
  215. path.setTextConfig({
  216. position: iconStyleEmphasisModel.get('textPosition') || defaultTextPosition
  217. });
  218. textContent.ignore = !toolboxModel.get('showTitle');
  219. // Use enterEmphasis and leaveEmphasis provide by ec.
  220. // There are flags managed by the echarts.
  221. api.enterEmphasis(this);
  222. }).on('mouseout', function () {
  223. if (featureModel.get(['iconStatus', iconName]) !== 'emphasis') {
  224. api.leaveEmphasis(this);
  225. }
  226. textContent.hide();
  227. });
  228. (featureModel.get(['iconStatus', iconName]) === 'emphasis' ? enterEmphasis : leaveEmphasis)(path);
  229. group.add(path);
  230. path.on('click', zrUtil.bind(feature.onclick, feature, ecModel, api, iconName));
  231. iconPaths[iconName] = path;
  232. });
  233. }
  234. var refContainer = createBoxLayoutReference(toolboxModel, api).refContainer;
  235. var boxLayoutParams = toolboxModel.getBoxLayoutParams();
  236. var padding = toolboxModel.get('padding');
  237. var viewRect = getLayoutRect(boxLayoutParams, refContainer, padding);
  238. box(toolboxModel.get('orient'), group, toolboxModel.get('itemGap'), viewRect.width, viewRect.height);
  239. positionElement(group, boxLayoutParams, refContainer, padding);
  240. // Render background after group is layout
  241. // FIXME
  242. group.add(listComponentHelper.makeBackground(group.getBoundingRect(), toolboxModel));
  243. // Adjust icon title positions to avoid them out of screen
  244. isVertical || group.eachChild(function (icon) {
  245. var titleText = icon.__title;
  246. // const hoverStyle = icon.hoverStyle;
  247. // TODO simplify code?
  248. var emphasisState = icon.ensureState('emphasis');
  249. var emphasisTextConfig = emphasisState.textConfig || (emphasisState.textConfig = {});
  250. var textContent = icon.getTextContent();
  251. var emphasisTextState = textContent && textContent.ensureState('emphasis');
  252. // May be background element
  253. if (emphasisTextState && !zrUtil.isFunction(emphasisTextState) && titleText) {
  254. var emphasisTextStyle = emphasisTextState.style || (emphasisTextState.style = {});
  255. var rect = textContain.getBoundingRect(titleText, ZRText.makeFont(emphasisTextStyle));
  256. var offsetX = icon.x + group.x;
  257. var offsetY = icon.y + group.y + itemSize;
  258. var needPutOnTop = false;
  259. if (offsetY + rect.height > api.getHeight()) {
  260. emphasisTextConfig.position = 'top';
  261. needPutOnTop = true;
  262. }
  263. var topOffset = needPutOnTop ? -5 - rect.height : itemSize + 10;
  264. if (offsetX + rect.width / 2 > api.getWidth()) {
  265. emphasisTextConfig.position = ['100%', topOffset];
  266. emphasisTextStyle.align = 'right';
  267. } else if (offsetX - rect.width / 2 < 0) {
  268. emphasisTextConfig.position = [0, topOffset];
  269. emphasisTextStyle.align = 'left';
  270. }
  271. }
  272. });
  273. };
  274. ToolboxView.prototype.updateView = function (toolboxModel, ecModel, api, payload) {
  275. zrUtil.each(this._features, function (feature) {
  276. feature instanceof ToolboxFeature && feature.updateView && feature.updateView(feature.model, ecModel, api, payload);
  277. });
  278. };
  279. // updateLayout(toolboxModel, ecModel, api, payload) {
  280. // zrUtil.each(this._features, function (feature) {
  281. // feature.updateLayout && feature.updateLayout(feature.model, ecModel, api, payload);
  282. // });
  283. // },
  284. ToolboxView.prototype.remove = function (ecModel, api) {
  285. zrUtil.each(this._features, function (feature) {
  286. feature instanceof ToolboxFeature && feature.remove && feature.remove(ecModel, api);
  287. });
  288. this.group.removeAll();
  289. };
  290. ToolboxView.prototype.dispose = function (ecModel, api) {
  291. zrUtil.each(this._features, function (feature) {
  292. feature instanceof ToolboxFeature && feature.dispose && feature.dispose(ecModel, api);
  293. });
  294. };
  295. ToolboxView.type = 'toolbox';
  296. return ToolboxView;
  297. }(ComponentView);
  298. function isUserFeatureName(featureName) {
  299. return featureName.indexOf('my') === 0;
  300. }
  301. export default ToolboxView;