File

lib/common/src/items/item.service.ts

Index

Properties
Methods

Constructor

constructor(itemRepresentations: ItemRepresentation[], itemActions: ItemAction[], injector: Injector, router: Router)
Parameters :
Name Type Optional
itemRepresentations ItemRepresentation[] No
itemActions ItemAction[] No
injector Injector No
router Router No

Methods

getActions
getActions(item: I, roles?: ItemActionRole[], extraItemActions: ItemAction<I>[], skipGlobalActions: boolean)
Type parameters :
  • I

Gets all Actions associated with an item by type and store, that hold it, optionally filtering them by role or adding extra actions.

Parameters :
Name Type Optional Default value
item I No
roles ItemActionRole[] Yes
extraItemActions ItemAction<I>[] No []
skipGlobalActions boolean No false
Returns : Action[]
getItemRepresentation
getItemRepresentation(item: I)
Type parameters :
  • I
Parameters :
Name Type Optional
item I No
Returns : ItemRepresentation

Properties

Public itemActions
Type : ItemAction[]
Decorators :
@Optional()
@Inject(ITEM_ACTIONS)
Public itemRepresentations
Type : ItemRepresentation[]
Decorators :
@Optional()
@Inject(ITEM_REPRESENTATIONS)
import	{
			Injectable,
			Inject,
			Injector,
			Optional,
			Component,
			Type,
		}									from '@angular/core'

import	{
			Router
		}									from '@angular/router'

import	{
			Item,
			sortByKeyFn,
			camel2Underscore,
		}									from '@rcc/core'
import	{
			Action,
		}									from '../actions'

import	{
			ItemAction,
			ItemActionRole,
			ItemRepresentation,
			ITEM_REPRESENTATIONS,
			ITEM_ACTIONS,
		}									from './item.commons'


@Component({
	template: `{{"ITEMS.MISSING_REPRESENTATION" | translate}}`
})
export class DefaultLabelComponent{}


@Injectable()
export class ItemService{

	constructor(
		@Optional() @Inject(ITEM_REPRESENTATIONS)
		public	itemRepresentations		: ItemRepresentation[],

		@Optional() @Inject(ITEM_ACTIONS)
		public	itemActions				: ItemAction[],

		protected injector				: Injector,
		protected router				: Router
	){

		if(!this.itemRepresentations) 	this.itemRepresentations	= []
		if(!this.itemActions)			this.itemActions			= []

	}


	getItemRepresentation<I extends Item>(item: I) : ItemRepresentation {

		const itemRepresentation 				=	this.itemRepresentations
													.find( itemRep => item instanceof itemRep.itemClass )


		if(itemRepresentation){
			itemRepresentation.name = itemRepresentation.name || camel2Underscore(itemRepresentation.itemClass.name).toUpperCase()
			return itemRepresentation
		}

		console.warn(`ItemService.getRepresentation(): missing item representation for: ${item.constructor.name}`)

		// Default name is the capitalized class name. (will be run through translations)
		const itemPresentationFallback = 	{
												itemClass :			item.constructor as Type<I>,
												name : 				"ITEMS.MISSING_REPRESENTATION",
												icon :				'item',
												labelComponent :	DefaultLabelComponent
											}

		return itemPresentationFallback
	}



	/**
	 * Gets all {@link Action | Actions} associated with an item by type and store, that hold it,
	 * optionally filtering them by role or adding extra actions.
	 */
	getActions<I extends Item> (

		/**
		 * The item to get actions for.
		 */
		item	: I,

		/**
		 * Only return actions that have one of the provided roles. If this array
		 * undefined return all action, if it is empty, none.
		 */
		roles?	: ItemActionRole[],

		/**
		 * Extra actions.
		 */
		extraItemActions: ItemAction<I>[] = [],

		/**
		 * Only use extra actions.
		 */
		skipGlobalActions: boolean = false

	) : Action[] {

		const actions =	(
							skipGlobalActions
							?	extraItemActions
							:	[...this.itemActions, ...extraItemActions]
						)
						.filter((itemAction : ItemAction<I>) => !roles || roles.length == 0 || roles.includes(itemAction.role) )
						.filter((itemAction : ItemAction<I>) => item instanceof itemAction.itemClass )
						.map( 	(itemAction : ItemAction<I>) => {

							const store 	= 	itemAction.storeClass
												?	this.injector.get(itemAction.storeClass)
												:	null

							if(store && !store.items.includes(item)) return;

							const action	=	store
												?	itemAction.getAction(item, store)
												:	itemAction.getAction(item)

							return action

						})
						.filter( x => !!x)
						.filter(
							action => 	typeof action.position == 'function'
										?	action.position() 	!== null
										:	action.position		!== null
						)
						.sort(sortByKeyFn('position'))

		return actions

	}

}

results matching ""

    No results matching ""