indexes.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. import { getRect, throttle } from '../common/utils';
  11. import pageScrollMixin from '../mixins/page-scroll';
  12. const { prefix } = config;
  13. const name = `${prefix}-indexes`;
  14. let Indexes = class Indexes extends SuperComponent {
  15. constructor() {
  16. super(...arguments);
  17. this.externalClasses = [`${prefix}-class`, `${prefix}-class-sidebar`, `${prefix}-class-sidebar-item`];
  18. this.properties = props;
  19. this.data = {
  20. prefix,
  21. classPrefix: name,
  22. _height: 0,
  23. _indexList: [],
  24. scrollTop: 0,
  25. activeAnchor: null,
  26. showTips: false,
  27. };
  28. this.relations = {
  29. '../indexes-anchor/indexes-anchor': {
  30. type: 'child',
  31. },
  32. };
  33. this.behaviors = [
  34. pageScrollMixin(function (event) {
  35. this.onScroll(event);
  36. }),
  37. ];
  38. this.timer = null;
  39. this.groupTop = [];
  40. this.sidebar = null;
  41. this.observers = {
  42. indexList(v) {
  43. this.setIndexList(v);
  44. },
  45. height(v) {
  46. this.setHeight(v);
  47. },
  48. };
  49. this.lifetimes = {
  50. ready() {
  51. var _a;
  52. if (this.data._height === 0) {
  53. this.setHeight();
  54. }
  55. if (((_a = this.data._indexList) === null || _a === void 0 ? void 0 : _a.length) === 0) {
  56. this.setIndexList();
  57. }
  58. },
  59. };
  60. this.methods = {
  61. setHeight(height) {
  62. if (!height) {
  63. const { windowHeight } = wx.getSystemInfoSync();
  64. height = windowHeight;
  65. }
  66. this.setData({
  67. _height: height,
  68. }, () => {
  69. this.getAllRect();
  70. });
  71. },
  72. setIndexList(list) {
  73. if (!list) {
  74. const start = 'A'.charCodeAt(0);
  75. const alphabet = [];
  76. for (let i = start, end = start + 26; i < end; i += 1) {
  77. alphabet.push(String.fromCharCode(i));
  78. }
  79. this.setData({ _indexList: alphabet });
  80. }
  81. else {
  82. this.setData({ _indexList: list });
  83. }
  84. },
  85. getAllRect() {
  86. this.getAnchorsRect().then(() => {
  87. this.groupTop.forEach((item, index) => {
  88. const next = this.groupTop[index + 1];
  89. item.totalHeight = ((next === null || next === void 0 ? void 0 : next.top) || Infinity) - item.top;
  90. });
  91. this.setAnchorOnScroll(0);
  92. });
  93. this.getSidebarRect();
  94. },
  95. getAnchorsRect() {
  96. return Promise.all(this.$children.map((child) => getRect(child, `.${name}-anchor`).then((rect) => {
  97. this.groupTop.push({
  98. height: rect.height,
  99. top: rect.top,
  100. anchor: child.data.index,
  101. });
  102. })));
  103. },
  104. getSidebarRect() {
  105. getRect(this, `#id-${name}__bar`).then((rect) => {
  106. const { top, height } = rect;
  107. const { length } = this.data._indexList;
  108. this.sidebar = {
  109. top,
  110. height,
  111. itemHeight: (height - (length - 1) * 2) / length,
  112. };
  113. });
  114. },
  115. toggleTips(flag) {
  116. if (!flag) {
  117. clearInterval(this.timer);
  118. this.timer = setTimeout(() => {
  119. this.setData({
  120. showTips: false,
  121. });
  122. }, 300);
  123. }
  124. else {
  125. this.setData({
  126. showTips: true,
  127. });
  128. }
  129. },
  130. setAnchorByIndex(index) {
  131. if (this.preIndex != null && this.preIndex === index)
  132. return;
  133. const { _indexList } = this.data;
  134. const activeAnchor = _indexList[index];
  135. const target = this.groupTop.find((item) => item.anchor === activeAnchor);
  136. if (target) {
  137. wx.pageScrollTo({
  138. scrollTop: target.top,
  139. duration: 0,
  140. });
  141. }
  142. this.preIndex = index;
  143. this.toggleTips(true);
  144. this.triggerEvent('select', { index: activeAnchor });
  145. if (activeAnchor !== this.data.activeAnchor) {
  146. this.triggerEvent('change', { index: activeAnchor });
  147. }
  148. },
  149. onClick(e) {
  150. const { index } = e.currentTarget.dataset;
  151. this.setAnchorByIndex(index);
  152. },
  153. onTouchMove(e) {
  154. this.onAnchorTouch(e);
  155. },
  156. onTouchCancel() {
  157. this.toggleTips(false);
  158. },
  159. onTouchEnd(e) {
  160. this.toggleTips(false);
  161. this.onAnchorTouch(e);
  162. },
  163. onAnchorTouch: throttle(function (e) {
  164. const getAnchorIndex = (clientY) => {
  165. const offsetY = clientY - this.sidebar.top;
  166. if (offsetY <= 0) {
  167. return 0;
  168. }
  169. if (offsetY > this.sidebar.height) {
  170. return this.data._indexList.length - 1;
  171. }
  172. return Math.floor(offsetY / this.sidebar.itemHeight);
  173. };
  174. const index = getAnchorIndex(e.changedTouches[0].clientY);
  175. this.setAnchorByIndex(index);
  176. }, 1000 / 30),
  177. setAnchorOnScroll(scrollTop) {
  178. if (!this.groupTop) {
  179. return;
  180. }
  181. const { sticky, stickyOffset } = this.data;
  182. scrollTop += stickyOffset;
  183. const curIndex = this.groupTop.findIndex((group) => scrollTop >= group.top - group.height && scrollTop <= group.top + group.totalHeight - group.height);
  184. if (curIndex === -1)
  185. return;
  186. const curGroup = this.groupTop[curIndex];
  187. if (this.data.activeAnchor !== curGroup.anchor) {
  188. this.triggerEvent('change', { index: curGroup.anchor });
  189. }
  190. this.setData({
  191. activeAnchor: curGroup.anchor,
  192. });
  193. if (sticky) {
  194. const offset = curGroup.top - scrollTop;
  195. const betwixt = offset < curGroup.height && offset > 0 && scrollTop > stickyOffset;
  196. this.$children.forEach((child, index) => {
  197. if (index === curIndex) {
  198. child.setData({
  199. sticky: scrollTop > stickyOffset,
  200. active: true,
  201. style: `height: ${curGroup.height}px`,
  202. anchorStyle: `transform: translate3d(0, ${betwixt ? offset : 0}px, 0); top: ${stickyOffset}px`,
  203. });
  204. }
  205. else if (index + 1 === curIndex) {
  206. child.setData({
  207. sticky: true,
  208. active: true,
  209. style: `height: ${curGroup.height}px`,
  210. anchorStyle: `transform: translate3d(0, ${betwixt ? offset - curGroup.height : 0}px, 0); top: ${stickyOffset}px`,
  211. });
  212. }
  213. else {
  214. child.setData({ active: false, sticky: false, anchorStyle: '' });
  215. }
  216. });
  217. }
  218. },
  219. onScroll({ scrollTop }) {
  220. this.setAnchorOnScroll(scrollTop);
  221. },
  222. };
  223. }
  224. };
  225. Indexes = __decorate([
  226. wxComponent()
  227. ], Indexes);
  228. export default Indexes;