index.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. if (!utils_1.isDef(res)) {
  110. return;
  111. }
  112. _this.sidebar = {
  113. height: res.height,
  114. top: res.top,
  115. };
  116. });
  117. },
  118. setDiffData: function (_a) {
  119. var target = _a.target,
  120. data = _a.data;
  121. var diffData = {};
  122. Object.keys(data).forEach(function (key) {
  123. if (target.data[key] !== data[key]) {
  124. diffData[key] = data[key];
  125. }
  126. });
  127. if (Object.keys(diffData).length) {
  128. target.setData(diffData);
  129. }
  130. },
  131. getAnchorRect: function (anchor) {
  132. return utils_1
  133. .getRect(anchor, '.van-index-anchor-wrapper')
  134. .then(function (rect) {
  135. return {
  136. height: rect.height,
  137. top: rect.top,
  138. };
  139. });
  140. },
  141. getActiveAnchorIndex: function () {
  142. var _a = this,
  143. children = _a.children,
  144. scrollTop = _a.scrollTop;
  145. var _b = this.data,
  146. sticky = _b.sticky,
  147. stickyOffsetTop = _b.stickyOffsetTop;
  148. for (var i = this.children.length - 1; i >= 0; i--) {
  149. var preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  150. var reachTop = sticky ? preAnchorHeight + stickyOffsetTop : 0;
  151. if (reachTop + scrollTop >= children[i].top) {
  152. return i;
  153. }
  154. }
  155. return -1;
  156. },
  157. onScroll: function () {
  158. var _this = this;
  159. var _a = this,
  160. _b = _a.children,
  161. children = _b === void 0 ? [] : _b,
  162. scrollTop = _a.scrollTop;
  163. if (!children.length) {
  164. return;
  165. }
  166. var _c = this.data,
  167. sticky = _c.sticky,
  168. stickyOffsetTop = _c.stickyOffsetTop,
  169. zIndex = _c.zIndex,
  170. highlightColor = _c.highlightColor;
  171. var active = this.getActiveAnchorIndex();
  172. this.setDiffData({
  173. target: this,
  174. data: {
  175. activeAnchorIndex: active,
  176. },
  177. });
  178. if (sticky) {
  179. var isActiveAnchorSticky_1 = false;
  180. if (active !== -1) {
  181. isActiveAnchorSticky_1 =
  182. children[active].top <= stickyOffsetTop + scrollTop;
  183. }
  184. children.forEach(function (item, index) {
  185. if (index === active) {
  186. var wrapperStyle = '';
  187. var anchorStyle =
  188. '\n color: ' + highlightColor + ';\n ';
  189. if (isActiveAnchorSticky_1) {
  190. wrapperStyle =
  191. '\n height: ' +
  192. children[index].height +
  193. 'px;\n ';
  194. anchorStyle =
  195. '\n position: fixed;\n top: ' +
  196. stickyOffsetTop +
  197. 'px;\n z-index: ' +
  198. zIndex +
  199. ';\n color: ' +
  200. highlightColor +
  201. ';\n ';
  202. }
  203. _this.setDiffData({
  204. target: item,
  205. data: {
  206. active: true,
  207. anchorStyle: anchorStyle,
  208. wrapperStyle: wrapperStyle,
  209. },
  210. });
  211. } else if (index === active - 1) {
  212. var currentAnchor = children[index];
  213. var currentOffsetTop = currentAnchor.top;
  214. var targetOffsetTop =
  215. index === children.length - 1
  216. ? _this.top
  217. : children[index + 1].top;
  218. var parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  219. var translateY = parentOffsetHeight - currentAnchor.height;
  220. var anchorStyle =
  221. '\n position: relative;\n transform: translate3d(0, ' +
  222. translateY +
  223. 'px, 0);\n z-index: ' +
  224. zIndex +
  225. ';\n color: ' +
  226. highlightColor +
  227. ';\n ';
  228. _this.setDiffData({
  229. target: item,
  230. data: {
  231. active: true,
  232. anchorStyle: anchorStyle,
  233. },
  234. });
  235. } else {
  236. _this.setDiffData({
  237. target: item,
  238. data: {
  239. active: false,
  240. anchorStyle: '',
  241. wrapperStyle: '',
  242. },
  243. });
  244. }
  245. });
  246. }
  247. },
  248. onClick: function (event) {
  249. this.scrollToAnchor(event.target.dataset.index);
  250. },
  251. onTouchMove: function (event) {
  252. var sidebarLength = this.children.length;
  253. var touch = event.touches[0];
  254. var itemHeight = this.sidebar.height / sidebarLength;
  255. var index = Math.floor((touch.clientY - this.sidebar.top) / itemHeight);
  256. if (index < 0) {
  257. index = 0;
  258. } else if (index > sidebarLength - 1) {
  259. index = sidebarLength - 1;
  260. }
  261. this.scrollToAnchor(index);
  262. },
  263. onTouchStop: function () {
  264. this.scrollToAnchorIndex = null;
  265. },
  266. scrollToAnchor: function (index) {
  267. var _this = this;
  268. if (typeof index !== 'number' || this.scrollToAnchorIndex === index) {
  269. return;
  270. }
  271. this.scrollToAnchorIndex = index;
  272. var anchor = this.children.find(function (item) {
  273. return item.data.index === _this.data.indexList[index];
  274. });
  275. if (anchor) {
  276. anchor.scrollIntoView(this.scrollTop);
  277. this.$emit('select', anchor.data.index);
  278. }
  279. },
  280. },
  281. });