From 7cc0b559e9e57fcb3baeae5d8d4c8964aa335b5e Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 19 Dec 2017 16:19:07 -0500 Subject: [PATCH] feat(weex): update new syntax for --- src/compiler/parser/index.js | 9 +++- .../compiler/modules/recycle-list/index.js | 2 + .../modules/recycle-list/recycle-list.js | 49 +++++++++++++++++++ test/weex/cases/recycle-list/attrs.vdom.js | 4 +- test/weex/cases/recycle-list/attrs.vue | 4 +- .../weex/cases/recycle-list/classname.vdom.js | 4 +- test/weex/cases/recycle-list/classname.vue | 4 +- .../components/stateful-lifecycle.vdom.js | 4 +- .../components/stateful-lifecycle.vue | 4 +- .../components/stateful-v-model.vdom.js | 4 +- .../components/stateful-v-model.vue | 4 +- .../recycle-list/components/stateful.vdom.js | 4 +- .../recycle-list/components/stateful.vue | 4 +- .../stateless-multi-components.vdom.js | 6 +-- .../components/stateless-multi-components.vue | 6 +-- .../components/stateless-with-props.vdom.js | 4 +- .../components/stateless-with-props.vue | 4 +- .../recycle-list/components/stateless.vdom.js | 4 +- .../recycle-list/components/stateless.vue | 4 +- .../cases/recycle-list/inline-style.vdom.js | 4 +- test/weex/cases/recycle-list/inline-style.vue | 4 +- .../weex/cases/recycle-list/text-node.vdom.js | 4 +- test/weex/cases/recycle-list/text-node.vue | 4 +- .../weex/cases/recycle-list/v-else-if.vdom.js | 4 +- test/weex/cases/recycle-list/v-else-if.vue | 4 +- test/weex/cases/recycle-list/v-else.vdom.js | 4 +- test/weex/cases/recycle-list/v-else.vue | 4 +- .../cases/recycle-list/v-for-iterator.vdom.js | 4 +- .../cases/recycle-list/v-for-iterator.vue | 4 +- test/weex/cases/recycle-list/v-for.vdom.js | 4 +- test/weex/cases/recycle-list/v-for.vue | 4 +- test/weex/cases/recycle-list/v-if.vdom.js | 4 +- test/weex/cases/recycle-list/v-if.vue | 4 +- .../cases/recycle-list/v-on-inline.vdom.js | 4 +- test/weex/cases/recycle-list/v-on-inline.vue | 4 +- test/weex/cases/recycle-list/v-on.vdom.js | 4 +- test/weex/cases/recycle-list/v-on.vue | 4 +- test/weex/compiler/append.spec.js | 4 +- 38 files changed, 131 insertions(+), 73 deletions(-) create mode 100644 src/platforms/weex/compiler/modules/recycle-list/recycle-list.js diff --git a/src/compiler/parser/index.js b/src/compiler/parser/index.js index 464ece1595..ba1b726870 100644 --- a/src/compiler/parser/index.js +++ b/src/compiler/parser/index.js @@ -360,7 +360,14 @@ export function processFor (el: ASTElement) { } } -export function parseFor (exp: string): ?Object { +type ForParseResult = { + for: string; + alias: string; + iterator1?: string; + iterator2?: string; +}; + +export function parseFor (exp: string): ?ForParseResult { const inMatch = exp.match(forAliasRE) if (!inMatch) return const res = {} diff --git a/src/platforms/weex/compiler/modules/recycle-list/index.js b/src/platforms/weex/compiler/modules/recycle-list/index.js index d163c2d854..1b85a45f99 100644 --- a/src/platforms/weex/compiler/modules/recycle-list/index.js +++ b/src/platforms/weex/compiler/modules/recycle-list/index.js @@ -1,5 +1,6 @@ /* @flow */ +import { preTransformRecycleList } from './recycle-list' import { postTransformComponent } from './component' import { postTransformComponentRoot } from './component-root' import { postTransformText } from './text' @@ -17,6 +18,7 @@ function shouldCompile (el: ASTElement, options: WeexCompilerOptions) { function preTransformNode (el: ASTElement, options: WeexCompilerOptions) { if (el.tag === 'recycle-list') { + preTransformRecycleList(el, options) currentRecycleList = el } if (shouldCompile(el, options)) { diff --git a/src/platforms/weex/compiler/modules/recycle-list/recycle-list.js b/src/platforms/weex/compiler/modules/recycle-list/recycle-list.js new file mode 100644 index 0000000000..22e3b5d552 --- /dev/null +++ b/src/platforms/weex/compiler/modules/recycle-list/recycle-list.js @@ -0,0 +1,49 @@ +/* @flow */ + +import { parseFor } from 'compiler/parser/index' +import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers' + +/** + * Map the following syntax to corresponding attrs: + * + * + * ... + * ... + * + */ + +export function preTransformRecycleList ( + el: ASTElement, + options: WeexCompilerOptions +) { + const exp = getAndRemoveAttr(el, 'for') + if (!exp) { + if (options.warn) { + options.warn(`Invalid syntax: missing "for" expression.`) + } + return + } + + const res = parseFor(exp) + if (!res) { + if (options.warn) { + options.warn(`Invalid syntax: ${exp}.`) + } + return + } + + addRawAttr(el, ':list-data', res.for) + addRawAttr(el, 'alias', res.alias) + if (res.iterator2) { + // (item, key, index) for object iteration + // is this even supported? + addRawAttr(el, 'index', res.iterator2) + } else if (res.iterator1) { + addRawAttr(el, 'index', res.iterator1) + } + + const switchKey = getAndRemoveAttr(el, 'switch') + if (switchKey) { + addRawAttr(el, 'switch', switchKey) + } +} diff --git a/test/weex/cases/recycle-list/attrs.vdom.js b/test/weex/cases/recycle-list/attrs.vdom.js index 0d15ed8341..ccce367fc3 100644 --- a/test/weex/cases/recycle-list/attrs.vdom.js +++ b/test/weex/cases/recycle-list/attrs.vdom.js @@ -7,12 +7,12 @@ { type: 'A', count: 2, source: 'http://whatever.com/y.png' }, { type: 'A', count: 3, source: 'http://whatever.com/z.png' } ], - templateKey: 'type', + switch: 'type', alias: 'item' }, children: [{ type: 'cell-slot', - attr: { append: 'tree', templateType: 'A' }, + attr: { append: 'tree', case: 'A' }, children: [{ type: 'image', attr: { diff --git a/test/weex/cases/recycle-list/attrs.vue b/test/weex/cases/recycle-list/attrs.vue index b9de8f9e71..1e2d0318af 100644 --- a/test/weex/cases/recycle-list/attrs.vue +++ b/test/weex/cases/recycle-list/attrs.vue @@ -1,6 +1,6 @@