service.js 6.4 KB
Newer Older
fxy060608's avatar
init v3  
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 227 228 229 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 271 272 273 274 275 276 277 278 279 280 281 282 283 284
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) { // <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) {
  // 因为时机问题,在最后处理根节点时,遍历处理 ifConditions
  !el.parent && processIfConditions(el)
}

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

  if (el.text) {
    const ret = parseText(el.text, false, state)
    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))
  }
}

function postTransformNode (el) {
  removeStatic(el)
  renameBinding(el)

  processText(el)

  updateForEleId(el)

  processKey(el)
  processIf(el)
  processDirs(el)
}

module.exports = {
  preTransformNode,
  postTransformNode,
  genData
}