储能智慧云小程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

indexes.js 8.9KB

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