File

lib/common/src/widgets/widgets.service.ts

Description

This is the central widget handling service.

Index

Properties
Methods

Constructor

constructor(widgets: WidgetComponentType[])
Parameters :
Name Type Optional
widgets WidgetComponentType[] No

Methods

Public getWidgetMatches
getWidgetMatches(control: CT)
Type parameters :
  • CT

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 :
Name Type Optional
control CT No

Properties

Public widgets
Type : WidgetComponentType[]
Decorators :
@Inject(WIDGETS)
@Optional()

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)

	}

}

results matching ""

    No results matching ""