glass-easel-miniprogram-adapter 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/.eslintignore +4 -0
  2. package/dist/glass_easel_miniprogram_adapter.d.ts +872 -1
  3. package/dist/glass_easel_miniprogram_adapter.es.js +2 -0
  4. package/dist/glass_easel_miniprogram_adapter.es.js.map +1 -0
  5. package/dist/glass_easel_miniprogram_adapter.js +2 -2
  6. package/dist/glass_easel_miniprogram_adapter.js.map +1 -1
  7. package/jest.config.js +7 -4
  8. package/package.json +8 -6
  9. package/rollup.config.ts +83 -0
  10. package/src/backend.ts +7 -3
  11. package/src/behavior.ts +8 -2
  12. package/src/builder/base_behavior_builder.ts +437 -0
  13. package/src/builder/behavior_builder.ts +265 -0
  14. package/src/builder/component_builder.ts +337 -0
  15. package/src/builder/index.ts +2 -0
  16. package/src/builder/type_utils.ts +98 -0
  17. package/src/component.ts +10 -7
  18. package/src/env.ts +73 -0
  19. package/src/index.ts +3 -77
  20. package/src/intersection.ts +2 -2
  21. package/src/media_query.ts +4 -6
  22. package/src/selector_query.ts +3 -3
  23. package/src/space.ts +11 -1030
  24. package/src/types.ts +7 -5
  25. package/tests/base/env.ts +1 -1
  26. package/tests/env.test.ts +3 -3
  27. package/tests/selector.test.ts +137 -14
  28. package/tests/space.test.ts +8 -14
  29. package/tsconfig.json +1 -3
  30. package/dist/types/src/backend.d.ts +0 -59
  31. package/dist/types/src/backend.d.ts.map +0 -1
  32. package/dist/types/src/behavior.d.ts +0 -35
  33. package/dist/types/src/behavior.d.ts.map +0 -1
  34. package/dist/types/src/component.d.ts +0 -170
  35. package/dist/types/src/component.d.ts.map +0 -1
  36. package/dist/types/src/index.d.ts +0 -58
  37. package/dist/types/src/index.d.ts.map +0 -1
  38. package/dist/types/src/intersection.d.ts +0 -24
  39. package/dist/types/src/intersection.d.ts.map +0 -1
  40. package/dist/types/src/media_query.d.ts +0 -13
  41. package/dist/types/src/media_query.d.ts.map +0 -1
  42. package/dist/types/src/selector_query.d.ts +0 -98
  43. package/dist/types/src/selector_query.d.ts.map +0 -1
  44. package/dist/types/src/space.d.ts +0 -369
  45. package/dist/types/src/space.d.ts.map +0 -1
  46. package/dist/types/src/types.d.ts +0 -77
  47. package/dist/types/src/types.d.ts.map +0 -1
  48. package/dist/types/src/utils.d.ts +0 -2
  49. package/dist/types/src/utils.d.ts.map +0 -1
  50. package/dist/types/tests/base/env.d.ts +0 -3
  51. package/dist/types/tests/base/env.d.ts.map +0 -1
  52. package/dist/types/tests/data_update.test.d.ts +0 -2
  53. package/dist/types/tests/data_update.test.d.ts.map +0 -1
  54. package/dist/types/tests/env.test.d.ts +0 -2
  55. package/dist/types/tests/env.test.d.ts.map +0 -1
  56. package/dist/types/tests/selector.test.d.ts +0 -2
  57. package/dist/types/tests/selector.test.d.ts.map +0 -1
  58. package/dist/types/tests/space.test.d.ts +0 -2
  59. package/dist/types/tests/space.test.d.ts.map +0 -1
  60. package/dist/types/tests/trait_behavior.test.d.ts +0 -2
  61. package/dist/types/tests/trait_behavior.test.d.ts.map +0 -1
  62. package/webpack.config.js +0 -79
package/src/space.ts CHANGED
@@ -1,25 +1,21 @@
1
1
  /* eslint-disable @typescript-eslint/no-unsafe-return */
2
2
 
3
3
  import * as glassEasel from 'glass-easel'
4
- import { MiniProgramEnv } from '.'
5
- import {
4
+ import { StyleIsolation } from './types'
5
+ import { guid } from './utils'
6
+ import { TraitBehavior } from './behavior'
7
+ import { BehaviorBuilder, ComponentBuilder } from './builder'
8
+ import type { MiniProgramEnv } from '.'
9
+ import type {
6
10
  ComponentStaticConfig,
7
11
  ComponentDefinitionOptions,
8
- StyleIsolation,
9
12
  BehaviorDefinition,
10
13
  ComponentDefinition,
11
14
  PageDefinition,
12
15
  utils as typeUtils,
13
16
  } from './types'
14
- import { guid } from './utils'
15
- import {
16
- Behavior,
17
- ComponentType,
18
- DefinitionFilter,
19
- GeneralBehavior,
20
- TraitBehavior,
21
- } from './behavior'
22
- import { AllData, Component, ComponentProto, GeneralComponent } from './component'
17
+ import type { Behavior } from './behavior'
18
+ import type { Component } from './component'
23
19
 
24
20
  // The page constructor
25
21
  export interface PageConstructor {
@@ -50,10 +46,9 @@ export interface BehaviorConstructor {
50
46
  TData extends DataList,
51
47
  TProperty extends PropertyList,
52
48
  TMethod extends MethodList,
53
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
54
49
  TComponentExport,
55
50
  >(
56
- definition: BehaviorDefinition<TData, TProperty, TMethod>,
51
+ definition: BehaviorDefinition<TData, TProperty, TMethod, TComponentExport>,
57
52
  ): Behavior<TData, TProperty, TMethod, never>
58
53
  trait<TIn extends { [key: string]: any }>(): TraitBehavior<TIn, TIn>
59
54
  trait<TIn extends { [key: string]: any }, TOut extends { [key: string]: any }>(
@@ -71,95 +66,7 @@ export interface ComponentEnv {
71
66
  type Empty = typeUtils.Empty
72
67
  type DataList = typeUtils.DataList
73
68
  type PropertyList = typeUtils.PropertyList
74
- type PropertyType = typeUtils.PropertyType
75
- type PropertyTypeToValueType<T extends PropertyType> = typeUtils.PropertyTypeToValueType<T>
76
69
  type MethodList = typeUtils.MethodList
77
- type ChainingFilterType = typeUtils.ChainingFilterType
78
- type ComponentMethod = typeUtils.ComponentMethod
79
- type TaggedMethod<Fn extends ComponentMethod> = typeUtils.TaggedMethod<Fn>
80
- type ChainingFilterFunc<
81
- TAddedFields extends { [key: string]: any },
82
- TRemovedFields extends string = never,
83
- > = typeUtils.ChainingFilterFunc<TAddedFields, TRemovedFields>
84
-
85
- export interface RelationHandler<TTarget, TOut> {
86
- list(): TTarget[]
87
- listAsTrait: TOut extends never ? undefined : () => TOut[]
88
- }
89
-
90
- export type TraitRelationParams<TOut extends { [key: string]: any }> = {
91
- target: TraitBehavior<any, TOut>
92
- type: 'ancestor' | 'descendant' | 'parent' | 'child' | 'parent-common-node' | 'child-common-node'
93
- linked?: (target: GeneralComponent) => void
94
- linkChanged?: (target: GeneralComponent) => void
95
- unlinked?: (target: GeneralComponent) => void
96
- linkFailed?: (target: GeneralComponent) => void
97
- }
98
-
99
- export type RelationParams = {
100
- target?: string | ComponentType<any, any, any, any> | GeneralBehavior | TraitBehavior<any>
101
- type: 'ancestor' | 'descendant' | 'parent' | 'child' | 'parent-common-node' | 'child-common-node'
102
- linked?: (target: GeneralComponent) => void
103
- linkChanged?: (target: GeneralComponent) => void
104
- unlinked?: (target: GeneralComponent) => void
105
- linkFailed?: (target: GeneralComponent) => void
106
- }
107
-
108
- export type Lifetimes = {
109
- created: () => void
110
- attached: () => void
111
- moved: () => void
112
- detached: () => void
113
- ready: () => void
114
- }
115
-
116
- export interface BuilderContext<
117
- TPrevData extends DataList,
118
- TProperty extends PropertyList,
119
- TMethodCaller,
120
- > extends ThisType<TMethodCaller> {
121
- self: TMethodCaller
122
- data: typeUtils.DeepReadonly<typeUtils.DataWithPropertyValues<TPrevData, TProperty>>
123
- setData: (newData: Partial<typeUtils.SetDataSetter<TPrevData>>, callback?: () => void) => void
124
- implement: <TIn extends { [x: string]: any }>(
125
- traitBehavior: TraitBehavior<TIn, any>,
126
- impl: TIn,
127
- ) => void
128
- relation<TOut extends { [key: string]: any }>(
129
- def: TraitRelationParams<TOut> & ThisType<TMethodCaller>,
130
- ): RelationHandler<any, TOut>
131
- relation(def: RelationParams & ThisType<TMethodCaller>): RelationHandler<any, never>
132
- observer<
133
- P extends typeUtils.ObserverDataPathStrings<
134
- typeUtils.DataWithPropertyValues<TPrevData, TProperty>
135
- >,
136
- V = typeUtils.DeepReadonly<
137
- typeUtils.GetFromObserverPathString<typeUtils.DataWithPropertyValues<TPrevData, TProperty>, P>
138
- >,
139
- >(
140
- paths: P,
141
- func: (newValue: V) => void,
142
- ): void
143
- observer<
144
- P extends typeUtils.ObserverDataPathStrings<
145
- typeUtils.DataWithPropertyValues<TPrevData, TProperty>
146
- >[],
147
- V = {
148
- [K in keyof P]: typeUtils.DeepReadonly<
149
- typeUtils.GetFromObserverPathString<
150
- typeUtils.DataWithPropertyValues<TPrevData, TProperty>,
151
- P[K]
152
- >
153
- >
154
- },
155
- >(
156
- paths: readonly [...P],
157
- func: (...newValues: V extends any[] ? V : never) => void,
158
- ): void
159
- lifetime: <L extends keyof Lifetimes>(name: L, func: Lifetimes[L]) => void
160
- pageLifetime: (name: string, func: (...args: any[]) => void) => void
161
- method: <Fn extends ComponentMethod>(func: Fn) => TaggedMethod<Fn>
162
- }
163
70
 
164
71
  /**
165
72
  * A component space with mini-program code manager
@@ -441,18 +348,16 @@ export class CodeSpace {
441
348
  TData extends DataList,
442
349
  TProperty extends PropertyList,
443
350
  TMethod extends MethodList,
444
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
445
351
  TComponentExport,
446
352
  >(
447
- definition: BehaviorDefinition<TData, TProperty, TMethod>,
353
+ definition: BehaviorDefinition<TData, TProperty, TMethod, TComponentExport>,
448
354
  ): Behavior<TData, TProperty, TMethod, never>
449
355
  function behaviorConstructor<
450
356
  TData extends DataList,
451
357
  TProperty extends PropertyList,
452
358
  TMethod extends MethodList,
453
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
454
359
  TComponentExport,
455
- >(definition?: BehaviorDefinition<TData, TProperty, TMethod>) {
360
+ >(definition?: BehaviorDefinition<TData, TProperty, TMethod, TComponentExport>) {
456
361
  if (definition !== undefined) {
457
362
  return self.behavior().definition(definition).register()
458
363
  }
@@ -504,927 +409,3 @@ export class CodeSpace {
504
409
  return new TraitBehavior(this.space.defineTraitBehavior<TIn, TOut>(trans))
505
410
  }
506
411
  }
507
-
508
- type ResolveBehaviorBuilder<
509
- B,
510
- TChainingFilter extends ChainingFilterType,
511
- > = typeUtils.IsNever<TChainingFilter> extends false
512
- ? TChainingFilter extends ChainingFilterType
513
- ? Omit<B, TChainingFilter['remove']> & TChainingFilter['add']
514
- : B
515
- : B
516
-
517
- export class BaseBehaviorBuilder<
518
- TPrevData extends DataList = Empty,
519
- TData extends DataList = Empty,
520
- TProperty extends PropertyList = Empty,
521
- TMethod extends MethodList = Empty,
522
- TChainingFilter extends ChainingFilterType = never,
523
- TPendingChainingFilter extends ChainingFilterType = never,
524
- TComponentExport = undefined,
525
- > {
526
- protected _$codeSpace!: CodeSpace
527
- protected _$!: glassEasel.BehaviorBuilder<
528
- TPrevData,
529
- TData,
530
- TProperty,
531
- TMethod,
532
- TChainingFilter,
533
- TPendingChainingFilter
534
- >
535
- protected _$parents: GeneralBehavior[] = []
536
-
537
- /** Implement a trait behavior */
538
- implement<TIn extends { [key: string]: any }>(
539
- traitBehavior: TraitBehavior<TIn, any>,
540
- impl: TIn,
541
- ): ResolveBehaviorBuilder<this, TChainingFilter> {
542
- this._$.implement(traitBehavior._$, impl)
543
- return this as any
544
- }
545
-
546
- /** Add external classes */
547
- externalClasses(list: string[]): this {
548
- this._$.externalClasses(list)
549
- return this
550
- }
551
-
552
- data<T extends DataList>(
553
- gen: () => typeUtils.NewFieldList<AllData<TData, TProperty>, T>,
554
- ): ResolveBehaviorBuilder<
555
- BaseBehaviorBuilder<
556
- T,
557
- TData & T,
558
- TProperty,
559
- TMethod,
560
- TChainingFilter,
561
- TPendingChainingFilter,
562
- TComponentExport
563
- >,
564
- TChainingFilter
565
- > {
566
- this._$.data(gen)
567
- return this as any
568
- }
569
-
570
- property<N extends string, T extends PropertyType, V extends PropertyTypeToValueType<T>>(
571
- name: N,
572
- def: N extends keyof (TData & TProperty) ? never : typeUtils.PropertyListItem<T, V>,
573
- ): ResolveBehaviorBuilder<
574
- BaseBehaviorBuilder<
575
- TPrevData,
576
- TData,
577
- TProperty & Record<N, unknown extends V ? T : typeUtils.PropertyOption<T, V>>,
578
- TMethod,
579
- TChainingFilter,
580
- TPendingChainingFilter,
581
- TComponentExport
582
- >,
583
- TChainingFilter
584
- > {
585
- this._$.property(name, def)
586
- return this as any
587
- }
588
-
589
- /**
590
- * Add some public methods
591
- *
592
- * The public method can be used as an event handler, and can be visited in component instance.
593
- */
594
- methods<T extends MethodList>(
595
- funcs: T & ThisType<Component<TData, TProperty, TMethod & T, any>>,
596
- ): ResolveBehaviorBuilder<
597
- BaseBehaviorBuilder<
598
- TPrevData,
599
- TData,
600
- TProperty,
601
- TMethod & T,
602
- TChainingFilter,
603
- TPendingChainingFilter,
604
- TComponentExport
605
- >,
606
- TChainingFilter
607
- > {
608
- this._$.methods(funcs)
609
- return this as any
610
- }
611
-
612
- /**
613
- * Add a data observer
614
- */
615
- observer<
616
- P extends typeUtils.ObserverDataPathStrings<
617
- typeUtils.DataWithPropertyValues<TPrevData, TProperty>
618
- >,
619
- V = typeUtils.DeepReadonly<
620
- typeUtils.GetFromObserverPathString<typeUtils.DataWithPropertyValues<TPrevData, TProperty>, P>
621
- >,
622
- >(
623
- paths: P,
624
- func: (this: Component<TData, TProperty, TMethod, any>, newValue: V) => void,
625
- once?: boolean,
626
- ): ResolveBehaviorBuilder<this, TChainingFilter>
627
- observer<
628
- P extends typeUtils.ObserverDataPathStrings<
629
- typeUtils.DataWithPropertyValues<TPrevData, TProperty>
630
- >[],
631
- V = {
632
- [K in keyof P]: typeUtils.DeepReadonly<
633
- typeUtils.GetFromObserverPathString<
634
- typeUtils.DataWithPropertyValues<TPrevData, TProperty>,
635
- P[K]
636
- >
637
- >
638
- },
639
- >(
640
- paths: readonly [...P],
641
- func: (
642
- this: Component<TData, TProperty, TMethod, any>,
643
- ...newValues: V extends any[] ? V : never
644
- ) => void,
645
- once?: boolean,
646
- ): ResolveBehaviorBuilder<this, TChainingFilter>
647
- observer(
648
- paths: string | readonly string[],
649
- func: (this: Component<TData, TProperty, TMethod, any>, ...args: any[]) => any,
650
- once = false,
651
- ): ResolveBehaviorBuilder<this, TChainingFilter> {
652
- this._$.observer(paths as any, func as any, once)
653
- return this as any
654
- }
655
-
656
- /**
657
- * Add a lifetime callback
658
- */
659
- lifetime<L extends keyof Lifetimes>(
660
- name: L,
661
- func: (
662
- this: Component<TData, TProperty, TMethod, any>,
663
- ...args: Parameters<Lifetimes[L]>
664
- ) => ReturnType<Lifetimes[L]>,
665
- once = false,
666
- ): ResolveBehaviorBuilder<this, TChainingFilter> {
667
- this._$.lifetime(name, func as any, once)
668
- return this as any
669
- }
670
-
671
- /**
672
- * Add a page-lifetime callback
673
- */
674
- pageLifetime(
675
- name: string,
676
- func: (this: Component<TData, TProperty, TMethod, any>, ...args: any[]) => any,
677
- once = false,
678
- ): ResolveBehaviorBuilder<this, TChainingFilter> {
679
- this._$.pageLifetime(name, func as any, once)
680
- return this as any
681
- }
682
-
683
- /**
684
- * Add a relation
685
- */
686
- relation(
687
- name: string,
688
- rel: RelationParams & ThisType<Component<TData, TProperty, TMethod, TComponentExport>>,
689
- ): ResolveBehaviorBuilder<this, TChainingFilter> {
690
- const target =
691
- rel.target instanceof Behavior || rel.target instanceof ComponentType
692
- ? rel.target._$
693
- : rel.target
694
- this._$.relation(name, {
695
- target,
696
- type: rel.type,
697
- linked: rel.linked,
698
- linkChanged: rel.linkChanged,
699
- unlinked: rel.unlinked,
700
- linkFailed: rel.linkFailed,
701
- } as any)
702
- return this as any
703
- }
704
-
705
- init<TExport extends Record<string, TaggedMethod<(...args: any[]) => any>> | void>(
706
- func: (
707
- this: Component<TData, TProperty, TMethod, TComponentExport>,
708
- builderContext: BuilderContext<
709
- TPrevData,
710
- TProperty,
711
- Component<TData, TProperty, TMethod, TComponentExport>
712
- >,
713
- ) => TExport,
714
- // eslint-disable-next-line function-paren-newline
715
- ): ResolveBehaviorBuilder<
716
- BaseBehaviorBuilder<
717
- TPrevData,
718
- TData,
719
- TProperty,
720
- TMethod,
721
- TChainingFilter,
722
- TPendingChainingFilter,
723
- TComponentExport
724
- >,
725
- TChainingFilter
726
- > {
727
- this._$.init(function ({
728
- data,
729
- setData,
730
- implement,
731
- relation,
732
- observer,
733
- lifetime,
734
- pageLifetime,
735
- method,
736
- }) {
737
- const relationInit = ((rel: RelationParams | TraitRelationParams<any>) => {
738
- if (rel.target instanceof TraitBehavior) {
739
- return relation({
740
- target: rel.target._$,
741
- type: rel.type,
742
- linked: rel.linked,
743
- linkChanged: rel.linkChanged,
744
- unlinked: rel.unlinked,
745
- linkFailed: rel.linkFailed,
746
- } as any)
747
- }
748
- const target =
749
- rel.target instanceof Behavior || rel.target instanceof ComponentType
750
- ? rel.target._$
751
- : rel.target
752
- return relation({
753
- target,
754
- type: rel.type,
755
- linked: rel.linked,
756
- linkChanged: rel.linkChanged,
757
- unlinked: rel.unlinked,
758
- linkFailed: rel.linkFailed,
759
- } as any)
760
- }) as BuilderContext<TPrevData, TProperty, TMethod>['relation']
761
-
762
- const implementInit = ((traitBehavior: TraitBehavior<any>, impl: Record<string, any>) => {
763
- implement(traitBehavior._$, impl)
764
- }) as BuilderContext<TPrevData, TProperty, TMethod>['implement']
765
-
766
- const methodCaller = this as unknown as Component<TData, TProperty, TMethod, TComponentExport>
767
-
768
- func.call(methodCaller, {
769
- self: methodCaller,
770
- data,
771
- setData: (newData, callback) => {
772
- setData(newData)
773
- if (callback) {
774
- glassEasel.triggerRender(methodCaller._$, () => {
775
- callback()
776
- })
777
- }
778
- },
779
- implement: implementInit,
780
- relation: relationInit,
781
- observer,
782
- lifetime,
783
- pageLifetime,
784
- method,
785
- })
786
- })
787
- return this as any
788
- }
789
-
790
- definition<
791
- TNewData extends DataList = Empty,
792
- TNewProperty extends PropertyList = Empty,
793
- TNewMethod extends MethodList = Empty,
794
- >(
795
- def: BehaviorDefinition<TNewData, TNewProperty, TNewMethod> &
796
- ThisType<
797
- Component<
798
- TData & TNewData,
799
- TProperty & TNewProperty,
800
- TMethod & TNewMethod,
801
- TComponentExport
802
- >
803
- >,
804
- ): ResolveBehaviorBuilder<
805
- BaseBehaviorBuilder<
806
- TPrevData,
807
- TData & TNewData,
808
- TProperty & TNewProperty,
809
- TMethod & TNewMethod,
810
- TChainingFilter,
811
- TPendingChainingFilter,
812
- TComponentExport
813
- >,
814
- TChainingFilter
815
- > {
816
- def.behaviors?.forEach((beh) => beh._$bindedDefinitionFilter?.(def))
817
- const inner = this._$
818
- const {
819
- behaviors,
820
- properties: rawProperties,
821
- data: rawData,
822
- observers: rawObservers,
823
- methods,
824
- created,
825
- attached,
826
- ready,
827
- moved,
828
- detached,
829
- lifetimes: rawLifetimes,
830
- pageLifetimes: rawPageLifetimes,
831
- relations: rawRelations,
832
- externalClasses,
833
- } = def
834
- behaviors?.forEach((beh) => {
835
- this._$parents.push(beh as GeneralBehavior)
836
- inner.behavior(beh._$)
837
- })
838
- if (rawData !== undefined) {
839
- if (typeof rawData === 'function') {
840
- inner.data(rawData as any)
841
- } else {
842
- inner.staticData(rawData as any)
843
- }
844
- }
845
- if (rawProperties !== undefined) {
846
- const keys = Object.keys(rawProperties)
847
- for (let i = 0; i < keys.length; i += 1) {
848
- const name = keys[i]!
849
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
850
- inner.property(name, rawProperties[name] as any)
851
- }
852
- }
853
- if (rawObservers !== undefined) {
854
- if (Array.isArray(rawObservers)) {
855
- for (let i = 0; i < rawObservers.length; i += 1) {
856
- const { fields, observer } = rawObservers[i]!
857
- inner.observer(fields || ('**' as any), observer)
858
- }
859
- } else {
860
- const keys = Object.keys(rawObservers)
861
- for (let i = 0; i < keys.length; i += 1) {
862
- const fields = keys[i]!
863
- const observer = rawObservers[fields]!
864
- inner.observer(fields as any, observer)
865
- }
866
- }
867
- }
868
- if (methods) inner.methods(methods)
869
- if (created && rawLifetimes?.created === undefined) inner.lifetime('created', created, true)
870
- if (attached && rawLifetimes?.attached === undefined) inner.lifetime('attached', attached, true)
871
- if (ready && rawLifetimes?.ready === undefined) inner.lifetime('ready', ready, true)
872
- if (moved && rawLifetimes?.moved === undefined) inner.lifetime('moved', moved, true)
873
- if (detached && rawLifetimes?.detached === undefined) inner.lifetime('detached', detached, true)
874
- if (rawLifetimes) {
875
- const keys = Object.keys(rawLifetimes)
876
- for (let i = 0; i < keys.length; i += 1) {
877
- const name = keys[i]!
878
- const func = rawLifetimes[name]!
879
- inner.lifetime(name as any, func, true)
880
- }
881
- }
882
- if (rawPageLifetimes) {
883
- const keys = Object.keys(rawPageLifetimes)
884
- for (let i = 0; i < keys.length; i += 1) {
885
- const name = keys[i]!
886
- const func = rawPageLifetimes[name]!
887
- inner.pageLifetime(name, func, true)
888
- }
889
- }
890
- if (rawRelations) {
891
- const keys = Object.keys(rawRelations)
892
- for (let i = 0; i < keys.length; i += 1) {
893
- const name = keys[i]!
894
- const rel = rawRelations[name]!
895
- inner.relation(name, rel)
896
- }
897
- }
898
- if (externalClasses) inner.externalClasses(externalClasses)
899
- return this as any
900
- }
901
- }
902
-
903
- export class BehaviorBuilder<
904
- TPrevData extends DataList = Empty,
905
- TData extends DataList = Empty,
906
- TProperty extends PropertyList = Empty,
907
- TMethod extends MethodList = Empty,
908
- TChainingFilter extends ChainingFilterType = never,
909
- TPendingChainingFilter extends ChainingFilterType = never,
910
- TComponentExport = undefined,
911
- > extends BaseBehaviorBuilder<
912
- TPrevData,
913
- TData,
914
- TProperty,
915
- TMethod,
916
- TChainingFilter,
917
- TPendingChainingFilter,
918
- TComponentExport
919
- > {
920
- private _$definitionFilter: DefinitionFilter | undefined
921
-
922
- /** @internal */
923
- static create(codeSpace: CodeSpace): BehaviorBuilder {
924
- const ret = new BehaviorBuilder()
925
- ret._$codeSpace = codeSpace
926
- ret._$ = codeSpace.getComponentSpace().defineWithMethodCaller()
927
- return ret
928
- }
929
-
930
- /** Define a chaining filter */
931
- chainingFilter<
932
- TAddedFields extends { [key: string]: any },
933
- TRemovedFields extends string = never,
934
- >(
935
- func: ChainingFilterFunc<TAddedFields, TRemovedFields>,
936
- ): ResolveBehaviorBuilder<
937
- BehaviorBuilder<
938
- TPrevData,
939
- TData,
940
- TProperty,
941
- TMethod,
942
- TChainingFilter,
943
- {
944
- add: TAddedFields
945
- remove: TRemovedFields
946
- },
947
- TComponentExport
948
- >,
949
- TChainingFilter
950
- > {
951
- this._$.chainingFilter(func as any)
952
- return this as any
953
- }
954
-
955
- /** Use another behavior */
956
- behavior<
957
- UData extends DataList,
958
- UProperty extends PropertyList,
959
- UMethod extends MethodList,
960
- UChainingFilter extends ChainingFilterType,
961
- >(
962
- behavior: Behavior<UData, UProperty, UMethod, UChainingFilter>,
963
- ): ResolveBehaviorBuilder<
964
- BehaviorBuilder<
965
- TPrevData,
966
- TData & UData,
967
- TProperty & UProperty,
968
- TMethod & UMethod,
969
- UChainingFilter,
970
- TPendingChainingFilter,
971
- TComponentExport
972
- >,
973
- UChainingFilter
974
- > {
975
- this._$parents.push(behavior as GeneralBehavior)
976
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
977
- this._$ = this._$.behavior(behavior._$)
978
- return this as any
979
- }
980
-
981
- /**
982
- * Add some template data fields
983
- *
984
- * It does not support raw data, but a `gen` function which returns the new data fields.
985
- * The `gen` function executes once during component creation.
986
- */
987
- override data<T extends DataList>(
988
- gen: () => typeUtils.NewFieldList<AllData<TData, TProperty>, T>,
989
- ): ResolveBehaviorBuilder<
990
- BehaviorBuilder<
991
- T,
992
- TData & T,
993
- TProperty,
994
- TMethod,
995
- TChainingFilter,
996
- TPendingChainingFilter,
997
- TComponentExport
998
- >,
999
- TChainingFilter
1000
- > {
1001
- return super.data(gen) as any
1002
- }
1003
-
1004
- /**
1005
- * Add a single property
1006
- *
1007
- * The property name should be different from other properties.
1008
- */
1009
- override property<N extends string, T extends PropertyType, V extends PropertyTypeToValueType<T>>(
1010
- name: N,
1011
- def: N extends keyof (TData & TProperty) ? never : typeUtils.PropertyListItem<T, V>,
1012
- ): ResolveBehaviorBuilder<
1013
- BehaviorBuilder<
1014
- TPrevData,
1015
- TData,
1016
- TProperty & Record<N, unknown extends V ? T : typeUtils.PropertyOption<T, V>>,
1017
- TMethod,
1018
- TChainingFilter,
1019
- TPendingChainingFilter,
1020
- TComponentExport
1021
- >,
1022
- TChainingFilter
1023
- > {
1024
- return super.property(name, def) as any
1025
- }
1026
-
1027
- /**
1028
- * Add some public methods
1029
- *
1030
- * The public method can be used as an event handler, and can be visited in component instance.
1031
- */
1032
- override methods<T extends MethodList>(
1033
- funcs: T & ThisType<Component<TData, TProperty, TMethod & T, any>>,
1034
- ): ResolveBehaviorBuilder<
1035
- BehaviorBuilder<
1036
- TPrevData,
1037
- TData,
1038
- TProperty,
1039
- TMethod & T,
1040
- TChainingFilter,
1041
- TPendingChainingFilter,
1042
- TComponentExport
1043
- >,
1044
- TChainingFilter
1045
- > {
1046
- return super.methods(funcs) as any
1047
- }
1048
-
1049
- /**
1050
- * Execute a function while component instance creation
1051
- *
1052
- * A `BuilderContext` is provided to tweak the component creation progress.
1053
- * The return value is used as the "export" value of the behavior,
1054
- * which can be imported by other behaviors.
1055
- */
1056
- override init<TExport extends Record<string, TaggedMethod<(...args: any[]) => any>> | void>(
1057
- func: (
1058
- this: Component<TData, TProperty, TMethod, TComponentExport>,
1059
- builderContext: BuilderContext<
1060
- TPrevData,
1061
- TProperty,
1062
- Component<TData, TProperty, TMethod, TComponentExport>
1063
- >,
1064
- ) => TExport,
1065
- // eslint-disable-next-line function-paren-newline
1066
- ): ResolveBehaviorBuilder<
1067
- BehaviorBuilder<
1068
- TPrevData,
1069
- TData,
1070
- TProperty,
1071
- TMethod,
1072
- TChainingFilter,
1073
- TPendingChainingFilter,
1074
- TComponentExport
1075
- >,
1076
- TChainingFilter
1077
- > {
1078
- return super.init(func) as any
1079
- }
1080
-
1081
- /** Apply a classic definition object */
1082
- override definition<
1083
- TNewData extends DataList = Empty,
1084
- TNewProperty extends PropertyList = Empty,
1085
- TNewMethod extends MethodList = Empty,
1086
- >(
1087
- def: BehaviorDefinition<TNewData, TNewProperty, TNewMethod> &
1088
- ThisType<
1089
- Component<
1090
- TData & TNewData,
1091
- TProperty & TNewProperty,
1092
- TMethod & TNewMethod,
1093
- TComponentExport
1094
- >
1095
- >,
1096
- ): ResolveBehaviorBuilder<
1097
- BehaviorBuilder<
1098
- TPrevData,
1099
- TData & TNewData,
1100
- TProperty & TNewProperty,
1101
- TMethod & TNewMethod,
1102
- TChainingFilter,
1103
- TPendingChainingFilter,
1104
- TComponentExport
1105
- >,
1106
- TChainingFilter
1107
- > {
1108
- super.definition(def)
1109
- if (def.definitionFilter) this._$definitionFilter = def.definitionFilter
1110
- return this as any
1111
- }
1112
-
1113
- /**
1114
- * Finish the behavior definition process
1115
- */
1116
- register(): Behavior<TData, TProperty, TMethod, TPendingChainingFilter> {
1117
- return new Behavior(this._$.registerBehavior(), this._$parents, this._$definitionFilter)
1118
- }
1119
- }
1120
-
1121
- /**
1122
- * A direct way to create a component
1123
- */
1124
- export class ComponentBuilder<
1125
- TPrevData extends DataList = Empty,
1126
- TData extends DataList = Empty,
1127
- TProperty extends PropertyList = Empty,
1128
- TMethod extends MethodList = Empty,
1129
- TChainingFilter extends ChainingFilterType = never,
1130
- TPendingChainingFilter extends ChainingFilterType = never,
1131
- TComponentExport = undefined,
1132
- > extends BaseBehaviorBuilder<
1133
- TPrevData,
1134
- TData,
1135
- TProperty,
1136
- TMethod,
1137
- TChainingFilter,
1138
- TPendingChainingFilter,
1139
- TComponentExport
1140
- > {
1141
- private _$is!: string
1142
- private _$alias?: string[]
1143
- private _$options?: ComponentDefinitionOptions
1144
- private _$export?: (source: GeneralComponent | null) => TComponentExport
1145
- private _$proto?: ComponentProto<TData, TProperty, TMethod, TComponentExport>
1146
-
1147
- /** @internal */
1148
- static create(codeSpace: CodeSpace, is: string, alias?: string[]) {
1149
- const ret = new ComponentBuilder()
1150
- const overallBehavior = codeSpace._$overallBehavior
1151
- ret._$codeSpace = codeSpace
1152
- ret._$ = codeSpace.getComponentSpace().defineWithMethodCaller(is || '')
1153
- ret._$is = is || ''
1154
- ret._$alias = alias
1155
- ret._$.methodCallerInit(function () {
1156
- const originalCaller = this as unknown as glassEasel.GeneralComponent
1157
- let proto = ret._$proto
1158
- if (proto === undefined) {
1159
- const methods = originalCaller.getComponentDefinition().behavior.getMethods()
1160
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1161
- proto = ret._$proto = new ComponentProto(methods) as any
1162
- }
1163
- const caller = proto!.derive()
1164
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1165
- caller._$ = originalCaller as any
1166
- caller._$export = ret._$export
1167
- return caller
1168
- })
1169
- if (overallBehavior) ret._$.behavior(overallBehavior)
1170
- return ret
1171
- }
1172
-
1173
- /**
1174
- * Set the component options
1175
- *
1176
- * If called multiple times, only the latest call is valid.
1177
- */
1178
- options(options: ComponentDefinitionOptions): ResolveBehaviorBuilder<this, TChainingFilter> {
1179
- this._$options = options
1180
- return this as any
1181
- }
1182
-
1183
- /** Use another behavior */
1184
- behavior<
1185
- UData extends DataList,
1186
- UProperty extends PropertyList,
1187
- UMethod extends MethodList,
1188
- UChainingFilter extends ChainingFilterType,
1189
- >(
1190
- behavior: Behavior<UData, UProperty, UMethod, UChainingFilter>,
1191
- ): ResolveBehaviorBuilder<
1192
- ComponentBuilder<
1193
- TPrevData,
1194
- TData & UData,
1195
- TProperty & UProperty,
1196
- TMethod & UMethod,
1197
- UChainingFilter,
1198
- TPendingChainingFilter,
1199
- TComponentExport
1200
- >,
1201
- UChainingFilter
1202
- > {
1203
- this._$parents.push(behavior as GeneralBehavior)
1204
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1205
- this._$ = this._$.behavior(behavior._$)
1206
- return this as any
1207
- }
1208
-
1209
- /** Set the export value when the component is being selected */
1210
- export<TNewComponentExport>(
1211
- f: (
1212
- this: Component<TData, TProperty, TMethod, TComponentExport>,
1213
- source: GeneralComponent | null,
1214
- ) => TNewComponentExport,
1215
- ): ResolveBehaviorBuilder<
1216
- ComponentBuilder<
1217
- TPrevData,
1218
- TData,
1219
- TProperty,
1220
- TMethod,
1221
- TChainingFilter,
1222
- TPendingChainingFilter,
1223
- TNewComponentExport
1224
- >,
1225
- TChainingFilter
1226
- > {
1227
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
1228
- this._$export = f as any
1229
- return this as any
1230
- }
1231
-
1232
- /**
1233
- * Add some template data fields
1234
- *
1235
- * It does not support raw data, but a `gen` function which returns the new data fields.
1236
- * The `gen` function executes once during component creation.
1237
- */
1238
- override data<T extends DataList>(
1239
- gen: () => typeUtils.NewFieldList<AllData<TData, TProperty>, T>,
1240
- ): ResolveBehaviorBuilder<
1241
- ComponentBuilder<
1242
- T,
1243
- TData & T,
1244
- TProperty,
1245
- TMethod,
1246
- TChainingFilter,
1247
- TPendingChainingFilter,
1248
- TComponentExport
1249
- >,
1250
- TChainingFilter
1251
- > {
1252
- return super.data(gen) as any
1253
- }
1254
-
1255
- /**
1256
- * Add a single property
1257
- *
1258
- * The property name should be different from other properties.
1259
- */
1260
- override property<N extends string, T extends PropertyType, V extends PropertyTypeToValueType<T>>(
1261
- name: N,
1262
- def: N extends keyof (TData & TProperty) ? never : typeUtils.PropertyListItem<T, V>,
1263
- ): ResolveBehaviorBuilder<
1264
- ComponentBuilder<
1265
- TPrevData,
1266
- TData,
1267
- TProperty & Record<N, unknown extends V ? T : typeUtils.PropertyOption<T, V>>,
1268
- TMethod,
1269
- TChainingFilter,
1270
- TPendingChainingFilter,
1271
- TComponentExport
1272
- >,
1273
- TChainingFilter
1274
- > {
1275
- return super.property(name, def) as any
1276
- }
1277
-
1278
- /**
1279
- * Add some public methods
1280
- *
1281
- * The public method can be used as an event handler, and can be visited in component instance.
1282
- */
1283
- override methods<T extends MethodList>(
1284
- funcs: T & ThisType<Component<TData, TProperty, TMethod & T, any>>,
1285
- ): ResolveBehaviorBuilder<
1286
- ComponentBuilder<
1287
- TPrevData,
1288
- TData,
1289
- TProperty,
1290
- TMethod & T,
1291
- TChainingFilter,
1292
- TPendingChainingFilter,
1293
- TComponentExport
1294
- >,
1295
- TChainingFilter
1296
- > {
1297
- return super.methods(funcs) as any
1298
- }
1299
-
1300
- /**
1301
- * Execute a function while component instance creation
1302
- *
1303
- * A `BuilderContext` is provided to tweak the component creation progress.
1304
- * The return value is used as the "export" value of the behavior,
1305
- * which can be imported by other behaviors.
1306
- */
1307
- override init<TExport extends Record<string, TaggedMethod<(...args: any[]) => any>> | void>(
1308
- func: (
1309
- this: Component<TData, TProperty, TMethod, TComponentExport>,
1310
- builderContext: BuilderContext<
1311
- TPrevData,
1312
- TProperty,
1313
- Component<TData, TProperty, TMethod, TComponentExport>
1314
- >,
1315
- ) => TExport,
1316
- // eslint-disable-next-line function-paren-newline
1317
- ): ResolveBehaviorBuilder<
1318
- ComponentBuilder<
1319
- TPrevData,
1320
- TData,
1321
- TProperty,
1322
- TMethod,
1323
- TChainingFilter,
1324
- TPendingChainingFilter,
1325
- TComponentExport
1326
- >,
1327
- TChainingFilter
1328
- > {
1329
- return super.init(func) as any
1330
- }
1331
-
1332
- /** Apply a classic definition object */
1333
- override definition<
1334
- TNewData extends DataList = Empty,
1335
- TNewProperty extends PropertyList = Empty,
1336
- TNewMethod extends MethodList = Empty,
1337
- TNewComponentExport = undefined,
1338
- >(
1339
- def: ComponentDefinition<TNewData, TNewProperty, TNewMethod, TNewComponentExport> &
1340
- ThisType<
1341
- Component<
1342
- TData & TNewData,
1343
- TProperty & TNewProperty,
1344
- TMethod & TNewMethod,
1345
- TNewComponentExport
1346
- >
1347
- >,
1348
- ): ResolveBehaviorBuilder<
1349
- ComponentBuilder<
1350
- TPrevData,
1351
- TData & TNewData,
1352
- TProperty & TNewProperty,
1353
- TMethod & TNewMethod,
1354
- TChainingFilter,
1355
- TPendingChainingFilter,
1356
- TNewComponentExport
1357
- >,
1358
- TChainingFilter
1359
- > {
1360
- super.definition(def)
1361
- if (def.options) this.options(def.options)
1362
- return this as any
1363
- }
1364
-
1365
- pageDefinition<TNewData extends DataList, TNewExtraFields extends { [k: PropertyKey]: any }>(
1366
- def: PageDefinition<TNewData, TNewExtraFields> &
1367
- ThisType<Component<TData & TNewData, TProperty, TMethod & TNewExtraFields, undefined>>,
1368
- ): ResolveBehaviorBuilder<
1369
- ComponentBuilder<
1370
- TPrevData,
1371
- TData & TNewData,
1372
- TProperty,
1373
- TMethod & TNewExtraFields,
1374
- TChainingFilter,
1375
- TPendingChainingFilter,
1376
- TComponentExport
1377
- >,
1378
- TChainingFilter
1379
- > {
1380
- const freeData = Object.create(null) as { [k: string]: any }
1381
- const compDef = {
1382
- methods: {},
1383
- } as { [k: string]: any }
1384
- const keys = Object.keys(def)
1385
- for (let i = 0; i < keys.length; i += 1) {
1386
- const k = keys[i]!
1387
- if (k === 'data' || k === 'options' || k === 'behaviors') {
1388
- compDef[k] = def[k]
1389
- } else if (typeof def[k] === 'function') {
1390
- ;(compDef.methods as { [k: string]: unknown })[k] = def[k] as unknown
1391
- } else {
1392
- ;(freeData as { [k: string]: unknown })[k] = def[k]
1393
- }
1394
- }
1395
- this.definition(compDef)
1396
- this._$.init(function () {
1397
- Object.assign(this, glassEasel.dataUtils.simpleDeepCopy(freeData))
1398
- })
1399
- return this as any
1400
- }
1401
-
1402
- /**
1403
- * Finish the component definition process
1404
- */
1405
- register(): ComponentType<TData, TProperty, TMethod, TComponentExport> {
1406
- const is = this._$is
1407
- const codeSpace = this._$codeSpace
1408
-
1409
- // processing common fields
1410
- const [options, styleIsolation] = codeSpace.prepareComponentOptions(is, this._$options)
1411
- this._$.options(options)
1412
- const staticConfig = codeSpace.getComponentStaticConfig(is)
1413
- const using = staticConfig?.usingComponents
1414
- const generics = staticConfig?.componentGenerics
1415
- const placeholder = staticConfig?.componentPlaceholder
1416
- if (using) this._$.usingComponents(using)
1417
- if (generics) this._$.generics(generics)
1418
- if (placeholder) this._$.placeholders(placeholder)
1419
- const template = codeSpace.getCompiledTemplate(is)
1420
- if (template) this._$.template(template)
1421
-
1422
- // do registration
1423
- codeSpace._$styleIsolationMap[is] = styleIsolation
1424
- const compDef = this._$.registerComponent()
1425
- this._$alias?.forEach((alias) => {
1426
- this._$codeSpace.getComponentSpace().exportComponent(alias, this._$is)
1427
- })
1428
- return new ComponentType(compDef)
1429
- }
1430
- }