2022-02-24 16:23:22 +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.
|
|
|
|
|
2022-03-11 11:42:44 +00:00
|
|
|
import { Component } from '@angular/core';
|
2022-03-16 12:49:58 +00:00
|
|
|
import { AsyncComponent } from '@classes/async-component';
|
2022-03-11 11:42:44 +00:00
|
|
|
import { CoreUtils } from '@services/utils/utils';
|
2022-03-31 09:02:52 +00:00
|
|
|
import { CoreLogger } from './logger';
|
2022-03-11 11:42:44 +00:00
|
|
|
|
2022-02-24 16:23:22 +00:00
|
|
|
/**
|
|
|
|
* Registry to keep track of component instances.
|
|
|
|
*/
|
|
|
|
export class CoreComponentsRegistry {
|
|
|
|
|
|
|
|
private static instances: WeakMap<Element, unknown> = new WeakMap();
|
2022-03-31 09:02:52 +00:00
|
|
|
protected static logger = CoreLogger.getInstance('CoreComponentsRegistry');
|
2022-02-24 16:23:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a component instance.
|
|
|
|
*
|
|
|
|
* @param element Root element.
|
|
|
|
* @param instance Component instance.
|
|
|
|
*/
|
|
|
|
static register(element: Element, instance: unknown): void {
|
|
|
|
this.instances.set(element, instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve a component instance.
|
|
|
|
*
|
|
|
|
* @param element Root element.
|
|
|
|
* @param componentClass Component class.
|
|
|
|
* @returns Component instance.
|
|
|
|
*/
|
2022-03-16 12:49:58 +00:00
|
|
|
static resolve<T>(element?: Element | null, componentClass?: ComponentConstructor<T>): T | null {
|
2022-02-24 16:23:22 +00:00
|
|
|
const instance = (element && this.instances.get(element) as T) ?? null;
|
|
|
|
|
|
|
|
return instance && (!componentClass || instance instanceof componentClass)
|
|
|
|
? instance
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
|
2022-03-08 16:39:22 +00:00
|
|
|
/**
|
|
|
|
* Get a component instances and fail if it cannot be resolved.
|
|
|
|
*
|
|
|
|
* @param element Root element.
|
|
|
|
* @param componentClass Component class.
|
|
|
|
* @returns Component instance.
|
|
|
|
*/
|
|
|
|
static require<T>(element: Element, componentClass?: ComponentConstructor<T>): T {
|
|
|
|
const instance = this.resolve(element, componentClass);
|
|
|
|
|
|
|
|
if (!instance) {
|
|
|
|
throw new Error('Couldn\'t resolve component instance');
|
|
|
|
}
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:42:44 +00:00
|
|
|
/**
|
2022-03-16 12:49:58 +00:00
|
|
|
* Get a component instances and wait to be ready.
|
2022-03-11 11:42:44 +00:00
|
|
|
*
|
2022-03-16 12:49:58 +00:00
|
|
|
* @param element Root element.
|
|
|
|
* @param componentClass Component class.
|
2022-12-01 11:31:00 +00:00
|
|
|
* @returns Promise resolved when done.
|
2022-03-11 11:42:44 +00:00
|
|
|
*/
|
2022-03-16 12:49:58 +00:00
|
|
|
static async waitComponentReady<T extends AsyncComponent>(
|
|
|
|
element: Element | null,
|
|
|
|
componentClass?: ComponentConstructor<T>,
|
2022-03-11 11:42:44 +00:00
|
|
|
): Promise<void> {
|
2022-03-16 12:49:58 +00:00
|
|
|
const instance = this.resolve(element, componentClass);
|
|
|
|
if (!instance) {
|
2022-03-31 09:02:52 +00:00
|
|
|
this.logger.error('No instance registered for element ' + componentClass, element);
|
|
|
|
|
2022-03-11 11:42:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-16 12:49:58 +00:00
|
|
|
await instance.ready();
|
|
|
|
}
|
2022-03-11 11:42:44 +00:00
|
|
|
|
2022-03-16 12:49:58 +00:00
|
|
|
/**
|
|
|
|
* Waits all elements matching to be ready.
|
|
|
|
*
|
|
|
|
* @param element Element where to search.
|
|
|
|
* @param selector Selector to search on parent.
|
|
|
|
* @param componentClass Component class.
|
2022-12-01 11:31:00 +00:00
|
|
|
* @returns Promise resolved when done.
|
2022-03-16 12:49:58 +00:00
|
|
|
*/
|
|
|
|
static async waitComponentsReady<T extends AsyncComponent>(
|
|
|
|
element: Element,
|
|
|
|
selector: string,
|
|
|
|
componentClass?: ComponentConstructor<T>,
|
|
|
|
): Promise<void> {
|
2022-03-31 09:02:52 +00:00
|
|
|
let elements: Element[] = [];
|
|
|
|
|
2022-03-16 12:49:58 +00:00
|
|
|
if (element.matches(selector)) {
|
|
|
|
// Element to wait is myself.
|
2022-03-31 09:02:52 +00:00
|
|
|
elements = [element];
|
2022-03-16 12:49:58 +00:00
|
|
|
} else {
|
2022-03-31 09:02:52 +00:00
|
|
|
elements = Array.from(element.querySelectorAll(selector));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!elements.length) {
|
|
|
|
return;
|
2022-03-16 12:49:58 +00:00
|
|
|
}
|
2022-03-11 14:58:02 +00:00
|
|
|
|
2022-03-31 09:02:52 +00:00
|
|
|
await Promise.all(elements.map(element => CoreComponentsRegistry.waitComponentReady<T>(element, componentClass)));
|
|
|
|
|
2022-03-11 14:58:02 +00:00
|
|
|
// Wait for next tick to ensure components are completely rendered.
|
2022-03-11 11:42:44 +00:00
|
|
|
await CoreUtils.nextTick();
|
|
|
|
}
|
|
|
|
|
2022-02-24 16:23:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component constructor.
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
2022-03-16 12:49:58 +00:00
|
|
|
export type ComponentConstructor<T = Component> = { new(...args: any[]): T };
|