lib/common/src/items/item.service.ts
Properties |
|
Methods |
constructor(itemRepresentations: ItemRepresentation[], itemActions: ItemAction[], injector: Injector, router: Router)
|
|||||||||||||||
Defined in lib/common/src/items/item.service.ts:39
|
|||||||||||||||
Parameters :
|
getActions | ||||||||||||||||||||
getActions(item: I, roles?: ItemActionRole[], extraItemActions: ItemAction<I>[], skipGlobalActions: boolean)
|
||||||||||||||||||||
Defined in lib/common/src/items/item.service.ts:88
|
||||||||||||||||||||
Type parameters :
|
||||||||||||||||||||
Gets all Actions associated with an item by type and store, that hold it, optionally filtering them by role or adding extra actions.
Parameters :
Returns :
Action[]
|
getItemRepresentation | ||||||
getItemRepresentation(item: I)
|
||||||
Defined in lib/common/src/items/item.service.ts:58
|
||||||
Type parameters :
|
||||||
Parameters :
Returns :
ItemRepresentation
|
Public itemActions |
Type : ItemAction[]
|
Decorators :
@Optional()
|
Defined in lib/common/src/items/item.service.ts:46
|
Public itemRepresentations |
Type : ItemRepresentation[]
|
Decorators :
@Optional()
|
Defined in lib/common/src/items/item.service.ts:43
|
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
}
}