index.js 13 KB

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