decorators.km 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. type Decorator
  2. &(Component) => Component;
  3. export function with:
  4. &(Component, Decorator) => Component
  5. &(cpt, f) => { f(cpt) };
  6. export function with:
  7. &(Component, List[Decorator]) => Component
  8. &(cpt, @f) => cpt.{apply-all @f.{Seq}};
  9. /*
  10. export function with:[T]
  11. &((&(T) => Component), Decorator) => (&(T) => Component)
  12. &(ctor, f) => (&(arg) => { f { ctor(arg) } });
  13. export function with:[T]
  14. &((&(T) => Component), List[Decorator]) => (&(T) => Component)
  15. &(ctor, @f) => (&(arg) => {ctor(arg)}.{apply-all @f.{Seq}});
  16. */
  17. export function RenderCond:
  18. &(Computed[Bool]) => Decorator
  19. &(@render) => &(cpt) =>
  20. \ render := switch-map @render,
  21. if render:
  22. cpt,
  23. else:
  24. Empty;
  25. export function ShowCond:
  26. &(Computed[Bool]) => Decorator
  27. &(@show) => &(@node) =>
  28. \ (node, show) := Computed (@node, @show),
  29. if show:
  30. node,
  31. else:
  32. (node with { Styles [('display','none')] });
  33. export function Style:
  34. &(List[String]) => Decorator
  35. &(class-list) => &(@node) =>
  36. let class := (class-list join ' '),
  37. (@node map &(node) => (node with { Attrs [('class',class)] }));
  38. export function Style:
  39. &(Computed[List[String]]) => Decorator
  40. &(@class-list) => &(@node) =>
  41. \ (class-list, node) := Computed (@class-list, @node),
  42. let class := (class-list join ' '),
  43. (node with { Attrs [('class',class)] });
  44. export function InlineStyle:
  45. &(List[(String,String)]) => Decorator
  46. &(rules) => &(@node) =>
  47. \ node := map @node,
  48. (node with { Styles rules });
  49. export function InlineStyle:
  50. &(Computed[List[(String,String)]]) => Decorator
  51. &(@rules) => &(@node) =>
  52. \ (rules, node) := Computed (@rules, @node),
  53. (node with { Styles rules });
  54. export function Event:
  55. &(String,EventPayloadConsumer) => Decorator
  56. &(name,consumer) => &(@node) =>
  57. let handler := consumer.{EventHandler},
  58. \ node := map @node,
  59. (node with { Events [(name,handler)] });
  60. export function Focusable:
  61. &() => Decorator
  62. &() => &(@node) =>
  63. \ node := map @node,
  64. (node with { Attrs [('tabindex','0')] });
  65. export function Autofocus:
  66. &() => Decorator
  67. &() => &(@node) =>
  68. \ node := map @node,
  69. (node with { Attrs [('webuiAutofocus','webuiAutofocus')] });
  70. export function EnableCond:
  71. &(Computed[Bool]) => Decorator
  72. &(@enabled) => &(@node) =>
  73. \ (enabled, node) := Computed (@enabled, @node),
  74. if enabled:
  75. node,
  76. else:
  77. (node with { Attrs [('disabled', 'disabled')] });