多模块重构 12:修改项目名字,按照新的规则

This commit is contained in:
YunaiV
2022-02-02 22:33:39 +08:00
parent 352a67c530
commit 0773a4c4d7
1040 changed files with 12 additions and 190 deletions

View File

@ -0,0 +1,113 @@
/**
* 使用普通的js方案实现slider
*/
export default {
watch: {
value(n) {
// 只有在非滑动状态时才可以通过value更新滑块值这里监听是为了让用户触发
if (this.status === 'end') {
this.updateSliderPlacement(n, true)
}
}
},
mounted() {
this.init()
},
methods: {
init() {
this.getSliderRect()
},
// 获取slider尺寸
getSliderRect() {
// 获取滑块条的尺寸信息
setTimeout(() => {
this.$uGetRect('.u-slider').then((rect) => {
this.sliderRect = rect
this.updateSliderPlacement(this.value, true)
})
}, 10)
},
// 是否可以操作
canNotDo() {
return this.disabled
},
// 获取当前手势点的X轴位移值
getTouchX(e) {
return e.touches[0].clientX
},
formatStep(value) {
// 移动点占总长度的百分比
return Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step
},
// 发出事件
emitEvent(event, value) {
this.$emit(event, value || this.value)
},
// 标记当前手势的状态
setTouchStatus(status) {
this.status = status
},
onTouchStart(e) {
if (this.canNotDo()) {
return
}
// 标示当前的状态为开始触摸滑动
this.emitEvent('start')
this.setTouchStatus('start')
},
onTouchMove(e) {
if (this.canNotDo()) {
return
}
// 滑块的左边不一定跟屏幕左边接壤,所以需要减去最外层父元素的左边值
const x = this.getTouchX(e)
const { left, width } = this.sliderRect
const distanceX = x - left
// 获得移动距离对整个滑块的百分比值,此为带有多位小数的值,不能用此更新视图
// 否则造成通信阻塞需要每改变一个step值时修改一次视图
const percent = (distanceX / width) * 100
this.setTouchStatus('moving')
this.updateSliderPlacement(percent, true, 'moving')
},
onTouchEnd() {
if (this.canNotDo()) {
return
}
this.emitEvent('end')
this.setTouchStatus('end')
},
// 设置滑点的位置
updateSliderPlacement(value, drag, event) {
// 去掉小数部分同时也是对step步进的处理
const { width } = this.sliderRect
const percent = this.formatStep(value)
// 设置移动的值
const barStyle = {
width: `${percent / 100 * width}px`
}
// 移动期间无需过渡动画
if (drag === true) {
barStyle.transition = 'none'
} else {
// 非移动期间删掉对过渡为空的声明让css中的声明起效
delete barStyle.transition
}
// 修改value值
this.$emit('input', percent)
// 事件的名称
if (event) {
this.emitEvent(event, percent)
}
this.barStyle = barStyle
},
onClick(e) {
if (this.canNotDo()) {
return
}
// 直接点击滑块的情况计算方式与onTouchMove方法相同
const { left, width } = this.sliderRect
const value = ((e.detail.x - left) / width) * 100
this.updateSliderPlacement(value, false, 'click')
}
}
}

View File

@ -0,0 +1,42 @@
export default {
data() {
return {
sliderRect: {},
info: {
width: null,
left: null,
step: this.step,
disabled: this.disabled,
min: this.min,
max: this.max,
value: this.value
}
}
},
mounted() {
this.init()
},
methods: {
init() {
this.getSliderRect()
},
// 获取slider尺寸
getSliderRect() {
// 获取滑块条的尺寸信息
uni.$u.sleep().then(() => {
this.$uGetRect('.u-slider').then((rect) => {
this.info.width = rect.width
this.info.left = rect.left
})
})
},
// 此方法由wxs调用用于修改v-model绑定的值
updateValue(value) {
this.$emit('input', value)
},
// 此方法由wxs调用发出事件
emitEvent(e) {
this.$emit(e.event, e.value ? e.value : this.value)
}
}
}

View File

@ -0,0 +1,121 @@
/**
* 使用wxs方案实现slider
* 兼容微信QQH5Vue版的安卓和iOS
*/
/**
* 开始滑动操作
* @param {Object} e
* @param {Object} ownerInstance
*/
function onTouchMove(e, ownerInstance) {
// wxs事件对象下有一个instance属性表示当前触发此事件的组件的实例通过该实例可以获取相关的dataset设置样式等信息
// https://developers.weixin.qq.com/miniprogram/dev/framework/view/interactive-animation.html
var instance = e.instance;
// getState()为一个对象挂载在instance上类似组件的data一样可以存放一些变量供以后的触发事件中使用
var state = instance.getState()
// 滑块组件的整体尺寸信息
var mp = state.mp
if(mp.disabled) {
return
}
var distanceX = getTouchX(e) - mp.left
// 获得移动距离对整个滑块的百分比值此为带有多位小数的值step大于1时不能用此更新视图
var percent = (distanceX / mp.width) * 100
updateSliderPlacement(instance, ownerInstance, percent, 'moving')
// 阻止页面滚动,可以保证在滑动过程中,不让页面可以上下滚动,造成不好的体验
e.stopPropagation && e.stopPropagation()
e.preventDefault && e.preventDefault()
}
function onClick(e, ownerInstance) {
var instance = e.instance
var state = instance.getState()
var mp = state.mp
if(mp.disabled) {
return
}
// 直接点击滑块的情况计算方式与onTouchMove方法相同
var value = ((e.detail.x - mp.left) / mp.width) * 100
updateSliderPlacement(instance, ownerInstance, value, 'click')
}
function sizeReady(newValue, oldValue, ownerInstance, instance) {
// 页面初始化时候,也会触发此方法,传递的值为空,这里不执行往后的逻辑
if(!newValue || newValue.disabled) {
return
}
var state = instance.getState()
state.mp = newValue
updateSliderPlacement(instance, ownerInstance, newValue.value)
}
// 设置滑点的位置
function updateSliderPlacement(instance, ownerInstance, value, event) {
var state = instance.getState()
var mp = state.mp
if(mp.disabled) {
return
}
var percent = 0
if (mp.step > 1) {
// 如果step步进大于1需要跳步所以需要使用Math.round进行取整
percent = Math.round(Math.max(mp.min, Math.min(value, mp.max)) / mp.step) * mp.step
} else {
// 当step=1时无需跳步充分利用wxs性能滑块实时跟随手势达到丝滑的效果
percent = Math.max(mp.min, Math.min(value, mp.max))
}
// 返回组件的实例
var gapInstance = ownerInstance.selectComponent('.u-slider__gap')
// 在移动期间不允许transition动画否则会造成卡顿
gapInstance[event === 'click' ? 'addClass' : 'removeClass']('u-slider__gap--ani')
// 调用逻辑层的方法修改v-model绑定的值
ownerInstance.callMethod('updateValue', Math.round(percent))
if(event) {
ownerInstance.callMethod('emitEvent', {
event: event,
value: Math.round(percent)
})
}
// 设置移动的值
gapInstance.requestAnimationFrame(function() {
gapInstance.setStyle({
width: percent / 100 * mp.width + 'px',
})
})
}
// 开始滑动
function onTouchStart(e, ownerInstance) {
ownerInstance.callMethod('emitEvent', {
event: 'start',
value: null
})
}
// 停止滑动
function onTouchEnd(e, ownerInstance) {
ownerInstance.callMethod('emitEvent', {
event: 'end',
value: null
})
}
// 获取当前手势点的X轴位移值
function getTouchX(e) {
return e.touches[0].clientX
}
module.exports = {
onTouchStart: onTouchStart,
onTouchMove: onTouchMove,
onTouchEnd: onTouchEnd,
sizeReady: sizeReady,
onClick: onClick
}

View File

@ -0,0 +1,180 @@
/**
* 使用bindingx方案实现slider
* 只能使用于nvue下
*/
// 引入bindingx此库类似于微信小程序wxs目的是让js运行在视图层减少视图层和逻辑层的通信折损
const BindingX = uni.requireNativePlugin('bindingx')
// nvue操作dom的库用于获取dom的尺寸信息
const dom = uni.requireNativePlugin('dom')
// nvue中用于操作元素动画的库类似于uni.animation只不过uni.animation不能用于nvue
const animation = uni.requireNativePlugin('animation')
export default {
data() {
return {
// bindingx的回调值用于取消绑定
panEvent: null,
// 标记是否移动状态
moving: false,
// 位移的偏移量
x: 0,
// 是否正在触摸过程中,用于标记动画类是否添加或移除
touching: false,
changeFromInside: false
}
},
watch: {
// 监听vlaue的变化此变化可能是由于内部修改v-model的值或者外部
// 从服务端获取一个值后赋值给slider的v-model而导致的
value(n) {
if (!this.changeFromInside) {
this.initX()
} else {
this.changeFromInside = false
}
}
},
mounted() {
this.init()
},
methods: {
init() {
this.getSliderRect()
},
// 获取节点信息
// 获取slider尺寸
getSliderRect() {
// 获取滑块条的尺寸信息
// 通过nvue的dom模块查询节点信息
setTimeout(() => {
dom.getComponentRect(this.$refs['slider'], res => {
this.sliderRect = res.size
this.initX()
})
}, 10)
},
// 初始化按钮位置
initButtonStyle({
barStyle,
buttonWrapperStyle
}) {
this.barStyle = barStyle
this.buttonWrapperStyle = buttonWrapperStyle
},
emitEvent(event, value) {
this.$emit(event, value ? value : this.value)
},
formatStep(value) {
// 移动点占总长度的百分比
return Math.round(Math.max(this.min, Math.min(value, this.max)) / this.step) * this.step
},
// 滑动开始
onTouchStart(e) {
// 阻止页面滚动,可以保证在滑动过程中,不让页面可以上下滚动,造成不好的体验
e.stopPropagation && e.stopPropagation()
e.preventDefault && e.preventDefault()
if (this.moving || this.disabled) {
// 释放上一次的资源
if (this.panEvent?.token != 0) {
BindingX.unbind({
token: this.panEvent.token,
// pan为手势事件
eventType: 'pan'
})
this.gesToken = 0
}
return
}
this.moving = true
this.touching = true
// 获取元素ref
const button = this.$refs['nvue-button'].ref
const gap = this.$refs['nvue-gap'].ref
const {
min,
max,
step
} = this
const {
left,
width
} = this.sliderRect
// 初始值为本次偏移量x加上次停止滑动时的结束值
let exporession = `(${this.x} + x)`
// 将偏移的x值转为总位移的百分比值为了和min和max进行判断
exporession = `(${exporession} / ${width}) * 100`
if (step > 1) {
// 如果step步进大于1需要跳步所以需要使用Math.round进行取整
exporession = `round(max(${min}, min(${exporession}, ${max})) / ${step}) * ${step}`
} else {
// 当step=1时无需跳步充分利用bindingx性能滑块实时跟随手势达到丝滑的效果
exporession = `max(${min}, min(${exporession}, ${max}))`
}
// 将百分比最后转化为对应的px值
exporession = `${exporession} / 100 * ${width}`
// 最大值不允许超过轨迹的宽度
const {
sliderWidth
} = this.sliderRect
exporession = `min(${sliderWidth}, ${exporession})`
// 滑块点总是需要一个左偏移的值,为自身宽度的一半
const buttonExpression = `${exporession} - ${this.blockHeight / 2}`
// 阿里为了KPI而开源的BindingX
this.panEvent = BindingX.bind({
anchor: button,
eventType: 'pan',
props: [{
element: gap,
// 绑定width属性设置其宽度值
property: 'width',
expression
}, {
element: button,
// 绑定width属性设置其宽度值
property: 'transform.translateX',
expression: buttonExpression
}]
}, (e) => {
if (e.state === 'end' || e.state === 'exit') {
//
this.x = uni.$u.range(0, left + width, e.deltaX + this.x)
// 根据偏移值得出移动的百分比进而修改双向绑定的v-model的值
const value = (this.x / width) * 100
const percent = this.formatStep(value)
// 修改value值
this.$emit('input', percent)
// 标记下一次触发value的watch时这个值的变化是由内部改变的
this.changeFromInside = true
this.moving = false
this.touching = false
}
})
},
// 从value的变化倒推得出x的值该为多少
initX() {
const {
left,
width
} = this.sliderRect
// 得出x的初始偏移值之所以需要这么做是因为在bindingX中触摸滑动时只能的值本次移动的偏移值
// 而无法的值准确的前后移动的两个点的坐标值weex纯粹为阿里巴巴的KPI(部门业绩考核)产物,也就这样了
this.x = this.value / 100 * width
// 设置移动的值
const barStyle = {
width: this.x + 'px'
}
// 按钮的初始值
const buttonWrapperStyle = {
transform: `translateX(${this.x - this.blockHeight / 2}px)`
}
this.initButtonStyle({
barStyle,
buttonWrapperStyle
})
}
}
}

View File

@ -0,0 +1,193 @@
/**
* 使用bindingx方案实现slider
* 只能使用于nvue下
*/
// 引入bindingx此库类似于微信小程序wxs目的是让js运行在视图层减少视图层和逻辑层的通信折损
const BindingX = uni.requireNativePlugin('bindingx')
// nvue操作dom的库用于获取dom的尺寸信息
const dom = uni.requireNativePlugin('dom')
// nvue中用于操作元素动画的库类似于uni.animation只不过uni.animation不能用于nvue
const animation = uni.requireNativePlugin('animation')
export default {
data() {
return {
// 位移的偏移量
x: 0,
// 是否正在触摸过程中,用于标记动画类是否添加或移除
touching: false,
changeFromInside: false
}
},
watch: {
// 监听vlaue的变化此变化可能是由于内部修改v-model的值或者外部
// 从服务端获取一个值后赋值给slider的v-model而导致的
value(n) {
if (!this.changeFromInside) {
this.initX()
} else {
this.changeFromInside = false
}
}
},
mounted() {
this.init()
},
methods: {
init() {
// 更新滑块尺寸信息
this.getSliderRect().then((size) => {
this.sliderRect = size
this.initX()
})
},
// 获取节点信息
// 获取slider尺寸
getSliderRect() {
// 获取滑块条的尺寸信息
// 通过nvue的dom模块查询节点信息
return new Promise((resolve) => {
this.$nextTick(() => {
dom.getComponentRect(this.$refs.slider, (res) => {
resolve(res.size)
})
})
})
},
// 初始化按钮位置
initButtonStyle({
barStyle,
buttonWrapperStyle
}) {
this.barStyle = barStyle
this.buttonWrapperStyle = buttonWrapperStyle
},
emitEvent(event, value) {
this.$emit(event, value || this.value)
},
// 滑动开始
async onTouchStart(e) {
// if (this.disabled) return
// // 阻止页面滚动,可以保证在滑动过程中,不让页面可以上下滚动,造成不好的体验
// e.stopPropagation && e.stopPropagation()
// e.preventDefault && e.preventDefault()
// // 更新滑块的尺寸信息
// this.sliderRect = await this.getSliderRect()
// // 标记滑动过程中触摸点的信息
// this.touchStart(e)
// this.startValue = this.format(this.value)
// this.dragStatus = 'start'
// 标记滑动过程中触摸点的信息
// this.touchStart(e)
},
// 开始滑动
onTouchMove(e) {
// if (this.disabled) return;
// if (this.dragStatus === 'start') {
// this.$emit('drag-start')
// }
// // 标记当前滑动过程中的触点信息此方法在touch mixin中
// this.touchMove(e)
// this.dragStatus = 'draging'
// const {
// width: sliderWidth
// } = this.sliderRect
// const diff = (this.deltaX / sliderWidth) * this.getRange()
// this.newValue = this.startValue + diff
// this.updateValue(this.newValue, false, true)
// 获取元素ref
// const button = this.$refs['nvue-button'].ref
// const gap = this.$refs['nvue-gap'].ref
// animation.transition(gap, {
// styles: {
// width: `${this.startX + this.deltaX}px`
// }
// })
// // console.log(this.startX + this.deltaX);
// animation.transition(button, {
// styles: {
// transform: `translateX(${this.startX + this.deltaX}px)`
// }
// })
// this.barStyle = {
// width: `${this.startX + this.deltaX}px`
// }
const {
x
} = this.getTouchPoint(e)
this.buttonWrapperStyle = {
transform: `translateX(${x}px)`
}
// this.buttonWrapperStyle = {
// transform: `translateX(${this.format(this.startX + this.deltaX)}px)`
// }
},
// onTouchEnd() {
// if (this.disabled) return;
// if (this.dragStatus === 'draging') {
// this.updateValue(this.newValue, true)
// this.$emit('drag-end');
// }
// },
updateValue(value, end, drag) {
value = this.format(value)
const {
width: sliderWidth
} = this.sliderRect
const width = `${((value - this.min) * sliderWidth) / this.getRange()}`
this.value = value
this.barStyle = {
width: `${width}px`
}
// console.log('width', width);
if (drag) {
this.$emit('drag', {
value
})
}
if (end) {
this.$emit('change', value)
}
if ((drag || end)) {
this.changeFromInside = true
this.$emit('update', value)
}
},
// 从value的变化倒推得出x的值该为多少
initX() {
const {
left,
width
} = this.sliderRect
// 得出x的初始偏移值之所以需要这么做是因为在bindingX中触摸滑动时只能的值本次移动的偏移值
// 而无法的值准确的前后移动的两个点的坐标值weex纯粹为阿里巴巴的KPI(部门业绩考核)产物,也就这样了
this.x = this.value / 100 * width
// 设置移动的值
const barStyle = {
width: `${this.x}px`
}
// 按钮的初始值
const buttonWrapperStyle = {
transform: `translateX(${this.x - this.blockHeight / 2}px)`
}
this.initButtonStyle({
barStyle,
buttonWrapperStyle
})
},
// 移动点占总长度的百分比此处需要先除以step是为了保证step大于1时比如10那么在滑动11,12px这样的
// 距离时实际上滑块是不会滑动的到了16,17px经过四舍五入后就变成了20px进行了下一个跳变
format(value) {
return Math.round(uni.$u.range(this.min, this.max, value) / this.step) * this.step
},
getRange() {
const {
max,
min
} = this
return max - min
}
}
}

View File

@ -0,0 +1,49 @@
export default {
props: {
// 最小可选值
min: {
type: [Number, String],
default: uni.$u.props.slider.min
},
// 最大可选值
max: {
type: [Number, String],
default: uni.$u.props.slider.max
},
// 步长,取值必须大于 0并且可被(max - min)整除
step: {
type: [Number, String],
default: uni.$u.props.slider.step
},
// 当前取值
value: {
type: [Number, String],
default: uni.$u.props.slider.value
},
// 滑块右侧已选择部分的背景色
activeColor: {
type: String,
default: uni.$u.props.slider.activeColor
},
// 滑块左侧未选择部分的背景色
inactiveColor: {
type: String,
default: uni.$u.props.slider.inactiveColor
},
// 滑块的大小,取值范围为 12 - 28
blockSize: {
type: [Number, String],
default: uni.$u.props.slider.blockSize
},
// 滑块的颜色
blockColor: {
type: String,
default: uni.$u.props.slider.blockColor
},
// 是否显示当前的选择值
showValue: {
type: Boolean,
default: uni.$u.props.slider.showValue
}
}
}

View File

@ -0,0 +1,54 @@
<template>
<view
class="u-slider"
:style="[$u.addStyle(customStyle)]"
>
<slider
:min="min"
:max="max"
:step="step"
:value="value"
:activeColor="activeColor"
:inactiveColor="inactiveColor"
:blockSize="$u.getPx(blockSize)"
:blockColor="blockColor"
:showValue="showValue"
@changing="changingHandler"
@change="changeHandler"
></slider>
</view>
</template>
<script>
import props from './props.js'
export default {
name: 'u--slider',
mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
methods: {
// 拖动过程中触发
changingHandler(e) {
const {
value
} = e.detail
// 更新v-model的值
this.$emit('input', value)
// 触发事件
this.$emit('changing', value)
},
// 滑动结束时触发
changeHandler(e) {
const {
value
} = e.detail
// 更新v-model的值
this.$emit('input', value)
// 触发事件
this.$emit('change', value)
}
},
}
</script>
<style lang="scss" scoped>
@import "../../libs/css/components.scss";
</style>