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.
|
|
|
|
|
|
|
|
import { CoreItemsManagerSource } from './items-manager-source';
|
|
|
|
|
2021-12-16 12:29:13 +01:00
|
|
|
/**
|
|
|
|
* Listeners.
|
|
|
|
*/
|
|
|
|
export interface CoreItemsanagerListener<Item> {
|
|
|
|
onSelectedItemUpdated?(item: Item): void;
|
|
|
|
}
|
|
|
|
|
2021-11-09 13:08:56 +01:00
|
|
|
/**
|
|
|
|
* Helper to manage a collection of items in a page.
|
|
|
|
*/
|
2021-12-10 07:19:33 +01:00
|
|
|
export abstract class CoreItemsManager<
|
|
|
|
Item = unknown,
|
|
|
|
Source extends CoreItemsManagerSource<Item> = CoreItemsManagerSource<Item>,
|
|
|
|
> {
|
2021-11-09 13:08:56 +01:00
|
|
|
|
2021-11-17 12:31:05 +01:00
|
|
|
protected source?: { instance: Source; unsubscribe: () => void };
|
2021-11-09 13:08:56 +01:00
|
|
|
protected itemsMap: Record<string, Item> | null = null;
|
|
|
|
protected selectedItem: Item | null = null;
|
2021-12-16 12:29:13 +01:00
|
|
|
protected listeners: CoreItemsanagerListener<Item>[] = [];
|
2021-11-09 13:08:56 +01:00
|
|
|
|
2021-11-17 12:31:05 +01:00
|
|
|
constructor(source: Source) {
|
2021-11-09 13:08:56 +01:00
|
|
|
this.setSource(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get source.
|
|
|
|
*
|
|
|
|
* @returns Source.
|
|
|
|
*/
|
2021-11-17 12:31:05 +01:00
|
|
|
getSource(): Source {
|
2021-11-09 13:08:56 +01:00
|
|
|
if (!this.source) {
|
|
|
|
throw new Error('Source is missing from items manager');
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.source.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set source.
|
|
|
|
*
|
|
|
|
* @param newSource New source.
|
|
|
|
*/
|
2021-11-17 12:31:05 +01:00
|
|
|
setSource(newSource: Source | null): void {
|
2021-11-09 13:08:56 +01:00
|
|
|
if (this.source) {
|
|
|
|
this.source.unsubscribe();
|
|
|
|
delete this.source;
|
|
|
|
|
|
|
|
this.onSourceReset();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newSource) {
|
|
|
|
this.source = {
|
|
|
|
instance: newSource,
|
|
|
|
unsubscribe: newSource.addListener({
|
|
|
|
onItemsUpdated: items => this.onSourceItemsUpdated(items),
|
|
|
|
onReset: () => this.onSourceReset(),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
const items = newSource.getItems();
|
|
|
|
|
|
|
|
if (items) {
|
|
|
|
this.onSourceItemsUpdated(items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process page destroyed operations.
|
|
|
|
*/
|
|
|
|
destroy(): void {
|
|
|
|
this.setSource(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-10 07:19:33 +01:00
|
|
|
* Get selected item.
|
2021-11-09 13:08:56 +01:00
|
|
|
*
|
2021-12-10 07:19:33 +01:00
|
|
|
* @return Selected item, null if none.
|
2021-11-09 13:08:56 +01:00
|
|
|
*/
|
2021-12-10 07:19:33 +01:00
|
|
|
getSelectedItem(): Item | null {
|
|
|
|
return this.selectedItem;
|
2021-11-09 13:08:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-12-10 07:19:33 +01:00
|
|
|
* Set selected item.
|
2021-11-09 13:08:56 +01:00
|
|
|
*
|
2021-12-10 07:19:33 +01:00
|
|
|
* @param item Item, null if none.
|
2021-11-09 13:08:56 +01:00
|
|
|
*/
|
2021-12-10 07:19:33 +01:00
|
|
|
setSelectedItem(item: Item | null): void {
|
|
|
|
this.selectedItem = item;
|
2021-12-16 12:29:13 +01:00
|
|
|
|
|
|
|
this.listeners.forEach(listener => listener.onSelectedItemUpdated?.call(listener, item));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a listener.
|
|
|
|
*
|
|
|
|
* @param listener Listener.
|
|
|
|
* @returns Unsubscribe function.
|
|
|
|
*/
|
|
|
|
addListener(listener: CoreItemsanagerListener<Item>): () => void {
|
|
|
|
this.listeners.push(listener);
|
|
|
|
|
|
|
|
return () => this.removeListener(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a listener.
|
|
|
|
*
|
|
|
|
* @param listener Listener.
|
|
|
|
*/
|
|
|
|
removeListener(listener: CoreItemsanagerListener<Item>): void {
|
|
|
|
const index = this.listeners.indexOf(listener);
|
|
|
|
|
|
|
|
if (index === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.listeners.splice(index, 1);
|
2021-11-09 13:08:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when source items have been updated.
|
|
|
|
*
|
|
|
|
* @param items New items.
|
|
|
|
*/
|
|
|
|
protected onSourceItemsUpdated(items: Item[]): void {
|
2021-12-16 12:29:13 +01:00
|
|
|
this.itemsMap = items.reduce((map, item) => {
|
|
|
|
map[this.getItemId(item)] = item;
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}, {});
|
2021-11-09 13:08:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when source has been updated.
|
|
|
|
*/
|
|
|
|
protected onSourceReset(): void {
|
|
|
|
this.itemsMap = null;
|
|
|
|
this.selectedItem = null;
|
|
|
|
}
|
|
|
|
|
2021-12-16 12:29:13 +01:00
|
|
|
/**
|
|
|
|
* Get item by ID.
|
|
|
|
*
|
|
|
|
* @param id ID
|
|
|
|
* @return Item, null if not found.
|
|
|
|
*/
|
|
|
|
getItemById(id: string | number): Item | null {
|
|
|
|
return this.itemsMap?.[id] ?? null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an ID to identify an item.
|
|
|
|
*
|
|
|
|
* @param item Data about the item.
|
|
|
|
* @return Item ID.
|
|
|
|
*/
|
|
|
|
abstract getItemId(item: Item): string | number;
|
|
|
|
|
2021-11-09 13:08:56 +01:00
|
|
|
}
|