cf904b5e9c71113df215139138435ce78cac084cfbe7b06b13d44a52afc5dd5d5c16c345ebeb35c8633785264bf0aa3beb1a2de50cfdeef453fae735c0d261 1.2 KB

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