index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import { __assign, __extends, __read, __rest, __spreadArray } from "tslib";
  2. import { ElementEvent } from '@antv/g';
  3. import { clamp, debounce } from '@antv/util';
  4. import { animate, onAnimateFinished } from '../../animation';
  5. import { GUI } from '../../core';
  6. import { Group } from '../../shapes';
  7. import { BBox, classNames, hide, scaleToPixel, select, show, splitStyle, subStyleProps, transpose, visibility, } from '../../util';
  8. import { button } from '../marker/symbol';
  9. var CLASS_NAMES = classNames({
  10. prevBtnGroup: 'prev-btn-group',
  11. prevBtn: 'prev-btn',
  12. nextBtnGroup: 'next-btn-group',
  13. nextBtn: 'next-btn',
  14. pageInfoGroup: 'page-info-group',
  15. pageInfo: 'page-info',
  16. playWindow: 'play-window',
  17. contentGroup: 'content-group',
  18. controller: 'controller',
  19. clipPath: 'clip-path',
  20. }, 'navigator');
  21. var Navigator = /** @class */ (function (_super) {
  22. __extends(Navigator, _super);
  23. function Navigator(options) {
  24. var _this = _super.call(this, options, {
  25. animate: {
  26. easing: 'linear',
  27. duration: 200,
  28. fill: 'both',
  29. },
  30. buttonCursor: 'pointer',
  31. buttonFill: 'black',
  32. buttonPath: button(0, 0, 6),
  33. buttonSize: 12,
  34. controllerPadding: 5,
  35. controllerSpacing: 5,
  36. formatter: function (curr, total) { return "".concat(curr, "/").concat(total); },
  37. defaultPage: 0,
  38. loop: false,
  39. orientation: 'horizontal',
  40. pageNumFill: 'black',
  41. pageNumFontSize: 12,
  42. pageNumTextAlign: 'start',
  43. pageNumTextBaseline: 'middle',
  44. }) || this;
  45. _this.playState = 'idle';
  46. _this.contentGroup = _this.appendChild(new Group({ class: CLASS_NAMES.contentGroup.name }));
  47. _this.playWindow = _this.contentGroup.appendChild(new Group({ class: CLASS_NAMES.playWindow.name }));
  48. _this.innerCurrPage = _this.defaultPage;
  49. return _this;
  50. }
  51. Object.defineProperty(Navigator.prototype, "defaultPage", {
  52. get: function () {
  53. var defaultPage = this.attributes.defaultPage;
  54. return clamp(defaultPage, 0, Math.max(this.pageViews.length - 1, 0));
  55. },
  56. enumerable: false,
  57. configurable: true
  58. });
  59. Object.defineProperty(Navigator.prototype, "pageViews", {
  60. get: function () {
  61. return this.playWindow.children;
  62. },
  63. enumerable: false,
  64. configurable: true
  65. });
  66. Object.defineProperty(Navigator.prototype, "controllerShape", {
  67. // todo fixme
  68. get: function () {
  69. return this.totalPages > 1 ? { width: 55, height: 0 } : { width: 0, height: 0 };
  70. },
  71. enumerable: false,
  72. configurable: true
  73. });
  74. Object.defineProperty(Navigator.prototype, "pageShape", {
  75. get: function () {
  76. var pageViews = this.pageViews;
  77. var _a = __read(transpose(pageViews.map(function (pageView) {
  78. var _a = pageView.getBBox(), width = _a.width, height = _a.height;
  79. return [width, height];
  80. })).map(function (arr) { return Math.max.apply(Math, __spreadArray([], __read(arr), false)); }), 2), maxWidth = _a[0], maxHeight = _a[1];
  81. var _b = this.attributes, _c = _b.pageWidth, pageWidth = _c === void 0 ? maxWidth : _c, _d = _b.pageHeight, pageHeight = _d === void 0 ? maxHeight : _d;
  82. return { pageWidth: pageWidth, pageHeight: pageHeight };
  83. },
  84. enumerable: false,
  85. configurable: true
  86. });
  87. Navigator.prototype.getContainer = function () {
  88. return this.playWindow;
  89. };
  90. Object.defineProperty(Navigator.prototype, "totalPages", {
  91. get: function () {
  92. return this.pageViews.length;
  93. },
  94. enumerable: false,
  95. configurable: true
  96. });
  97. Object.defineProperty(Navigator.prototype, "currPage", {
  98. get: function () {
  99. return this.innerCurrPage;
  100. },
  101. enumerable: false,
  102. configurable: true
  103. });
  104. Navigator.prototype.getBBox = function () {
  105. var _a = _super.prototype.getBBox.call(this), x = _a.x, y = _a.y;
  106. var controllerShape = this.controllerShape;
  107. var _b = this.pageShape, pageWidth = _b.pageWidth, pageHeight = _b.pageHeight;
  108. return new BBox(x, y, pageWidth + controllerShape.width, pageHeight);
  109. };
  110. Navigator.prototype.goTo = function (pageNum) {
  111. var _this = this;
  112. var animateOptions = this.attributes.animate;
  113. var _a = this, currPage = _a.currPage, playState = _a.playState, playWindow = _a.playWindow, pageViews = _a.pageViews;
  114. if (playState !== 'idle' || pageNum < 0 || pageViews.length <= 0 || pageNum >= pageViews.length)
  115. return null;
  116. pageViews[currPage].setLocalPosition(0, 0);
  117. this.prepareFollowingPage(pageNum);
  118. var _b = __read(this.getFollowingPageDiff(pageNum), 2), dx = _b[0], dy = _b[1];
  119. this.playState = 'running';
  120. var animation = animate(playWindow, [{ transform: "translate(0, 0)" }, { transform: "translate(".concat(-dx, ", ").concat(-dy, ")") }], animateOptions);
  121. onAnimateFinished(animation, function () {
  122. _this.innerCurrPage = pageNum;
  123. _this.playState = 'idle';
  124. _this.setVisiblePages([pageNum]);
  125. _this.updatePageInfo();
  126. });
  127. return animation;
  128. };
  129. Navigator.prototype.prev = function () {
  130. var loop = this.attributes.loop;
  131. var pages = this.pageViews.length;
  132. var page = this.currPage;
  133. if (!loop && page <= 0)
  134. return null;
  135. var following = loop ? (page - 1 + pages) % pages : clamp(page - 1, 0, pages);
  136. return this.goTo(following);
  137. };
  138. Navigator.prototype.next = function () {
  139. var loop = this.attributes.loop;
  140. var pages = this.pageViews.length;
  141. var page = this.currPage;
  142. if (!loop && page >= pages - 1)
  143. return null;
  144. var following = loop ? (page + 1) % pages : clamp(page + 1, 0, pages);
  145. return this.goTo(following);
  146. };
  147. Navigator.prototype.renderClipPath = function (container) {
  148. var _a = this.pageShape, pageWidth = _a.pageWidth, pageHeight = _a.pageHeight;
  149. if (!pageWidth || !pageHeight) {
  150. this.contentGroup.style.clipPath = undefined;
  151. return;
  152. }
  153. this.clipPath = container.maybeAppendByClassName(CLASS_NAMES.clipPath, 'rect').styles({
  154. width: pageWidth,
  155. height: pageHeight,
  156. });
  157. this.contentGroup.attr('clipPath', this.clipPath.node());
  158. };
  159. Navigator.prototype.setVisiblePages = function (pages) {
  160. this.playWindow.children.forEach(function (page, index) {
  161. if (pages.includes(index))
  162. show(page);
  163. else
  164. hide(page);
  165. });
  166. };
  167. Navigator.prototype.adjustControllerLayout = function () {
  168. var _a = this, prevBtn = _a.prevBtnGroup, nextBtn = _a.nextBtnGroup, pageNum = _a.pageInfoGroup;
  169. var _b = this.attributes, orientation = _b.orientation, padding = _b.controllerPadding;
  170. var _c = pageNum.getBBox(), pW = _c.width, pH = _c.height;
  171. var _d = __read(orientation === 'horizontal' ? [-180, 0] : [-90, 90], 2), r1 = _d[0], r2 = _d[1];
  172. prevBtn.setLocalEulerAngles(r1);
  173. nextBtn.setLocalEulerAngles(r2);
  174. var _e = prevBtn.getBBox(), bpW = _e.width, bpH = _e.height;
  175. var _f = nextBtn.getBBox(), bnW = _f.width, bnH = _f.height;
  176. var maxWidth = Math.max(bpW, pW, bnW);
  177. var _g = orientation === 'horizontal'
  178. ? {
  179. offset: [
  180. [0, 0],
  181. [bpW / 2 + padding, 0],
  182. [bpW + pW + padding * 2, 0],
  183. ],
  184. textAlign: 'start',
  185. }
  186. : {
  187. offset: [
  188. [maxWidth / 2, -bpH - padding],
  189. [maxWidth / 2, 0],
  190. [maxWidth / 2, bnH + padding],
  191. ],
  192. textAlign: 'center',
  193. }, _h = __read(_g.offset, 3), _j = __read(_h[0], 2), o1x = _j[0], o1y = _j[1], _k = __read(_h[1], 2), o2x = _k[0], o2y = _k[1], _l = __read(_h[2], 2), o3x = _l[0], o3y = _l[1], textAlign = _g.textAlign;
  194. var pageNumText = pageNum.querySelector('text');
  195. pageNumText && (pageNumText.style.textAlign = textAlign);
  196. prevBtn.setLocalPosition(o1x, o1y);
  197. pageNum.setLocalPosition(o2x, o2y);
  198. nextBtn.setLocalPosition(o3x, o3y);
  199. };
  200. Navigator.prototype.updatePageInfo = function () {
  201. var _a;
  202. var _b = this, currPage = _b.currPage, pageViews = _b.pageViews, formatter = _b.attributes.formatter;
  203. if (pageViews.length < 2)
  204. return;
  205. (_a = this.pageInfoGroup.querySelector(CLASS_NAMES.pageInfo.class)) === null || _a === void 0 ? void 0 : _a.attr('text', formatter(currPage + 1, pageViews.length));
  206. this.adjustControllerLayout();
  207. };
  208. Navigator.prototype.getFollowingPageDiff = function (pageNum) {
  209. var currPage = this.currPage;
  210. if (currPage === pageNum)
  211. return [0, 0];
  212. var orientation = this.attributes.orientation;
  213. var _a = this.pageShape, pageWidth = _a.pageWidth, pageHeight = _a.pageHeight;
  214. var sign = pageNum < currPage ? -1 : 1;
  215. return orientation === 'horizontal' ? [sign * pageWidth, 0] : [0, sign * pageHeight];
  216. };
  217. Navigator.prototype.prepareFollowingPage = function (pageNum) {
  218. var _a = this, currPage = _a.currPage, pageViews = _a.pageViews;
  219. this.setVisiblePages([pageNum, currPage]);
  220. if (pageNum !== currPage) {
  221. var _b = __read(this.getFollowingPageDiff(pageNum), 2), dx = _b[0], dy = _b[1];
  222. pageViews[pageNum].setLocalPosition(dx, dy);
  223. }
  224. };
  225. Navigator.prototype.renderController = function (container) {
  226. var _this = this;
  227. var spacing = this.attributes.controllerSpacing;
  228. var _a = this.pageShape, pageWidth = _a.pageWidth, pageHeight = _a.pageHeight;
  229. var visible = this.pageViews.length >= 2;
  230. var group = container.maybeAppendByClassName(CLASS_NAMES.controller, 'g');
  231. visibility(group.node(), visible);
  232. if (!visible)
  233. return;
  234. var style = subStyleProps(this.attributes, 'button');
  235. var textStyle = subStyleProps(this.attributes, 'pageNum');
  236. var _b = __read(splitStyle(style), 2), _c = _b[0], groupStyle = _b[1], size = _c.size, pathStyle = __rest(_c, ["size"]);
  237. var whetherToAddEventListener = !group.select(CLASS_NAMES.prevBtnGroup.class).node();
  238. var prevBtnGroup = group.maybeAppendByClassName(CLASS_NAMES.prevBtnGroup, 'g').styles(groupStyle);
  239. this.prevBtnGroup = prevBtnGroup.node();
  240. var prevBtn = prevBtnGroup.maybeAppendByClassName(CLASS_NAMES.prevBtn, 'path');
  241. var nextBtnGroup = group.maybeAppendByClassName(CLASS_NAMES.nextBtnGroup, 'g').styles(groupStyle);
  242. this.nextBtnGroup = nextBtnGroup.node();
  243. var nextBtn = nextBtnGroup.maybeAppendByClassName(CLASS_NAMES.nextBtn, 'path');
  244. [prevBtn, nextBtn].forEach(function (btn) {
  245. btn.styles(__assign(__assign({}, pathStyle), { transformOrigin: 'center' }));
  246. scaleToPixel(btn.node(), size, true);
  247. });
  248. var pageInfoGroup = group.maybeAppendByClassName(CLASS_NAMES.pageInfoGroup, 'g');
  249. this.pageInfoGroup = pageInfoGroup.node();
  250. pageInfoGroup.maybeAppendByClassName(CLASS_NAMES.pageInfo, 'text').styles(textStyle);
  251. this.updatePageInfo();
  252. // put it on the right side of the container
  253. group.node().setLocalPosition(pageWidth + spacing, pageHeight / 2);
  254. if (whetherToAddEventListener) {
  255. this.prevBtnGroup.addEventListener('click', function () {
  256. _this.prev();
  257. });
  258. this.nextBtnGroup.addEventListener('click', function () {
  259. _this.next();
  260. });
  261. }
  262. };
  263. Navigator.prototype.render = function (attributes, container) {
  264. /**
  265. * container
  266. * |- contentGroup (with clip path)
  267. * |- playWindow (with animation)
  268. * |- pages
  269. * |- clipPath
  270. */
  271. var containerSelection = select(container);
  272. this.renderClipPath(containerSelection);
  273. this.renderController(containerSelection);
  274. this.setVisiblePages([this.defaultPage]);
  275. this.goTo(this.defaultPage);
  276. };
  277. Navigator.prototype.bindEvents = function () {
  278. var _this = this;
  279. var render = debounce(function () { return _this.render(_this.attributes, _this); }, 50);
  280. this.playWindow.addEventListener(ElementEvent.INSERTED, render);
  281. this.playWindow.addEventListener(ElementEvent.REMOVED, render);
  282. };
  283. return Navigator;
  284. }(GUI));
  285. export { Navigator };
  286. //# sourceMappingURL=index.js.map