123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
- 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;
- return c > 3 && r && Object.defineProperty(target, key, r), r;
- };
- import { SuperComponent, wxComponent } from '../common/src/index';
- import config from '../common/config';
- import props from './props';
- const { prefix } = config;
- const classPrefix = `${prefix}-indexes`;
- const topOffset = 40;
- let IndexBar = class IndexBar extends SuperComponent {
- constructor() {
- super(...arguments);
- this.externalClasses = [`${prefix}-class`, `${prefix}-class-index`];
- this.properties = props;
- this.observers = {
- list(newValue) {
- let groups = newValue;
- if (!!newValue.length && newValue[0].title === undefined) {
- groups = groups.map((g) => {
- return Object.assign({ title: g.index }, g);
- });
- }
- this.setData({ groups });
- },
- height() {
- this.getDomInfo();
- },
- };
- this.data = {
- prefix,
- classPrefix,
- clientHeight: 0,
- groups: [],
- activeGroup: null,
- currentGroup: null,
- showScrollTip: false,
- };
- this.timer = null;
- this.groupTop = null;
- this.btnBar = null;
- }
- ready() {
- this.getHeight();
- }
- getHeight() {
- wx.getSystemInfo({
- success: (res) => {
- this.setData({
- clientHeight: res.windowHeight,
- }, () => {
- this.getDomInfo();
- });
- },
- });
- }
- getDomInfo() {
- const query = this.createSelectorQuery();
- query.select(`#id-${classPrefix}__bar`).boundingClientRect();
- query.selectAll(`.${classPrefix}__group`).boundingClientRect();
- query.exec((res) => {
- if (!res[0])
- return;
- this.btnBar = {
- top: res[0].top,
- height: res[0].height,
- itemHeight: res[0].height / this.data.groups.length,
- };
- if (!res[1])
- return;
- this.groupTop = res[1].map((element) => element.height);
- this.groupTop.reduce((acc, cur, index) => {
- const amount = acc + cur;
- this.groupTop[index] = amount;
- return amount;
- });
- });
- }
- computedIndex(tapY) {
- const offsetY = tapY - this.btnBar.top;
- let index;
- if (offsetY < 0) {
- index = 0;
- }
- else if (offsetY > this.btnBar.height) {
- index = this.data.groups.length - 1;
- }
- else {
- index = Math.floor(offsetY / this.btnBar.itemHeight);
- }
- return index;
- }
- computedIndexByScrollTop(scrollTop) {
- if (!this.groupTop) {
- return -1;
- }
- return this.groupTop.findIndex((element) => element - topOffset > scrollTop);
- }
- activeIndexWhenScroll(scrollTop) {
- const curIndex = this.computedIndexByScrollTop(scrollTop);
- if (curIndex >= 0) {
- this.setData({
- activeGroup: this.data.groups[curIndex],
- });
- }
- }
- scrollToY(tapY) {
- const index = this.computedIndex(tapY);
- this.scrollToAnchor(index);
- }
- scrollToAnchor(index) {
- this.switchScrollTip(true);
- const curGroup = this.data.groups[index];
- this.setData({
- activeGroup: curGroup,
- currentGroup: curGroup,
- });
- }
- switchScrollTip(val) {
- val = !!val;
- const switchFun = (value) => {
- if (this.data.showScrollTip === value) {
- return;
- }
- this.setData({
- showScrollTip: value,
- });
- };
- if (!val) {
- clearInterval(this.timer);
- this.timer = setTimeout(() => {
- switchFun(false);
- }, 300);
- }
- else {
- switchFun(true);
- }
- }
- throttleScroll() {
- return new Promise((resolve) => {
- const delay = 100;
- const now = Date.now();
- if (this.lastScrollTime && this.lastScrollTime + delay > now) {
- if (this.scrollTimer) {
- clearTimeout(this.scrollTimer);
- }
- this.scrollTimer = setTimeout(() => {
- this.lastScrollTime = now;
- resolve();
- }, delay);
- }
- else {
- this.lastScrollTime = now;
- resolve();
- }
- });
- }
- onTouchStart() { }
- onTouchMove(e) {
- this.throttleScroll().then(() => this.scrollToY(e.changedTouches[0].pageY));
- }
- onTouchCancel() {
- this.switchScrollTip(false);
- }
- onTouchEnd(e) {
- this.switchScrollTip(false);
- this.scrollToY(e.changedTouches[0].pageY);
- }
- onCellTap(e) {
- const { indexes } = e.currentTarget.dataset;
- this.triggerEvent('select', { indexes });
- }
- onListScroll(e) {
- this.throttleScroll().then(() => {
- const { scrollTop } = e.detail;
- this.activeIndexWhenScroll(scrollTop);
- });
- }
- };
- IndexBar = __decorate([
- wxComponent()
- ], IndexBar);
- export default IndexBar;
|