c52d7a83baf20def93ad7241094b016a7a6301c7409152e4af4f5228101f238d280848b70332c221b62ee06cf329de14f9f15e209b7286a9809be339948f08-exec 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* @flow */
  2. import { extend, cached, camelize } from 'shared/util'
  3. const normalize = cached(camelize)
  4. function createStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
  5. if (!vnode.data.staticStyle) {
  6. updateStyle(oldVnode, vnode)
  7. return
  8. }
  9. const elm = vnode.elm
  10. const staticStyle = vnode.data.staticStyle
  11. const supportBatchUpdate = typeof elm.setStyles === 'function'
  12. const batchedStyles = {}
  13. for (const name in staticStyle) {
  14. if (staticStyle[name]) {
  15. supportBatchUpdate
  16. ? (batchedStyles[normalize(name)] = staticStyle[name])
  17. : elm.setStyle(normalize(name), staticStyle[name])
  18. }
  19. }
  20. if (supportBatchUpdate) {
  21. elm.setStyles(batchedStyles)
  22. }
  23. updateStyle(oldVnode, vnode)
  24. }
  25. function updateStyle (oldVnode: VNodeWithData, vnode: VNodeWithData) {
  26. if (!oldVnode.data.style && !vnode.data.style) {
  27. return
  28. }
  29. let cur, name
  30. const elm = vnode.elm
  31. const oldStyle: any = oldVnode.data.style || {}
  32. let style: any = vnode.data.style || {}
  33. const needClone = style.__ob__
  34. // handle array syntax
  35. if (Array.isArray(style)) {
  36. style = vnode.data.style = toObject(style)
  37. }
  38. // clone the style for future updates,
  39. // in case the user mutates the style object in-place.
  40. if (needClone) {
  41. style = vnode.data.style = extend({}, style)
  42. }
  43. const supportBatchUpdate = typeof elm.setStyles === 'function'
  44. const batchedStyles = {}
  45. for (name in oldStyle) {
  46. if (!style[name]) {
  47. supportBatchUpdate
  48. ? (batchedStyles[normalize(name)] = '')
  49. : elm.setStyle(normalize(name), '')
  50. }
  51. }
  52. for (name in style) {
  53. cur = style[name]
  54. supportBatchUpdate
  55. ? (batchedStyles[normalize(name)] = cur)
  56. : elm.setStyle(normalize(name), cur)
  57. }
  58. if (supportBatchUpdate) {
  59. elm.setStyles(batchedStyles)
  60. }
  61. }
  62. function toObject (arr) {
  63. const res = {}
  64. for (let i = 0; i < arr.length; i++) {
  65. if (arr[i]) {
  66. extend(res, arr[i])
  67. }
  68. }
  69. return res
  70. }
  71. export default {
  72. create: createStyle,
  73. update: updateStyle
  74. }