MOBILE-3814 collapsible: Listen to resize to recalculate size

main
Pau Ferrer Ocaña 2022-03-14 13:09:41 +01:00
parent 275ef0ac21
commit bcb5d937ee
13 changed files with 178 additions and 101 deletions

View File

@ -14,6 +14,7 @@
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { CoreEventObserver } from '@singletons/events';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { AddonModQuizDdImageOrTextQuestionData } from '../component/ddimageortext'; import { AddonModQuizDdImageOrTextQuestionData } from '../component/ddimageortext';
@ -28,7 +29,7 @@ export class AddonQtypeDdImageOrTextQuestion {
protected afterImageLoadDone = false; protected afterImageLoadDone = false;
protected proportion = 1; protected proportion = 1;
protected selected?: HTMLElement | null; // Selected element (being "dragged"). protected selected?: HTMLElement | null; // Selected element (being "dragged").
protected resizeFunction?: (ev?: Event) => void; protected resizeListener?: CoreEventObserver;
/** /**
* Create the this. * Create the this.
@ -174,9 +175,7 @@ export class AddonQtypeDdImageOrTextQuestion {
destroy(): void { destroy(): void {
this.stopPolling(); this.stopPolling();
if (this.resizeFunction) { this.resizeListener?.off();
window.removeEventListener('resize', this.resizeFunction);
}
} }
/** /**
@ -360,8 +359,9 @@ export class AddonQtypeDdImageOrTextQuestion {
this.pollForImageLoad(); this.pollForImageLoad();
}); });
this.resizeFunction = this.windowResized.bind(this); this.resizeListener = CoreDomUtils.onWindowResize(() => {
window.addEventListener('resize', this.resizeFunction!); this.windowResized();
});
} }
/** /**

View File

@ -14,6 +14,7 @@
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { CoreEventObserver } from '@singletons/events';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { AddonQtypeDdMarkerQuestionData } from '../component/ddmarker'; import { AddonQtypeDdMarkerQuestionData } from '../component/ddmarker';
import { AddonQtypeDdMarkerGraphicsApi } from './graphics_api'; import { AddonQtypeDdMarkerGraphicsApi } from './graphics_api';
@ -41,7 +42,7 @@ export class AddonQtypeDdMarkerQuestion {
protected proportion = 1; protected proportion = 1;
protected selected?: HTMLElement; // Selected element (being "dragged"). protected selected?: HTMLElement; // Selected element (being "dragged").
protected graphics: AddonQtypeDdMarkerGraphicsApi; protected graphics: AddonQtypeDdMarkerGraphicsApi;
protected resizeFunction?: () => void; protected resizeListener?: CoreEventObserver;
doc!: AddonQtypeDdMarkerQuestionDocStructure; doc!: AddonQtypeDdMarkerQuestionDocStructure;
shapes: SVGElement[] = []; shapes: SVGElement[] = [];
@ -160,9 +161,7 @@ export class AddonQtypeDdMarkerQuestion {
* Function to call when the instance is no longer needed. * Function to call when the instance is no longer needed.
*/ */
destroy(): void { destroy(): void {
if (this.resizeFunction) { this.resizeListener?.off();
window.removeEventListener('resize', this.resizeFunction);
}
} }
/** /**
@ -601,8 +600,9 @@ export class AddonQtypeDdMarkerQuestion {
this.pollForImageLoad(); this.pollForImageLoad();
}); });
this.resizeFunction = this.windowResized.bind(this); this.resizeListener = CoreDomUtils.onWindowResize(() => {
window.addEventListener('resize', this.resizeFunction!); this.windowResized();
});
} }
/** /**

View File

@ -15,6 +15,7 @@
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { CoreEventObserver } from '@singletons/events';
import { CoreLogger } from '@singletons/logger'; import { CoreLogger } from '@singletons/logger';
import { AddonModQuizDdwtosQuestionData } from '../component/ddwtos'; import { AddonModQuizDdwtosQuestionData } from '../component/ddwtos';
@ -28,13 +29,11 @@ export class AddonQtypeDdwtosQuestion {
protected selectors!: AddonQtypeDdwtosQuestionCSSSelectors; // Result of cssSelectors. protected selectors!: AddonQtypeDdwtosQuestionCSSSelectors; // Result of cssSelectors.
protected placed: {[no: number]: number} = {}; // Map that relates drag elements numbers with drop zones numbers. protected placed: {[no: number]: number} = {}; // Map that relates drag elements numbers with drop zones numbers.
protected selected?: HTMLElement; // Selected element (being "dragged"). protected selected?: HTMLElement; // Selected element (being "dragged").
protected resizeFunction?: () => void; protected resizeListener?: CoreEventObserver;
/** /**
* Create the instance. * Create the instance.
* *
* @param logger Logger provider.
* @param domUtils Dom Utils provider.
* @param container The container HTMLElement of the question. * @param container The container HTMLElement of the question.
* @param question The question instance. * @param question The question instance.
* @param readOnly Whether it's read only. * @param readOnly Whether it's read only.
@ -122,9 +121,7 @@ export class AddonQtypeDdwtosQuestion {
* Function to call when the instance is no longer needed. * Function to call when the instance is no longer needed.
*/ */
destroy(): void { destroy(): void {
if (this.resizeFunction) { this.resizeListener?.off();
window.removeEventListener('resize', this.resizeFunction);
}
} }
/** /**
@ -214,8 +211,9 @@ export class AddonQtypeDdwtosQuestion {
this.positionDragItems(); this.positionDragItems();
this.resizeFunction = this.windowResized.bind(this); this.resizeListener = CoreDomUtils.onWindowResize(() => {
window.addEventListener('resize', this.resizeFunction!); this.windowResized();
});
} }
/** /**

View File

@ -32,6 +32,8 @@ import { Subscription } from 'rxjs';
import { Platform, Translate } from '@singletons'; import { Platform, Translate } from '@singletons';
import { CoreSettingsHelper } from '@features/settings/services/settings-helper'; import { CoreSettingsHelper } from '@features/settings/services/settings-helper';
import { CoreAriaRoleTab, CoreAriaRoleTabFindable } from './aria-role-tab'; import { CoreAriaRoleTab, CoreAriaRoleTabFindable } from './aria-role-tab';
import { CoreEventObserver } from '@singletons/events';
import { CoreDomUtils } from '@services/utils/dom';
/** /**
* Class to abstract some common code for tabs. * Class to abstract some common code for tabs.
@ -75,7 +77,7 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
protected tabsElement?: HTMLElement; // The tabs parent element. It's the element that will be "scrolled" to hide tabs. protected tabsElement?: HTMLElement; // The tabs parent element. It's the element that will be "scrolled" to hide tabs.
protected tabBarElement?: HTMLIonTabBarElement; // The top tab bar element. protected tabBarElement?: HTMLIonTabBarElement; // The top tab bar element.
protected tabsShown = true; protected tabsShown = true;
protected resizeFunction: EventListenerOrEventListenerObject; protected resizeListener?: CoreEventObserver;
protected isDestroyed = false; protected isDestroyed = false;
protected isCurrentView = true; protected isCurrentView = true;
protected shouldSlideToInitial = false; // Whether we need to slide to the initial slide because it's out of view. protected shouldSlideToInitial = false; // Whether we need to slide to the initial slide because it's out of view.
@ -99,7 +101,6 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
protected element: ElementRef, protected element: ElementRef,
) { ) {
this.backButtonFunction = this.backButtonClicked.bind(this); this.backButtonFunction = this.backButtonClicked.bind(this);
this.resizeFunction = this.windowResized.bind(this);
this.tabAction = new CoreTabsRoleTab(this); this.tabAction = new CoreTabsRoleTab(this);
} }
@ -134,7 +135,9 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
await this.initializeTabs(); await this.initializeTabs();
} }
window.addEventListener('resize', this.resizeFunction); this.resizeListener = CoreDomUtils.onWindowResize(() => {
this.windowResized();
});
} }
/** /**
@ -419,24 +422,24 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
*/ */
async slideNext(): Promise<void> { async slideNext(): Promise<void> {
// Stop if slides are in transition. // Stop if slides are in transition.
if (!this.showNextButton || this.isInTransition) { if (!this.showNextButton || this.isInTransition || !this.slides) {
return; return;
} }
if (await this.slides!.isBeginning()) { if (await this.slides.isBeginning()) {
// Slide to the second page. // Slide to the second page.
this.slides!.slideTo(this.maxSlides); this.slides.slideTo(this.maxSlides);
} else { } else {
const currentIndex = await this.slides!.getActiveIndex(); const currentIndex = await this.slides.getActiveIndex();
if (currentIndex !== undefined) { if (currentIndex !== undefined) {
const nextSlideIndex = currentIndex + this.maxSlides; const nextSlideIndex = currentIndex + this.maxSlides;
this.isInTransition = true; this.isInTransition = true;
if (nextSlideIndex < this.numTabsShown) { if (nextSlideIndex < this.numTabsShown) {
// Slide to the next page. // Slide to the next page.
await this.slides!.slideTo(nextSlideIndex); await this.slides.slideTo(nextSlideIndex);
} else { } else {
// Slide to the latest slide. // Slide to the latest slide.
await this.slides!.slideTo(this.numTabsShown - 1); await this.slides.slideTo(this.numTabsShown - 1);
} }
} }
@ -448,24 +451,24 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
*/ */
async slidePrev(): Promise<void> { async slidePrev(): Promise<void> {
// Stop if slides are in transition. // Stop if slides are in transition.
if (!this.showPrevButton || this.isInTransition) { if (!this.showPrevButton || this.isInTransition || !this.slides) {
return; return;
} }
if (await this.slides!.isEnd()) { if (await this.slides.isEnd()) {
this.slides!.slideTo(this.numTabsShown - this.maxSlides * 2); this.slides.slideTo(this.numTabsShown - this.maxSlides * 2);
// Slide to the previous of the latest page. // Slide to the previous of the latest page.
} else { } else {
const currentIndex = await this.slides!.getActiveIndex(); const currentIndex = await this.slides.getActiveIndex();
if (currentIndex !== undefined) { if (currentIndex !== undefined) {
const prevSlideIndex = currentIndex - this.maxSlides; const prevSlideIndex = currentIndex - this.maxSlides;
this.isInTransition = true; this.isInTransition = true;
if (prevSlideIndex >= 0) { if (prevSlideIndex >= 0) {
// Slide to the previous page. // Slide to the previous page.
await this.slides!.slideTo(prevSlideIndex); await this.slides.slideTo(prevSlideIndex);
} else { } else {
// Slide to the first page. // Slide to the first page.
await this.slides!.slideTo(0); await this.slides.slideTo(0);
} }
} }
} }
@ -646,9 +649,7 @@ export class CoreTabsBaseComponent<T extends CoreTabBase> implements OnInit, Aft
ngOnDestroy(): void { ngOnDestroy(): void {
this.isDestroyed = true; this.isDestroyed = true;
if (this.resizeFunction) { this.resizeListener?.off();
window.removeEventListener('resize', this.resizeFunction);
}
this.languageChangedSubscription?.unsubscribe(); this.languageChangedSubscription?.unsubscribe();
} }

View File

@ -21,6 +21,7 @@ import { CoreComponentsRegistry } from '@singletons/components-registry';
import { CoreFormatTextDirective } from './format-text'; import { CoreFormatTextDirective } from './format-text';
import { CoreEventObserver } from '@singletons/events'; import { CoreEventObserver } from '@singletons/events';
import { CoreLoadingComponent } from '@components/loading/loading'; import { CoreLoadingComponent } from '@components/loading/loading';
import { CoreDomUtils } from '@services/utils/dom';
/** /**
* Directive to make an element fixed at the bottom collapsible when scrolling. * Directive to make an element fixed at the bottom collapsible when scrolling.
@ -37,7 +38,7 @@ export class CoreCollapsibleFooterDirective implements OnInit, OnDestroy {
@Input() appearOnBottom = false; @Input() appearOnBottom = false;
protected element: HTMLElement; protected element: HTMLElement;
protected initialHeight = 0; protected initialHeight = 48;
protected finalHeight = 0; protected finalHeight = 0;
protected initialPaddingBottom = '0px'; protected initialPaddingBottom = '0px';
protected previousTop = 0; protected previousTop = 0;
@ -46,22 +47,38 @@ export class CoreCollapsibleFooterDirective implements OnInit, OnDestroy {
protected loadingChangedListener?: CoreEventObserver; protected loadingChangedListener?: CoreEventObserver;
protected contentScrollListener?: EventListener; protected contentScrollListener?: EventListener;
protected endContentScrollListener?: EventListener; protected endContentScrollListener?: EventListener;
protected resizeListener?: CoreEventObserver;
constructor(el: ElementRef, protected ionContent: IonContent) { constructor(el: ElementRef, protected ionContent: IonContent) {
this.element = el.nativeElement; this.element = el.nativeElement;
this.element.setAttribute('slot', 'fixed'); // Just in case somebody forgets to add it. this.element.setAttribute('slot', 'fixed'); // Just in case somebody forgets to add it.
} }
/**
* @inheritdoc
*/
async ngOnInit(): Promise<void> {
// Only if not present or explicitly falsy it will be false.
this.appearOnBottom = !CoreUtils.isFalseOrZero(this.appearOnBottom);
await CoreDomUtils.waitToBeInDOM(this.element);
await this.waitLoadingsDone();
await this.waitFormatTextsRendered(this.element);
await this.calculateHeight();
this.listenScrollEvents();
}
/** /**
* Calculate the height of the footer. * Calculate the height of the footer.
*/ */
protected async calculateHeight(): Promise<void> { protected async calculateHeight(): Promise<void> {
await this.waitFormatTextsRendered(this.element); this.element.classList.remove('is-active');
await CoreUtils.nextTick(); await CoreUtils.nextTick();
// Set a minimum height value. // Set a minimum height value.
this.initialHeight = this.element.getBoundingClientRect().height || 48; this.initialHeight = this.element.getBoundingClientRect().height || this.initialHeight;
const moduleNav = this.element.querySelector('core-course-module-navigation'); const moduleNav = this.element.querySelector('core-course-module-navigation');
if (moduleNav) { if (moduleNav) {
this.element.classList.add('has-module-nav'); this.element.classList.add('has-module-nav');
@ -71,6 +88,7 @@ export class CoreCollapsibleFooterDirective implements OnInit, OnDestroy {
this.previousHeight = this.initialHeight; this.previousHeight = this.initialHeight;
this.content?.style.setProperty('--core-collapsible-footer-max-height', this.initialHeight + 'px'); this.content?.style.setProperty('--core-collapsible-footer-max-height', this.initialHeight + 'px');
this.element.classList.add('is-active');
this.setBarHeight(this.initialHeight); this.setBarHeight(this.initialHeight);
} }
@ -131,6 +149,10 @@ export class CoreCollapsibleFooterDirective implements OnInit, OnDestroy {
this.setBarHeight(newHeight); } this.setBarHeight(newHeight); }
}); });
this.resizeListener = CoreDomUtils.onWindowResize(() => {
this.calculateHeight();
}, 50);
} }
/** /**
@ -180,20 +202,6 @@ export class CoreCollapsibleFooterDirective implements OnInit, OnDestroy {
this.previousHeight = height; this.previousHeight = height;
} }
/**
* @inheritdoc
*/
async ngOnInit(): Promise<void> {
// Only if not present or explicitly falsy it will be false.
this.appearOnBottom = !CoreUtils.isFalseOrZero(this.appearOnBottom);
await this.waitLoadingsDone();
await this.calculateHeight();
this.listenScrollEvents();
}
/** /**
* Wait until all <core-loading> children inside the page. * Wait until all <core-loading> children inside the page.
* *
@ -225,6 +233,8 @@ export class CoreCollapsibleFooterDirective implements OnInit, OnDestroy {
if (this.content && this.endContentScrollListener) { if (this.content && this.endContentScrollListener) {
this.content.removeEventListener('ionScrollEnd', this.endContentScrollListener); this.content.removeEventListener('ionScrollEnd', this.endContentScrollListener);
} }
this.resizeListener?.off();
} }
} }

View File

@ -17,8 +17,10 @@ import { CorePromisedValue } from '@classes/promised-value';
import { CoreLoadingComponent } from '@components/loading/loading'; import { CoreLoadingComponent } from '@components/loading/loading';
import { CoreTabsOutletComponent } from '@components/tabs-outlet/tabs-outlet'; import { CoreTabsOutletComponent } from '@components/tabs-outlet/tabs-outlet';
import { ScrollDetail } from '@ionic/core'; import { ScrollDetail } from '@ionic/core';
import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { CoreComponentsRegistry } from '@singletons/components-registry'; import { CoreComponentsRegistry } from '@singletons/components-registry';
import { CoreEventObserver } from '@singletons/events';
import { CoreMath } from '@singletons/math'; import { CoreMath } from '@singletons/math';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { CoreFormatTextDirective } from './format-text'; import { CoreFormatTextDirective } from './format-text';
@ -66,6 +68,7 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnChanges, OnDest
protected contentScrollListener?: EventListener; protected contentScrollListener?: EventListener;
protected endContentScrollListener?: EventListener; protected endContentScrollListener?: EventListener;
protected pageDidEnterListener?: EventListener; protected pageDidEnterListener?: EventListener;
protected resizeListener?: CoreEventObserver;
protected floatingTitle?: HTMLHeadingElement; protected floatingTitle?: HTMLHeadingElement;
protected scrollingHeight?: number; protected scrollingHeight?: number;
protected subscriptions: Subscription[] = []; protected subscriptions: Subscription[] = [];
@ -120,6 +123,8 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnChanges, OnDest
if (this.page && this.pageDidEnterListener) { if (this.page && this.pageDidEnterListener) {
this.page.removeEventListener('ionViewDidEnter', this.pageDidEnterListener); this.page.removeEventListener('ionViewDidEnter', this.pageDidEnterListener);
} }
this.resizeListener?.off();
} }
/** /**
@ -147,6 +152,10 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnChanges, OnDest
const timeout = window.setTimeout(() => { const timeout = window.setTimeout(() => {
this.enteredPromise.reject(new Error('[collapsible-header] Waiting for ionViewDidEnter timeout reached')); this.enteredPromise.reject(new Error('[collapsible-header] Waiting for ionViewDidEnter timeout reached'));
}, 5000); }, 5000);
this.resizeListener = CoreDomUtils.onWindowResize(() => {
this.initializeFloatingTitle();
}, 50);
} }
/** /**
@ -217,16 +226,26 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnChanges, OnDest
throw new Error('[collapsible-header] Couldn\'t create floating title'); throw new Error('[collapsible-header] Couldn\'t create floating title');
} }
this.page.classList.remove('is-active');
CoreUtils.nextTick();
// Add floating title and measure initial position. // Add floating title and measure initial position.
const collapsedHeaderTitle = this.collapsedHeader.querySelector('h1') as HTMLHeadingElement; const collapsedHeaderTitle = this.collapsedHeader.querySelector('h1') as HTMLHeadingElement;
const originalTitle = this.expandedHeader.querySelector('h1') as HTMLHeadingElement; const originalTitle = this.expandedHeader.querySelector('h1.collapsible-header-original-title') ||
const floatingTitleWrapper = originalTitle.parentElement as HTMLElement; this.expandedHeader.querySelector('h1') as HTMLHeadingElement;
const floatingTitle = originalTitle.cloneNode(true) as HTMLHeadingElement;
originalTitle.classList.add('collapsible-header-original-title'); const floatingTitleWrapper = originalTitle.parentElement as HTMLElement;
floatingTitle.classList.add('collapsible-header-floating-title'); let floatingTitle = floatingTitleWrapper.querySelector('.collapsible-header-floating-title') as HTMLHeadingElement;
floatingTitleWrapper.classList.add('collapsible-header-floating-title-wrapper'); if (!floatingTitle) {
floatingTitleWrapper.insertBefore(floatingTitle, originalTitle); // First time, create it.
floatingTitle = originalTitle.cloneNode(true) as HTMLHeadingElement;
floatingTitle.classList.add('collapsible-header-floating-title');
floatingTitleWrapper.classList.add('collapsible-header-floating-title-wrapper');
floatingTitleWrapper.insertBefore(floatingTitle, originalTitle);
originalTitle.classList.add('collapsible-header-original-title');
}
const floatingTitleBoundingBox = floatingTitle.getBoundingClientRect(); const floatingTitleBoundingBox = floatingTitle.getBoundingClientRect();

View File

@ -12,12 +12,13 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
import { Directive, ElementRef, Input, OnInit } from '@angular/core'; import { Directive, ElementRef, Input, OnDestroy, OnInit } from '@angular/core';
import { CoreLoadingComponent } from '@components/loading/loading'; import { CoreLoadingComponent } from '@components/loading/loading';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
import { Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreComponentsRegistry } from '@singletons/components-registry'; import { CoreComponentsRegistry } from '@singletons/components-registry';
import { CoreEventObserver } from '@singletons/events';
import { CoreFormatTextDirective } from './format-text'; import { CoreFormatTextDirective } from './format-text';
const defaultMaxHeight = 80; const defaultMaxHeight = 80;
@ -33,7 +34,7 @@ const minMaxHeight = 56;
@Directive({ @Directive({
selector: '[collapsible-item]', selector: '[collapsible-item]',
}) })
export class CoreCollapsibleItemDirective implements OnInit { export class CoreCollapsibleItemDirective implements OnInit, OnDestroy {
/** /**
* Max height in pixels to render the content box. It should be 56 at least to make sense. * Max height in pixels to render the content box. It should be 56 at least to make sense.
@ -47,6 +48,7 @@ export class CoreCollapsibleItemDirective implements OnInit {
protected expanded = false; protected expanded = false;
protected maxHeight = defaultMaxHeight; protected maxHeight = defaultMaxHeight;
protected expandedHeight = 0; protected expandedHeight = 0;
protected resizeListener?: CoreEventObserver;
constructor(el: ElementRef<HTMLElement>) { constructor(el: ElementRef<HTMLElement>) {
this.element = el.nativeElement; this.element = el.nativeElement;
@ -81,6 +83,10 @@ export class CoreCollapsibleItemDirective implements OnInit {
await this.waitLoadingsDone(); await this.waitLoadingsDone();
await this.calculateHeight(); await this.calculateHeight();
this.resizeListener = CoreDomUtils.onWindowResize(() => {
this.calculateHeight();
}, 50);
} }
/** /**
@ -126,7 +132,7 @@ export class CoreCollapsibleItemDirective implements OnInit {
await this.waitFormatTextsRendered(this.element); await this.waitFormatTextsRendered(this.element);
this.expandedHeight = CoreDomUtils.getElementHeight(this.element) || 0; this.expandedHeight = this.element.getBoundingClientRect().height;
// Restore the max height now. // Restore the max height now.
this.element.classList.remove('collapsible-loading-height'); this.element.classList.remove('collapsible-loading-height');
@ -229,4 +235,11 @@ export class CoreCollapsibleItemDirective implements OnInit {
this.toggleExpand(); this.toggleExpand();
} }
/**
* @inheritdoc
*/
ngOnDestroy(): void {
this.resizeListener?.off();
}
} }

View File

@ -29,6 +29,7 @@ import { filter } from 'rxjs/operators';
import { NavigationEnd } from '@angular/router'; import { NavigationEnd } from '@angular/router';
import { trigger, state, style, transition, animate } from '@angular/animations'; import { trigger, state, style, transition, animate } from '@angular/animations';
import { CoreSites } from '@services/sites'; import { CoreSites } from '@services/sites';
import { CoreDomUtils } from '@services/utils/dom';
/** /**
* Page that displays the main menu of the app. * Page that displays the main menu of the app.
@ -73,7 +74,7 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
protected navSubscription?: Subscription; protected navSubscription?: Subscription;
protected keyboardObserver?: CoreEventObserver; protected keyboardObserver?: CoreEventObserver;
protected badgeUpdateObserver?: CoreEventObserver; protected badgeUpdateObserver?: CoreEventObserver;
protected resizeFunction: () => void; protected resizeListener?: CoreEventObserver;
protected backButtonFunction: (event: BackButtonEvent) => void; protected backButtonFunction: (event: BackButtonEvent) => void;
protected selectHistory: string[] = []; protected selectHistory: string[] = [];
protected firstSelectedTab?: string; protected firstSelectedTab?: string;
@ -86,7 +87,6 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
tabAction: CoreMainMenuRoleTab; tabAction: CoreMainMenuRoleTab;
constructor() { constructor() {
this.resizeFunction = this.initHandlers.bind(this);
this.backButtonFunction = this.backButtonClicked.bind(this); this.backButtonFunction = this.backButtonClicked.bind(this);
this.tabAction = new CoreMainMenuRoleTab(this); this.tabAction = new CoreMainMenuRoleTab(this);
@ -122,7 +122,9 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
} }
}); });
window.addEventListener('resize', this.resizeFunction); this.resizeListener = CoreDomUtils.onWindowResize(() => {
this.initHandlers();
});
document.addEventListener('ionBackButton', this.backButtonFunction); document.addEventListener('ionBackButton', this.backButtonFunction);
if (CoreApp.isIOS()) { if (CoreApp.isIOS()) {
@ -221,10 +223,10 @@ export class CoreMainMenuPage implements OnInit, OnDestroy {
ngOnDestroy(): void { ngOnDestroy(): void {
this.subscription?.unsubscribe(); this.subscription?.unsubscribe();
this.navSubscription?.unsubscribe(); this.navSubscription?.unsubscribe();
window.removeEventListener('resize', this.resizeFunction);
document.removeEventListener('ionBackButton', this.backButtonFunction); document.removeEventListener('ionBackButton', this.backButtonFunction);
this.keyboardObserver?.off(); this.keyboardObserver?.off();
this.badgeUpdateObserver?.off(); this.badgeUpdateObserver?.off();
this.resizeListener?.off();
} }
/** /**

View File

@ -26,6 +26,7 @@ import { CoreContentLinksHelper } from '@features/contentlinks/services/contentl
import { CoreTextUtils } from '@services/utils/text'; import { CoreTextUtils } from '@services/utils/text';
import { Translate } from '@singletons'; import { Translate } from '@singletons';
import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager'; import { CoreMainMenuDeepLinkManager } from '@features/mainmenu/classes/deep-link-manager';
import { CoreDomUtils } from '@services/utils/dom';
/** /**
* Page that displays the more page of the app. * Page that displays the more page of the app.
@ -46,6 +47,7 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy {
protected subscription!: Subscription; protected subscription!: Subscription;
protected langObserver: CoreEventObserver; protected langObserver: CoreEventObserver;
protected updateSiteObserver: CoreEventObserver; protected updateSiteObserver: CoreEventObserver;
protected resizeListener?: CoreEventObserver;
constructor() { constructor() {
this.langObserver = CoreEvents.on(CoreEvents.LANGUAGE_CHANGED, this.loadCustomMenuItems.bind(this)); this.langObserver = CoreEvents.on(CoreEvents.LANGUAGE_CHANGED, this.loadCustomMenuItems.bind(this));
@ -71,7 +73,9 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy {
this.initHandlers(); this.initHandlers();
}); });
window.addEventListener('resize', this.initHandlers.bind(this)); this.resizeListener = CoreDomUtils.onWindowResize(() => {
this.initHandlers();
});
const deepLinkManager = new CoreMainMenuDeepLinkManager(); const deepLinkManager = new CoreMainMenuDeepLinkManager();
deepLinkManager.treatLink(); deepLinkManager.treatLink();
@ -81,10 +85,10 @@ export class CoreMainMenuMorePage implements OnInit, OnDestroy {
* @inheritdoc * @inheritdoc
*/ */
ngOnDestroy(): void { ngOnDestroy(): void {
window.removeEventListener('resize', this.initHandlers.bind(this));
this.langObserver?.off(); this.langObserver?.off();
this.updateSiteObserver?.off(); this.updateSiteObserver?.off();
this.subscription?.unsubscribe(); this.subscription?.unsubscribe();
this.resizeListener?.off();
} }
/** /**

View File

@ -53,6 +53,7 @@ import { NavigationStart } from '@angular/router';
import { filter } from 'rxjs/operators'; import { filter } from 'rxjs/operators';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { CoreComponentsRegistry } from '@singletons/components-registry'; import { CoreComponentsRegistry } from '@singletons/components-registry';
import { CoreEventObserver } from '@singletons/events';
/* /*
* "Utils" service with helper functions for UI, DOM elements and HTML code. * "Utils" service with helper functions for UI, DOM elements and HTML code.
@ -127,6 +128,28 @@ export class CoreDomUtilsProvider {
}); });
} }
/**
* Window resize is widely checked and may have many performance issues, debouce usage is needed to avoid calling it too much.
* This function helps setting up the debounce feature and remove listener easily.
*
* @param resizeFunction Function to execute on resize.
* @param debounceDelay Debounce time in ms.
* @return Event observer to call off when finished.
*/
onWindowResize(resizeFunction: (ev?: Event) => void, debounceDelay = 20): CoreEventObserver {
const resizeListener = CoreUtils.debounce((ev?: Event) => {
resizeFunction(ev);
}, debounceDelay);
window.addEventListener('resize', resizeListener);
return {
off: (): void => {
window.removeEventListener('resize', resizeListener);
},
};
}
/** /**
* Equivalent to element.closest(). If the browser doesn't support element.closest, it will * Equivalent to element.closest(). If the browser doesn't support element.closest, it will
* traverse the parents to achieve the same functionality. * traverse the parents to achieve the same functionality.

View File

@ -8,6 +8,7 @@
--collapsible-header-floating-title-width: 0px; --collapsible-header-floating-title-width: 0px;
--collapsible-header-floating-title-x-delta: 0px; --collapsible-header-floating-title-x-delta: 0px;
--collapsible-header-floating-title-width-delta: 0px; --collapsible-header-floating-title-width-delta: 0px;
ion-header.core-header-shadow { ion-header.core-header-shadow {
--core-header-shadow: none; --core-header-shadow: none;
} }
@ -19,9 +20,8 @@
.collapsible-header-floating-title { .collapsible-header-floating-title {
position: absolute; position: absolute;
top: var(--collapsible-header-floating-title-top); top: 0;
left: var(--collapsible-header-floating-title-left); left: 0;
transform: translateX(calc(var(--collapsible-header-floating-title-x-delta) * var(--collapsible-header-progress)));
opacity: 0; opacity: 0;
} }
@ -52,6 +52,9 @@
.collapsible-header-floating-title { .collapsible-header-floating-title {
opacity: 1; opacity: 1;
top: var(--collapsible-header-floating-title-top);
left: var(--collapsible-header-floating-title-left);
transform: translateX(calc(var(--collapsible-header-floating-title-x-delta) * var(--collapsible-header-progress)));
width: calc(var(--collapsible-header-floating-title-width) + var(--collapsible-header-progress) * var(--collapsible-header-floating-title-width-delta)); width: calc(var(--collapsible-header-floating-title-width) + var(--collapsible-header-progress) * var(--collapsible-header-floating-title-width-delta));
@include core-transition(width transform, 200ms, linear); @include core-transition(width transform, 200ms, linear);

View File

@ -1421,37 +1421,42 @@ ion-grid.core-no-grid > ion-row {
} }
[collapsible-footer] { [collapsible-footer] {
&.footer-collapsed { box-shadow: var(--drop-shadow-top, none);
--core-collapsible-footer-height: 0; width: 100%;
opacity: 0; bottom: 0;
} z-index: 3;
&.has-module-nav.footer-collapsed { display: block;
--core-collapsible-footer-height: auto; background-color: var(--core-collapsible-footer-background);
opacity: 1;
core-course-module-navigation {
height: 0;
opacity: 0;
@include core-transition(all, 200ms);
}
}
&.footer-expanded {
--core-collapsible-footer-height: auto;
}
.ion-margin { .ion-margin {
margin-top: 8px; margin-top: 8px;
margin-bottom: 8px; margin-bottom: 8px;
} }
box-shadow: var(--drop-shadow-top, none); &.is-active {
width: 100%; height: var(--core-collapsible-footer-height, auto);
bottom: 0; @include core-transition(all, 200ms);
z-index: 3;
height: var(--core-collapsible-footer-height, auto); &.footer-collapsed {
background-color: var(--core-collapsible-footer-background); --core-collapsible-footer-height: 0;
display: block; opacity: 0;
@include core-transition(all, 200ms); }
&.has-module-nav.footer-collapsed {
--core-collapsible-footer-height: auto;
opacity: 1;
core-course-module-navigation {
height: 0;
opacity: 0;
@include core-transition(all, 200ms);
}
}
&.footer-expanded {
--core-collapsible-footer-height: auto;
}
}
} }
.core-iframe-fullscreen [collapsible-footer] { .core-iframe-fullscreen [collapsible-footer] {

View File

@ -329,7 +329,6 @@
--core-courseimage-on-course-size: 72px; --core-courseimage-on-course-size: 72px;
--core-courseimage-radius: var(--medium-radius); --core-courseimage-radius: var(--medium-radius);
--core-collapsible-footer-height: 48px;
--core-navigation-background: var(--contrast-background); --core-navigation-background: var(--contrast-background);
--core-collapsible-footer-background: var(--contrast-background); --core-collapsible-footer-background: var(--contrast-background);