فهرست منبع

feat(weex): update new syntax for <recycle-list>

Evan You 8 سال پیش
والد
کامیت
7cc0b559e9
38فایلهای تغییر یافته به همراه131 افزوده شده و 73 حذف شده
  1. 8 1
      src/compiler/parser/index.js
  2. 2 0
      src/platforms/weex/compiler/modules/recycle-list/index.js
  3. 49 0
      src/platforms/weex/compiler/modules/recycle-list/recycle-list.js
  4. 2 2
      test/weex/cases/recycle-list/attrs.vdom.js
  5. 2 2
      test/weex/cases/recycle-list/attrs.vue
  6. 2 2
      test/weex/cases/recycle-list/classname.vdom.js
  7. 2 2
      test/weex/cases/recycle-list/classname.vue
  8. 2 2
      test/weex/cases/recycle-list/components/stateful-lifecycle.vdom.js
  9. 2 2
      test/weex/cases/recycle-list/components/stateful-lifecycle.vue
  10. 2 2
      test/weex/cases/recycle-list/components/stateful-v-model.vdom.js
  11. 2 2
      test/weex/cases/recycle-list/components/stateful-v-model.vue
  12. 2 2
      test/weex/cases/recycle-list/components/stateful.vdom.js
  13. 2 2
      test/weex/cases/recycle-list/components/stateful.vue
  14. 3 3
      test/weex/cases/recycle-list/components/stateless-multi-components.vdom.js
  15. 3 3
      test/weex/cases/recycle-list/components/stateless-multi-components.vue
  16. 2 2
      test/weex/cases/recycle-list/components/stateless-with-props.vdom.js
  17. 2 2
      test/weex/cases/recycle-list/components/stateless-with-props.vue
  18. 2 2
      test/weex/cases/recycle-list/components/stateless.vdom.js
  19. 2 2
      test/weex/cases/recycle-list/components/stateless.vue
  20. 2 2
      test/weex/cases/recycle-list/inline-style.vdom.js
  21. 2 2
      test/weex/cases/recycle-list/inline-style.vue
  22. 2 2
      test/weex/cases/recycle-list/text-node.vdom.js
  23. 2 2
      test/weex/cases/recycle-list/text-node.vue
  24. 2 2
      test/weex/cases/recycle-list/v-else-if.vdom.js
  25. 2 2
      test/weex/cases/recycle-list/v-else-if.vue
  26. 2 2
      test/weex/cases/recycle-list/v-else.vdom.js
  27. 2 2
      test/weex/cases/recycle-list/v-else.vue
  28. 2 2
      test/weex/cases/recycle-list/v-for-iterator.vdom.js
  29. 2 2
      test/weex/cases/recycle-list/v-for-iterator.vue
  30. 2 2
      test/weex/cases/recycle-list/v-for.vdom.js
  31. 2 2
      test/weex/cases/recycle-list/v-for.vue
  32. 2 2
      test/weex/cases/recycle-list/v-if.vdom.js
  33. 2 2
      test/weex/cases/recycle-list/v-if.vue
  34. 2 2
      test/weex/cases/recycle-list/v-on-inline.vdom.js
  35. 2 2
      test/weex/cases/recycle-list/v-on-inline.vue
  36. 2 2
      test/weex/cases/recycle-list/v-on.vdom.js
  37. 2 2
      test/weex/cases/recycle-list/v-on.vue
  38. 2 2
      test/weex/compiler/append.spec.js

+ 8 - 1
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 = {}

+ 2 - 0
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)) {

+ 49 - 0
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:
+ *
+ * <recycle-list for="(item, i) in longList" switch="cellType">
+ *   <cell-slot case="A"> ... </cell-slot>
+ *   <cell-slot case="B"> ... </cell-slot>
+ * </recycle-list>
+ */
+
+export function preTransformRecycleList (
+  el: ASTElement,
+  options: WeexCompilerOptions
+) {
+  const exp = getAndRemoveAttr(el, 'for')
+  if (!exp) {
+    if (options.warn) {
+      options.warn(`Invalid <recycle-list> syntax: missing "for" expression.`)
+    }
+    return
+  }
+
+  const res = parseFor(exp)
+  if (!res) {
+    if (options.warn) {
+      options.warn(`Invalid <recycle-list> 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)
+  }
+}

+ 2 - 2
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: {

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

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

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

@@ -6,12 +6,12 @@
       { type: 'A', color: 'red' },
       { type: 'A', color: 'blue' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     style: {
       backgroundColor: '#FF6600'
     },

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A" class="cell">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A" class="cell">
       <text :class="['text', item.color]">content</text>
     </cell-slot>
   </recycle-list>

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

@@ -6,12 +6,12 @@
       { type: 'X' },
       { type: 'X' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'X' },
+    attr: { append: 'tree', case: 'X' },
     children: [{
       type: 'div',
       attr: {

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="X">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="X">
       <lifecycle></lifecycle>
     </cell-slot>
   </recycle-list>

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

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'div',
       attr: {

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <editor message="No binding"></editor>
     </cell-slot>
   </recycle-list>

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

@@ -6,12 +6,12 @@
       { type: 'A', number: 24 },
       { type: 'A', number: 42 }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'div',
       attr: {

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <counter :start="item.number"></counter>
       <text>other</text>
     </cell-slot>

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

@@ -7,12 +7,12 @@
       { type: 'B', poster: 'yy', title: 'y' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'div',
       attr: {
@@ -52,7 +52,7 @@
     }]
   }, {
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'B' },
+    attr: { append: 'tree', case: 'B' },
     children: [{
       type: 'div',
       attr: {

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

@@ -1,11 +1,11 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <banner></banner>
       <text>----</text>
       <footer></footer>
     </cell-slot>
-    <cell-slot template-type="B">
+    <cell-slot case="B">
       <banner></banner>
       <poster :image-url="item.poster" :title="item.title"></poster>
     </cell-slot>

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

@@ -6,12 +6,12 @@
       { type: 'A', poster: 'xx', title: 'x' },
       { type: 'A', poster: 'yy', title: 'y' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'div',
       attr: {

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <poster :image-url="item.poster" :title="item.title"></poster>
       <text>content</text>
     </cell-slot>

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

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'div',
       attr: {

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <banner></banner>
       <text>content</text>
     </cell-slot>

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

@@ -6,12 +6,12 @@
       { type: 'A', color: '#606060' },
       { type: 'A', color: '#E5E5E5' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     style: {
       backgroundColor: '#FF6600'
     },

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A" style="background-color:#FF6600">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A" style="background-color:#FF6600">
       <text :style="{ fontSize: '100px', color: item.color }">content</text>
     </cell-slot>
   </recycle-list>

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

@@ -6,12 +6,12 @@
       { type: 'A', dynamic: 'decimal', two: '2', four: '4' },
       { type: 'A', dynamic: 'binary', two: '10', four: '100' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'text',
       attr: {

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <text>static</text>
       <text>{{item.dynamic}}</text>
       <text>one {{item.two}} three {{ item.four }} five</text>

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

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'image',
       attr: {

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <image v-if="item.sourceA" :src="item.sourceA"></image>
       <image v-else-if="item.sourceB" :src="item.sourceB"></image>
       <image v-else :src="item.placeholder"></image>

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

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'image',
       attr: {

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

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <image v-if="item.source" :src="item.source"></image>
       <image v-else v-bind:src="item.placeholder"></image>
     </cell-slot>

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

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'div',
       attr: {

+ 2 - 2
test/weex/cases/recycle-list/v-for-iterator.vue

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <div v-for="(object, index) in item.list" :key="index">
         <text>{{object.name}}</text>
         <text v-for="(v, k, i) in object" :key="k">{{v}}</text>

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

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'div',
       attr: {

+ 2 - 2
test/weex/cases/recycle-list/v-for.vue

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <div v-for="panel in item.list" :key="panel.id">
         <text>{{panel.label}}</text>
       </div>

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

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'image',
       attr: {

+ 2 - 2
test/weex/cases/recycle-list/v-if.vue

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <image v-if="item.source" :src="item.source"></image>
       <text v-if="!item.source">Title</text>
     </cell-slot>

+ 2 - 2
test/weex/cases/recycle-list/v-on-inline.vdom.js

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'text',
       event: ['click', {

+ 2 - 2
test/weex/cases/recycle-list/v-on-inline.vue

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <text v-on:click="toggle()" @longpress="toggle(item.key)"></text>
       <text @appear="onappear(item.index, 'static', item.type, $event)">Button</text>
       <text @disappear="onappear(25, 'static')">Tips</text>

+ 2 - 2
test/weex/cases/recycle-list/v-on.vdom.js

@@ -6,12 +6,12 @@
       { type: 'A' },
       { type: 'A' }
     ],
-    templateKey: 'type',
+    switch: 'type',
     alias: 'item'
   },
   children: [{
     type: 'cell-slot',
-    attr: { append: 'tree', templateType: 'A' },
+    attr: { append: 'tree', case: 'A' },
     children: [{
       type: 'text',
       event: ['click', 'longpress'],

+ 2 - 2
test/weex/cases/recycle-list/v-on.vue

@@ -1,6 +1,6 @@
 <template>
-  <recycle-list :list-data="longList" template-key="type" alias="item">
-    <cell-slot template-type="A">
+  <recycle-list for="item in longList" switch="type">
+    <cell-slot case="A">
       <text v-on:click="handler" @longpress="move">A</text>
       <text @touchend="move">B</text>
     </cell-slot>

+ 2 - 2
test/weex/compiler/append.spec.js

@@ -26,9 +26,9 @@ describe('append props', () => {
   })
 
   it('add append="tree" on <recycle-list>', () => {
-    const { render, staticRenderFns, errors } = compile(`<recycle-list><div></div></recycle-list>`)
+    const { render, staticRenderFns, errors } = compile(`<recycle-list for="item in list"><div></div></recycle-list>`)
     expect(render + staticRenderFns).toMatch(strToRegExp(`appendAsTree:true`))
-    expect(render + staticRenderFns).toMatch(strToRegExp(`attrs:{"append":"tree"}`))
+    expect(render + staticRenderFns).toMatch(strToRegExp(`attrs:{"listData":list,"alias":"item","append":"tree"}`))
     expect(errors).toEqual([])
   })