/
AbstractVueTransitionController.ts
49 lines (43 loc) · 1.79 KB
/
AbstractVueTransitionController.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import AbstractTransitionController from 'transition-controller';
import isString from 'lodash/isString';
import isElement from 'lodash/isElement';
import { IAbstractTransitionComponent } from '../interface/IAbstractTransitionComponent';
export default abstract class AbstractVueTransitionController extends AbstractTransitionController<
IAbstractTransitionComponent
> {
/**
* This method finds a component based on a string, a html element or the instance.
*
* @protected
* @param {string | HTMLElement | IAbstractTransitionComponent} component The selector for the component
* @returns {IAbstractTransitionComponent} The instance of the component you requested
*/
protected getComponent(
component: string | HTMLElement | IAbstractTransitionComponent,
): IAbstractTransitionComponent {
let instance: IAbstractTransitionComponent;
if (isElement(component)) {
instance = <IAbstractTransitionComponent>this.parentController.$children.find(
child => child.$el === component,
);
} else if (isString(component)) {
const instances = (<Array<IAbstractTransitionComponent>>this.parentController.$children)
.map(
(child: IAbstractTransitionComponent) => (child.componentId === component ? child : null),
)
.filter((child: IAbstractTransitionComponent) => child !== null);
if (instances.length > 1) {
throw new Error(
`Found multiple components matching [${component}], use a unique ref when requesting a component with an id`,
);
}
instance = instances.pop();
} else {
instance = <IAbstractTransitionComponent>component;
}
if (instance === undefined) {
throw new Error(`The requested component [${component}] does not exist`);
}
return instance;
}
}