2
0
Fork 0

MOBILE-3814 collapsible: Now collapsible header can be disabled

main
Pau Ferrer Ocaña 2022-03-04 15:42:03 +01:00
parent 52a3d62a44
commit f5303b3cc6
3 changed files with 46 additions and 10 deletions

View File

@ -12,7 +12,7 @@
// 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, OnDestroy, OnInit } from '@angular/core'; import { Directive, ElementRef, Input, OnChanges, OnDestroy, OnInit, SimpleChange } from '@angular/core';
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 { CoreUtils } from '@services/utils/utils'; import { CoreUtils } from '@services/utils/utils';
@ -50,7 +50,9 @@ import { CoreFormatTextDirective } from './format-text';
@Directive({ @Directive({
selector: 'ion-header[collapsible]', selector: 'ion-header[collapsible]',
}) })
export class CoreCollapsibleHeaderDirective implements OnInit, OnDestroy { export class CoreCollapsibleHeaderDirective implements OnInit, OnChanges, OnDestroy {
@Input() collapsible = true;
protected page?: HTMLElement; protected page?: HTMLElement;
protected collapsedHeader?: Element; protected collapsedHeader?: Element;
@ -63,6 +65,7 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnDestroy {
protected floatingTitle?: HTMLElement; protected floatingTitle?: HTMLElement;
protected scrollingHeight?: number; protected scrollingHeight?: number;
protected subscriptions: Subscription[] = []; protected subscriptions: Subscription[] = [];
protected enabled = true;
constructor(protected el: ElementRef) {} constructor(protected el: ElementRef) {}
@ -82,6 +85,18 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnDestroy {
this.initializeContent(); this.initializeContent();
} }
/**
* @inheritdoc
*/
ngOnChanges(changes: {[name: string]: SimpleChange}): void {
if (changes.collapsible) {
this.enabled = !CoreUtils.isFalseOrZero(changes.collapsible.currentValue);
setTimeout(() => {
this.setEnabled(this.enabled);
}, 200);
}
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
@ -277,6 +292,29 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnDestroy {
content && this.trackContentScroll(content); content && this.trackContentScroll(content);
} }
/**
* Set collapsed/expanded based on properties.
*
* @param enable True to enable, false otherwise
*/
async setEnabled(enable: boolean): Promise<void> {
if (!this.page || !this.content) {
return;
}
if (enable) {
const contentScroll = await this.content.getScrollElement();
// Do nothing, since scroll has already started on the page.
if (contentScroll.scrollTop > 0) {
return;
}
}
this.page.style.setProperty('--collapsible-header-progress', enable ? '0' : '1');
this.page.classList.toggle('is-collapsed', !enable);
}
/** /**
* Listen to a content element for scroll events that will control the header state transition. * Listen to a content element for scroll events that will control the header state transition.
* *
@ -287,6 +325,8 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnDestroy {
return; return;
} }
this.content = content;
const page = this.page; const page = this.page;
const scrollingHeight = this.scrollingHeight; const scrollingHeight = this.scrollingHeight;
const expandedHeader = this.expandedHeader; const expandedHeader = this.expandedHeader;
@ -294,7 +334,7 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnDestroy {
const expandedFontStyles = this.expandedFontStyles; const expandedFontStyles = this.expandedFontStyles;
const collapsedFontStyles = this.collapsedFontStyles; const collapsedFontStyles = this.collapsedFontStyles;
const floatingTitle = this.floatingTitle; const floatingTitle = this.floatingTitle;
const contentScroll = await content.getScrollElement(); const contentScroll = await this.content.getScrollElement();
if ( if (
!page || !page ||
@ -308,17 +348,15 @@ export class CoreCollapsibleHeaderDirective implements OnInit, OnDestroy {
throw new Error('[collapsible-header] Couldn\'t set up scrolling'); throw new Error('[collapsible-header] Couldn\'t set up scrolling');
} }
page.style.setProperty('--collapsible-header-progress', '0');
page.classList.toggle('is-within-content', content.contains(expandedHeader)); page.classList.toggle('is-within-content', content.contains(expandedHeader));
page.classList.toggle('is-collapsed', false); this.setEnabled(this.enabled);
Object Object
.entries(expandedFontStyles) .entries(expandedFontStyles)
.forEach(([property, value]) => floatingTitle.style.setProperty(property, value as string)); .forEach(([property, value]) => floatingTitle.style.setProperty(property, value as string));
this.content = content;
this.content.addEventListener('ionScroll', this.contentScrollListener = ({ target }: CustomEvent<ScrollDetail>): void => { this.content.addEventListener('ionScroll', this.contentScrollListener = ({ target }: CustomEvent<ScrollDetail>): void => {
if (target !== this.content) { if (target !== this.content || !this.enabled) {
return; return;
} }

View File

@ -1,4 +1,4 @@
<ion-header collapsible> <ion-header [collapsible]="tabsComponent?.selectedIndex == 0 || tabsComponent?.selectedIndex === undefined">
<ion-toolbar> <ion-toolbar>
<ion-buttons slot="start"> <ion-buttons slot="start">
<ion-back-button [text]="'core.back' | translate"></ion-back-button> <ion-back-button [text]="'core.back' | translate"></ion-back-button>

View File

@ -27,7 +27,6 @@ import { CoreTextUtils } from '@services/utils/text';
import { CoreNavigationOptions, CoreNavigator } from '@services/navigator'; import { CoreNavigationOptions, CoreNavigator } from '@services/navigator';
import { CONTENTS_PAGE_NAME } from '@features/course/course.module'; import { CONTENTS_PAGE_NAME } from '@features/course/course.module';
import { CoreDomUtils } from '@services/utils/dom'; import { CoreDomUtils } from '@services/utils/dom';
import { CoreCollapsibleHeaderDirective } from '@directives/collapsible-header';
import { CoreCourseSummaryPage } from '../course-summary/course-summary'; import { CoreCourseSummaryPage } from '../course-summary/course-summary';
/** /**
@ -41,7 +40,6 @@ import { CoreCourseSummaryPage } from '../course-summary/course-summary';
export class CoreCourseIndexPage implements OnInit, OnDestroy { export class CoreCourseIndexPage implements OnInit, OnDestroy {
@ViewChild(CoreTabsOutletComponent) tabsComponent?: CoreTabsOutletComponent; @ViewChild(CoreTabsOutletComponent) tabsComponent?: CoreTabsOutletComponent;
@ViewChild(CoreCollapsibleHeaderDirective) ionCollapsibleHeader?: CoreCollapsibleHeaderDirective;
title = ''; title = '';
category = ''; category = '';