Evan You
8 years ago
14 changed files with 166 additions and 157 deletions
@ -1,148 +0,0 @@ |
|||||
/* @flow */ |
|
||||
|
|
||||
import { isPrimitive, warn } from '../util/index' |
|
||||
import VNode from './vnode' |
|
||||
|
|
||||
export function normalizeChildren ( |
|
||||
children: any, |
|
||||
ns: string | void, |
|
||||
nestedIndex: number | void |
|
||||
): Array<VNode> | void { |
|
||||
if (isPrimitive(children)) { |
|
||||
return [createTextVNode(children)] |
|
||||
} |
|
||||
if (Array.isArray(children)) { |
|
||||
const res = [] |
|
||||
for (let i = 0, l = children.length; i < l; i++) { |
|
||||
const c = children[i] |
|
||||
const last = res[res.length - 1] |
|
||||
// nested
|
|
||||
if (Array.isArray(c)) { |
|
||||
res.push.apply(res, normalizeChildren(c, ns, i)) |
|
||||
} else if (isPrimitive(c)) { |
|
||||
if (last && last.text) { |
|
||||
last.text += String(c) |
|
||||
} else if (c !== '') { |
|
||||
// convert primitive to vnode
|
|
||||
res.push(createTextVNode(c)) |
|
||||
} |
|
||||
} else if (c instanceof VNode) { |
|
||||
if (c.text && last && last.text) { |
|
||||
last.text += c.text |
|
||||
} else { |
|
||||
// inherit parent namespace
|
|
||||
if (ns) { |
|
||||
applyNS(c, ns) |
|
||||
} |
|
||||
// default key for nested array children (likely generated by v-for)
|
|
||||
if (c.tag && c.key == null && nestedIndex != null) { |
|
||||
c.key = `__vlist_${nestedIndex}_${i}__` |
|
||||
} |
|
||||
res.push(c) |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return res |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
function createTextVNode (val) { |
|
||||
return new VNode(undefined, undefined, undefined, String(val)) |
|
||||
} |
|
||||
|
|
||||
function applyNS (vnode, ns) { |
|
||||
if (vnode.tag && !vnode.ns) { |
|
||||
vnode.ns = ns |
|
||||
if (vnode.children) { |
|
||||
for (let i = 0, l = vnode.children.length; i < l; i++) { |
|
||||
applyNS(vnode.children[i], ns) |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
export function getFirstComponentChild (children: ?Array<any>) { |
|
||||
return children && children.filter(c => c && c.componentOptions)[0] |
|
||||
} |
|
||||
|
|
||||
export function mergeVNodeHook (def: Object, hookKey: string, hook: Function, key: string) { |
|
||||
key = key + hookKey |
|
||||
const injectedHash = def.__injected || (def.__injected = {}) |
|
||||
if (!injectedHash[key]) { |
|
||||
injectedHash[key] = true |
|
||||
const oldHook = def[hookKey] |
|
||||
if (oldHook) { |
|
||||
def[hookKey] = function () { |
|
||||
oldHook.apply(this, arguments) |
|
||||
hook.apply(this, arguments) |
|
||||
} |
|
||||
} else { |
|
||||
def[hookKey] = hook |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
export function updateListeners ( |
|
||||
on: Object, |
|
||||
oldOn: Object, |
|
||||
add: Function, |
|
||||
remove: Function, |
|
||||
vm: Component |
|
||||
) { |
|
||||
let name, cur, old, fn, event, capture |
|
||||
for (name in on) { |
|
||||
cur = on[name] |
|
||||
old = oldOn[name] |
|
||||
if (!cur) { |
|
||||
process.env.NODE_ENV !== 'production' && warn( |
|
||||
`Invalid handler for event "${name}": got ` + String(cur), |
|
||||
vm |
|
||||
) |
|
||||
} else if (!old) { |
|
||||
capture = name.charAt(0) === '!' |
|
||||
event = capture ? name.slice(1) : name |
|
||||
if (Array.isArray(cur)) { |
|
||||
add(event, (cur.invoker = arrInvoker(cur)), capture) |
|
||||
} else { |
|
||||
if (!cur.invoker) { |
|
||||
fn = cur |
|
||||
cur = on[name] = {} |
|
||||
cur.fn = fn |
|
||||
cur.invoker = fnInvoker(cur) |
|
||||
} |
|
||||
add(event, cur.invoker, capture) |
|
||||
} |
|
||||
} else if (cur !== old) { |
|
||||
if (Array.isArray(old)) { |
|
||||
old.length = cur.length |
|
||||
for (let i = 0; i < old.length; i++) old[i] = cur[i] |
|
||||
on[name] = old |
|
||||
} else { |
|
||||
old.fn = cur |
|
||||
on[name] = old |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
for (name in oldOn) { |
|
||||
if (!on[name]) { |
|
||||
event = name.charAt(0) === '!' ? name.slice(1) : name |
|
||||
remove(event, oldOn[name].invoker) |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
function arrInvoker (arr: Array<Function>): Function { |
|
||||
return function (ev) { |
|
||||
const single = arguments.length === 1 |
|
||||
for (let i = 0; i < arr.length; i++) { |
|
||||
single ? arr[i](ev) : arr[i].apply(null, arguments) |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
function fnInvoker (o: { fn: Function }): Function { |
|
||||
return function (ev) { |
|
||||
const single = arguments.length === 1 |
|
||||
single ? o.fn(ev) : o.fn.apply(null, arguments) |
|
||||
} |
|
||||
} |
|
@ -0,0 +1,9 @@ |
|||||
|
/* @flow */ |
||||
|
|
||||
|
export * from './merge-hook' |
||||
|
export * from './update-listeners' |
||||
|
export * from './normalize-children' |
||||
|
|
||||
|
export function getFirstComponentChild (children: ?Array<any>): ?VNodeWithData { |
||||
|
return children && children.filter(c => c && c.componentOptions)[0] |
||||
|
} |
@ -0,0 +1,18 @@ |
|||||
|
/* @flow */ |
||||
|
|
||||
|
export function mergeVNodeHook (def: Object, hookKey: string, hook: Function, key: string) { |
||||
|
key = key + hookKey |
||||
|
const injectedHash = def.__injected || (def.__injected = {}) |
||||
|
if (!injectedHash[key]) { |
||||
|
injectedHash[key] = true |
||||
|
const oldHook = def[hookKey] |
||||
|
if (oldHook) { |
||||
|
def[hookKey] = function () { |
||||
|
oldHook.apply(this, arguments) |
||||
|
hook.apply(this, arguments) |
||||
|
} |
||||
|
} else { |
||||
|
def[hookKey] = hook |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,62 @@ |
|||||
|
/* @flow */ |
||||
|
|
||||
|
import { isPrimitive } from 'core/util/index' |
||||
|
import VNode from 'core/vdom/vnode' |
||||
|
|
||||
|
export function normalizeChildren ( |
||||
|
children: any, |
||||
|
ns: string | void, |
||||
|
nestedIndex: number | void |
||||
|
): Array<VNode> | void { |
||||
|
if (isPrimitive(children)) { |
||||
|
return [createTextVNode(children)] |
||||
|
} |
||||
|
if (Array.isArray(children)) { |
||||
|
const res = [] |
||||
|
for (let i = 0, l = children.length; i < l; i++) { |
||||
|
const c = children[i] |
||||
|
const last = res[res.length - 1] |
||||
|
// nested
|
||||
|
if (Array.isArray(c)) { |
||||
|
res.push.apply(res, normalizeChildren(c, ns, i)) |
||||
|
} else if (isPrimitive(c)) { |
||||
|
if (last && last.text) { |
||||
|
last.text += String(c) |
||||
|
} else if (c !== '') { |
||||
|
// convert primitive to vnode
|
||||
|
res.push(createTextVNode(c)) |
||||
|
} |
||||
|
} else if (c instanceof VNode) { |
||||
|
if (c.text && last && last.text) { |
||||
|
last.text += c.text |
||||
|
} else { |
||||
|
// inherit parent namespace
|
||||
|
if (ns) { |
||||
|
applyNS(c, ns) |
||||
|
} |
||||
|
// default key for nested array children (likely generated by v-for)
|
||||
|
if (c.tag && c.key == null && nestedIndex != null) { |
||||
|
c.key = `__vlist_${nestedIndex}_${i}__` |
||||
|
} |
||||
|
res.push(c) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return res |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function createTextVNode (val) { |
||||
|
return new VNode(undefined, undefined, undefined, String(val)) |
||||
|
} |
||||
|
|
||||
|
function applyNS (vnode, ns) { |
||||
|
if (vnode.tag && !vnode.ns) { |
||||
|
vnode.ns = ns |
||||
|
if (vnode.children) { |
||||
|
for (let i = 0, l = vnode.children.length; i < l; i++) { |
||||
|
applyNS(vnode.children[i], ns) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,68 @@ |
|||||
|
/* @flow */ |
||||
|
|
||||
|
import { warn } from 'core/util/index' |
||||
|
|
||||
|
export function updateListeners ( |
||||
|
on: Object, |
||||
|
oldOn: Object, |
||||
|
add: Function, |
||||
|
remove: Function, |
||||
|
vm: Component |
||||
|
) { |
||||
|
let name, cur, old, fn, event, capture |
||||
|
for (name in on) { |
||||
|
cur = on[name] |
||||
|
old = oldOn[name] |
||||
|
if (!cur) { |
||||
|
process.env.NODE_ENV !== 'production' && warn( |
||||
|
`Invalid handler for event "${name}": got ` + String(cur), |
||||
|
vm |
||||
|
) |
||||
|
} else if (!old) { |
||||
|
capture = name.charAt(0) === '!' |
||||
|
event = capture ? name.slice(1) : name |
||||
|
if (Array.isArray(cur)) { |
||||
|
add(event, (cur.invoker = arrInvoker(cur)), capture) |
||||
|
} else { |
||||
|
if (!cur.invoker) { |
||||
|
fn = cur |
||||
|
cur = on[name] = {} |
||||
|
cur.fn = fn |
||||
|
cur.invoker = fnInvoker(cur) |
||||
|
} |
||||
|
add(event, cur.invoker, capture) |
||||
|
} |
||||
|
} else if (cur !== old) { |
||||
|
if (Array.isArray(old)) { |
||||
|
old.length = cur.length |
||||
|
for (let i = 0; i < old.length; i++) old[i] = cur[i] |
||||
|
on[name] = old |
||||
|
} else { |
||||
|
old.fn = cur |
||||
|
on[name] = old |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
for (name in oldOn) { |
||||
|
if (!on[name]) { |
||||
|
event = name.charAt(0) === '!' ? name.slice(1) : name |
||||
|
remove(event, oldOn[name].invoker) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function arrInvoker (arr: Array<Function>): Function { |
||||
|
return function (ev) { |
||||
|
const single = arguments.length === 1 |
||||
|
for (let i = 0; i < arr.length; i++) { |
||||
|
single ? arr[i](ev) : arr[i].apply(null, arguments) |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function fnInvoker (o: { fn: Function }): Function { |
||||
|
return function (ev) { |
||||
|
const single = arguments.length === 1 |
||||
|
single ? o.fn(ev) : o.fn.apply(null, arguments) |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue