Skip to content

Commit

Permalink
feat(compile): supports compiling v-if to the weex native directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Hanks10100 authored and yyx990803 committed Dec 19, 2017
1 parent 8b893c1 commit 7ad368e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
6 changes: 3 additions & 3 deletions flow/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ declare type ASTNode = ASTElement | ASTText | ASTExpression;
declare type ASTElement = {
type: 1;
tag: string;
attrsList: Array<{ name: string; value: string }>;
attrsMap: { [key: string]: string | null };
attrsList: Array<{ name: string; value: any }>;
attrsMap: { [key: string]: any };
parent: ASTElement | void;
children: Array<ASTNode>;

Expand All @@ -90,7 +90,7 @@ declare type ASTElement = {
hasBindings?: boolean;

text?: string;
attrs?: Array<{ name: string; value: string }>;
attrs?: Array<{ name: string; value: any }>;
props?: Array<{ name: string; value: string }>;
plain?: boolean;
pre?: true;
Expand Down
4 changes: 3 additions & 1 deletion src/platforms/weex/compiler/modules/recycle-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { transformText } from './text'
import { transformVBind } from './v-bind'
import { transformVIf } from './v-if'

let currentRecycleList = null

Expand All @@ -14,6 +15,8 @@ function preTransformNode (el: ASTElement) {
function transformNode (el: ASTElement) {
if (currentRecycleList) {
// TODO
transformVIf(el)
transformVBind(el)
}
}

Expand All @@ -23,7 +26,6 @@ function postTransformNode (el: ASTElement) {
if (el.tag === 'text') {
transformText(el)
}
transformVBind(el)
}
if (el === currentRecycleList) {
currentRecycleList = null
Expand Down
19 changes: 7 additions & 12 deletions src/platforms/weex/compiler/modules/recycle-list/v-bind.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
/* @flow */

import { camelize } from 'shared/util'
import { getAndRemoveAttr, addAttr } from 'compiler/helpers'

function isBindingAttr (name) {
function isBindingAttr (name: string): boolean {
return /^(v\-bind)?\:/.test(name)
}

function parseRealName (name: string): string {
return name.replace(/^(v\-bind)?\:/, '')
function parseAttrName (name: string): string {
return camelize(name.replace(/^(v\-bind)?\:/, ''))
}

export function transformVBind (el: ASTElement) {
if (!el.attrsList.length) {
if (!el.attrsList || !el.attrsList.length) {
return
}
el.attrsList.forEach(attr => {
// console.log('is binding attr:', attr.name, isBindingAttr(attr.name))
if (isBindingAttr(attr.name)) {
const realName: string = parseRealName(attr.name)
const name: string = parseAttrName(attr.name)
const binding = getAndRemoveAttr(el, attr.name)
if (el.attrs) {
el.attrs = el.attrs.filter(at => at.name !== realName) // omit duplicated
}
getAndRemoveAttr(el, realName)
addAttr(el, realName, { '@binding': binding })
addAttr(el, name, { '@binding': binding })
}
})
el.hasBindings = false
// el.plain = true
}
29 changes: 29 additions & 0 deletions src/platforms/weex/compiler/modules/recycle-list/v-if.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* @flow */

import { getAndRemoveAttr, addAttr } from 'compiler/helpers'

function isConditionAttr (name: string): boolean {
return /^v\-if|v\-else|v\-else\-if/.test(name)
}

export function transformVIf (el: ASTElement) {
for (const attr in el.attrsMap) {
if (!isConditionAttr(attr)) {
continue
}
const binding = getAndRemoveAttr(el, attr)
switch (attr) {
case 'v-if': {
addAttr(el, '[[match]]', binding)
el.attrsMap['[[match]]'] = binding
el.attrsList.push({ name: '[[match]]', value: binding })
delete el.attrsMap[attr]
delete el.if
delete el.ifConditions
break
}

// TODO: support v-else and v-else-if
}
}
}

0 comments on commit 7ad368e

Please sign in to comment.