indexes.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. const { prefix } = config;
  11. const classPrefix = `${prefix}-indexes`;
  12. const topOffset = 40;
  13. let IndexBar = class IndexBar extends SuperComponent {
  14. constructor() {
  15. super(...arguments);
  16. this.externalClasses = [`${prefix}-class`, `${prefix}-class-index`];
  17. this.properties = props;
  18. this.observers = {
  19. list(newValue) {
  20. let groups = newValue;
  21. if (!!newValue.length && newValue[0].title === undefined) {
  22. groups = groups.map((g) => {
  23. return Object.assign({ title: g.index }, g);
  24. });
  25. }
  26. this.setData({ groups });
  27. },
  28. height() {
  29. this.getDomInfo();
  30. },
  31. };
  32. this.data = {
  33. prefix,
  34. classPrefix,
  35. clientHeight: 0,
  36. groups: [],
  37. activeGroup: null,
  38. currentGroup: null,
  39. showScrollTip: false,
  40. };
  41. this.timer = null;
  42. this.groupTop = null;
  43. this.btnBar = null;
  44. }
  45. ready() {
  46. this.getHeight();
  47. }
  48. getHeight() {
  49. wx.getSystemInfo({
  50. success: (res) => {
  51. this.setData({
  52. clientHeight: res.windowHeight,
  53. }, () => {
  54. this.getDomInfo();
  55. });
  56. },
  57. });
  58. }
  59. getDomInfo() {
  60. const query = this.createSelectorQuery();
  61. query.select(`#id-${classPrefix}__bar`).boundingClientRect();
  62. query.selectAll(`.${classPrefix}__group`).boundingClientRect();
  63. query.exec((res) => {
  64. if (!res[0])
  65. return;
  66. this.btnBar = {
  67. top: res[0].top,
  68. height: res[0].height,
  69. itemHeight: res[0].height / this.data.groups.length,
  70. };
  71. if (!res[1])
  72. return;
  73. this.groupTop = res[1].map((element) => element.height);
  74. this.groupTop.reduce((acc, cur, index) => {
  75. const amount = acc + cur;
  76. this.groupTop[index] = amount;
  77. return amount;
  78. });
  79. });
  80. }
  81. computedIndex(tapY) {
  82. const offsetY = tapY - this.btnBar.top;
  83. let index;
  84. if (offsetY < 0) {
  85. index = 0;
  86. }
  87. else if (offsetY > this.btnBar.height) {
  88. index = this.data.groups.length - 1;
  89. }
  90. else {
  91. index = Math.floor(offsetY / this.btnBar.itemHeight);
  92. }
  93. return index;
  94. }
  95. computedIndexByScrollTop(scrollTop) {
  96. if (!this.groupTop) {
  97. return -1;
  98. }
  99. return this.groupTop.findIndex((element) => element - topOffset > scrollTop);
  100. }
  101. activeIndexWhenScroll(scrollTop) {
  102. const curIndex = this.computedIndexByScrollTop(scrollTop);
  103. if (curIndex >= 0) {
  104. this.setData({
  105. activeGroup: this.data.groups[curIndex],
  106. });
  107. }
  108. }
  109. scrollToY(tapY) {
  110. const index = this.computedIndex(tapY);
  111. this.scrollToAnchor(index);
  112. }
  113. scrollToAnchor(index) {
  114. this.switchScrollTip(true);
  115. const curGroup = this.data.groups[index];
  116. this.setData({
  117. activeGroup: curGroup,
  118. currentGroup: curGroup,
  119. });
  120. }
  121. switchScrollTip(val) {
  122. val = !!val;
  123. const switchFun = (value) => {
  124. if (this.data.showScrollTip === value) {
  125. return;
  126. }
  127. this.setData({
  128. showScrollTip: value,
  129. });
  130. };
  131. if (!val) {
  132. clearInterval(this.timer);
  133. this.timer = setTimeout(() => {
  134. switchFun(false);
  135. }, 300);
  136. }
  137. else {
  138. switchFun(true);
  139. }
  140. }
  141. throttleScroll() {
  142. return new Promise((resolve) => {
  143. const delay = 100;
  144. const now = Date.now();
  145. if (this.lastScrollTime && this.lastScrollTime + delay > now) {
  146. if (this.scrollTimer) {
  147. clearTimeout(this.scrollTimer);
  148. }
  149. this.scrollTimer = setTimeout(() => {
  150. this.lastScrollTime = now;
  151. resolve();
  152. }, delay);
  153. }
  154. else {
  155. this.lastScrollTime = now;
  156. resolve();
  157. }
  158. });
  159. }
  160. onTouchStart() { }
  161. onTouchMove(e) {
  162. this.throttleScroll().then(() => this.scrollToY(e.changedTouches[0].pageY));
  163. }
  164. onTouchCancel() {
  165. this.switchScrollTip(false);
  166. }
  167. onTouchEnd(e) {
  168. this.switchScrollTip(false);
  169. this.scrollToY(e.changedTouches[0].pageY);
  170. }
  171. onCellTap(e) {
  172. const { indexes } = e.currentTarget.dataset;
  173. this.triggerEvent('select', { indexes });
  174. }
  175. onListScroll(e) {
  176. this.throttleScroll().then(() => {
  177. const { scrollTop } = e.detail;
  178. this.activeIndexWhenScroll(scrollTop);
  179. });
  180. }
  181. };
  182. IndexBar = __decorate([
  183. wxComponent()
  184. ], IndexBar);
  185. export default IndexBar;