ToolboxView.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. var echarts = require("../../echarts");
  20. var zrUtil = require("zrender/lib/core/util");
  21. var textContain = require("zrender/lib/contain/text");
  22. var featureManager = require("./featureManager");
  23. var graphic = require("../../util/graphic");
  24. var Model = require("../../model/Model");
  25. var DataDiffer = require("../../data/DataDiffer");
  26. var listComponentHelper = require("../helper/listComponent");
  27. /*
  28. * Licensed to the Apache Software Foundation (ASF) under one
  29. * or more contributor license agreements. See the NOTICE file
  30. * distributed with this work for additional information
  31. * regarding copyright ownership. The ASF licenses this file
  32. * to you under the Apache License, Version 2.0 (the
  33. * "License"); you may not use this file except in compliance
  34. * with the License. You may obtain a copy of the License at
  35. *
  36. * http://www.apache.org/licenses/LICENSE-2.0
  37. *
  38. * Unless required by applicable law or agreed to in writing,
  39. * software distributed under the License is distributed on an
  40. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  41. * KIND, either express or implied. See the License for the
  42. * specific language governing permissions and limitations
  43. * under the License.
  44. */
  45. var _default = echarts.extendComponentView({
  46. type: 'toolbox',
  47. render: function (toolboxModel, ecModel, api, payload) {
  48. var group = this.group;
  49. group.removeAll();
  50. if (!toolboxModel.get('show')) {
  51. return;
  52. }
  53. var itemSize = +toolboxModel.get('itemSize');
  54. var featureOpts = toolboxModel.get('feature') || {};
  55. var features = this._features || (this._features = {});
  56. var featureNames = [];
  57. zrUtil.each(featureOpts, function (opt, name) {
  58. featureNames.push(name);
  59. });
  60. new DataDiffer(this._featureNames || [], featureNames).add(processFeature).update(processFeature).remove(zrUtil.curry(processFeature, null)).execute(); // Keep for diff.
  61. this._featureNames = featureNames;
  62. function processFeature(newIndex, oldIndex) {
  63. var featureName = featureNames[newIndex];
  64. var oldName = featureNames[oldIndex];
  65. var featureOpt = featureOpts[featureName];
  66. var featureModel = new Model(featureOpt, toolboxModel, toolboxModel.ecModel);
  67. var feature; // FIX#11236, merge feature title from MagicType newOption. TODO: consider seriesIndex ?
  68. if (payload && payload.newTitle != null && payload.featureName === featureName) {
  69. featureOpt.title = payload.newTitle;
  70. }
  71. if (featureName && !oldName) {
  72. // Create
  73. if (isUserFeatureName(featureName)) {
  74. feature = {
  75. model: featureModel,
  76. onclick: featureModel.option.onclick,
  77. featureName: featureName
  78. };
  79. } else {
  80. var Feature = featureManager.get(featureName);
  81. if (!Feature) {
  82. return;
  83. }
  84. feature = new Feature(featureModel, ecModel, api);
  85. }
  86. features[featureName] = feature;
  87. } else {
  88. feature = features[oldName]; // If feature does not exsit.
  89. if (!feature) {
  90. return;
  91. }
  92. feature.model = featureModel;
  93. feature.ecModel = ecModel;
  94. feature.api = api;
  95. }
  96. if (!featureName && oldName) {
  97. feature.dispose && feature.dispose(ecModel, api);
  98. return;
  99. }
  100. if (!featureModel.get('show') || feature.unusable) {
  101. feature.remove && feature.remove(ecModel, api);
  102. return;
  103. }
  104. createIconPaths(featureModel, feature, featureName);
  105. featureModel.setIconStatus = function (iconName, status) {
  106. var option = this.option;
  107. var iconPaths = this.iconPaths;
  108. option.iconStatus = option.iconStatus || {};
  109. option.iconStatus[iconName] = status; // FIXME
  110. iconPaths[iconName] && iconPaths[iconName].trigger(status);
  111. };
  112. if (feature.render) {
  113. feature.render(featureModel, ecModel, api, payload);
  114. }
  115. }
  116. function createIconPaths(featureModel, feature, featureName) {
  117. var iconStyleModel = featureModel.getModel('iconStyle');
  118. var iconStyleEmphasisModel = featureModel.getModel('emphasis.iconStyle'); // If one feature has mutiple icon. they are orginaized as
  119. // {
  120. // icon: {
  121. // foo: '',
  122. // bar: ''
  123. // },
  124. // title: {
  125. // foo: '',
  126. // bar: ''
  127. // }
  128. // }
  129. var icons = feature.getIcons ? feature.getIcons() : featureModel.get('icon');
  130. var titles = featureModel.get('title') || {};
  131. if (typeof icons === 'string') {
  132. var icon = icons;
  133. var title = titles;
  134. icons = {};
  135. titles = {};
  136. icons[featureName] = icon;
  137. titles[featureName] = title;
  138. }
  139. var iconPaths = featureModel.iconPaths = {};
  140. zrUtil.each(icons, function (iconStr, iconName) {
  141. var path = graphic.createIcon(iconStr, {}, {
  142. x: -itemSize / 2,
  143. y: -itemSize / 2,
  144. width: itemSize,
  145. height: itemSize
  146. });
  147. path.setStyle(iconStyleModel.getItemStyle());
  148. path.hoverStyle = iconStyleEmphasisModel.getItemStyle(); // Text position calculation
  149. path.setStyle({
  150. text: titles[iconName],
  151. textAlign: iconStyleEmphasisModel.get('textAlign'),
  152. textBorderRadius: iconStyleEmphasisModel.get('textBorderRadius'),
  153. textPadding: iconStyleEmphasisModel.get('textPadding'),
  154. textFill: null
  155. });
  156. var tooltipModel = toolboxModel.getModel('tooltip');
  157. if (tooltipModel && tooltipModel.get('show')) {
  158. path.attr('tooltip', zrUtil.extend({
  159. content: titles[iconName],
  160. formatter: tooltipModel.get('formatter', true) || function () {
  161. return titles[iconName];
  162. },
  163. formatterParams: {
  164. componentType: 'toolbox',
  165. name: iconName,
  166. title: titles[iconName],
  167. $vars: ['name', 'title']
  168. },
  169. position: tooltipModel.get('position', true) || 'bottom'
  170. }, tooltipModel.option));
  171. }
  172. graphic.setHoverStyle(path);
  173. if (toolboxModel.get('showTitle')) {
  174. path.__title = titles[iconName];
  175. path.on('mouseover', function () {
  176. // Should not reuse above hoverStyle, which might be modified.
  177. var hoverStyle = iconStyleEmphasisModel.getItemStyle();
  178. var defaultTextPosition = toolboxModel.get('orient') === 'vertical' ? toolboxModel.get('right') == null ? 'right' : 'left' : toolboxModel.get('bottom') == null ? 'bottom' : 'top';
  179. path.setStyle({
  180. textFill: iconStyleEmphasisModel.get('textFill') || hoverStyle.fill || hoverStyle.stroke || '#000',
  181. textBackgroundColor: iconStyleEmphasisModel.get('textBackgroundColor'),
  182. textPosition: iconStyleEmphasisModel.get('textPosition') || defaultTextPosition
  183. });
  184. }).on('mouseout', function () {
  185. path.setStyle({
  186. textFill: null,
  187. textBackgroundColor: null
  188. });
  189. });
  190. }
  191. path.trigger(featureModel.get('iconStatus.' + iconName) || 'normal');
  192. group.add(path);
  193. path.on('click', zrUtil.bind(feature.onclick, feature, ecModel, api, iconName));
  194. iconPaths[iconName] = path;
  195. });
  196. }
  197. listComponentHelper.layout(group, toolboxModel, api); // Render background after group is layout
  198. // FIXME
  199. group.add(listComponentHelper.makeBackground(group.getBoundingRect(), toolboxModel)); // Adjust icon title positions to avoid them out of screen
  200. group.eachChild(function (icon) {
  201. var titleText = icon.__title;
  202. var hoverStyle = icon.hoverStyle; // May be background element
  203. if (hoverStyle && titleText) {
  204. var rect = textContain.getBoundingRect(titleText, textContain.makeFont(hoverStyle));
  205. var offsetX = icon.position[0] + group.position[0];
  206. var offsetY = icon.position[1] + group.position[1] + itemSize;
  207. var needPutOnTop = false;
  208. if (offsetY + rect.height > api.getHeight()) {
  209. hoverStyle.textPosition = 'top';
  210. needPutOnTop = true;
  211. }
  212. var topOffset = needPutOnTop ? -5 - rect.height : itemSize + 8;
  213. if (offsetX + rect.width / 2 > api.getWidth()) {
  214. hoverStyle.textPosition = ['100%', topOffset];
  215. hoverStyle.textAlign = 'right';
  216. } else if (offsetX - rect.width / 2 < 0) {
  217. hoverStyle.textPosition = [0, topOffset];
  218. hoverStyle.textAlign = 'left';
  219. }
  220. }
  221. });
  222. },
  223. updateView: function (toolboxModel, ecModel, api, payload) {
  224. zrUtil.each(this._features, function (feature) {
  225. feature.updateView && feature.updateView(feature.model, ecModel, api, payload);
  226. });
  227. },
  228. // updateLayout: function (toolboxModel, ecModel, api, payload) {
  229. // zrUtil.each(this._features, function (feature) {
  230. // feature.updateLayout && feature.updateLayout(feature.model, ecModel, api, payload);
  231. // });
  232. // },
  233. remove: function (ecModel, api) {
  234. zrUtil.each(this._features, function (feature) {
  235. feature.remove && feature.remove(ecModel, api);
  236. });
  237. this.group.removeAll();
  238. },
  239. dispose: function (ecModel, api) {
  240. zrUtil.each(this._features, function (feature) {
  241. feature.dispose && feature.dispose(ecModel, api);
  242. });
  243. }
  244. });
  245. function isUserFeatureName(featureName) {
  246. return featureName.indexOf('my') === 0;
  247. }
  248. module.exports = _default;