index.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. import styleToCssString from '../helpers/styleToCssString'
  4. import { getTouchPoints, getPointsNumber } from '../helpers/gestures'
  5. /**
  6. * 获取小数位数
  7. */
  8. const getPrecision = (step) => {
  9. const stepString = step.toString()
  10. return stepString.indexOf('.') >= 0 ? stepString.length - stepString.indexOf('.') - 1 : 0
  11. }
  12. /**
  13. * 返回精度正确的值
  14. */
  15. const checkValuePrecision = (val, step, min) => {
  16. const closestStep = Math.round((val - min) / step) * step + min
  17. const precision = getPrecision(step)
  18. return parseFloat(closestStep.toFixed(precision))
  19. }
  20. baseComponent({
  21. properties: {
  22. prefixCls: {
  23. type: String,
  24. value: 'wux-slider',
  25. },
  26. min: {
  27. type: Number,
  28. value: 0,
  29. observer: 'getMarks',
  30. },
  31. max: {
  32. type: Number,
  33. value: 100,
  34. observer: 'getMarks',
  35. },
  36. step: {
  37. type: Number,
  38. value: 1,
  39. observer: 'getMarks',
  40. },
  41. defaultValue: {
  42. type: Array,
  43. value: [0],
  44. },
  45. value: {
  46. type: Array,
  47. value: [0],
  48. observer(newVal) {
  49. if (this.data.controlled) {
  50. this.updated(newVal)
  51. }
  52. },
  53. },
  54. controlled: {
  55. type: Boolean,
  56. value: false,
  57. },
  58. disabled: {
  59. type: Boolean,
  60. value: false,
  61. },
  62. showMark: {
  63. type: Boolean,
  64. value: false,
  65. },
  66. showValue: {
  67. type: [Boolean, Object],
  68. value: false,
  69. },
  70. markStyle: {
  71. type: [String, Object, Array],
  72. value: '',
  73. observer(newVal) {
  74. this.setData({
  75. extMarkStyle: Array.isArray(newVal) ? newVal.map((n) => styleToCssString(n)) : styleToCssString(newVal),
  76. })
  77. },
  78. },
  79. handleStyle: {
  80. type: [String, Object, Array],
  81. value: '',
  82. observer(newVal) {
  83. this.setData({
  84. extHandleStyle: Array.isArray(newVal) ? newVal.map((n) => styleToCssString(n)) : styleToCssString(newVal),
  85. })
  86. },
  87. },
  88. trackStyle: {
  89. type: [String, Object, Array],
  90. value: '',
  91. observer(newVal) {
  92. this.setData({
  93. extTrackStyle: Array.isArray(newVal) ? newVal.map((n) => styleToCssString(n)) : styleToCssString(newVal),
  94. })
  95. },
  96. },
  97. railStyle: {
  98. type: [String, Object],
  99. value: '',
  100. observer(newVal) {
  101. this.setData({
  102. extRailStyle: styleToCssString(newVal),
  103. })
  104. },
  105. },
  106. wrapStyle: {
  107. type: [String, Object],
  108. value: '',
  109. observer(newVal) {
  110. this.setData({
  111. extWrapStyle: styleToCssString(newVal),
  112. })
  113. },
  114. },
  115. },
  116. data: {
  117. offsets: [],
  118. sliderValue: [],
  119. extMarkStyle: '',
  120. extHandleStyle: '',
  121. extTrackStyle: '',
  122. extRailStyle: '',
  123. extWrapStyle: '',
  124. },
  125. computed: {
  126. classes() {
  127. const { prefixCls, disabled } = this.data
  128. const wrap = classNames(prefixCls, {
  129. [`${prefixCls}--disabled`]: disabled,
  130. })
  131. const min = `${prefixCls}__min`
  132. const rail = `${prefixCls}__rail`
  133. const mark = `${prefixCls}__mark`
  134. const track = `${prefixCls}__track`
  135. const handle = `${prefixCls}__handle`
  136. const max = `${prefixCls}__max`
  137. return {
  138. wrap,
  139. min,
  140. rail,
  141. mark,
  142. track,
  143. handle,
  144. max,
  145. }
  146. },
  147. },
  148. methods: {
  149. /**
  150. * 更新选中值及偏移量
  151. */
  152. updated(sliderValue) {
  153. const offsets = sliderValue.map((value) => this.calcOffset(this.checkValue(value)))
  154. this.setData({ offsets, sliderValue })
  155. },
  156. /**
  157. * 手指触摸动作开始
  158. */
  159. onTouchStart(e) {
  160. if (this.data.disabled || getPointsNumber(e) > 1) return
  161. const { index } = e.currentTarget.dataset
  162. this.isMoved = false
  163. this.startX = getTouchPoints(e).x
  164. this.moveX = 0
  165. // 记录选中值发生改变时的初始偏移量
  166. this.startPos = this.data.offsets[index] || 0
  167. // 记录最后一次选中项
  168. this.setData({ last: index })
  169. },
  170. /**
  171. * 手指触摸后移动
  172. */
  173. onTouchMove(e) {
  174. if (this.data.disabled || getPointsNumber(e) > 1) return
  175. const { index } = e.currentTarget.dataset
  176. const { prefixCls } = this.data
  177. this.isMoved = true
  178. this.moveX = getTouchPoints(e).x
  179. this.getRect(`.${prefixCls}__rail`).then((rect) => {
  180. if (!rect || !this.isMoved) return
  181. const diffX = (this.moveX - this.startX) / rect.width * (this.data.max - this.data.min)
  182. const nextOffsets = [...this.data.offsets]
  183. const offset = this.checkValue(this.startPos + diffX, 0, 100)
  184. const { sliderValue } = this.data
  185. const currentValue = this.calcValue(offset)
  186. const prevValue = sliderValue[index - 1]
  187. const nextValue = sliderValue[index + 1]
  188. // 通过合法的当前值反算偏移量
  189. nextOffsets[index] = this.calcOffset(currentValue)
  190. // 判断当前值是否小于前一值,是则重新计算偏移量
  191. if (prevValue && prevValue > currentValue) {
  192. nextOffsets[index] = this.calcOffset(prevValue)
  193. }
  194. // 判断当前值是否大于后一值,是则重新计算偏移量
  195. if (nextValue && nextValue < currentValue) {
  196. nextOffsets[index] = this.calcOffset(nextValue)
  197. }
  198. // 判断当前值是否发生变化,是则触发 change 事件
  199. if (sliderValue[index] !== currentValue) {
  200. const value = this.getValue(nextOffsets)
  201. if (!this.data.controlled) {
  202. this.setData({ offsets: nextOffsets, sliderValue: value })
  203. }
  204. this.triggerEvent('change', { offsets: nextOffsets, value })
  205. }
  206. })
  207. },
  208. /**
  209. * 手指触摸动作结束
  210. */
  211. onTouchEnd(e) {
  212. if (this.data.disabled || getPointsNumber(e) > 1) return
  213. this.isMoved = false
  214. const { offsets } = this.data
  215. const value = this.getValue(offsets)
  216. this.triggerEvent('afterChange', { offsets, value })
  217. },
  218. /**
  219. * 获取界面上的节点信息
  220. */
  221. getRect(selector, all) {
  222. return new Promise((resolve) => {
  223. wx
  224. .createSelectorQuery()
  225. .in(this)[all ? 'selectAll' : 'select'](selector)
  226. .boundingClientRect((rect) => {
  227. if (all && Array.isArray(rect) && rect.length) {
  228. resolve(rect)
  229. }
  230. if (!all && rect) {
  231. resolve(rect)
  232. }
  233. })
  234. .exec()
  235. })
  236. },
  237. /**
  238. * 计算选中值
  239. */
  240. calcValue(ratio) {
  241. const { min, max } = this.data
  242. return this.trimValue(ratio * (max - min) / 100 + min)
  243. },
  244. /**
  245. * 计算偏移量
  246. */
  247. calcOffset(value) {
  248. const { min, max } = this.data
  249. const ratio = (value - min) / (max - min)
  250. return ratio * 100
  251. },
  252. /**
  253. * 判断元素是否在指定的范围内
  254. */
  255. checkValue(val, min = this.data.min, max = this.data.max) {
  256. if (val <= min) {
  257. return min
  258. }
  259. if (val >= max) {
  260. return max
  261. }
  262. return val
  263. },
  264. /**
  265. * 验证选中值
  266. */
  267. trimValue(val) {
  268. return checkValuePrecision(this.checkValue(val), this.data.step, this.data.min)
  269. },
  270. /**
  271. * 获取选中值
  272. */
  273. getValue(offsets = this.data.offsets) {
  274. return offsets.map((offset) => this.calcValue(offset))
  275. },
  276. /**
  277. * 获取间断点位置
  278. */
  279. getMarks() {
  280. if (!this.data.showMark) return
  281. const {min, max, step } = this.data
  282. const count = (max - min) / step
  283. const marks = []
  284. const offset = 100 * step / (max - min)
  285. for (let i = 1; i < count; i++) {
  286. marks.push(i * offset)
  287. }
  288. this.setData({ marks })
  289. },
  290. },
  291. attached() {
  292. const { defaultValue, value, controlled } = this.data
  293. const sliderValue = controlled ? value : defaultValue
  294. this.updated(sliderValue)
  295. this.getMarks()
  296. },
  297. })