2021-11-09 13:08:56 +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-11-25 12:42:22 +01:00
|
|
|
import { ActivatedRoute, ActivatedRouteSnapshot, UrlSegment } from '@angular/router';
|
2021-11-09 13:08:56 +01:00
|
|
|
|
|
|
|
import { CoreNavigator } from '@services/navigator';
|
|
|
|
|
2021-12-10 07:19:33 +01:00
|
|
|
import { CoreRoutedItemsManager } from './routed-items-manager';
|
|
|
|
import { CoreRoutedItemsManagerSource } from './routed-items-manager-source';
|
2021-11-09 13:08:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class to manage the state and routing of a swipeable page.
|
|
|
|
*/
|
2021-12-16 12:29:13 +01:00
|
|
|
export class CoreSwipeNavigationItemsManager<
|
2021-11-17 12:31:05 +01:00
|
|
|
Item = unknown,
|
2021-12-10 07:19:33 +01:00
|
|
|
Source extends CoreRoutedItemsManagerSource<Item> = CoreRoutedItemsManagerSource<Item>
|
2021-11-17 12:31:05 +01:00
|
|
|
>
|
2021-12-10 07:19:33 +01:00
|
|
|
extends CoreRoutedItemsManager<Item, Source> {
|
2021-11-09 13:08:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process page started operations.
|
|
|
|
*/
|
|
|
|
async start(): Promise<void> {
|
|
|
|
this.updateSelectedItem();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to the next item.
|
|
|
|
*/
|
|
|
|
async navigateToNextItem(): Promise<void> {
|
|
|
|
await this.navigateToItemBy(-1, 'back');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to the previous item.
|
|
|
|
*/
|
|
|
|
async navigateToPreviousItem(): Promise<void> {
|
|
|
|
await this.navigateToItemBy(1, 'forward');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
protected getCurrentPageRoute(): ActivatedRoute | null {
|
|
|
|
return CoreNavigator.getCurrentRoute();
|
|
|
|
}
|
|
|
|
|
2021-11-22 14:30:17 +01:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2021-11-25 12:42:22 +01:00
|
|
|
protected getSelectedItemPathFromRoute(route: ActivatedRouteSnapshot): string | null {
|
|
|
|
const segments: UrlSegment[] = [];
|
|
|
|
|
|
|
|
while (route) {
|
|
|
|
segments.push(...route.url);
|
|
|
|
|
|
|
|
if (!route.firstChild) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
route = route.firstChild;
|
2021-11-22 14:30:17 +01:00
|
|
|
}
|
|
|
|
|
2021-11-25 12:42:22 +01:00
|
|
|
return segments.map(segment => segment.path).join('/').replace(/\/+/, '/').trim() || null;
|
2021-11-22 14:30:17 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 13:08:56 +01:00
|
|
|
/**
|
|
|
|
* Navigate to an item by an offset.
|
|
|
|
*
|
|
|
|
* @param delta Index offset.
|
|
|
|
* @param animationDirection Animation direction.
|
|
|
|
*/
|
|
|
|
protected async navigateToItemBy(delta: number, animationDirection: 'forward' | 'back'): Promise<void> {
|
|
|
|
const item = await this.getItemBy(delta);
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.navigateToItem(item, { animationDirection, replace: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get item by an offset.
|
|
|
|
*
|
|
|
|
* @param delta Index offset.
|
|
|
|
*/
|
|
|
|
protected async getItemBy(delta: number): Promise<Item | null> {
|
|
|
|
const items = this.getSource().getItems();
|
|
|
|
|
2021-12-16 12:29:13 +01:00
|
|
|
// Get selected item.
|
2021-11-09 13:08:56 +01:00
|
|
|
const index = (this.selectedItem && items?.indexOf(this.selectedItem)) ?? -1;
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get item by delta.
|
|
|
|
const item = items?.[index + delta] ?? null;
|
|
|
|
|
|
|
|
if (!item && !this.getSource().isCompleted()) {
|
2021-12-01 13:59:19 +01:00
|
|
|
await this.getSource().load();
|
2021-11-09 13:08:56 +01:00
|
|
|
|
|
|
|
return this.getItemBy(delta);
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|