index.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var color_1 = require("../common/color");
  4. var component_1 = require("../common/component");
  5. var relation_1 = require("../common/relation");
  6. var utils_1 = require("../common/utils");
  7. var page_scroll_1 = require("../mixins/page-scroll");
  8. var indexList = function () {
  9. var indexList = [];
  10. var charCodeOfA = 'A'.charCodeAt(0);
  11. for (var i = 0; i < 26; i++) {
  12. indexList.push(String.fromCharCode(charCodeOfA + i));
  13. }
  14. return indexList;
  15. };
  16. (0, component_1.VantComponent)({
  17. relation: (0, relation_1.useChildren)('index-anchor', function () {
  18. this.updateData();
  19. }),
  20. props: {
  21. sticky: {
  22. type: Boolean,
  23. value: true,
  24. },
  25. zIndex: {
  26. type: Number,
  27. value: 1,
  28. },
  29. highlightColor: {
  30. type: String,
  31. value: color_1.GREEN,
  32. },
  33. stickyOffsetTop: {
  34. type: Number,
  35. value: 0,
  36. },
  37. indexList: {
  38. type: Array,
  39. value: indexList(),
  40. },
  41. },
  42. mixins: [
  43. (0, page_scroll_1.pageScrollMixin)(function (event) {
  44. this.scrollTop = (event === null || event === void 0 ? void 0 : event.scrollTop) || 0;
  45. this.onScroll();
  46. }),
  47. ],
  48. data: {
  49. activeAnchorIndex: null,
  50. showSidebar: false,
  51. },
  52. created: function () {
  53. this.scrollTop = 0;
  54. },
  55. methods: {
  56. updateData: function () {
  57. var _this = this;
  58. wx.nextTick(function () {
  59. if (_this.timer != null) {
  60. clearTimeout(_this.timer);
  61. }
  62. _this.timer = setTimeout(function () {
  63. _this.setData({
  64. showSidebar: !!_this.children.length,
  65. });
  66. _this.setRect().then(function () {
  67. _this.onScroll();
  68. });
  69. }, 0);
  70. });
  71. },
  72. setRect: function () {
  73. return Promise.all([
  74. this.setAnchorsRect(),
  75. this.setListRect(),
  76. this.setSiderbarRect(),
  77. ]);
  78. },
  79. setAnchorsRect: function () {
  80. var _this = this;
  81. return Promise.all(this.children.map(function (anchor) {
  82. return (0, utils_1.getRect)(anchor, '.van-index-anchor-wrapper').then(function (rect) {
  83. Object.assign(anchor, {
  84. height: rect.height,
  85. top: rect.top + _this.scrollTop,
  86. });
  87. });
  88. }));
  89. },
  90. setListRect: function () {
  91. var _this = this;
  92. return (0, utils_1.getRect)(this, '.van-index-bar').then(function (rect) {
  93. if (!(0, utils_1.isDef)(rect)) {
  94. return;
  95. }
  96. Object.assign(_this, {
  97. height: rect.height,
  98. top: rect.top + _this.scrollTop,
  99. });
  100. });
  101. },
  102. setSiderbarRect: function () {
  103. var _this = this;
  104. return (0, utils_1.getRect)(this, '.van-index-bar__sidebar').then(function (res) {
  105. if (!(0, utils_1.isDef)(res)) {
  106. return;
  107. }
  108. _this.sidebar = {
  109. height: res.height,
  110. top: res.top,
  111. };
  112. });
  113. },
  114. setDiffData: function (_a) {
  115. var target = _a.target, data = _a.data;
  116. var diffData = {};
  117. Object.keys(data).forEach(function (key) {
  118. if (target.data[key] !== data[key]) {
  119. diffData[key] = data[key];
  120. }
  121. });
  122. if (Object.keys(diffData).length) {
  123. target.setData(diffData);
  124. }
  125. },
  126. getAnchorRect: function (anchor) {
  127. return (0, utils_1.getRect)(anchor, '.van-index-anchor-wrapper').then(function (rect) { return ({
  128. height: rect.height,
  129. top: rect.top,
  130. }); });
  131. },
  132. getActiveAnchorIndex: function () {
  133. var _a = this, children = _a.children, scrollTop = _a.scrollTop;
  134. var _b = this.data, sticky = _b.sticky, stickyOffsetTop = _b.stickyOffsetTop;
  135. for (var i = this.children.length - 1; i >= 0; i--) {
  136. var preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  137. var reachTop = sticky ? preAnchorHeight + stickyOffsetTop : 0;
  138. if (reachTop + scrollTop >= children[i].top) {
  139. return i;
  140. }
  141. }
  142. return -1;
  143. },
  144. onScroll: function () {
  145. var _this = this;
  146. var _a = this, _b = _a.children, children = _b === void 0 ? [] : _b, scrollTop = _a.scrollTop;
  147. if (!children.length) {
  148. return;
  149. }
  150. var _c = this.data, sticky = _c.sticky, stickyOffsetTop = _c.stickyOffsetTop, zIndex = _c.zIndex, highlightColor = _c.highlightColor;
  151. var active = this.getActiveAnchorIndex();
  152. this.setDiffData({
  153. target: this,
  154. data: {
  155. activeAnchorIndex: active,
  156. },
  157. });
  158. if (sticky) {
  159. var isActiveAnchorSticky_1 = false;
  160. if (active !== -1) {
  161. isActiveAnchorSticky_1 =
  162. children[active].top <= stickyOffsetTop + scrollTop;
  163. }
  164. children.forEach(function (item, index) {
  165. if (index === active) {
  166. var wrapperStyle = '';
  167. var anchorStyle = "\n color: ".concat(highlightColor, ";\n ");
  168. if (isActiveAnchorSticky_1) {
  169. wrapperStyle = "\n height: ".concat(children[index].height, "px;\n ");
  170. anchorStyle = "\n position: fixed;\n top: ".concat(stickyOffsetTop, "px;\n z-index: ").concat(zIndex, ";\n color: ").concat(highlightColor, ";\n ");
  171. }
  172. _this.setDiffData({
  173. target: item,
  174. data: {
  175. active: true,
  176. anchorStyle: anchorStyle,
  177. wrapperStyle: wrapperStyle,
  178. },
  179. });
  180. }
  181. else if (index === active - 1) {
  182. var currentAnchor = children[index];
  183. var currentOffsetTop = currentAnchor.top;
  184. var targetOffsetTop = index === children.length - 1
  185. ? _this.top
  186. : children[index + 1].top;
  187. var parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  188. var translateY = parentOffsetHeight - currentAnchor.height;
  189. var anchorStyle = "\n position: relative;\n transform: translate3d(0, ".concat(translateY, "px, 0);\n z-index: ").concat(zIndex, ";\n color: ").concat(highlightColor, ";\n ");
  190. _this.setDiffData({
  191. target: item,
  192. data: {
  193. active: true,
  194. anchorStyle: anchorStyle,
  195. },
  196. });
  197. }
  198. else {
  199. _this.setDiffData({
  200. target: item,
  201. data: {
  202. active: false,
  203. anchorStyle: '',
  204. wrapperStyle: '',
  205. },
  206. });
  207. }
  208. });
  209. }
  210. },
  211. onClick: function (event) {
  212. this.scrollToAnchor(event.target.dataset.index);
  213. },
  214. onTouchMove: function (event) {
  215. var sidebarLength = this.children.length;
  216. var touch = event.touches[0];
  217. var itemHeight = this.sidebar.height / sidebarLength;
  218. var index = Math.floor((touch.clientY - this.sidebar.top) / itemHeight);
  219. if (index < 0) {
  220. index = 0;
  221. }
  222. else if (index > sidebarLength - 1) {
  223. index = sidebarLength - 1;
  224. }
  225. this.scrollToAnchor(index);
  226. },
  227. onTouchStop: function () {
  228. this.scrollToAnchorIndex = null;
  229. },
  230. scrollToAnchor: function (index) {
  231. var _this = this;
  232. if (typeof index !== 'number' || this.scrollToAnchorIndex === index) {
  233. return;
  234. }
  235. this.scrollToAnchorIndex = index;
  236. var anchor = this.children.find(function (item) { return item.data.index === _this.data.indexList[index]; });
  237. if (anchor) {
  238. anchor.scrollIntoView(this.scrollTop);
  239. this.$emit('select', anchor.data.index);
  240. }
  241. },
  242. },
  243. });