To enable the developer to get notified when certain events in golden-layout occur, there are several lifecycle interfaces which might be implemented by the developer.
Namely:
- GlOnShow - Notifies the component when it will be shown
- GlOnHide - Notifies the component when it will be hidden
- GlOnTab - Notifies the component when the user activates another tab
- GlOnResize - Notifies the component when it is resized
- GlOnPopout - Notifies the component when it is about to pop out
- GlOnPopin - Notifies the component when it is about to pop back in
- GlOnUnload - Notifies the component when the window is about to close.
- GlOnClose - Notifies the component when the user wants to close the component via the tab close button or the stack close button.
Each of these interfaces defines one method to be implemented, for reference, here is a component which implements them all:
@Component({
template: `<h1>Test</h1>`,
selector: `app-test`,
})
export class TestComponent implements GlOnPopout, GlOnClose, GlOnHide, GlOnShow, GlOnPopin, GlOnResize, GlOnTab, GlOnUnload {
glOnHide(): void {
console.log('glOnHide');
}
glOnShow() {
console.log('glOnShow');
}
glOnResize() {
console.log('glOnResize');
}
glOnTab() {
console.log('glOnTab');
}
glOnPopin() {
console.log('glOnPopin');
}
glOnPopout() {
console.log('glOnPopout');
}
/**
* You return a promise to either:
* - cancel closing (reject)
* - defer closing (resolve later)
*/
async glOnClose(): Promise<void> {
console.log('glOnClose');
}
glOnUnload() {
console.log('glOnUnload');
}
ngOnInit() {
console.log('Initialized');
}
ngOnDestroy() {
console.log('Destroyed');
}
}
To understand the following chapter, it is required to understand the Angular Lifecycle Hooks.
A golden-layout components/panes lifecycle is decoupled from the lifecycle of the underlying Angular component. This means that your Angular component instance will be destroyed and re-initialized at certain state changes.
In a simple example, the lifecycle hooks might look like this:
- ngOnInit
- glOnShow (this might not fire, depending on the exact layout)
- glOnResize
- glOnClose
- ngOnDestroy
In this case, the Angular component lifetime equals the golden-layout panes lifetime.
When the user wants to popout/popin, this no longer holds:
- ngOnInit (root window)
- glOnShow (depends)
- glOnPopout (called BEFORE it pops out)
- ngOnDestroy (root window)
- ngOnInit (child window)
- glOnShow (depends)
- glOnPopin (called BEFORE it pops back in)
- ngOnDestroy (child window)
- ngOnInit (root window)
- glOnShow (depends)
- glOnClose
- ngOnDestroy
In this example, the golden-layout panes lifecycle spans the lifecycle of three Angular components.