ToolboxView.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. var echarts = require("../../echarts");
  2. var zrUtil = require("zrender/lib/core/util");
  3. var textContain = require("zrender/lib/contain/text");
  4. var featureManager = require("./featureManager");
  5. var graphic = require("../../util/graphic");
  6. var Model = require("../../model/Model");
  7. var DataDiffer = require("../../data/DataDiffer");
  8. var listComponentHelper = require("../helper/listComponent");
  9. var _default = echarts.extendComponentView({
  10. type: 'toolbox',
  11. render: function (toolboxModel, ecModel, api, payload) {
  12. var group = this.group;
  13. group.removeAll();
  14. if (!toolboxModel.get('show')) {
  15. return;
  16. }
  17. var itemSize = +toolboxModel.get('itemSize');
  18. var featureOpts = toolboxModel.get('feature') || {};
  19. var features = this._features || (this._features = {});
  20. var featureNames = [];
  21. zrUtil.each(featureOpts, function (opt, name) {
  22. featureNames.push(name);
  23. });
  24. new DataDiffer(this._featureNames || [], featureNames).add(processFeature).update(processFeature).remove(zrUtil.curry(processFeature, null)).execute(); // Keep for diff.
  25. this._featureNames = featureNames;
  26. function processFeature(newIndex, oldIndex) {
  27. var featureName = featureNames[newIndex];
  28. var oldName = featureNames[oldIndex];
  29. var featureOpt = featureOpts[featureName];
  30. var featureModel = new Model(featureOpt, toolboxModel, toolboxModel.ecModel);
  31. var feature;
  32. if (featureName && !oldName) {
  33. // Create
  34. if (isUserFeatureName(featureName)) {
  35. feature = {
  36. model: featureModel,
  37. onclick: featureModel.option.onclick,
  38. featureName: featureName
  39. };
  40. } else {
  41. var Feature = featureManager.get(featureName);
  42. if (!Feature) {
  43. return;
  44. }
  45. feature = new Feature(featureModel, ecModel, api);
  46. }
  47. features[featureName] = feature;
  48. } else {
  49. feature = features[oldName]; // If feature does not exsit.
  50. if (!feature) {
  51. return;
  52. }
  53. feature.model = featureModel;
  54. feature.ecModel = ecModel;
  55. feature.api = api;
  56. }
  57. if (!featureName && oldName) {
  58. feature.dispose && feature.dispose(ecModel, api);
  59. return;
  60. }
  61. if (!featureModel.get('show') || feature.unusable) {
  62. feature.remove && feature.remove(ecModel, api);
  63. return;
  64. }
  65. createIconPaths(featureModel, feature, featureName);
  66. featureModel.setIconStatus = function (iconName, status) {
  67. var option = this.option;
  68. var iconPaths = this.iconPaths;
  69. option.iconStatus = option.iconStatus || {};
  70. option.iconStatus[iconName] = status; // FIXME
  71. iconPaths[iconName] && iconPaths[iconName].trigger(status);
  72. };
  73. if (feature.render) {
  74. feature.render(featureModel, ecModel, api, payload);
  75. }
  76. }
  77. function createIconPaths(featureModel, feature, featureName) {
  78. var iconStyleModel = featureModel.getModel('iconStyle'); // If one feature has mutiple icon. they are orginaized as
  79. // {
  80. // icon: {
  81. // foo: '',
  82. // bar: ''
  83. // },
  84. // title: {
  85. // foo: '',
  86. // bar: ''
  87. // }
  88. // }
  89. var icons = feature.getIcons ? feature.getIcons() : featureModel.get('icon');
  90. var titles = featureModel.get('title') || {};
  91. if (typeof icons === 'string') {
  92. var icon = icons;
  93. var title = titles;
  94. icons = {};
  95. titles = {};
  96. icons[featureName] = icon;
  97. titles[featureName] = title;
  98. }
  99. var iconPaths = featureModel.iconPaths = {};
  100. zrUtil.each(icons, function (iconStr, iconName) {
  101. var path = graphic.createIcon(iconStr, {}, {
  102. x: -itemSize / 2,
  103. y: -itemSize / 2,
  104. width: itemSize,
  105. height: itemSize
  106. });
  107. path.setStyle(iconStyleModel.getModel('normal').getItemStyle());
  108. path.hoverStyle = iconStyleModel.getModel('emphasis').getItemStyle();
  109. graphic.setHoverStyle(path);
  110. if (toolboxModel.get('showTitle')) {
  111. path.__title = titles[iconName];
  112. path.on('mouseover', function () {
  113. // Should not reuse above hoverStyle, which might be modified.
  114. var hoverStyle = iconStyleModel.getModel('emphasis').getItemStyle();
  115. path.setStyle({
  116. text: titles[iconName],
  117. textPosition: hoverStyle.textPosition || 'bottom',
  118. textFill: hoverStyle.fill || hoverStyle.stroke || '#000',
  119. textAlign: hoverStyle.textAlign || 'center'
  120. });
  121. }).on('mouseout', function () {
  122. path.setStyle({
  123. textFill: null
  124. });
  125. });
  126. }
  127. path.trigger(featureModel.get('iconStatus.' + iconName) || 'normal');
  128. group.add(path);
  129. path.on('click', zrUtil.bind(feature.onclick, feature, ecModel, api, iconName));
  130. iconPaths[iconName] = path;
  131. });
  132. }
  133. listComponentHelper.layout(group, toolboxModel, api); // Render background after group is layout
  134. // FIXME
  135. group.add(listComponentHelper.makeBackground(group.getBoundingRect(), toolboxModel)); // Adjust icon title positions to avoid them out of screen
  136. group.eachChild(function (icon) {
  137. var titleText = icon.__title;
  138. var hoverStyle = icon.hoverStyle; // May be background element
  139. if (hoverStyle && titleText) {
  140. var rect = textContain.getBoundingRect(titleText, textContain.makeFont(hoverStyle));
  141. var offsetX = icon.position[0] + group.position[0];
  142. var offsetY = icon.position[1] + group.position[1] + itemSize;
  143. var needPutOnTop = false;
  144. if (offsetY + rect.height > api.getHeight()) {
  145. hoverStyle.textPosition = 'top';
  146. needPutOnTop = true;
  147. }
  148. var topOffset = needPutOnTop ? -5 - rect.height : itemSize + 8;
  149. if (offsetX + rect.width / 2 > api.getWidth()) {
  150. hoverStyle.textPosition = ['100%', topOffset];
  151. hoverStyle.textAlign = 'right';
  152. } else if (offsetX - rect.width / 2 < 0) {
  153. hoverStyle.textPosition = [0, topOffset];
  154. hoverStyle.textAlign = 'left';
  155. }
  156. }
  157. });
  158. },
  159. updateView: function (toolboxModel, ecModel, api, payload) {
  160. zrUtil.each(this._features, function (feature) {
  161. feature.updateView && feature.updateView(feature.model, ecModel, api, payload);
  162. });
  163. },
  164. updateLayout: function (toolboxModel, ecModel, api, payload) {
  165. zrUtil.each(this._features, function (feature) {
  166. feature.updateLayout && feature.updateLayout(feature.model, ecModel, api, payload);
  167. });
  168. },
  169. remove: function (ecModel, api) {
  170. zrUtil.each(this._features, function (feature) {
  171. feature.remove && feature.remove(ecModel, api);
  172. });
  173. this.group.removeAll();
  174. },
  175. dispose: function (ecModel, api) {
  176. zrUtil.each(this._features, function (feature) {
  177. feature.dispose && feature.dispose(ecModel, api);
  178. });
  179. }
  180. });
  181. function isUserFeatureName(featureName) {
  182. return featureName.indexOf('my') === 0;
  183. }
  184. module.exports = _default;