fa5879ea316f5b8613834265a48f9caeb5ff257fdf7061920c03255979f312f28487fdbf0f6485362194630370b5ad0400aa7b9f74a0a9c7b3bfdfa8097bf7-exec 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* @flow */
  2. import { extend, isObject } from 'shared/util'
  3. function updateClass (oldVnode: VNodeWithData, vnode: VNodeWithData) {
  4. const el = vnode.elm
  5. const ctx = vnode.context
  6. const data: VNodeData = vnode.data
  7. const oldData: VNodeData = oldVnode.data
  8. if (!data.staticClass &&
  9. !data.class &&
  10. (!oldData || (!oldData.staticClass && !oldData.class))
  11. ) {
  12. return
  13. }
  14. const oldClassList = makeClassList(oldData)
  15. const classList = makeClassList(data)
  16. if (typeof el.setClassList === 'function') {
  17. el.setClassList(classList)
  18. } else {
  19. const style = getStyle(oldClassList, classList, ctx)
  20. if (typeof el.setStyles === 'function') {
  21. el.setStyles(style)
  22. } else {
  23. for (const key in style) {
  24. el.setStyle(key, style[key])
  25. }
  26. }
  27. }
  28. }
  29. function makeClassList (data: VNodeData): Array<string> {
  30. const classList = []
  31. // unlike web, weex vnode staticClass is an Array
  32. const staticClass: any = data.staticClass
  33. const dataClass = data.class
  34. if (staticClass) {
  35. classList.push.apply(classList, staticClass)
  36. }
  37. if (Array.isArray(dataClass)) {
  38. classList.push.apply(classList, dataClass)
  39. } else if (isObject(dataClass)) {
  40. classList.push.apply(classList, Object.keys(dataClass).filter(className => dataClass[className]))
  41. } else if (typeof dataClass === 'string') {
  42. classList.push.apply(classList, dataClass.trim().split(/\s+/))
  43. }
  44. return classList
  45. }
  46. function getStyle (oldClassList: Array<string>, classList: Array<string>, ctx: Component): Object {
  47. // style is a weex-only injected object
  48. // compiled from <style> tags in weex files
  49. const stylesheet: any = ctx.$options.style || {}
  50. const result = {}
  51. classList.forEach(name => {
  52. const style = stylesheet[name]
  53. extend(result, style)
  54. })
  55. oldClassList.forEach(name => {
  56. const style = stylesheet[name]
  57. for (const key in style) {
  58. if (!result.hasOwnProperty(key)) {
  59. result[key] = ''
  60. }
  61. }
  62. })
  63. return result
  64. }
  65. export default {
  66. create: updateClass,
  67. update: updateClass
  68. }