Skip to content

Commit 7cc0b55

Browse files
committed
feat(weex): update new syntax for <recycle-list>
1 parent 472a289 commit 7cc0b55

38 files changed

+131
-73
lines changed

src/compiler/parser/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,14 @@ export function processFor (el: ASTElement) {
360360
}
361361
}
362362

363-
export function parseFor (exp: string): ?Object {
363+
type ForParseResult = {
364+
for: string;
365+
alias: string;
366+
iterator1?: string;
367+
iterator2?: string;
368+
};
369+
370+
export function parseFor (exp: string): ?ForParseResult {
364371
const inMatch = exp.match(forAliasRE)
365372
if (!inMatch) return
366373
const res = {}

src/platforms/weex/compiler/modules/recycle-list/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* @flow */
22

3+
import { preTransformRecycleList } from './recycle-list'
34
import { postTransformComponent } from './component'
45
import { postTransformComponentRoot } from './component-root'
56
import { postTransformText } from './text'
@@ -17,6 +18,7 @@ function shouldCompile (el: ASTElement, options: WeexCompilerOptions) {
1718

1819
function preTransformNode (el: ASTElement, options: WeexCompilerOptions) {
1920
if (el.tag === 'recycle-list') {
21+
preTransformRecycleList(el, options)
2022
currentRecycleList = el
2123
}
2224
if (shouldCompile(el, options)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* @flow */
2+
3+
import { parseFor } from 'compiler/parser/index'
4+
import { getAndRemoveAttr, addRawAttr } from 'compiler/helpers'
5+
6+
/**
7+
* Map the following syntax to corresponding attrs:
8+
*
9+
* <recycle-list for="(item, i) in longList" switch="cellType">
10+
* <cell-slot case="A"> ... </cell-slot>
11+
* <cell-slot case="B"> ... </cell-slot>
12+
* </recycle-list>
13+
*/
14+
15+
export function preTransformRecycleList (
16+
el: ASTElement,
17+
options: WeexCompilerOptions
18+
) {
19+
const exp = getAndRemoveAttr(el, 'for')
20+
if (!exp) {
21+
if (options.warn) {
22+
options.warn(`Invalid <recycle-list> syntax: missing "for" expression.`)
23+
}
24+
return
25+
}
26+
27+
const res = parseFor(exp)
28+
if (!res) {
29+
if (options.warn) {
30+
options.warn(`Invalid <recycle-list> syntax: ${exp}.`)
31+
}
32+
return
33+
}
34+
35+
addRawAttr(el, ':list-data', res.for)
36+
addRawAttr(el, 'alias', res.alias)
37+
if (res.iterator2) {
38+
// (item, key, index) for object iteration
39+
// is this even supported?
40+
addRawAttr(el, 'index', res.iterator2)
41+
} else if (res.iterator1) {
42+
addRawAttr(el, 'index', res.iterator1)
43+
}
44+
45+
const switchKey = getAndRemoveAttr(el, 'switch')
46+
if (switchKey) {
47+
addRawAttr(el, 'switch', switchKey)
48+
}
49+
}

test/weex/cases/recycle-list/attrs.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
{ type: 'A', count: 2, source: 'http://whatever.com/y.png' },
88
{ type: 'A', count: 3, source: 'http://whatever.com/z.png' }
99
],
10-
templateKey: 'type',
10+
switch: 'type',
1111
alias: 'item'
1212
},
1313
children: [{
1414
type: 'cell-slot',
15-
attr: { append: 'tree', templateType: 'A' },
15+
attr: { append: 'tree', case: 'A' },
1616
children: [{
1717
type: 'image',
1818
attr: {

test/weex/cases/recycle-list/attrs.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<image resize="cover" :src="item.source">
55
<text lines="3" v-bind:count="item.count"></text>
66
</cell-slot>

test/weex/cases/recycle-list/classname.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A', color: 'red' },
77
{ type: 'A', color: 'blue' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
style: {
1616
backgroundColor: '#FF6600'
1717
},

test/weex/cases/recycle-list/classname.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A" class="cell">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A" class="cell">
44
<text :class="['text', item.color]">content</text>
55
</cell-slot>
66
</recycle-list>

test/weex/cases/recycle-list/components/stateful-lifecycle.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'X' },
77
{ type: 'X' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'X' },
14+
attr: { append: 'tree', case: 'X' },
1515
children: [{
1616
type: 'div',
1717
attr: {

test/weex/cases/recycle-list/components/stateful-lifecycle.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="X">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="X">
44
<lifecycle></lifecycle>
55
</cell-slot>
66
</recycle-list>

test/weex/cases/recycle-list/components/stateful-v-model.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A' },
77
{ type: 'A' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
children: [{
1616
type: 'div',
1717
attr: {

test/weex/cases/recycle-list/components/stateful-v-model.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<editor message="No binding"></editor>
55
</cell-slot>
66
</recycle-list>

test/weex/cases/recycle-list/components/stateful.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A', number: 24 },
77
{ type: 'A', number: 42 }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
children: [{
1616
type: 'div',
1717
attr: {

test/weex/cases/recycle-list/components/stateful.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<counter :start="item.number"></counter>
55
<text>other</text>
66
</cell-slot>

test/weex/cases/recycle-list/components/stateless-multi-components.vdom.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
{ type: 'B', poster: 'yy', title: 'y' },
88
{ type: 'A' }
99
],
10-
templateKey: 'type',
10+
switch: 'type',
1111
alias: 'item'
1212
},
1313
children: [{
1414
type: 'cell-slot',
15-
attr: { append: 'tree', templateType: 'A' },
15+
attr: { append: 'tree', case: 'A' },
1616
children: [{
1717
type: 'div',
1818
attr: {
@@ -52,7 +52,7 @@
5252
}]
5353
}, {
5454
type: 'cell-slot',
55-
attr: { append: 'tree', templateType: 'B' },
55+
attr: { append: 'tree', case: 'B' },
5656
children: [{
5757
type: 'div',
5858
attr: {

test/weex/cases/recycle-list/components/stateless-multi-components.vue

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<banner></banner>
55
<text>----</text>
66
<footer></footer>
77
</cell-slot>
8-
<cell-slot template-type="B">
8+
<cell-slot case="B">
99
<banner></banner>
1010
<poster :image-url="item.poster" :title="item.title"></poster>
1111
</cell-slot>

test/weex/cases/recycle-list/components/stateless-with-props.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A', poster: 'xx', title: 'x' },
77
{ type: 'A', poster: 'yy', title: 'y' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
children: [{
1616
type: 'div',
1717
attr: {

test/weex/cases/recycle-list/components/stateless-with-props.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<poster :image-url="item.poster" :title="item.title"></poster>
55
<text>content</text>
66
</cell-slot>

test/weex/cases/recycle-list/components/stateless.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A' },
77
{ type: 'A' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
children: [{
1616
type: 'div',
1717
attr: {

test/weex/cases/recycle-list/components/stateless.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<banner></banner>
55
<text>content</text>
66
</cell-slot>

test/weex/cases/recycle-list/inline-style.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A', color: '#606060' },
77
{ type: 'A', color: '#E5E5E5' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
style: {
1616
backgroundColor: '#FF6600'
1717
},

test/weex/cases/recycle-list/inline-style.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A" style="background-color:#FF6600">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A" style="background-color:#FF6600">
44
<text :style="{ fontSize: '100px', color: item.color }">content</text>
55
</cell-slot>
66
</recycle-list>

test/weex/cases/recycle-list/text-node.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A', dynamic: 'decimal', two: '2', four: '4' },
77
{ type: 'A', dynamic: 'binary', two: '10', four: '100' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
children: [{
1616
type: 'text',
1717
attr: {

test/weex/cases/recycle-list/text-node.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<text>static</text>
55
<text>{{item.dynamic}}</text>
66
<text>one {{item.two}} three {{ item.four }} five</text>

test/weex/cases/recycle-list/v-else-if.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A' },
77
{ type: 'A' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
children: [{
1616
type: 'image',
1717
attr: {

test/weex/cases/recycle-list/v-else-if.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<image v-if="item.sourceA" :src="item.sourceA"></image>
55
<image v-else-if="item.sourceB" :src="item.sourceB"></image>
66
<image v-else :src="item.placeholder"></image>

test/weex/cases/recycle-list/v-else.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A' },
77
{ type: 'A' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
children: [{
1616
type: 'image',
1717
attr: {

test/weex/cases/recycle-list/v-else.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<recycle-list :list-data="longList" template-key="type" alias="item">
3-
<cell-slot template-type="A">
2+
<recycle-list for="item in longList" switch="type">
3+
<cell-slot case="A">
44
<image v-if="item.source" :src="item.source"></image>
55
<image v-else v-bind:src="item.placeholder"></image>
66
</cell-slot>

test/weex/cases/recycle-list/v-for-iterator.vdom.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
{ type: 'A' },
77
{ type: 'A' }
88
],
9-
templateKey: 'type',
9+
switch: 'type',
1010
alias: 'item'
1111
},
1212
children: [{
1313
type: 'cell-slot',
14-
attr: { append: 'tree', templateType: 'A' },
14+
attr: { append: 'tree', case: 'A' },
1515
children: [{
1616
type: 'div',
1717
attr: {

0 commit comments

Comments
 (0)