2021-02-04 18:55:26 +00: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-02-24 13:26:38 +00:00
|
|
|
import { ActivatedRoute, ActivatedRouteSnapshot, Params } from '@angular/router';
|
2021-02-04 18:55:26 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
|
|
|
|
import { CoreSplitViewComponent } from '@components/split-view/split-view';
|
|
|
|
import { CoreNavigator } from '@services/navigator';
|
|
|
|
import { CoreScreen } from '@services/screen';
|
|
|
|
import { CoreUtils } from '@services/utils/utils';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class to manage the state and routing of a list of items in a page, for example on pages using a split view.
|
|
|
|
*/
|
|
|
|
export abstract class CorePageItemsListManager<Item> {
|
|
|
|
|
|
|
|
protected itemsList: Item[] | null = null;
|
|
|
|
protected itemsMap: Record<string, Item> | null = null;
|
2021-02-10 09:42:32 +00:00
|
|
|
protected hasMoreItems = true;
|
2021-02-04 18:55:26 +00:00
|
|
|
protected selectedItem: Item | null = null;
|
|
|
|
protected pageComponent: unknown;
|
|
|
|
protected splitView?: CoreSplitViewComponent;
|
|
|
|
protected splitViewOutletSubscription?: Subscription;
|
|
|
|
|
|
|
|
constructor(pageComponent: unknown) {
|
|
|
|
this.pageComponent = pageComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
get items(): Item[] {
|
|
|
|
return this.itemsList || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
get loaded(): boolean {
|
|
|
|
return this.itemsMap !== null;
|
|
|
|
}
|
|
|
|
|
2021-02-10 09:42:32 +00:00
|
|
|
get completed(): boolean {
|
|
|
|
return !this.hasMoreItems;
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:55:26 +00:00
|
|
|
get empty(): boolean {
|
|
|
|
return this.itemsList === null || this.itemsList.length === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process page started operations.
|
2021-02-22 14:58:20 +00:00
|
|
|
*
|
|
|
|
* @param splitView Split view component.
|
2021-02-04 18:55:26 +00:00
|
|
|
*/
|
2021-02-22 14:58:20 +00:00
|
|
|
async start(splitView: CoreSplitViewComponent): Promise<void> {
|
|
|
|
this.watchSplitViewOutlet(splitView);
|
|
|
|
|
2021-02-04 18:55:26 +00:00
|
|
|
// Calculate current selected item.
|
|
|
|
const route = CoreNavigator.instance.getCurrentRoute({ pageComponent: this.pageComponent });
|
|
|
|
if (route !== null && route.firstChild) {
|
|
|
|
this.updateSelectedItem(route.firstChild.snapshot);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select default item if none is selected on a non-mobile layout.
|
|
|
|
if (!CoreScreen.instance.isMobile && this.selectedItem === null) {
|
|
|
|
const defaultItem = this.getDefaultItem();
|
|
|
|
|
|
|
|
if (defaultItem) {
|
|
|
|
this.select(defaultItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log activity.
|
|
|
|
await CoreUtils.instance.ignoreErrors(this.logActivity());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process page destroyed operations.
|
|
|
|
*/
|
|
|
|
destroy(): void {
|
|
|
|
this.splitViewOutletSubscription?.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Watch a split view outlet to keep track of the selected item.
|
|
|
|
*
|
|
|
|
* @param splitView Split view component.
|
|
|
|
*/
|
|
|
|
watchSplitViewOutlet(splitView: CoreSplitViewComponent): void {
|
|
|
|
this.splitView = splitView;
|
|
|
|
this.splitViewOutletSubscription = splitView.outletRouteObservable.subscribe(route => this.updateSelectedItem(route));
|
|
|
|
|
|
|
|
this.updateSelectedItem(splitView.outletRoute);
|
|
|
|
}
|
|
|
|
|
2021-02-10 16:03:39 +00:00
|
|
|
/**
|
|
|
|
* Reset items data.
|
|
|
|
*/
|
|
|
|
resetItems(): void {
|
|
|
|
this.itemsList = null;
|
|
|
|
this.itemsMap = null;
|
|
|
|
this.hasMoreItems = true;
|
|
|
|
this.selectedItem = null;
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:55:26 +00:00
|
|
|
// @todo Implement watchResize.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether the given item is selected or not.
|
|
|
|
*
|
|
|
|
* @param item Item.
|
|
|
|
* @return Whether the given item is selected.
|
|
|
|
*/
|
|
|
|
isSelected(item: Item): boolean {
|
|
|
|
return this.selectedItem === item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Select an item.
|
|
|
|
*
|
|
|
|
* @param item Item.
|
|
|
|
*/
|
|
|
|
async select(item: Item): Promise<void> {
|
|
|
|
// Get current route in the page.
|
|
|
|
const route = CoreNavigator.instance.getCurrentRoute({ pageComponent: this.pageComponent });
|
|
|
|
|
|
|
|
if (route === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this item is already selected, do nothing.
|
2021-02-24 13:26:38 +00:00
|
|
|
const itemRoute = this.getItemRoute(route);
|
2021-02-04 18:55:26 +00:00
|
|
|
const itemPath = this.getItemPath(item);
|
2021-02-24 13:26:38 +00:00
|
|
|
const selectedItemPath = itemRoute ? this.getSelectedItemPath(itemRoute.snapshot) : null;
|
2021-02-04 18:55:26 +00:00
|
|
|
|
2021-02-24 13:26:38 +00:00
|
|
|
if (selectedItemPath === itemPath) {
|
2021-02-04 18:55:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Navigate to item.
|
|
|
|
const params = this.getItemQueryParams(item);
|
2021-02-24 13:26:38 +00:00
|
|
|
const pathPrefix = selectedItemPath ? selectedItemPath.split('/').fill('../').join('') : '';
|
2021-02-04 18:55:26 +00:00
|
|
|
|
2021-02-24 13:26:38 +00:00
|
|
|
await CoreNavigator.instance.navigate(pathPrefix + itemPath, { params, reset: true });
|
2021-02-04 18:55:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the list of items.
|
|
|
|
*
|
|
|
|
* @param items Items.
|
2021-02-10 09:42:32 +00:00
|
|
|
* @param hasMoreItems Whether the list has more items that haven't been loaded.
|
2021-02-04 18:55:26 +00:00
|
|
|
*/
|
2021-02-10 09:42:32 +00:00
|
|
|
setItems(items: Item[], hasMoreItems: boolean = false): void {
|
|
|
|
this.hasMoreItems = hasMoreItems;
|
2021-02-04 18:55:26 +00:00
|
|
|
this.itemsList = items.slice(0);
|
|
|
|
this.itemsMap = items.reduce((map, item) => {
|
|
|
|
map[this.getItemPath(item)] = item;
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
this.updateSelectedItem(this.splitView?.outletRoute);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log activity when the page starts.
|
|
|
|
*/
|
|
|
|
protected async logActivity(): Promise<void> {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the selected item given the current route.
|
|
|
|
*
|
|
|
|
* @param route Current route.
|
|
|
|
*/
|
|
|
|
protected updateSelectedItem(route?: ActivatedRouteSnapshot | null): void {
|
|
|
|
const selectedItemPath = route ? this.getSelectedItemPath(route) : null;
|
|
|
|
|
|
|
|
this.selectedItem = selectedItemPath
|
|
|
|
? this.itemsMap?.[selectedItemPath] ?? null
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the item that should be selected by default.
|
|
|
|
*/
|
|
|
|
protected getDefaultItem(): Item | null {
|
|
|
|
return this.itemsList?.[0] || null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the query parameters to use when navigating to an item page.
|
|
|
|
*
|
|
|
|
* @param item Item.
|
|
|
|
* @return Query parameters to use when navigating to the item page.
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
protected getItemQueryParams(item: Item): Params {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path to use when navigating to an item page.
|
|
|
|
*
|
|
|
|
* @param item Item.
|
|
|
|
* @return Path to use when navigating to the item page.
|
|
|
|
*/
|
|
|
|
protected abstract getItemPath(item: Item): string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the path of the selected item given the current route.
|
|
|
|
*
|
|
|
|
* @param route Current route.
|
|
|
|
* @return Path of the selected item in the given route.
|
|
|
|
*/
|
|
|
|
protected abstract getSelectedItemPath(route: ActivatedRouteSnapshot): string | null;
|
|
|
|
|
2021-02-24 13:26:38 +00:00
|
|
|
/**
|
|
|
|
* Get the active item route, if any.
|
|
|
|
*
|
|
|
|
* @param pageRoute Page route.
|
|
|
|
* @return Item route.
|
|
|
|
*/
|
|
|
|
private getItemRoute(pageRoute: ActivatedRoute): ActivatedRoute | null {
|
|
|
|
let itemRoute = pageRoute.firstChild;
|
|
|
|
|
|
|
|
while (itemRoute && !itemRoute.component) {
|
|
|
|
itemRoute = itemRoute.firstChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemRoute;
|
|
|
|
}
|
|
|
|
|
2021-02-04 18:55:26 +00:00
|
|
|
}
|