|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import { cloneVNode, cloneVNodes } from 'core/vdom/vnode' |
| 4 | + |
| 5 | +/** |
| 6 | + * Runtime helper for rendering static trees. |
| 7 | + */ |
| 8 | +export function renderStatic ( |
| 9 | + index: number, |
| 10 | + isInFor?: boolean |
| 11 | +): VNode | Array<VNode> { |
| 12 | + let tree = this._staticTrees[index] |
| 13 | + // if has already-rendered static tree and not inside v-for, |
| 14 | + // we can reuse the same tree by doing a shallow clone. |
| 15 | + if (tree && !isInFor) { |
| 16 | + return Array.isArray(tree) |
| 17 | + ? cloneVNodes(tree) |
| 18 | + : cloneVNode(tree) |
| 19 | + } |
| 20 | + // otherwise, render a fresh tree. |
| 21 | + tree = this._staticTrees[index] = |
| 22 | + this.$options.staticRenderFns[index].call(this._renderProxy) |
| 23 | + markStatic(tree, `__static__${index}`, false) |
| 24 | + return tree |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Runtime helper for v-once. |
| 29 | + * Effectively it means marking the node as static with a unique key. |
| 30 | + */ |
| 31 | +export function markOnce ( |
| 32 | + tree: VNode | Array<VNode>, |
| 33 | + index: number, |
| 34 | + key: string |
| 35 | +) { |
| 36 | + markStatic(tree, `__once__${index}${key ? `_${key}` : ``}`, true) |
| 37 | + return tree |
| 38 | +} |
| 39 | + |
| 40 | +function markStatic ( |
| 41 | + tree: VNode | Array<VNode>, |
| 42 | + key: string, |
| 43 | + isOnce: boolean |
| 44 | +) { |
| 45 | + if (Array.isArray(tree)) { |
| 46 | + for (let i = 0; i < tree.length; i++) { |
| 47 | + if (tree[i] && typeof tree[i] !== 'string') { |
| 48 | + markStaticNode(tree[i], `${key}_${i}`, isOnce) |
| 49 | + } |
| 50 | + } |
| 51 | + } else { |
| 52 | + markStaticNode(tree, key, isOnce) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function markStaticNode (node, key, isOnce) { |
| 57 | + node.isStatic = true |
| 58 | + node.key = key |
| 59 | + node.isOnce = isOnce |
| 60 | +} |
0 commit comments