|
| 1 | +const BindingX = weex.requireModule('bindingx'); |
| 2 | +const dom = weex.requireModule('dom'); |
| 3 | +const animation = weex.requireModule('animation'); |
| 4 | + |
| 5 | +export default { |
| 6 | + data() { |
| 7 | + return { |
| 8 | + right: 0, |
| 9 | + button: [], |
| 10 | + preventGesture: false |
| 11 | + } |
| 12 | + }, |
| 13 | + mounted() { |
| 14 | + this.boxSelector = this.getEl(this.$refs['selector-box-hock']); |
| 15 | + this.selector = this.getEl(this.$refs['selector-content-hock']); |
| 16 | + this.buttonSelector = this.getEl(this.$refs['selector-button-hock']); |
| 17 | + this.position = {} |
| 18 | + this.x = 0 |
| 19 | + setTimeout(() => { |
| 20 | + this.getSelectorQuery() |
| 21 | + }, 200) |
| 22 | + }, |
| 23 | + beforeDestoy() { |
| 24 | + BindingX.unbind({ |
| 25 | + token: this.timing.token, |
| 26 | + eventType: 'timing' |
| 27 | + }) |
| 28 | + BindingX.unbind({ |
| 29 | + token: this.pan.token, |
| 30 | + eventType: 'pan' |
| 31 | + }) |
| 32 | + }, |
| 33 | + watch: { |
| 34 | + show(newVal) { |
| 35 | + if (!this.position || JSON.stringify(this.position) === '{}') return; |
| 36 | + if (this.autoClose) return |
| 37 | + if (this.isInAnimation) return |
| 38 | + if (newVal) { |
| 39 | + this.open() |
| 40 | + } else { |
| 41 | + this.close() |
| 42 | + } |
| 43 | + }, |
| 44 | + }, |
| 45 | + methods: { |
| 46 | + onClick(index, item) { |
| 47 | + this.$emit('click', { |
| 48 | + content: item, |
| 49 | + index |
| 50 | + }) |
| 51 | + }, |
| 52 | + touchstart(e) { |
| 53 | + if (this.isInAnimation) return |
| 54 | + if (this.stop) return |
| 55 | + this.stop = true |
| 56 | + let endWidth = this.right |
| 57 | + let boxStep = `(x+${this.x})` |
| 58 | + let pageX = `${boxStep}> ${-endWidth} && ${boxStep} < 0?${boxStep}:(x+${this.x} < 0? ${-endWidth}:0)` |
| 59 | + |
| 60 | + let props = [{ |
| 61 | + element: this.selector, |
| 62 | + property: 'transform.translateX', |
| 63 | + expression: pageX |
| 64 | + }] |
| 65 | + |
| 66 | + let left = 0 |
| 67 | + for (let i = 0; i < this.options.length; i++) { |
| 68 | + let buttonSelectors = this.getEl(this.$refs['button-hock'][i]); |
| 69 | + if (this.button.length === 0 || !this.button[i] || !this.button[i].width) return |
| 70 | + let moveMix = endWidth - left |
| 71 | + left += this.button[i].width |
| 72 | + let step = `(${this.x}+x)/${endWidth}` |
| 73 | + let moveX = `(${step}) * ${moveMix}` |
| 74 | + let pageButtonX = `${moveX}&& (x+${this.x} > ${-endWidth})?${moveX}:${-moveMix}` |
| 75 | + props.push({ |
| 76 | + element: buttonSelectors, |
| 77 | + property: 'transform.translateX', |
| 78 | + expression: pageButtonX |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + this.eventpan = this._bind(this.boxSelector, props, 'pan', (e) => { |
| 83 | + if (e.state === 'end') { |
| 84 | + this.x = e.deltaX + this.x; |
| 85 | + if (this.x < -endWidth) { |
| 86 | + this.x = -endWidth |
| 87 | + } |
| 88 | + if (this.x > 0) { |
| 89 | + this.x = 0 |
| 90 | + } |
| 91 | + this.stop = false |
| 92 | + this.bindTiming(); |
| 93 | + } |
| 94 | + }) |
| 95 | + }, |
| 96 | + touchend(e) { |
| 97 | + this.$nextTick(() => { |
| 98 | + if (this.isopen && !this.isDrag && !this.isInAnimation) { |
| 99 | + this.close() |
| 100 | + } |
| 101 | + }) |
| 102 | + }, |
| 103 | + bindTiming() { |
| 104 | + if (this.isopen) { |
| 105 | + this.move(this.x, -this.right) |
| 106 | + } else { |
| 107 | + this.move(this.x, -40) |
| 108 | + } |
| 109 | + }, |
| 110 | + move(left, value) { |
| 111 | + if (left >= value) { |
| 112 | + this.close() |
| 113 | + } else { |
| 114 | + this.open() |
| 115 | + } |
| 116 | + }, |
| 117 | + /** |
| 118 | + * 开启swipe |
| 119 | + */ |
| 120 | + open() { |
| 121 | + this.animation(true) |
| 122 | + }, |
| 123 | + /** |
| 124 | + * 关闭swipe |
| 125 | + */ |
| 126 | + close() { |
| 127 | + this.animation(false) |
| 128 | + }, |
| 129 | + /** |
| 130 | + * 开启关闭动画 |
| 131 | + * @param {Object} type |
| 132 | + */ |
| 133 | + animation(type) { |
| 134 | + this.isDrag = true |
| 135 | + let endWidth = this.right |
| 136 | + let time = 200 |
| 137 | + this.isInAnimation = true; |
| 138 | + |
| 139 | + let exit = `t>${time}`; |
| 140 | + let translate_x_expression = `easeOutExpo(t,${this.x},${type?(-endWidth-this.x):(-this.x)},${time})` |
| 141 | + let props = [{ |
| 142 | + element: this.selector, |
| 143 | + property: 'transform.translateX', |
| 144 | + expression: translate_x_expression |
| 145 | + }] |
| 146 | + |
| 147 | + let left = 0 |
| 148 | + for (let i = 0; i < this.options.length; i++) { |
| 149 | + let buttonSelectors = this.getEl(this.$refs['button-hock'][i]); |
| 150 | + if (this.button.length === 0 || !this.button[i] || !this.button[i].width) return |
| 151 | + let moveMix = endWidth - left |
| 152 | + left += this.button[i].width |
| 153 | + let step = `${this.x}/${endWidth}` |
| 154 | + let moveX = `(${step}) * ${moveMix}` |
| 155 | + let pageButtonX = `easeOutExpo(t,${moveX},${type ? -moveMix + '-' + moveX: 0 + '-' + moveX},${time})` |
| 156 | + props.push({ |
| 157 | + element: buttonSelectors, |
| 158 | + property: 'transform.translateX', |
| 159 | + expression: pageButtonX |
| 160 | + }) |
| 161 | + } |
| 162 | + |
| 163 | + this.timing = BindingX.bind({ |
| 164 | + eventType: 'timing', |
| 165 | + exitExpression: exit, |
| 166 | + props: props |
| 167 | + }, (e) => { |
| 168 | + if (e.state === 'end' || e.state === 'exit') { |
| 169 | + this.x = type ? -endWidth : 0 |
| 170 | + this.isInAnimation = false; |
| 171 | + this.isopen = type |
| 172 | + this.isDrag = false |
| 173 | + this.$emit('change', type) |
| 174 | + } |
| 175 | + }); |
| 176 | + }, |
| 177 | + /** |
| 178 | + * 绑定 BindingX |
| 179 | + * @param {Object} anchor |
| 180 | + * @param {Object} props |
| 181 | + * @param {Object} fn |
| 182 | + */ |
| 183 | + _bind(anchor, props, eventType, fn) { |
| 184 | + return BindingX.bind({ |
| 185 | + anchor, |
| 186 | + eventType, |
| 187 | + props |
| 188 | + }, (e) => { |
| 189 | + typeof(fn) === 'function' && fn(e) |
| 190 | + }); |
| 191 | + }, |
| 192 | + /** |
| 193 | + * 获取ref |
| 194 | + * @param {Object} el |
| 195 | + */ |
| 196 | + getEl(el) { |
| 197 | + return el.ref |
| 198 | + }, |
| 199 | + /** |
| 200 | + * 获取节点信息 |
| 201 | + */ |
| 202 | + getSelectorQuery() { |
| 203 | + dom.getComponentRect(this.$refs['selector-content-hock'], (data) => { |
| 204 | + if (this.position.content) return |
| 205 | + this.position.content = data.size |
| 206 | + }) |
| 207 | + for (let i = 0; i < this.options.length; i++) { |
| 208 | + dom.getComponentRect(this.$refs['button-hock'][i], (data) => { |
| 209 | + if (!this.button) { |
| 210 | + this.button = [] |
| 211 | + } |
| 212 | + if (this.options.length === this.button.length) return |
| 213 | + this.button.push(data.size) |
| 214 | + this.right += data.size.width |
| 215 | + if (this.autoClose) return |
| 216 | + if (this.show) { |
| 217 | + this.open() |
| 218 | + } |
| 219 | + }) |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments