index.js 9.0 KB

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