Browse Source

move class rendering into vdom

dev
Evan You 9 years ago
parent
commit
56607f081a
  1. 12
      src/compiler/codegen.js
  2. 11
      src/instance/index.js
  3. 35
      src/vdom/modules/class.js

12
src/compiler/codegen.js

@ -41,9 +41,15 @@ function genData (el, key) {
if (!el.attrs.length) {
return '{}'
}
let data = key ? `{key:${ key },` : `{`
if (el.attrsMap[':class'] || el.attrsMap['class']) {
data += `class: _renderClass(${ el.attrsMap[':class'] }, "${ el.attrsMap['class'] || '' }"),`
let data = '{'
if (key) {
data += `key:${ key },`
}
if (el.attrsMap[':class']) {
data += `class: ${ el.attrsMap[':class'] },`
}
if (el.attrsMap['class']) {
data += `staticClass: "${ el.attrsMap['class'] }"`
}
let attrs = `attrs:{`
let props = `props:{`

11
src/instance/index.js

@ -32,17 +32,6 @@ export default class Component {
this._tree = vtree
}
_renderClass (dynamic, cls) {
dynamic = dynamic
? typeof dynamic === 'string'
? dynamic
: Object.keys(dynamic).filter(key => dynamic[key]).join(' ')
: ''
return cls
? cls + (dynamic ? ' ' + dynamic : '')
: dynamic
}
_proxy (key) {
if (!isReserved(key)) {
// need to store ref to self here

35
src/vdom/modules/class.js

@ -1,8 +1,37 @@
import { setClass } from '../../util/index'
import { isArray, setClass } from '../../util/index'
function genClass (data) {
if (!data) {
return ''
}
if (typeof data === 'object') {
let res = ''
for (var key in data) {
if (data[key]) res += key + ' '
}
return res.slice(0, -1)
}
if (isArray(data)) {
let res = ''
for (let i = 0, l = data.length; i < l; i++) {
if (data[i]) res += genClass(data[i]) + ' '
}
return res.slice(0, -1)
}
if (typeof data === 'string') {
return data
}
}
function updateClass (oldVnode, vnode) {
if (vnode.data.class !== undefined) {
setClass(vnode.elm, vnode.data.class || '')
let dynamicClass = vnode.data.class
let staticClass = vnode.data.staticClass
if (staticClass || dynamicClass) {
dynamicClass = genClass(dynamicClass)
let cls = staticClass
? staticClass + (dynamicClass ? ' ' + dynamicClass : '')
: dynamicClass
setClass(vnode.elm, cls)
}
}

Loading…
Cancel
Save