lib/common/src/widgets/widgets.service.ts
This is the central widget handling service.
Properties |
|
Methods |
|
constructor(widgets: WidgetComponentType[])
|
||||||
Defined in lib/common/src/widgets/widgets.service.ts:24
|
||||||
Parameters :
|
Public getWidgetMatches | ||||||
getWidgetMatches(control: CT)
|
||||||
Defined in lib/common/src/widgets/widgets.service.ts:46
|
||||||
Type parameters :
|
||||||
Returns a sorted array of widget compontents ordered by matching score; highest comes first. (See WidgetMatchFn and WidgetComponent.) Includes only those widget components that match the type of control passed and have a matching score >= 0.
Parameters :
Returns :
WidgetComponentType[]
|
Public widgets |
Type : WidgetComponentType[]
|
Decorators :
@Inject(WIDGETS)
|
Defined in lib/common/src/widgets/widgets.service.ts:32
|
Array of all registerd widget components |
import {
Inject,
Injectable,
Optional,
} from '@angular/core'
import {
} from '@rcc/core'
import {
WidgetControl,
WidgetComponentType,
WIDGETS,
} from './widgets.commons'
/**
* This is the central widget handling service.
*/
@Injectable()
export class WidgetsService {
constructor(
/**
* Array of all registerd widget components
*/
@Inject(WIDGETS)@Optional()
public widgets: WidgetComponentType[]
){
this.widgets = widgets || []
}
/**
* Returns a sorted array of widget compontents ordered by matching score; highest comes first.
* (See {@link WidgetMatchFn} and {@link WidgetComponent}.)
* Includes only those widget components that match the type of control passed and have a matching score >= 0.
*
*/
public getWidgetMatches<CT extends WidgetControl = WidgetControl>(control: CT): WidgetComponentType<CT>[] {
const isOfMatchingControlType = function(widget : WidgetComponentType<unknown>) : widget is WidgetComponentType<CT> {
return control instanceof widget.controlType
}
const widgetsOfMatchingControlType = this.widgets.filter( isOfMatchingControlType )
return widgetsOfMatchingControlType
.map( widget => ({
widget,
match: widget.widgetMatch
? widget.widgetMatch(control)
: -1,
})
)
.filter( item => item.match >= 0 )
.sort( (item1, item2) => Math.sign(item2.match - item1.match) )
.map( item => item.widget)
}
}