index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. component_1.VantComponent({
  17. relation: 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. page_scroll_1.pageScrollMixin(function (event) {
  44. this.scrollTop =
  45. (event === null || event === void 0 ? void 0 : event.scrollTop) || 0;
  46. this.onScroll();
  47. }),
  48. ],
  49. data: {
  50. activeAnchorIndex: null,
  51. showSidebar: false,
  52. },
  53. created: function () {
  54. this.scrollTop = 0;
  55. },
  56. methods: {
  57. updateData: function () {
  58. var _this = this;
  59. wx.nextTick(function () {
  60. if (_this.timer != null) {
  61. clearTimeout(_this.timer);
  62. }
  63. _this.timer = setTimeout(function () {
  64. _this.setData({
  65. showSidebar: !!_this.children.length,
  66. });
  67. _this.setRect().then(function () {
  68. _this.onScroll();
  69. });
  70. }, 0);
  71. });
  72. },
  73. setRect: function () {
  74. return Promise.all([
  75. this.setAnchorsRect(),
  76. this.setListRect(),
  77. this.setSiderbarRect(),
  78. ]);
  79. },
  80. setAnchorsRect: function () {
  81. var _this = this;
  82. return Promise.all(
  83. this.children.map(function (anchor) {
  84. return utils_1
  85. .getRect(anchor, '.van-index-anchor-wrapper')
  86. .then(function (rect) {
  87. Object.assign(anchor, {
  88. height: rect.height,
  89. top: rect.top + _this.scrollTop,
  90. });
  91. });
  92. })
  93. );
  94. },
  95. setListRect: function () {
  96. var _this = this;
  97. return utils_1.getRect(this, '.van-index-bar').then(function (rect) {
  98. Object.assign(_this, {
  99. height: rect.height,
  100. top: rect.top + _this.scrollTop,
  101. });
  102. });
  103. },
  104. setSiderbarRect: function () {
  105. var _this = this;
  106. return utils_1
  107. .getRect(this, '.van-index-bar__sidebar')
  108. .then(function (res) {
  109. _this.sidebar = {
  110. height: res.height,
  111. top: res.top,
  112. };
  113. });
  114. },
  115. setDiffData: function (_a) {
  116. var target = _a.target,
  117. data = _a.data;
  118. var diffData = {};
  119. Object.keys(data).forEach(function (key) {
  120. if (target.data[key] !== data[key]) {
  121. diffData[key] = data[key];
  122. }
  123. });
  124. if (Object.keys(diffData).length) {
  125. target.setData(diffData);
  126. }
  127. },
  128. getAnchorRect: function (anchor) {
  129. return utils_1
  130. .getRect(anchor, '.van-index-anchor-wrapper')
  131. .then(function (rect) {
  132. return {
  133. height: rect.height,
  134. top: rect.top,
  135. };
  136. });
  137. },
  138. getActiveAnchorIndex: function () {
  139. var _a = this,
  140. children = _a.children,
  141. scrollTop = _a.scrollTop;
  142. var _b = this.data,
  143. sticky = _b.sticky,
  144. stickyOffsetTop = _b.stickyOffsetTop;
  145. for (var i = this.children.length - 1; i >= 0; i--) {
  146. var preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  147. var reachTop = sticky ? preAnchorHeight + stickyOffsetTop : 0;
  148. if (reachTop + scrollTop >= children[i].top) {
  149. return i;
  150. }
  151. }
  152. return -1;
  153. },
  154. onScroll: function () {
  155. var _this = this;
  156. var _a = this,
  157. _b = _a.children,
  158. children = _b === void 0 ? [] : _b,
  159. scrollTop = _a.scrollTop;
  160. if (!children.length) {
  161. return;
  162. }
  163. var _c = this.data,
  164. sticky = _c.sticky,
  165. stickyOffsetTop = _c.stickyOffsetTop,
  166. zIndex = _c.zIndex,
  167. highlightColor = _c.highlightColor;
  168. var active = this.getActiveAnchorIndex();
  169. this.setDiffData({
  170. target: this,
  171. data: {
  172. activeAnchorIndex: active,
  173. },
  174. });
  175. if (sticky) {
  176. var isActiveAnchorSticky_1 = false;
  177. if (active !== -1) {
  178. isActiveAnchorSticky_1 =
  179. children[active].top <= stickyOffsetTop + scrollTop;
  180. }
  181. children.forEach(function (item, index) {
  182. if (index === active) {
  183. var wrapperStyle = '';
  184. var anchorStyle =
  185. '\n color: ' + highlightColor + ';\n ';
  186. if (isActiveAnchorSticky_1) {
  187. wrapperStyle =
  188. '\n height: ' +
  189. children[index].height +
  190. 'px;\n ';
  191. anchorStyle =
  192. '\n position: fixed;\n top: ' +
  193. stickyOffsetTop +
  194. 'px;\n z-index: ' +
  195. zIndex +
  196. ';\n color: ' +
  197. highlightColor +
  198. ';\n ';
  199. }
  200. _this.setDiffData({
  201. target: item,
  202. data: {
  203. active: true,
  204. anchorStyle: anchorStyle,
  205. wrapperStyle: wrapperStyle,
  206. },
  207. });
  208. } else if (index === active - 1) {
  209. var currentAnchor = children[index];
  210. var currentOffsetTop = currentAnchor.top;
  211. var targetOffsetTop =
  212. index === children.length - 1
  213. ? _this.top
  214. : children[index + 1].top;
  215. var parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  216. var translateY = parentOffsetHeight - currentAnchor.height;
  217. var anchorStyle =
  218. '\n position: relative;\n transform: translate3d(0, ' +
  219. translateY +
  220. 'px, 0);\n z-index: ' +
  221. zIndex +
  222. ';\n color: ' +
  223. highlightColor +
  224. ';\n ';
  225. _this.setDiffData({
  226. target: item,
  227. data: {
  228. active: true,
  229. anchorStyle: anchorStyle,
  230. },
  231. });
  232. } else {
  233. _this.setDiffData({
  234. target: item,
  235. data: {
  236. active: false,
  237. anchorStyle: '',
  238. wrapperStyle: '',
  239. },
  240. });
  241. }
  242. });
  243. }
  244. },
  245. onClick: function (event) {
  246. this.scrollToAnchor(event.target.dataset.index);
  247. },
  248. onTouchMove: function (event) {
  249. var sidebarLength = this.children.length;
  250. var touch = event.touches[0];
  251. var itemHeight = this.sidebar.height / sidebarLength;
  252. var index = Math.floor((touch.clientY - this.sidebar.top) / itemHeight);
  253. if (index < 0) {
  254. index = 0;
  255. } else if (index > sidebarLength - 1) {
  256. index = sidebarLength - 1;
  257. }
  258. this.scrollToAnchor(index);
  259. },
  260. onTouchStop: function () {
  261. this.scrollToAnchorIndex = null;
  262. },
  263. scrollToAnchor: function (index) {
  264. var _this = this;
  265. if (typeof index !== 'number' || this.scrollToAnchorIndex === index) {
  266. return;
  267. }
  268. this.scrollToAnchorIndex = index;
  269. var anchor = this.children.find(function (item) {
  270. return item.data.index === _this.data.indexList[index];
  271. });
  272. if (anchor) {
  273. anchor.scrollIntoView(this.scrollTop);
  274. this.$emit('select', anchor.data.index);
  275. }
  276. },
  277. },
  278. });