2021-01-19 16:28:37 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-01-26 09:28:46 +01:00
|
|
|
import { AfterViewInit, Component, ElementRef, HostBinding, OnDestroy, ViewChild } from '@angular/core';
|
2021-01-19 16:28:37 +01:00
|
|
|
import { IonRouterOutlet } from '@ionic/angular';
|
2021-01-26 09:28:46 +01:00
|
|
|
import { CoreScreen } from '@services/screen';
|
2021-01-19 16:28:37 +01:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
|
2021-01-26 09:28:46 +01:00
|
|
|
enum CoreSplitViewMode {
|
|
|
|
Default = '', // Shows both menu and content.
|
|
|
|
MenuOnly = 'menu-only', // Hides content.
|
|
|
|
ContentOnly = 'content-only', // Hides menu.
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:28:37 +01:00
|
|
|
@Component({
|
|
|
|
selector: 'core-split-view',
|
|
|
|
templateUrl: 'split-view.html',
|
|
|
|
styleUrls: ['split-view.scss'],
|
|
|
|
})
|
|
|
|
export class CoreSplitViewComponent implements AfterViewInit, OnDestroy {
|
|
|
|
|
|
|
|
@ViewChild(IonRouterOutlet) outlet!: IonRouterOutlet;
|
2021-01-26 09:28:46 +01:00
|
|
|
@HostBinding('class') classes = '';
|
2021-01-19 16:28:37 +01:00
|
|
|
|
2021-01-26 09:28:46 +01:00
|
|
|
private isNestedSplitView = false;
|
2021-01-19 16:28:37 +01:00
|
|
|
private subscriptions?: Subscription[];
|
|
|
|
|
2021-01-26 09:28:46 +01:00
|
|
|
constructor(private element: ElementRef<HTMLElement>) {}
|
|
|
|
|
2021-01-19 16:28:37 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ngAfterViewInit(): void {
|
2021-01-26 09:28:46 +01:00
|
|
|
this.isNestedSplitView = !!this.element.nativeElement.parentElement?.closest('core-split-view');
|
2021-01-19 16:28:37 +01:00
|
|
|
this.subscriptions = [
|
2021-01-26 09:28:46 +01:00
|
|
|
this.outlet.activateEvents.subscribe(() => this.updateClasses()),
|
|
|
|
this.outlet.deactivateEvents.subscribe(() => this.updateClasses()),
|
|
|
|
CoreScreen.instance.layoutObservable.subscribe(() => this.updateClasses()),
|
2021-01-19 16:28:37 +01:00
|
|
|
];
|
2021-01-26 09:28:46 +01:00
|
|
|
|
|
|
|
this.updateClasses();
|
2021-01-19 16:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.subscriptions?.forEach(subscription => subscription.unsubscribe());
|
|
|
|
}
|
|
|
|
|
2021-01-26 09:28:46 +01:00
|
|
|
/**
|
|
|
|
* Update host classes.
|
|
|
|
*/
|
|
|
|
private updateClasses(): void {
|
|
|
|
this.classes = this.getCurrentMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current mode. Depending on the layout, outlet status, and whether this split view
|
|
|
|
* is nested or not, this method will indicate which parts of the split view should be visible.
|
|
|
|
*
|
|
|
|
* @return Split view mode.
|
|
|
|
*/
|
|
|
|
private getCurrentMode(): CoreSplitViewMode {
|
|
|
|
if (this.isNestedSplitView) {
|
|
|
|
return CoreSplitViewMode.MenuOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CoreScreen.instance.isMobile) {
|
|
|
|
return this.outlet.isActivated
|
|
|
|
? CoreSplitViewMode.ContentOnly
|
|
|
|
: CoreSplitViewMode.MenuOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CoreSplitViewMode.Default;
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:28:37 +01:00
|
|
|
}
|