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

fxy060608's avatar
fxy060608 已提交
10
const parseTag = require('./tag-parser')
fxy060608's avatar
init v3  
fxy060608 已提交
11
const parseText = require('./text-parser')
fxy060608's avatar
fxy060608 已提交
12 13
const parseEvent = require('./event-parser')
const parseComponent = require('./component-parser')
fxy060608's avatar
init v3  
fxy060608 已提交
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

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

function createGenVar (id) {
  return function genVar (name, extra = '') {
    if (/^\d+$/.test(id)) {
      return `${DATA_ROOT}['${id}']['${name}']${extra}`
    }
    return `${DATA_ROOT}[${id}]['${name}']${extra}`
  }
}

// if 使用该方案是因为 template 节点之类无法挂靠 extras
function processIfConditions (el) {
  if (el.if) {
    el.ifConditions.forEach(con => {
      if (isVar(con.exp)) {
        con.exp = createGenVar(con.block.attrsMap[ID])(con.block.elseif ? 'v-else-if' : 'v-if')
      }
    })

    el.if = createGenVar(el.attrsMap[ID])('v-if')
  }

  el.children && el.children.forEach(child => {
    processIfConditions(child)
  })

  el.scopedSlots && Object.values(el.scopedSlots).forEach(child => {
    processIfConditions(child)
  })
}

function processBinding (el, genVar) {
  if (el.classBinding) {
    el.classBinding = genVar('c')
  }

  if (el.styleBinding) {
    el.styleBinding = genVar('s')
  }
}

function processFor (el, genVal) {
  if (el.for && isVar(el.for)) {
    el.for = createGenVar(el.forId)('v-for')
    // <div><li v-for=" { a, b }  in items"></li></div>
    // =>
    // <div><li v-for="$item in items"></li></div>
    if (el.alias[0] === '{') {
      el.alias = '$item'
    }
    // items 只有两种格式 [1,2,3],[{k0:'1-0',k1:'1-1'}]
    // <div><li v-for="(item,key,index) in items"></li></div>
    // =>
    // <div><li v-for="(item,index) in items"></li></div>
    if (el.iterator2) {
      el.iterator1 = el.iterator2
      delete el.iterator2
    }
  }
}

function processKey (el) {
  // add default key
  processForKey(el)

  if (el.key) { // renderList key
    const forEl = getForEl(el)
    if (forEl) {
      if (!isVar(forEl.for)) {
        return
      }
      if (forEl === el) { // <view v-for="item in items" :key="item.id"></view>
        el.key = forEl.alias
      } 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 = `${forEl.alias}['k${keyIndex}']`
      }
    } else {
      isVar(el.key) && (el.key = createGenVar(el.attrsMap[ID])('a-key'))
    }
  }
}

function processIf (el) {
  // 因为时机问题,在最后处理根节点时,遍历处理 ifConditions
  !el.parent && processIfConditions(el)
}

function processDirs (el, genVar) {
  if (el.directives) {
    el.directives.forEach(dir => {
      dir.value && (dir.value = genVar('v-' + dir.name))
      dir.isDynamicArg && (dir.arg = genVar('v-' + dir.name + '-arg'))
    })
  }
}

function processAttrs (el, genVar) {
  el.attrs.forEach(attr => {
    attr.name !== ID && isVar(attr.value) && (attr.value = genVar('a-' + attr.name))
  })
}

function processProps (el, genVar) {
  el.props && el.props.forEach(prop => {
    isVar(prop.value) && (prop.value = genVar('a-' + prop.name))
  })
}

fxy060608's avatar
fxy060608 已提交
125
function processText (el, parent) {
fxy060608's avatar
init v3  
fxy060608 已提交
126 127 128
  const state = {
    index: 0,
    view: true,
fxy060608's avatar
fxy060608 已提交
129
    genVar: createGenVar(parent.attrsMap[ID])
fxy060608's avatar
init v3  
fxy060608 已提交
130
  }
fxy060608's avatar
fxy060608 已提交
131
  el.expression = parseText(el.text, false, state).expression
fxy060608's avatar
init v3  
fxy060608 已提交
132 133
}

fxy060608's avatar
fxy060608 已提交
134 135
function transformNode (el, parent) {
  // 更新 id
fxy060608's avatar
init v3  
fxy060608 已提交
136 137
  updateForEleId(el)

fxy060608's avatar
fxy060608 已提交
138 139 140 141
  if (el.type !== 1) {
    return (el.type === 2 && processText(el, parent))
  }

fxy060608's avatar
init v3  
fxy060608 已提交
142 143 144 145 146 147 148 149 150 151
  const id = el.attrsMap[ID]
  const genVar = createGenVar(id)

  processFor(el, genVar)
  processKey(el)
  processIf(el)
  processBinding(el, genVar)
  processDirs(el, genVar)
  processAttrs(el, genVar)
  processProps(el, genVar)
fxy060608's avatar
fxy060608 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

function traverseNode (el, parent) {
  transformNode(el, parent)
  el.children && el.children.forEach(child => traverseNode(child, el))
  el.scopedSlots && Object.values(el.scopedSlots).forEach(slot => traverseNode(slot, el))
}

function postTransformNode (el) {
  // 需要提前处理的内容
  parseComponent(el)
  parseTag(el)
  parseEvent(el)

  if (!el.parent) { // 从根节点开始递归处理
    traverseNode(el)
  }
fxy060608's avatar
init v3  
fxy060608 已提交
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
}

function processEvents (events) {
  Object.keys(events).forEach(name => {
    const modifiers = Object.create(null)

    let type = name
    const isPassive = type.charAt(0) === '&'
    type = isPassive ? type.slice(1) : type

    const isOnce = type.charAt(0) === '~'
    type = isOnce ? type.slice(1) : type

    const isCapture = type.charAt(0) === '!'
    type = isCapture ? type.slice(1) : type

    isPassive && (modifiers.passive = true)
    isOnce && (modifiers.once = true)
    isCapture && (modifiers.capture = true)

    const eventOpts = events[name]
    if (Array.isArray(eventOpts)) {
      eventOpts.forEach(eventOpt => {
        eventOpt.modifiers && Object.assign(modifiers, eventOpt.modifiers)
      })
    } else {
      eventOpts.modifiers && Object.assign(modifiers, eventOpts.modifiers)
    }
    if (Object.keys(modifiers).length) {
      events[name] = {
        value: `$handleViewEvent($event,${JSON.stringify(modifiers)})`
      }
    } else {
      events[name] = {
        value: `$handleViewEvent($event)`
      }
    }
  })
}

function genData (el) {
  if (el.model) {
    el.model.callback = `function ($$v) {}`
  }

  // 放在 postTransformNode 中处理的时机太靠前,v-model 等指令会新增 event
  // 理想情况,应该移除自定义组件的 events 配置,但目前不太好准确确定是自定义组件
  el.events && processEvents(el.events)
  el.nativeEvents && processEvents(el.nativeEvents)
  return ''
}

module.exports = {
  preTransformNode,
  postTransformNode,
  genData
}