ToolboxView.js 12 KB

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