11import { Component , Renderer2 , ViewChild , SimpleChanges , ElementRef , ChangeDetectorRef , ChangeDetectionStrategy } from '@angular/core' ;
2- import { WindowRefService } from '@servoy/public' ;
2+ import { WindowRefService , ServoyPublicService } from '@servoy/public' ;
33
44import { ServoyBootstrapBaseTabPanel , Tab } from '../bts_basetabpanel' ;
55
@@ -13,14 +13,26 @@ export class ServoyBootstrapAccordion extends ServoyBootstrapBaseTabPanel<HTMLDi
1313
1414 @ViewChild ( 'content' , { static : false , read : ElementRef } ) contentElementRef : ElementRef < HTMLDivElement > ;
1515 panelHeight : number ;
16+
17+ formHeightMap : { [ formName : string ] : number } = { } ;
1618
17- constructor ( renderer : Renderer2 , protected cdRef : ChangeDetectorRef , windowRefService : WindowRefService ) {
19+ constructor ( renderer : Renderer2 , protected cdRef : ChangeDetectorRef , windowRefService : WindowRefService , protected servoyPublic : ServoyPublicService ) {
1820 super ( renderer , cdRef , windowRefService ) ;
1921 }
2022
2123 svyOnChanges ( changes : SimpleChanges ) {
22- if ( changes [ 'height' ] || changes [ 'tabs' ] ) {
23- this . updateContentHeight ( ) ;
24+ if ( changes [ 'height' ] || changes [ 'tabs' ] || changes [ 'tabIndex' ] ) {
25+ const currentTab = this . tabs ?. [ this . getRealTabIndex ( ) ] ;
26+ const formName = currentTab ?. containedForm ;
27+
28+ if ( formName ) {
29+ const cachedHeight = this . formHeightMap [ formName ] ;
30+ if ( cachedHeight ) {
31+ this . updateContentHeight ( ) ;
32+ } else {
33+ this . getFormState ( formName , currentTab , true ) ;
34+ }
35+ }
2436 }
2537 super . svyOnChanges ( changes ) ;
2638 }
@@ -31,7 +43,15 @@ export class ServoyBootstrapAccordion extends ServoyBootstrapBaseTabPanel<HTMLDi
3143 }
3244
3345 private updateContentHeight ( ) {
34- let totalHeight = this . height ;
46+ const currentTab = this . tabs ?. [ this . getRealTabIndex ( ) ] ;
47+ const formName = currentTab ?. containedForm ;
48+ if ( formName && this . formHeightMap [ formName ] ) {
49+ this . panelHeight = this . formHeightMap [ formName ] ;
50+ this . cdRef . detectChanges ( ) ;
51+ return ;
52+ }
53+
54+ let totalHeight = typeof this . height === 'string' ? parseInt ( this . height , 10 ) : this . height ;
3555 let paneHeight = 49 ;
3656 let borderWidth = 2 ;
3757 let wrapper = null ;
@@ -46,7 +66,14 @@ export class ServoyBootstrapAccordion extends ServoyBootstrapBaseTabPanel<HTMLDi
4666 if ( headerElement ) {
4767 paneHeight = headerElement . offsetHeight ;
4868 }
49- totalHeight = totalHeight - paneHeight * this . tabs . length - borderWidth ;
69+
70+ if ( paneHeight * this . tabs . length + borderWidth + 50 <= totalHeight ) {
71+ // If all headers fit, use remaining space
72+ totalHeight = totalHeight - paneHeight * this . tabs . length - borderWidth ;
73+ } else {
74+ // Not enough space: show current tab + one extra
75+ totalHeight = totalHeight - ( paneHeight * 2 ) - ( borderWidth * 2 ) ;
76+ }
5077 }
5178 this . panelHeight = totalHeight ;
5279
@@ -72,4 +99,17 @@ export class ServoyBootstrapAccordion extends ServoyBootstrapBaseTabPanel<HTMLDi
7299 tabClicked ( tab : Tab , tabIndexClicked : number , event ) {
73100 this . servoyApi . callServerSideApi ( 'setTabIndexInternal' , [ tabIndexClicked + 1 ] ) ;
74101 }
102+
103+ private getFormState ( form : string , tab : Tab , formWillShow : boolean ) {
104+ if ( formWillShow ) {
105+ this . servoyApi . formWillShow ( form , ( 'relationName' in tab ) ? tab . relationName : null ) . then ( ( ) => {
106+ const formCache = this . servoyPublic . getFormCacheByName ( form ) ;
107+ if ( formCache && formCache . absolute ) {
108+ this . formHeightMap [ form ] = formCache . size . height ;
109+ this . panelHeight = this . formHeightMap [ form ] ;
110+ this . cdRef . detectChanges ( ) ;
111+ }
112+ } ) ;
113+ }
114+ }
75115}
0 commit comments