|
| 1 | +import classNames from 'classnames' |
| 2 | +import Taro from '@tarojs/taro' |
| 3 | +import mixins from '../mixins' |
| 4 | +import { delayQuerySelector, isTest, uuid } from '../../utils/common' |
| 5 | +import AtList from '../list/index' |
| 6 | +import AtListItem from '../list/item/index' |
| 7 | +import AtToast from '../toast/index' |
| 8 | + |
| 9 | +const ENV = Taro.getEnv() |
| 10 | + |
| 11 | +export default { |
| 12 | + name: 'AtIndexes', |
| 13 | + mixins: [mixins], |
| 14 | + props: { |
| 15 | + customStyle: { |
| 16 | + type: [Object, String], |
| 17 | + default: '', |
| 18 | + }, |
| 19 | + className: { |
| 20 | + type: [Array, String], |
| 21 | + default: '', |
| 22 | + }, |
| 23 | + animation: { |
| 24 | + type: Boolean, |
| 25 | + default: false, |
| 26 | + }, |
| 27 | + isVibrate: { |
| 28 | + type: Boolean, |
| 29 | + default: true, |
| 30 | + }, |
| 31 | + isShowToast: { |
| 32 | + type: Boolean, |
| 33 | + default: true, |
| 34 | + }, |
| 35 | + list: { |
| 36 | + type: Array, |
| 37 | + default: () => [], |
| 38 | + }, |
| 39 | + topKey: { |
| 40 | + type: String, |
| 41 | + default: 'Top', |
| 42 | + }, |
| 43 | + onClick: { |
| 44 | + type: Function, |
| 45 | + default: () => () => {}, |
| 46 | + }, |
| 47 | + onScrollIntoView: { |
| 48 | + type: Function, |
| 49 | + default: () => () => {}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + data() { |
| 53 | + return { |
| 54 | + // 右侧导航高度 |
| 55 | + menuHeight: 0, |
| 56 | + // 右侧导航距离顶部高度 |
| 57 | + startTop: 0, |
| 58 | + // 右侧导航元素高度 |
| 59 | + itemHeight: 0, |
| 60 | + // 当前索引 |
| 61 | + currentIndex: -1, |
| 62 | + listId: isTest() ? 'indexes-list-AOTU2018' : `list-${uuid()}`, |
| 63 | + timeoutTimerL: null, |
| 64 | + state: { |
| 65 | + _scrollIntoView: '', |
| 66 | + _scrollTop: 0, |
| 67 | + _tipText: '', |
| 68 | + _isShowToast: false, |
| 69 | + isWEB: Taro.getEnv() === Taro.ENV_TYPE.WEB, |
| 70 | + }, |
| 71 | + } |
| 72 | + }, |
| 73 | + computed: { |
| 74 | + listLen() { |
| 75 | + return this.list.length |
| 76 | + }, |
| 77 | + }, |
| 78 | + |
| 79 | + watch: { |
| 80 | + listLen() { |
| 81 | + this.initData() |
| 82 | + }, |
| 83 | + }, |
| 84 | + beforeMount() { |
| 85 | + this.onScrollIntoView && this.onScrollIntoView(this.__jumpTarget.bind(this)) |
| 86 | + }, |
| 87 | + mounted() { |
| 88 | + if (ENV === Taro.ENV_TYPE.WEB) { |
| 89 | + this.listRef = document.getElementById(this.listId) |
| 90 | + } |
| 91 | + this.initData() |
| 92 | + }, |
| 93 | + methods: { |
| 94 | + handleClick(item) { |
| 95 | + this.onClick && this.onClick(item) |
| 96 | + }, |
| 97 | + handleTouchMove(event) { |
| 98 | + event.stopPropagation() |
| 99 | + event.preventDefault() |
| 100 | + |
| 101 | + const { list } = this |
| 102 | + const pageY = event.touches[0].pageY |
| 103 | + const index = Math.floor((pageY - this.startTop) / this.itemHeight) |
| 104 | + |
| 105 | + if (index >= 0 && index <= list.length && this.currentIndex !== index) { |
| 106 | + this.currentIndex = index |
| 107 | + const key = index > 0 ? list[index - 1].key : 'top' |
| 108 | + const touchView = `at-indexes__list-${key}` |
| 109 | + this.jumpTarget(touchView, index) |
| 110 | + } |
| 111 | + }, |
| 112 | + handleTouchEnd() { |
| 113 | + this.currentIndex = -1 |
| 114 | + }, |
| 115 | + /** |
| 116 | + * |
| 117 | + * @param {string} _scrollIntoView |
| 118 | + * @param {number} idx |
| 119 | + */ |
| 120 | + jumpTarget(_scrollIntoView, idx) { |
| 121 | + const { topKey, list } = this |
| 122 | + const _tipText = idx === 0 ? topKey : list[idx - 1].key |
| 123 | + |
| 124 | + if (ENV === Taro.ENV_TYPE.WEB) { |
| 125 | + delayQuerySelector(this, '.at-indexes', 0).then((rect) => { |
| 126 | + const targetOffsetTop = this.listRef.childNodes[idx].offsetTop |
| 127 | + const _scrollTop = targetOffsetTop - rect[0].top |
| 128 | + this.updateState({ |
| 129 | + _scrollTop, |
| 130 | + _scrollIntoView, |
| 131 | + _tipText, |
| 132 | + }) |
| 133 | + }) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + this.updateState({ |
| 138 | + _scrollIntoView, |
| 139 | + _tipText, |
| 140 | + }) |
| 141 | + }, |
| 142 | + /** |
| 143 | + * |
| 144 | + * @param {string} key |
| 145 | + */ |
| 146 | + __jumpTarget(key) { |
| 147 | + const { list } = this |
| 148 | + // const index = _findIndex(list, ['key', key]) |
| 149 | + const index = list.findIndex((item) => item.key === key) |
| 150 | + const targetView = `at-indexes__list-${key}` |
| 151 | + this.jumpTarget(targetView, index + 1) |
| 152 | + }, |
| 153 | + updateState(state) { |
| 154 | + const { isShowToast, isVibrate } = this |
| 155 | + const { _scrollIntoView, _tipText, _scrollTop } = state |
| 156 | + // TODO: Fix dirty hack |
| 157 | + this.setState( |
| 158 | + { |
| 159 | + _scrollIntoView: _scrollIntoView, |
| 160 | + _tipText: _tipText, |
| 161 | + _scrollTop: _scrollTop, |
| 162 | + _isShowToast: isShowToast, |
| 163 | + }, |
| 164 | + () => { |
| 165 | + clearTimeout(this.timeoutTimer) |
| 166 | + this.timeoutTimer = setTimeout(() => { |
| 167 | + this.setState({ |
| 168 | + _tipText: '', |
| 169 | + _isShowToast: false, |
| 170 | + }) |
| 171 | + }, 3000) |
| 172 | + } |
| 173 | + ) |
| 174 | + |
| 175 | + if (isVibrate) { |
| 176 | + Taro.vibrateShort() |
| 177 | + } |
| 178 | + }, |
| 179 | + initData() { |
| 180 | + delayQuerySelector(this, '.at-indexes__menu').then((rect) => { |
| 181 | + const len = this.list.length |
| 182 | + this.menuHeight = rect[0].height |
| 183 | + this.startTop = rect[0].top |
| 184 | + this.itemHeight = Math.floor(this.menuHeight / (len + 1)) |
| 185 | + }) |
| 186 | + }, |
| 187 | + handleScroll(e) { |
| 188 | + if (e && e.detail) { |
| 189 | + this.setState({ |
| 190 | + _scrollTop: e.detail.scrollTop, |
| 191 | + }) |
| 192 | + } |
| 193 | + }, |
| 194 | + }, |
| 195 | + render() { |
| 196 | + const { className, customStyle, animation, topKey, list } = this |
| 197 | + const { _scrollTop, _scrollIntoView, _tipText, _isShowToast, isWEB } = this.state |
| 198 | + |
| 199 | + const toastStyle = { minWidth: Taro.pxTransform(100) } |
| 200 | + const rootCls = classNames('at-indexes', className) |
| 201 | + |
| 202 | + const menuList = list.map((dataList, i) => { |
| 203 | + const { key } = dataList |
| 204 | + const targetView = `at-indexes__list-${key}` |
| 205 | + return ( |
| 206 | + <view |
| 207 | + class="at-indexes__menu-item" |
| 208 | + key={key} |
| 209 | + onTap={this.jumpTarget.bind(this, targetView, i + 1)}> |
| 210 | + {key} |
| 211 | + </view> |
| 212 | + ) |
| 213 | + }) |
| 214 | + |
| 215 | + const indexesList = list.map((dataList) => ( |
| 216 | + <view id={`at-indexes__list-${dataList.key}`} class="at-indexes__list" key={dataList.key}> |
| 217 | + <view class="at-indexes__list-title">{dataList.title}</view> |
| 218 | + <AtList> |
| 219 | + {dataList.items && |
| 220 | + dataList.items.map((item) => ( |
| 221 | + <AtListItem |
| 222 | + key={item.name} |
| 223 | + title={item.name} |
| 224 | + onTap={this.handleClick.bind(this, item)} |
| 225 | + /> |
| 226 | + ))} |
| 227 | + </AtList> |
| 228 | + </view> |
| 229 | + )) |
| 230 | + |
| 231 | + return ( |
| 232 | + <view class={rootCls} style={customStyle}> |
| 233 | + <AtToast customStyle={toastStyle} isOpened={_isShowToast} text={_tipText} duration={2000} /> |
| 234 | + <view |
| 235 | + class="at-indexes__menu" |
| 236 | + onTouchMove={this.handleTouchMove} |
| 237 | + onTouchEnd={this.handleTouchEnd}> |
| 238 | + <view |
| 239 | + class="at-indexes__menu-item" |
| 240 | + onTap={this.jumpTarget.bind(this, 'at-indexes__top', 0)}> |
| 241 | + {topKey} |
| 242 | + </view> |
| 243 | + {menuList} |
| 244 | + </view> |
| 245 | + <scroll-view |
| 246 | + class="at-indexes__body" |
| 247 | + id={this.listId} |
| 248 | + scrollY |
| 249 | + scrollWithAnimation={animation} |
| 250 | + scrollTop={isWEB ? _scrollTop : undefined} |
| 251 | + scrollIntoView={!isWEB ? _scrollIntoView : ''} |
| 252 | + onScroll={this.handleScroll.bind(this)}> |
| 253 | + <view class="at-indexes__content" id="at-indexes__top"> |
| 254 | + {this.$slots.default} |
| 255 | + </view> |
| 256 | + {indexesList} |
| 257 | + </scroll-view> |
| 258 | + </view> |
| 259 | + ) |
| 260 | + }, |
| 261 | +} |
0 commit comments