ScrollableLegendView.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. var zrUtil = require("zrender/lib/core/util");
  2. var graphic = require("../../util/graphic");
  3. var layoutUtil = require("../../util/layout");
  4. var LegendView = require("./LegendView");
  5. /**
  6. * Separate legend and scrollable legend to reduce package size.
  7. */
  8. var Group = graphic.Group;
  9. var WH = ['width', 'height'];
  10. var XY = ['x', 'y'];
  11. var ScrollableLegendView = LegendView.extend({
  12. type: 'legend.scroll',
  13. newlineDisabled: true,
  14. init: function () {
  15. ScrollableLegendView.superCall(this, 'init');
  16. /**
  17. * @private
  18. * @type {number} For `scroll`.
  19. */
  20. this._currentIndex = 0;
  21. /**
  22. * @private
  23. * @type {module:zrender/container/Group}
  24. */
  25. this.group.add(this._containerGroup = new Group());
  26. this._containerGroup.add(this.getContentGroup());
  27. /**
  28. * @private
  29. * @type {module:zrender/container/Group}
  30. */
  31. this.group.add(this._controllerGroup = new Group());
  32. /**
  33. *
  34. * @private
  35. */
  36. this._showController;
  37. },
  38. /**
  39. * @override
  40. */
  41. resetInner: function () {
  42. ScrollableLegendView.superCall(this, 'resetInner');
  43. this._controllerGroup.removeAll();
  44. this._containerGroup.removeClipPath();
  45. this._containerGroup.__rectSize = null;
  46. },
  47. /**
  48. * @override
  49. */
  50. renderInner: function (itemAlign, legendModel, ecModel, api) {
  51. var me = this; // Render content items.
  52. ScrollableLegendView.superCall(this, 'renderInner', itemAlign, legendModel, ecModel, api);
  53. var controllerGroup = this._controllerGroup;
  54. var pageIconSize = legendModel.get('pageIconSize', true);
  55. if (!zrUtil.isArray(pageIconSize)) {
  56. pageIconSize = [pageIconSize, pageIconSize];
  57. }
  58. createPageButton('pagePrev', 0);
  59. var pageTextStyleModel = legendModel.getModel('pageTextStyle');
  60. controllerGroup.add(new graphic.Text({
  61. name: 'pageText',
  62. style: {
  63. textFill: pageTextStyleModel.getTextColor(),
  64. font: pageTextStyleModel.getFont(),
  65. textVerticalAlign: 'middle',
  66. textAlign: 'center'
  67. },
  68. silent: true
  69. }));
  70. createPageButton('pageNext', 1);
  71. function createPageButton(name, iconIdx) {
  72. var pageDataIndexName = name + 'DataIndex';
  73. var icon = graphic.createIcon(legendModel.get('pageIcons', true)[legendModel.getOrient().name][iconIdx], {
  74. // Buttons will be created in each render, so we do not need
  75. // to worry about avoiding using legendModel kept in scope.
  76. onclick: zrUtil.bind(me._pageGo, me, pageDataIndexName, legendModel, api)
  77. }, {
  78. x: -pageIconSize[0] / 2,
  79. y: -pageIconSize[1] / 2,
  80. width: pageIconSize[0],
  81. height: pageIconSize[1]
  82. });
  83. icon.name = name;
  84. controllerGroup.add(icon);
  85. }
  86. },
  87. /**
  88. * @override
  89. */
  90. layoutInner: function (legendModel, itemAlign, maxSize) {
  91. var contentGroup = this.getContentGroup();
  92. var containerGroup = this._containerGroup;
  93. var controllerGroup = this._controllerGroup;
  94. var orientIdx = legendModel.getOrient().index;
  95. var wh = WH[orientIdx];
  96. var hw = WH[1 - orientIdx];
  97. var yx = XY[1 - orientIdx]; // Place items in contentGroup.
  98. layoutUtil.box(legendModel.get('orient'), contentGroup, legendModel.get('itemGap'), !orientIdx ? null : maxSize.width, orientIdx ? null : maxSize.height);
  99. layoutUtil.box( // Buttons in controller are layout always horizontally.
  100. 'horizontal', controllerGroup, legendModel.get('pageButtonItemGap', true));
  101. var contentRect = contentGroup.getBoundingRect();
  102. var controllerRect = controllerGroup.getBoundingRect();
  103. var showController = this._showController = contentRect[wh] > maxSize[wh];
  104. var contentPos = [-contentRect.x, -contentRect.y]; // Remain contentPos when scroll animation perfroming.
  105. contentPos[orientIdx] = contentGroup.position[orientIdx]; // Layout container group based on 0.
  106. var containerPos = [0, 0];
  107. var controllerPos = [-controllerRect.x, -controllerRect.y];
  108. var pageButtonGap = zrUtil.retrieve2(legendModel.get('pageButtonGap', true), legendModel.get('itemGap', true)); // Place containerGroup and controllerGroup and contentGroup.
  109. if (showController) {
  110. var pageButtonPosition = legendModel.get('pageButtonPosition', true); // controller is on the right / bottom.
  111. if (pageButtonPosition === 'end') {
  112. controllerPos[orientIdx] += maxSize[wh] - controllerRect[wh];
  113. } // controller is on the left / top.
  114. else {
  115. containerPos[orientIdx] += controllerRect[wh] + pageButtonGap;
  116. }
  117. } // Always align controller to content as 'middle'.
  118. controllerPos[1 - orientIdx] += contentRect[hw] / 2 - controllerRect[hw] / 2;
  119. contentGroup.attr('position', contentPos);
  120. containerGroup.attr('position', containerPos);
  121. controllerGroup.attr('position', controllerPos); // Calculate `mainRect` and set `clipPath`.
  122. // mainRect should not be calculated by `this.group.getBoundingRect()`
  123. // for sake of the overflow.
  124. var mainRect = this.group.getBoundingRect();
  125. var mainRect = {
  126. x: 0,
  127. y: 0
  128. }; // Consider content may be overflow (should be clipped).
  129. mainRect[wh] = showController ? maxSize[wh] : contentRect[wh];
  130. mainRect[hw] = Math.max(contentRect[hw], controllerRect[hw]); // `containerRect[yx] + containerPos[1 - orientIdx]` is 0.
  131. mainRect[yx] = Math.min(0, controllerRect[yx] + controllerPos[1 - orientIdx]);
  132. containerGroup.__rectSize = maxSize[wh];
  133. if (showController) {
  134. var clipShape = {
  135. x: 0,
  136. y: 0
  137. };
  138. clipShape[wh] = Math.max(maxSize[wh] - controllerRect[wh] - pageButtonGap, 0);
  139. clipShape[hw] = mainRect[hw];
  140. containerGroup.setClipPath(new graphic.Rect({
  141. shape: clipShape
  142. })); // Consider content may be larger than container, container rect
  143. // can not be obtained from `containerGroup.getBoundingRect()`.
  144. containerGroup.__rectSize = clipShape[wh];
  145. } else {
  146. // Do not remove or ignore controller. Keep them set as place holders.
  147. controllerGroup.eachChild(function (child) {
  148. child.attr({
  149. invisible: true,
  150. silent: true
  151. });
  152. });
  153. } // Content translate animation.
  154. var pageInfo = this._getPageInfo(legendModel);
  155. pageInfo.pageIndex != null && graphic.updateProps(contentGroup, {
  156. position: pageInfo.contentPosition
  157. }, // When switch from "show controller" to "not show controller", view should be
  158. // updated immediately without animation, otherwise causes weird efffect.
  159. showController ? legendModel : false);
  160. this._updatePageInfoView(legendModel, pageInfo);
  161. return mainRect;
  162. },
  163. _pageGo: function (to, legendModel, api) {
  164. var scrollDataIndex = this._getPageInfo(legendModel)[to];
  165. scrollDataIndex != null && api.dispatchAction({
  166. type: 'legendScroll',
  167. scrollDataIndex: scrollDataIndex,
  168. legendId: legendModel.id
  169. });
  170. },
  171. _updatePageInfoView: function (legendModel, pageInfo) {
  172. var controllerGroup = this._controllerGroup;
  173. zrUtil.each(['pagePrev', 'pageNext'], function (name) {
  174. var canJump = pageInfo[name + 'DataIndex'] != null;
  175. var icon = controllerGroup.childOfName(name);
  176. if (icon) {
  177. icon.setStyle('fill', canJump ? legendModel.get('pageIconColor', true) : legendModel.get('pageIconInactiveColor', true));
  178. icon.cursor = canJump ? 'pointer' : 'default';
  179. }
  180. });
  181. var pageText = controllerGroup.childOfName('pageText');
  182. var pageFormatter = legendModel.get('pageFormatter');
  183. var pageIndex = pageInfo.pageIndex;
  184. var current = pageIndex != null ? pageIndex + 1 : 0;
  185. var total = pageInfo.pageCount;
  186. pageText && pageFormatter && pageText.setStyle('text', zrUtil.isString(pageFormatter) ? pageFormatter.replace('{current}', current).replace('{total}', total) : pageFormatter({
  187. current: current,
  188. total: total
  189. }));
  190. },
  191. /**
  192. * @param {module:echarts/model/Model} legendModel
  193. * @return {Object} {
  194. * contentPosition: Array.<number>, null when data item not found.
  195. * pageIndex: number, null when data item not found.
  196. * pageCount: number, always be a number, can be 0.
  197. * pagePrevDataIndex: number, null when no next page.
  198. * pageNextDataIndex: number, null when no previous page.
  199. * }
  200. */
  201. _getPageInfo: function (legendModel) {
  202. // Align left or top by the current dataIndex.
  203. var currDataIndex = legendModel.get('scrollDataIndex', true);
  204. var contentGroup = this.getContentGroup();
  205. var contentRect = contentGroup.getBoundingRect();
  206. var containerRectSize = this._containerGroup.__rectSize;
  207. var orientIdx = legendModel.getOrient().index;
  208. var wh = WH[orientIdx];
  209. var hw = WH[1 - orientIdx];
  210. var xy = XY[orientIdx];
  211. var contentPos = contentGroup.position.slice();
  212. var pageIndex;
  213. var pagePrevDataIndex;
  214. var pageNextDataIndex;
  215. var targetItemGroup;
  216. if (this._showController) {
  217. contentGroup.eachChild(function (child) {
  218. if (child.__legendDataIndex === currDataIndex) {
  219. targetItemGroup = child;
  220. }
  221. });
  222. } else {
  223. targetItemGroup = contentGroup.childAt(0);
  224. }
  225. var pageCount = containerRectSize ? Math.ceil(contentRect[wh] / containerRectSize) : 0;
  226. if (targetItemGroup) {
  227. var itemRect = targetItemGroup.getBoundingRect();
  228. var itemLoc = targetItemGroup.position[orientIdx] + itemRect[xy];
  229. contentPos[orientIdx] = -itemLoc - contentRect[xy];
  230. pageIndex = Math.floor(pageCount * (itemLoc + itemRect[xy] + containerRectSize / 2) / contentRect[wh]);
  231. pageIndex = contentRect[wh] && pageCount ? Math.max(0, Math.min(pageCount - 1, pageIndex)) : -1;
  232. var winRect = {
  233. x: 0,
  234. y: 0
  235. };
  236. winRect[wh] = containerRectSize;
  237. winRect[hw] = contentRect[hw];
  238. winRect[xy] = -contentPos[orientIdx] - contentRect[xy];
  239. var startIdx;
  240. var children = contentGroup.children();
  241. contentGroup.eachChild(function (child, index) {
  242. var itemRect = getItemRect(child);
  243. if (itemRect.intersect(winRect)) {
  244. startIdx == null && (startIdx = index); // It is user-friendly that the last item shown in the
  245. // current window is shown at the begining of next window.
  246. pageNextDataIndex = child.__legendDataIndex;
  247. } // If the last item is shown entirely, no next page.
  248. if (index === children.length - 1 && itemRect[xy] + itemRect[wh] <= winRect[xy] + winRect[wh]) {
  249. pageNextDataIndex = null;
  250. }
  251. }); // Always align based on the left/top most item, so the left/top most
  252. // item in the previous window is needed to be found here.
  253. if (startIdx != null) {
  254. var startItem = children[startIdx];
  255. var startRect = getItemRect(startItem);
  256. winRect[xy] = startRect[xy] + startRect[wh] - winRect[wh]; // If the first item is shown entirely, no previous page.
  257. if (startIdx <= 0 && startRect[xy] >= winRect[xy]) {
  258. pagePrevDataIndex = null;
  259. } else {
  260. while (startIdx > 0 && getItemRect(children[startIdx - 1]).intersect(winRect)) {
  261. startIdx--;
  262. }
  263. pagePrevDataIndex = children[startIdx].__legendDataIndex;
  264. }
  265. }
  266. }
  267. return {
  268. contentPosition: contentPos,
  269. pageIndex: pageIndex,
  270. pageCount: pageCount,
  271. pagePrevDataIndex: pagePrevDataIndex,
  272. pageNextDataIndex: pageNextDataIndex
  273. };
  274. function getItemRect(el) {
  275. var itemRect = el.getBoundingRect().clone();
  276. itemRect[xy] += el.position[orientIdx];
  277. return itemRect;
  278. }
  279. }
  280. });
  281. var _default = ScrollableLegendView;
  282. module.exports = _default;