service.js 7.1 KB
Newer Older
fxy060608's avatar
init v3  
fxy060608 已提交
1 2 3
const {
  ID,
  ITERATOR,
fxy060608's avatar
fxy060608 已提交
4
  isVar,
fxy060608's avatar
init v3  
fxy060608 已提交
5 6
  getForEl,
  processForKey,
fxy060608's avatar
fxy060608 已提交
7 8
  updateForEleId,
  traverseNode
fxy060608's avatar
init v3  
fxy060608 已提交
9 10
} = require('./util')

fxy060608's avatar
fxy060608 已提交
11 12 13 14
const {
  isComponent
} = require('../util')

fxy060608's avatar
fxy060608 已提交
15 16 17
const parseText = require('./parser/text-parser')
const parseEvent = require('./parser/event-parser')
const parseBlock = require('./parser/block-parser')
fxy060608's avatar
init v3  
fxy060608 已提交
18 19 20

const preTransformNode = require('./pre-transform-node')

fxy060608's avatar
fxy060608 已提交
21 22 23
const optimize = require('./optimizer')

function genData (el) {
fxy060608's avatar
fxy060608 已提交
24 25
  delete el.$parentIterator3

fxy060608's avatar
init v3  
fxy060608 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
  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})`
  }
}

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 下仅文本时,处理第一个动态文本
  }
fxy060608's avatar
fxy060608 已提交
163 164 165 166
  if (el.key && (
    el.key.indexOf('_$f') !== 0 &&
      el.key.indexOf('_$s') !== 0
  )) { // renderList key
fxy060608's avatar
init v3  
fxy060608 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    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) { // <view v-for="item in items" :key="item.id"></view>
        el.key = `_$f(${forId},{forIndex:${it},key:${el.key}})`
      } else { // <template v-for="item in items"><view :key="item.id+'1'"></view><view :key="item.id+'2'"></view></template>
        const keyIndex = forEl.children.indexOf(el)
        el.key = `_$f(${forId},{forIndex:${it},keyIndex:${keyIndex},key:${el.key}})`
      }
    } else {
      isVar(el.key) && (el.key = `_$s(${el.attrsMap[ID]},'a-key',${el.key})`)
    }
  }
}

function processIf (el) {
fxy060608's avatar
fxy060608 已提交
187
  processIfConditions(el)
fxy060608's avatar
init v3  
fxy060608 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
}

function processDirs (el) {
  if (el.directives) {
    const directivesBinding = []
    el.directives = el.directives.filter(dir => {
      directivesBinding.push(dir)
      if (dir.name === 'model') {
        return true
      }
    })
    if (directivesBinding.length) {
      el.directivesBinding = directivesBinding
    }
    if (!el.directives.length) {
      delete el.directives
    }
  }
}

function processDynamicText (el, state) {
  el.type = 1
  el.tag = 'text'
  el.attrsList = [{
    name: ID,
    value: state.id
  }]
  el.attrsMap = {
    [ID]: state.id
  }
  el.rawAttrsMap = {}
  el.children = []
  el.plain = false
  el.attrs = [{
    name: ID,
    value: String(state.id)
  }]
  el.hasBindings = true

fxy060608's avatar
fxy060608 已提交
227
  if (el.text) {
fxy060608's avatar
fxy060608 已提交
228 229
    // fixed by xxxxxx 注意:保持平台一致性,trim 一下
    const ret = parseText(el.text.trim(), false, state)
fxy060608's avatar
init v3  
fxy060608 已提交
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    if (ret && ret.dynamicTexts.length) {
      el.dynamicTexts = ret.dynamicTexts
    }
  }

  delete el.expression
  delete el.tokens
  delete el.text

  return el
}

function processText (el) {
  if (!el.children.length) {
    return
  }

  const state = {
    id: el.attrsMap[ID],
    index: 0,
    service: true
  }

  el.children = el.children.filter(child => {
    if (child.type === 3) { // ASTText
      return false
    } else if (child.type === 2 && child.text) { // ASTExpression
      processDynamicText(child, state)
      child.parent = el
    }
    return true
  })

  // <div><template v-for="item in items">item</template></div>
  if (!el.children.length && el.tag === 'template' && el.for) {
    el.children.push(processDynamicText({
      parent: el
    }, state))
  }
}

271
function processAttrs (el) {
fxy060608's avatar
fxy060608 已提交
272 273 274
  if (!isComponent(el.tag)) { // 自定义组件,不能移除静态 attr
    el.attrs = el.attrs.filter(attr => attr.name === ID || isVar(attr.value))
  }
275 276
}

fxy060608's avatar
fxy060608 已提交
277 278 279 280 281 282 283 284
function processFor (el) {
  if (el.for && isVar(el.for)) {
    el.for = `_$f(${el.forId},{forItems:${el.for}})`
  }
}

function transformNode (el, parent, state) {
  parseBlock(el)
fxy060608's avatar
fxy060608 已提交
285 286
  parseEvent(el)

fxy060608's avatar
init v3  
fxy060608 已提交
287 288 289
  removeStatic(el)
  renameBinding(el)

290
  processAttrs(el)
fxy060608's avatar
init v3  
fxy060608 已提交
291 292
  processText(el)

fxy060608's avatar
fxy060608 已提交
293
  updateForEleId(el, state)
fxy060608's avatar
init v3  
fxy060608 已提交
294

fxy060608's avatar
fxy060608 已提交
295
  processFor(el)
fxy060608's avatar
init v3  
fxy060608 已提交
296 297 298 299 300
  processKey(el)
  processIf(el)
  processDirs(el)
}

fxy060608's avatar
fxy060608 已提交
301
function postTransformNode (el, options) {
fxy060608's avatar
fxy060608 已提交
302 303 304 305 306
  if (!el.parent) { // 从根节点开始递归处理
    traverseNode(el, false, {
      forIteratorId: 0,
      transformNode
    })
fxy060608's avatar
fxy060608 已提交
307
    optimize(el, options)
fxy060608's avatar
fxy060608 已提交
308 309
  }
}
fxy060608's avatar
init v3  
fxy060608 已提交
310 311 312 313
module.exports = {
  preTransformNode,
  postTransformNode,
  genData
fxy060608's avatar
fxy060608 已提交
314
}