ToolboxView.js 13 KB

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