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