Commit e103d5a88b3f46a963d74e7297529dd66ae16d4b
1 parent
c9e3a62f
UI: Entity details panel improvements
Showing
2 changed files
with
65 additions
and
31 deletions
... | ... | @@ -227,7 +227,7 @@ |
227 | 227 | </table> |
228 | 228 | <span [fxShow]="!(isLoading$ | async) && (dataSource.isEmpty() | async)" |
229 | 229 | fxLayoutAlign="center center" |
230 | - class="no-data-found" translate>{{ translations.noEntities }}</span> | |
230 | + class="no-data-found">{{ translations.noEntities | translate }}</span> | |
231 | 231 | </div> |
232 | 232 | <mat-divider *ngIf="displayPagination"></mat-divider> |
233 | 233 | <mat-paginator *ngIf="displayPagination" | ... | ... |
... | ... | @@ -18,7 +18,7 @@ import { |
18 | 18 | AfterViewInit, |
19 | 19 | ChangeDetectionStrategy, ChangeDetectorRef, |
20 | 20 | Component, |
21 | - ComponentFactoryResolver, | |
21 | + ComponentFactoryResolver, ComponentRef, | |
22 | 22 | EventEmitter, Injector, |
23 | 23 | Input, |
24 | 24 | OnDestroy, |
... | ... | @@ -60,7 +60,10 @@ export class EntityDetailsPanelComponent extends PageComponent implements OnInit |
60 | 60 | @Output() |
61 | 61 | entityAction = new EventEmitter<EntityAction<BaseData<HasId>>>(); |
62 | 62 | |
63 | + entityComponentRef: ComponentRef<EntityComponent<BaseData<HasId>>>; | |
63 | 64 | entityComponent: EntityComponent<BaseData<HasId>>; |
65 | + | |
66 | + entityTabsComponentRef: ComponentRef<EntityTabsComponent<BaseData<HasId>>>; | |
64 | 67 | entityTabsComponent: EntityTabsComponent<BaseData<HasId>>; |
65 | 68 | detailsForm: NgForm; |
66 | 69 | |
... | ... | @@ -84,7 +87,9 @@ export class EntityDetailsPanelComponent extends PageComponent implements OnInit |
84 | 87 | editingEntity: BaseData<HasId>; |
85 | 88 | |
86 | 89 | private currentEntityId: HasId; |
87 | - private entityActionSubscription: Subscription; | |
90 | + private subscriptions: Subscription[] = []; | |
91 | + private viewInited = false; | |
92 | + private pendingTabs: MatTab[]; | |
88 | 93 | |
89 | 94 | constructor(protected store: Store<AppState>, |
90 | 95 | private injector: Injector, |
... | ... | @@ -103,12 +108,14 @@ export class EntityDetailsPanelComponent extends PageComponent implements OnInit |
103 | 108 | |
104 | 109 | @Input() |
105 | 110 | set entitiesTableConfig(entitiesTableConfig: EntityTableConfig<BaseData<HasId>>) { |
106 | - this.entitiesTableConfigValue = entitiesTableConfig; | |
107 | - if (this.entityComponent) { | |
108 | - this.entityComponent.entitiesTableConfig = entitiesTableConfig; | |
109 | - } | |
110 | - if (this.entityTabsComponent) { | |
111 | - this.entityTabsComponent.entitiesTableConfig = entitiesTableConfig; | |
111 | + if (this.entitiesTableConfigValue !== entitiesTableConfig) { | |
112 | + this.entitiesTableConfigValue = entitiesTableConfig; | |
113 | + if (this.entitiesTableConfigValue) { | |
114 | + this.currentEntityId = null; | |
115 | + this.isEdit = false; | |
116 | + this.entity = null; | |
117 | + this.init(); | |
118 | + } | |
112 | 119 | } |
113 | 120 | } |
114 | 121 | |
... | ... | @@ -131,19 +138,33 @@ export class EntityDetailsPanelComponent extends PageComponent implements OnInit |
131 | 138 | } |
132 | 139 | |
133 | 140 | ngOnInit(): void { |
141 | + this.init(); | |
142 | + } | |
143 | + | |
144 | + private init() { | |
134 | 145 | this.translations = this.entitiesTableConfig.entityTranslations; |
135 | 146 | this.resources = this.entitiesTableConfig.entityResources; |
136 | 147 | this.buildEntityComponent(); |
137 | 148 | } |
138 | 149 | |
150 | + private clearSubscriptions() { | |
151 | + this.subscriptions.forEach((subscription) => { | |
152 | + subscription.unsubscribe(); | |
153 | + }); | |
154 | + this.subscriptions.length = 0; | |
155 | + } | |
156 | + | |
139 | 157 | ngOnDestroy(): void { |
140 | 158 | super.ngOnDestroy(); |
141 | - if (this.entityActionSubscription) { | |
142 | - this.entityActionSubscription.unsubscribe(); | |
143 | - } | |
159 | + this.clearSubscriptions(); | |
144 | 160 | } |
145 | 161 | |
146 | 162 | buildEntityComponent() { |
163 | + this.clearSubscriptions(); | |
164 | + if (this.entityComponentRef) { | |
165 | + this.entityComponentRef.destroy(); | |
166 | + this.entityComponentRef = null; | |
167 | + } | |
147 | 168 | const componentFactory = this.componentFactoryResolver.resolveComponentFactory(this.entitiesTableConfig.entityComponent); |
148 | 169 | const viewContainerRef = this.entityDetailsFormAnchor.viewContainerRef; |
149 | 170 | viewContainerRef.clear(); |
... | ... | @@ -162,29 +183,46 @@ export class EntityDetailsPanelComponent extends PageComponent implements OnInit |
162 | 183 | parent: this.injector |
163 | 184 | } |
164 | 185 | ); |
165 | - const componentRef = viewContainerRef.createComponent(componentFactory, 0, injector); | |
166 | - this.entityComponent = componentRef.instance; | |
186 | + this.entityComponentRef = viewContainerRef.createComponent(componentFactory, 0, injector); | |
187 | + this.entityComponent = this.entityComponentRef.instance; | |
167 | 188 | this.entityComponent.isEdit = this.isEdit; |
168 | 189 | this.detailsForm = this.entityComponent.entityNgForm; |
169 | - this.entityActionSubscription = this.entityComponent.entityAction.subscribe((action) => { | |
190 | + this.subscriptions.push(this.entityComponent.entityAction.subscribe((action) => { | |
170 | 191 | this.entityAction.emit(action); |
171 | - }); | |
192 | + })); | |
172 | 193 | this.buildEntityTabsComponent(); |
173 | - this.entityComponent.entityForm.valueChanges.subscribe(() => { | |
194 | + this.subscriptions.push(this.entityComponent.entityForm.valueChanges.subscribe(() => { | |
174 | 195 | this.cd.detectChanges(); |
175 | - }); | |
196 | + })); | |
176 | 197 | } |
177 | 198 | |
178 | 199 | buildEntityTabsComponent() { |
200 | + if (this.entityTabsComponentRef) { | |
201 | + this.entityTabsComponentRef.destroy(); | |
202 | + this.entityTabsComponentRef = null; | |
203 | + } | |
204 | + const viewContainerRef = this.entityTabsAnchor.viewContainerRef; | |
205 | + viewContainerRef.clear(); | |
206 | + this.entityTabsComponent = null; | |
179 | 207 | if (this.entitiesTableConfig.entityTabsComponent) { |
180 | 208 | const componentTabsFactory = this.componentFactoryResolver.resolveComponentFactory(this.entitiesTableConfig.entityTabsComponent); |
181 | - const viewContainerRef = this.entityTabsAnchor.viewContainerRef; | |
182 | - viewContainerRef.clear(); | |
183 | - const componentTabsRef = viewContainerRef.createComponent(componentTabsFactory); | |
184 | - this.entityTabsComponent = componentTabsRef.instance; | |
209 | + this.entityTabsComponentRef = viewContainerRef.createComponent(componentTabsFactory); | |
210 | + this.entityTabsComponent = this.entityTabsComponentRef.instance; | |
185 | 211 | this.entityTabsComponent.isEdit = this.isEdit; |
186 | 212 | this.entityTabsComponent.entitiesTableConfig = this.entitiesTableConfig; |
187 | 213 | this.entityTabsComponent.detailsForm = this.detailsForm; |
214 | + this.subscriptions.push(this.entityTabsComponent.entityTabsChanged.subscribe( | |
215 | + (entityTabs) => { | |
216 | + if (entityTabs) { | |
217 | + if (this.viewInited) { | |
218 | + this.matTabGroup._tabs.reset([...this.inclusiveTabs.toArray(), ...entityTabs]); | |
219 | + this.matTabGroup._tabs.notifyOnChanges(); | |
220 | + } else { | |
221 | + this.pendingTabs = entityTabs; | |
222 | + } | |
223 | + } | |
224 | + } | |
225 | + )); | |
188 | 226 | } |
189 | 227 | } |
190 | 228 | |
... | ... | @@ -254,15 +292,11 @@ export class EntityDetailsPanelComponent extends PageComponent implements OnInit |
254 | 292 | } |
255 | 293 | |
256 | 294 | ngAfterViewInit(): void { |
257 | - if (this.entityTabsComponent) { | |
258 | - this.entityTabsComponent.entityTabsChanged.subscribe( | |
259 | - (entityTabs) => { | |
260 | - if (entityTabs) { | |
261 | - this.matTabGroup._tabs.reset([...this.inclusiveTabs.toArray(), ...entityTabs]); | |
262 | - this.matTabGroup._tabs.notifyOnChanges(); | |
263 | - } | |
264 | - } | |
265 | - ); | |
295 | + this.viewInited = true; | |
296 | + if (this.pendingTabs) { | |
297 | + this.matTabGroup._tabs.reset([...this.inclusiveTabs.toArray(), ...this.pendingTabs]); | |
298 | + this.matTabGroup._tabs.notifyOnChanges(); | |
299 | + this.pendingTabs = null; | |
266 | 300 | } |
267 | 301 | } |
268 | 302 | ... | ... |