const {
ID,
ITERATOR,
isVar,
getForEl,
processForKey,
updateForEleId
} = require('./util')
const parseText = require('./text-parser')
const preTransformNode = require('./pre-transform-node')
function genData (el) {
const {
events,
dynamicClass,
dynamicStyle,
dynamicAttrs,
dynamicTexts,
directivesBinding
} = el
let extras = ''
if (directivesBinding) {
const dirs = genDirectives(directivesBinding)
dirs && (extras += dirs + ',')
}
if (dynamicClass) {
extras += genDynamicClass(dynamicClass) + ','
}
if (dynamicStyle) {
extras += genDynamicStyle(dynamicStyle) + ','
}
if (dynamicTexts) {
extras += genDynamicTexts(dynamicTexts) + ','
}
if (events) {
const dynamicHandlers = genDynamicHandlers(events)
dynamicHandlers && (extras += dynamicHandlers + ',')
}
if (Array.isArray(dynamicAttrs)) {
const dynamicProps = genDynamicProps(dynamicAttrs)
dynamicProps && (extras += dynamicProps + ',')
}
extras = extras.replace(/,$/, '')
if (extras) {
return `extras:{${extras}},`
}
return ''
}
function genDirectives (dirs) {
let directives = []
dirs.forEach(dir => {
isVar(dir.value) && directives.push(`"v-${dir.name}":${dir.value}`)
dir.isDynamicArg && (directives.push(`"v-${dir.name}-arg":${dir.arg}`))
})
return directives.join(',')
}
function genDynamicClass (classBinding) {
return `c:${classBinding}`
}
function genDynamicStyle (styleBinding) {
return `s:${styleBinding}`
}
function genDynamicTexts (dynamicTexts) {
return dynamicTexts.map(({
name,
value
}) => {
return `${name}:${value}`
}).join(',')
}
function genDynamicHandlers (events) {
let dynamicHandlers = []
let index = 0
for (let name in events) {
const eventHandler = events[name]
if (eventHandler.dynamic) {
eventHandler.index = index++
dynamicHandlers.push('"de-' + (eventHandler.index) + '":' + name)
}
}
return dynamicHandlers.join(',')
}
function genDynamicProps (props, prefix = 'da') {
let dynamicProps = []
let index = 0
for (let i = 0; i < props.length; i++) {
const {
name,
value,
dynamic
} = props[i]
if (dynamic) {
props[i].index = index++
dynamicProps.push(`"${prefix}-` + (props[i].index) + '-n":' + name)
dynamicProps.push(`"${prefix}-` + (props[i].index) + '-v":' + value)
}
}
return dynamicProps.join(',')
}
// if 使用该方案是因为 template 节点之类无法挂靠 extras
function processIfConditions (el) {
if (el.if) {
el.ifConditions.forEach(con => {
if (isVar(con.exp)) {
const method = con.block.elseif ? '_$e' : '_$i'
con.exp = `${method}(${con.block.attrsMap[ID]},${con.exp})`
}
})
el.if = `_$i(${el.attrsMap[ID]},${el.if})`
}
el.children && el.children.forEach(child => {
processIfConditions(child)
})
el.scopedSlots && Object.values(el.scopedSlots).forEach(child => {
processIfConditions(child)
})
}
function removeStatic (el) {
delete el.staticClass
delete el.staticStyle
}
function renameBinding (el) {
// 重命名 classBinding,styleBinding,避免生成 class,style 属性
if (el.classBinding) {
el.dynamicClass = el.classBinding
delete el.classBinding
}
if (el.styleBinding) {
el.dynamicStyle = el.styleBinding
delete el.styleBinding
}
}
function processKey (el) {
// add default key
if (processForKey(el)) {
el = el.children[0] // 当 template 下仅文本时,处理第一个动态文本
}
if (el.key) { // renderList key
const forEl = getForEl(el)
if (forEl) {
if (!isVar(forEl.for)) {
return
}
const forId = forEl.forId
const it = forEl.iterator2 || forEl.iterator1 || ITERATOR
if (forEl === el) { //