index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import baseComponent from '../helpers/baseComponent'
  2. import classNames from '../helpers/classNames'
  3. import { $wuxBackdrop } from '../index'
  4. function getLabels(children = []) {
  5. return children.filter((v) => v.checked).map((v) => v.label).join(',')
  6. }
  7. function getDisplayValues(options = [], extra = true) {
  8. return options.reduce((acc, option) => {
  9. switch (option.type) {
  10. case 'radio':
  11. case 'checkbox':
  12. acc.push(getLabels(option.children || []) || (extra ? option.label : ''))
  13. break
  14. case 'filter':
  15. acc.push(getDisplayValues(option.children || [], false))
  16. break
  17. default:
  18. acc.push(option.label)
  19. }
  20. return acc
  21. }, [])
  22. }
  23. function getSortValue(sort) {
  24. if (typeof sort === 'number' && [1, -1].includes(sort)) {
  25. return sort
  26. }
  27. return 1
  28. }
  29. function getValue(children = [], single = false) {
  30. const allValues = children.filter((v) => v.checked).map((v) => v.value)
  31. if (!single) return allValues
  32. return allValues[0] || ''
  33. }
  34. function getValues(options = []) {
  35. return options.reduce((acc, option) => {
  36. switch (option.type) {
  37. case 'radio':
  38. acc.push(getValue(option.children, true))
  39. break
  40. case 'checkbox':
  41. acc.push(getValue(option.children, false))
  42. break
  43. case 'text':
  44. acc.push(option.checked ? option.value : '')
  45. break
  46. case 'sort':
  47. acc.push(option.checked ? getSortValue(option.sort) : '')
  48. break
  49. case 'filter':
  50. acc.push(getValues(option.children))
  51. break
  52. }
  53. return acc
  54. }, [])
  55. }
  56. function getChangedValues(options = [], values = [], prefix = 'options') {
  57. return options.reduce((acc, option, index) => {
  58. if (option.type === 'radio') {
  59. return {
  60. ...acc,
  61. [`${prefix}[${index}].children`]: option.children.map((v) => ({ ...v, checked: v.value === values[index] })),
  62. }
  63. }
  64. if (option.type === 'checkbox') {
  65. return {
  66. ...acc,
  67. [`${prefix}[${index}].children`]: option.children.map((v) => ({ ...v, checked: Array.isArray(values[index]) ? values[index].includes(v.value) : false })),
  68. }
  69. }
  70. if (option.type === 'filter') {
  71. return {
  72. ...acc,
  73. ...getChangedValues(option.children, values[index] || [], `options[${index}].children`),
  74. }
  75. }
  76. return acc
  77. }, {})
  78. }
  79. function getShowOptions(options = [], values = []) {
  80. return options.reduce((acc ,option, index) => {
  81. if (['radio', 'checkbox'].includes(option.type)) {
  82. return [...acc, { ...option, selected: getLabels(option.children || []) }]
  83. }
  84. if (option.type === 'filter') {
  85. return [...acc, { ...option, children: getShowOptions(option.children || [], values[index]) }]
  86. }
  87. return acc
  88. }, [])
  89. }
  90. baseComponent({
  91. properties: {
  92. prefixCls: {
  93. type: String,
  94. value: 'wux-filterbar',
  95. },
  96. items: {
  97. type: Array,
  98. value: [],
  99. },
  100. cancelText: {
  101. type: String,
  102. value: '重置',
  103. },
  104. confirmText: {
  105. type: String,
  106. value: '确定',
  107. },
  108. },
  109. data: {
  110. displayValues: [],
  111. values: [],
  112. },
  113. observers: {
  114. ['items.**'](newVal) {
  115. this.setData({ options: newVal, values: getValues(newVal) })
  116. },
  117. ['options.**'](newVal) {
  118. this.updatedDisplayValues(newVal)
  119. },
  120. },
  121. computed: {
  122. classes: ['prefixCls', function(prefixCls) {
  123. const wrap = classNames(prefixCls)
  124. const bd = `${prefixCls}__bd`
  125. const item = `${prefixCls}__item`
  126. const text = `${prefixCls}__text`
  127. const icon = `${prefixCls}__icon`
  128. const pop = `${prefixCls}__pop`
  129. const scrollView = `${prefixCls}__scroll-view`
  130. const panel = `${prefixCls}__panel`
  131. const panelHd = `${prefixCls}__panel-hd`
  132. const panelTitle = `${prefixCls}__panel-title`
  133. const panelSelected = `${prefixCls}__panel-selected`
  134. const panelBd = `${prefixCls}__panel-bd`
  135. const groups = `${prefixCls}__groups`
  136. const group = `${prefixCls}__group`
  137. const radio = `${prefixCls}__radio`
  138. const btn = `${prefixCls}__btn`
  139. const check = `${prefixCls}__check`
  140. const btns = `${prefixCls}__btns`
  141. const select = `${prefixCls}__select`
  142. return {
  143. wrap,
  144. bd,
  145. item,
  146. text,
  147. icon,
  148. pop,
  149. scrollView,
  150. panel,
  151. panelHd,
  152. panelTitle,
  153. panelSelected,
  154. panelBd,
  155. groups,
  156. group,
  157. radio,
  158. btn,
  159. check,
  160. btns,
  161. select,
  162. }
  163. }],
  164. },
  165. methods: {
  166. updatedValues(values, callback) {
  167. if (this.data.values !== values) {
  168. this.setData({ values }, callback)
  169. }
  170. },
  171. updatedDisplayValues(options = this.data.options) {
  172. const displayValues = getDisplayValues(options)
  173. if (this.data.displayValues !== displayValues) {
  174. this.setData({ displayValues })
  175. }
  176. },
  177. /**
  178. * 关闭侧边栏筛选框
  179. * @param {Object} e 事件对象
  180. * @param {Function} callback 回调函数
  181. */
  182. onClose(e) {
  183. const { index } = e.currentTarget.dataset
  184. this.onSelectClose(index)
  185. },
  186. onPopupSelectChange(e) {
  187. const values = [...this.data.values]
  188. const options = this.showOptions || JSON.parse(JSON.stringify(this.data.options))
  189. const { value } = e.detail
  190. const { index, parentIndex } = e.currentTarget.dataset
  191. values[parentIndex] = values[parentIndex] || []
  192. values[parentIndex][index] = value
  193. if (options[parentIndex].children[index] && options[parentIndex].children[index].children) {
  194. options[parentIndex].children[index].children = options[parentIndex].children[index].children.map((v) => ({ ...v, checked: value.includes(v.value) }))
  195. this.updatedDisplayValues(options)
  196. this.showOptions = options
  197. }
  198. this.updatedValues(values)
  199. },
  200. /**
  201. * 下拉框内多项选择触发 change 事件
  202. * @param {Object} e 事件对象
  203. */
  204. onSelectChange(e) {
  205. const values = [...this.data.values]
  206. const { index, type } = e.currentTarget.dataset
  207. const { selectedValue: value } = e.detail
  208. values[index] = value
  209. this.updatedValues(values)
  210. // trigger onChange
  211. if (type === 'radio') {
  212. this.onSelectConfirm(e)
  213. }
  214. },
  215. onSelectClose(index, callback) {
  216. const params = {
  217. values: getValues(this.data.options),
  218. [`options[${index}].visible`]: false,
  219. }
  220. this.setData(params, () => {
  221. if (typeof callback === 'function') {
  222. callback.call(this)
  223. }
  224. this.showOptions = null
  225. this.$wuxBackdrop.release()
  226. })
  227. },
  228. onSelectReset(e) {
  229. const values = [...this.data.values]
  230. const { index } = e.currentTarget.dataset
  231. values[index] = []
  232. this.updatedValues(values)
  233. const showOptions = this.showOptions || JSON.parse(JSON.stringify(this.data.options))
  234. if (showOptions && showOptions.length > 0) {
  235. showOptions.forEach((option, index) => {
  236. if (option.type === 'filter') {
  237. option.children = option.children.reduce((acc, child) => {
  238. return [...acc, { ...child, children: child.children.map((v) => ({ ...v, checked: false })) }]
  239. }, [])
  240. }
  241. })
  242. this.updatedDisplayValues(showOptions)
  243. this.showOptions = null
  244. }
  245. },
  246. onSelectConfirm(e) {
  247. const { options, values } = this.data
  248. const { index } = e.currentTarget.dataset
  249. const params = getChangedValues(options, values)
  250. this.setData(params, () => this.onSelectClose(index, this.onChange))
  251. },
  252. /**
  253. * 点击事件
  254. * @param {Object} e 事件对象
  255. */
  256. onClick(e) {
  257. const { index } = e.currentTarget.dataset
  258. const { options } = this.data
  259. const values = getValues(options)
  260. // calc real values
  261. if (!options[index].visible) {
  262. this.setData({ values })
  263. }
  264. // open
  265. this.onOpenSelect(options, index)
  266. },
  267. /**
  268. * 打开下拉框
  269. * @param {Array} data 菜单数据
  270. * @param {Number} index 当前索引
  271. */
  272. onOpenSelect(data = [], index = 0) {
  273. const current = data[index]
  274. const options = data.map((n, i) => {
  275. const params = Object.assign({}, n, {
  276. checked: index === i ? !n.checked : false,
  277. })
  278. // 判断已选择的元素是否同组
  279. if (n.checked) {
  280. const has = this.getDifference(n.groups, current.groups)
  281. params.checked = !!has.length
  282. // 判断非同组的元素清空选择内容
  283. if (index !== i && !has.length) {
  284. if (typeof params.children === 'object') {
  285. if (['radio', 'checkbox'].includes(n.type)) {
  286. params.children = params.children.map((n) => Object.assign({}, n, {
  287. checked: false,
  288. }))
  289. }
  290. if (['filter'].includes(n.type)) {
  291. params.children = params.children.map((n) => {
  292. return Object.assign({}, n, {
  293. children: n.children.map((m) => Object.assign({}, m, {
  294. checked: false,
  295. })),
  296. selected: '',
  297. })
  298. })
  299. }
  300. }
  301. if (['sort'].includes(n.type)) {
  302. params.sort = undefined
  303. }
  304. }
  305. }
  306. // 展开或隐藏下拉框
  307. if (['radio', 'checkbox', 'filter'].includes(n.type)) {
  308. params.visible = index === i ? !n.visible : false
  309. if (n.type === 'filter') {
  310. this.$wuxBackdrop[index === i ? !n.visible ? 'retain' : 'release' : 'release']()
  311. }
  312. }
  313. // 当前点击排序做出处理
  314. if (index === i && ['sort'].includes(n.type)) {
  315. params.sort = typeof params.sort === 'number' ? -params.sort : 1
  316. }
  317. return params
  318. })
  319. this.setData({ options, index }, () => {
  320. if (!['radio', 'checkbox', 'filter'].includes(current.type)) {
  321. this.onChange()
  322. }
  323. })
  324. },
  325. /**
  326. * 关闭下拉框
  327. */
  328. onCloseSelect() {
  329. const params = this.data.options.reduce((acc, option, index) => {
  330. if (option.checked && option.visible) {
  331. return { ...acc, [`options[${index}].visible`]: false }
  332. }
  333. return acc
  334. }, {})
  335. this.setData(params)
  336. },
  337. /**
  338. * 获取两个数组相同的元素
  339. * @param {Array} data 数组
  340. * @param {Array} values 数组
  341. */
  342. getDifference(data = [], values = []) {
  343. return data.filter(v => values.includes(v))
  344. },
  345. /**
  346. * 元素发生变化时的事件
  347. */
  348. onChange() {
  349. const { options } = this.data
  350. const checkedValues = getValues(options)
  351. const items = getShowOptions(options, checkedValues)
  352. this.updatedValues(checkedValues, () => {
  353. this.onCloseSelect()
  354. this.triggerEvent('change', {
  355. checkedItems: items.filter((n) => n.checked),
  356. items,
  357. checkedValues,
  358. })
  359. })
  360. },
  361. /**
  362. * scroll-view 滚动时触发的事件
  363. * @param {Object} e 事件对象
  364. */
  365. onScroll(e) {
  366. this.triggerEvent('scroll', e)
  367. },
  368. /**
  369. * 打开 select 或 filter 时触发的回调函数
  370. * @param {Object} e 事件对象
  371. */
  372. onEnter(e) {
  373. this.triggerEvent('open', e)
  374. },
  375. /**
  376. * 关闭 select 或 filter 时触发的回调函数
  377. * @param {Object} e 事件对象
  378. */
  379. onExit(e) {
  380. this.triggerEvent('close', e)
  381. },
  382. },
  383. created() {
  384. this.$wuxBackdrop = $wuxBackdrop('#wux-backdrop', this)
  385. },
  386. attached() {
  387. const { items: newVal } = this.data
  388. this.setData({ options: newVal, values: getValues(newVal) })
  389. },
  390. })