index.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 = (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 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 utils_1.getRect(this, '.van-index-bar').then(function (rect) {
  93. Object.assign(_this, {
  94. height: rect.height,
  95. top: rect.top + _this.scrollTop,
  96. });
  97. });
  98. },
  99. setSiderbarRect: function () {
  100. var _this = this;
  101. return utils_1.getRect(this, '.van-index-bar__sidebar').then(function (res) {
  102. if (!utils_1.isDef(res)) {
  103. return;
  104. }
  105. _this.sidebar = {
  106. height: res.height,
  107. top: res.top,
  108. };
  109. });
  110. },
  111. setDiffData: function (_a) {
  112. var target = _a.target, data = _a.data;
  113. var diffData = {};
  114. Object.keys(data).forEach(function (key) {
  115. if (target.data[key] !== data[key]) {
  116. diffData[key] = data[key];
  117. }
  118. });
  119. if (Object.keys(diffData).length) {
  120. target.setData(diffData);
  121. }
  122. },
  123. getAnchorRect: function (anchor) {
  124. return utils_1.getRect(anchor, '.van-index-anchor-wrapper').then(function (rect) { return ({
  125. height: rect.height,
  126. top: rect.top,
  127. }); });
  128. },
  129. getActiveAnchorIndex: function () {
  130. var _a = this, children = _a.children, scrollTop = _a.scrollTop;
  131. var _b = this.data, sticky = _b.sticky, stickyOffsetTop = _b.stickyOffsetTop;
  132. for (var i = this.children.length - 1; i >= 0; i--) {
  133. var preAnchorHeight = i > 0 ? children[i - 1].height : 0;
  134. var reachTop = sticky ? preAnchorHeight + stickyOffsetTop : 0;
  135. if (reachTop + scrollTop >= children[i].top) {
  136. return i;
  137. }
  138. }
  139. return -1;
  140. },
  141. onScroll: function () {
  142. var _this = this;
  143. var _a = this, _b = _a.children, children = _b === void 0 ? [] : _b, scrollTop = _a.scrollTop;
  144. if (!children.length) {
  145. return;
  146. }
  147. var _c = this.data, sticky = _c.sticky, stickyOffsetTop = _c.stickyOffsetTop, zIndex = _c.zIndex, highlightColor = _c.highlightColor;
  148. var active = this.getActiveAnchorIndex();
  149. this.setDiffData({
  150. target: this,
  151. data: {
  152. activeAnchorIndex: active,
  153. },
  154. });
  155. if (sticky) {
  156. var isActiveAnchorSticky_1 = false;
  157. if (active !== -1) {
  158. isActiveAnchorSticky_1 =
  159. children[active].top <= stickyOffsetTop + scrollTop;
  160. }
  161. children.forEach(function (item, index) {
  162. if (index === active) {
  163. var wrapperStyle = '';
  164. var anchorStyle = "\n color: " + highlightColor + ";\n ";
  165. if (isActiveAnchorSticky_1) {
  166. wrapperStyle = "\n height: " + children[index].height + "px;\n ";
  167. anchorStyle = "\n position: fixed;\n top: " + stickyOffsetTop + "px;\n z-index: " + zIndex + ";\n color: " + highlightColor + ";\n ";
  168. }
  169. _this.setDiffData({
  170. target: item,
  171. data: {
  172. active: true,
  173. anchorStyle: anchorStyle,
  174. wrapperStyle: wrapperStyle,
  175. },
  176. });
  177. }
  178. else if (index === active - 1) {
  179. var currentAnchor = children[index];
  180. var currentOffsetTop = currentAnchor.top;
  181. var targetOffsetTop = index === children.length - 1
  182. ? _this.top
  183. : children[index + 1].top;
  184. var parentOffsetHeight = targetOffsetTop - currentOffsetTop;
  185. var translateY = parentOffsetHeight - currentAnchor.height;
  186. var anchorStyle = "\n position: relative;\n transform: translate3d(0, " + translateY + "px, 0);\n z-index: " + zIndex + ";\n color: " + highlightColor + ";\n ";
  187. _this.setDiffData({
  188. target: item,
  189. data: {
  190. active: true,
  191. anchorStyle: anchorStyle,
  192. },
  193. });
  194. }
  195. else {
  196. _this.setDiffData({
  197. target: item,
  198. data: {
  199. active: false,
  200. anchorStyle: '',
  201. wrapperStyle: '',
  202. },
  203. });
  204. }
  205. });
  206. }
  207. },
  208. onClick: function (event) {
  209. this.scrollToAnchor(event.target.dataset.index);
  210. },
  211. onTouchMove: function (event) {
  212. var sidebarLength = this.children.length;
  213. var touch = event.touches[0];
  214. var itemHeight = this.sidebar.height / sidebarLength;
  215. var index = Math.floor((touch.clientY - this.sidebar.top) / itemHeight);
  216. if (index < 0) {
  217. index = 0;
  218. }
  219. else if (index > sidebarLength - 1) {
  220. index = sidebarLength - 1;
  221. }
  222. this.scrollToAnchor(index);
  223. },
  224. onTouchStop: function () {
  225. this.scrollToAnchorIndex = null;
  226. },
  227. scrollToAnchor: function (index) {
  228. var _this = this;
  229. if (typeof index !== 'number' || this.scrollToAnchorIndex === index) {
  230. return;
  231. }
  232. this.scrollToAnchorIndex = index;
  233. var anchor = this.children.find(function (item) { return item.data.index === _this.data.indexList[index]; });
  234. if (anchor) {
  235. anchor.scrollIntoView(this.scrollTop);
  236. this.$emit('select', anchor.data.index);
  237. }
  238. },
  239. },
  240. });