index.js 8.0 KB

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