ToolboxView.js 12 KB

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