indexes.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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, systemInfo } 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 = [pageScrollMixin()];
  34. this.timer = null;
  35. this.groupTop = [];
  36. this.sidebar = null;
  37. this.currentTouchAnchor = null;
  38. this.observers = {
  39. indexList(v) {
  40. this.setIndexList(v);
  41. this.setHeight(this.data._height);
  42. },
  43. height(v) {
  44. this.setHeight(v);
  45. },
  46. };
  47. this.lifetimes = {
  48. ready() {
  49. this.timer = null;
  50. this.groupTop = [];
  51. this.sidebar = null;
  52. if (this.data._height === 0) {
  53. this.setHeight();
  54. }
  55. if (this.data.indexList === null) {
  56. this.setIndexList();
  57. }
  58. },
  59. };
  60. this.methods = {
  61. setHeight(height) {
  62. if (!height) {
  63. const { windowHeight } = systemInfo;
  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. const { _indexList, stickyOffset } = this.data;
  132. const activeAnchor = _indexList[index];
  133. if (this.data.activeAnchor !== null && this.data.activeAnchor === activeAnchor)
  134. return;
  135. const target = this.groupTop.find((item) => item.anchor === activeAnchor);
  136. if (target) {
  137. this.currentTouchAnchor = activeAnchor;
  138. const scrollTop = target.top - stickyOffset;
  139. wx.pageScrollTo({
  140. scrollTop,
  141. duration: 0,
  142. });
  143. this.toggleTips(true);
  144. this.triggerEvent('select', { index: activeAnchor });
  145. this.setData({ activeAnchor });
  146. }
  147. },
  148. onClick(e) {
  149. const { index } = e.currentTarget.dataset;
  150. this.setAnchorByIndex(index);
  151. },
  152. onTouchMove(e) {
  153. this.onAnchorTouch(e);
  154. },
  155. onTouchCancel() {
  156. this.toggleTips(false);
  157. },
  158. onTouchEnd(e) {
  159. this.toggleTips(false);
  160. this.onAnchorTouch(e);
  161. },
  162. onAnchorTouch: throttle(function (e) {
  163. const getAnchorIndex = (clientY) => {
  164. const offsetY = clientY - this.sidebar.top;
  165. if (offsetY <= 0) {
  166. return 0;
  167. }
  168. if (offsetY > this.sidebar.height) {
  169. return this.data._indexList.length - 1;
  170. }
  171. return Math.floor(offsetY / this.sidebar.itemHeight);
  172. };
  173. const index = getAnchorIndex(e.changedTouches[0].clientY);
  174. this.setAnchorByIndex(index);
  175. }, 1000 / 30),
  176. setAnchorOnScroll(scrollTop) {
  177. if (!this.groupTop) {
  178. return;
  179. }
  180. const { sticky, stickyOffset, activeAnchor } = this.data;
  181. scrollTop += stickyOffset;
  182. const curIndex = this.groupTop.findIndex((group) => scrollTop >= group.top - group.height && scrollTop <= group.top + group.totalHeight - group.height);
  183. if (curIndex === -1)
  184. return;
  185. const curGroup = this.groupTop[curIndex];
  186. if (this.currentTouchAnchor !== null) {
  187. this.triggerEvent('change', { index: curGroup.anchor });
  188. this.currentTouchAnchor = null;
  189. }
  190. else if (activeAnchor !== curGroup.anchor) {
  191. this.triggerEvent('change', { index: curGroup.anchor });
  192. this.setData({ activeAnchor: curGroup.anchor });
  193. }
  194. if (sticky) {
  195. const offset = curGroup.top - scrollTop;
  196. const betwixt = offset < curGroup.height && offset > 0 && scrollTop > stickyOffset;
  197. this.$children.forEach((child, index) => {
  198. if (index === curIndex) {
  199. const sticky = scrollTop > stickyOffset;
  200. const anchorStyle = `transform: translate3d(0, ${betwixt ? offset : 0}px, 0); top: ${stickyOffset}px`;
  201. if (anchorStyle !== child.data.anchorStyle || sticky !== child.data.sticky) {
  202. child.setData({
  203. sticky,
  204. active: true,
  205. style: `height: ${curGroup.height}px`,
  206. anchorStyle,
  207. });
  208. }
  209. }
  210. else if (index + 1 === curIndex) {
  211. const anchorStyle = `transform: translate3d(0, ${betwixt ? offset - curGroup.height : 0}px, 0); top: ${stickyOffset}px`;
  212. if (anchorStyle !== child.data.anchorStyle) {
  213. child.setData({
  214. sticky: true,
  215. active: true,
  216. style: `height: ${curGroup.height}px`,
  217. anchorStyle,
  218. });
  219. }
  220. }
  221. else {
  222. child.setData({ active: false, sticky: false, anchorStyle: '' });
  223. }
  224. });
  225. }
  226. },
  227. onScroll({ scrollTop }) {
  228. this.setAnchorOnScroll(scrollTop);
  229. },
  230. };
  231. }
  232. };
  233. Indexes = __decorate([
  234. wxComponent()
  235. ], Indexes);
  236. export default Indexes;