You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.2 KiB

4 years ago
  1. // Expressions
  2. export enum ExpressionType {
  3. ATTRIBUTE,
  4. EVENT,
  5. TEXT,
  6. VALUE,
  7. }
  8. export interface BaseExpressionData {
  9. type: ExpressionType
  10. evaluate(scope: any): any
  11. }
  12. export interface AttributeExpressionData extends BaseExpressionData {
  13. name: string
  14. }
  15. export interface EventExpressionData extends BaseExpressionData {
  16. name: string
  17. }
  18. export interface TextExpressionData extends BaseExpressionData {
  19. childNodeIndex: number
  20. }
  21. export interface ValueExpressionData extends BaseExpressionData {}
  22. export type ExpressionData = AttributeExpressionData | EventExpressionData | TextExpressionData | ValueExpressionData
  23. export interface Expression<Scope = any> {
  24. type: ExpressionType
  25. node: HTMLElement
  26. value: any
  27. mount(scope: Scope): Expression
  28. update(scope: Scope): Expression
  29. unmount(scope: Scope): Expression
  30. }
  31. // Bindings
  32. export enum BindingType {
  33. EACH,
  34. IF,
  35. SIMPLE,
  36. TAG,
  37. SLOT,
  38. }
  39. export interface BaseBindingData {
  40. selector?: string
  41. redundantAttribute?: string
  42. type?: BindingType
  43. evaluate?(scope: any): any
  44. }
  45. export interface EachBindingData extends BaseBindingData {
  46. itemName: string
  47. indexName?: number
  48. template: TemplateChunk
  49. getKey?: ((scope: any) => any) | null
  50. condition?: ((scope: any) => any) | null
  51. }
  52. export interface IfBindingData extends BaseBindingData {
  53. template: TemplateChunk
  54. }
  55. export interface SimpleBindingData extends BaseBindingData {
  56. expressions: ExpressionData[]
  57. }
  58. export interface SlotBindingData extends BaseBindingData {
  59. id: string
  60. html: string
  61. bindings: BindingData
  62. }
  63. export interface TagBindingData extends BaseBindingData {
  64. getComponent(name: string): TemplateChunk
  65. attributes: AttributeExpressionData[]
  66. slots: SlotBindingData[]
  67. }
  68. export type BindingData = IfBindingData | EachBindingData | SimpleBindingData | SlotBindingData | TagBindingData
  69. export interface Binding<Scope = any, ParentScope = any> {
  70. mount(el: HTMLElement, scope: Scope, parentScope: ParentScope, meta: TemplateChunkMeta): Binding
  71. update(scope: Scope, parentScope: ParentScope): Binding
  72. unmount(scope: Scope, parentScope: ParentScope, mustRemoveRoot: boolean): Binding
  73. }
  74. // Template Object
  75. export interface TemplateChunkMeta {
  76. fragment: DocumentFragment
  77. children: HTMLCollection
  78. avoidDOMInjection: boolean
  79. }
  80. export interface TemplateChunk<Scope = any, ParentScope = any> {
  81. mount(el: HTMLElement, scope: Scope, parentScope: ParentScope, meta: TemplateChunkMeta): TemplateChunk
  82. update(scope: Scope, parentScope: ParentScope): TemplateChunk
  83. unmount(scope: Scope, parentScope: ParentScope, mustRemoveRoot: boolean): TemplateChunk
  84. clone(): TemplateChunk
  85. createDOM(el: HTMLElement): TemplateChunk
  86. bindings?: Binding<Scope, ParentScope>[]
  87. bindingsData?: BindingData[]
  88. html?: string | null
  89. isTemplateTag?: boolean
  90. fragment?: DocumentFragment
  91. children?: HTMLCollection
  92. dom?: HTMLElement
  93. el?: HTMLElement
  94. }
  95. // API
  96. export function template(template: string, bindings: BindingData[]): TemplateChunk
  97. export function createBinding(root: HTMLElement, binding: BindingData, templateTagOffset?: number | null): Binding
  98. export function createExpression(node: HTMLElement, expression: ExpressionData): Expression
  99. export const bindingTypes:BindingType
  100. export const expressionTypes:ExpressionType