lib/common/src/meta-store/content/meta-store.component.ts
changeDetection | ChangeDetectionStrategy.OnPush |
selector | rcc-meta-store |
styleUrls | ./meta-store.component.scss |
templateUrl | ./meta-store.component.html |
Properties |
|
Methods |
|
Inputs |
Accessors |
constructor(changeDetectorRef: ChangeDetectorRef)
|
||||||
Parameters :
|
filterControl | |
Type : FormControl | null
|
|
filterQuery | |
Type : string
|
|
itemClass | |
Type : Type<Item>
|
|
metaStore | |
Type : MetaStore
|
|
selectInto | |
Type : [] | null
|
|
Public filterItems | ||||||
filterItems(items: Item[])
|
||||||
Parameters :
Returns :
Item[]
|
ngOnDestroy |
ngOnDestroy()
|
Returns :
void
|
ngOnInit |
ngOnInit()
|
Returns :
void
|
Public setMetaStore | ||||||
setMetaStore(metaStore: MetaStore)
|
||||||
Parameters :
Returns :
void
|
Public toggleSelect | ||||||
toggleSelect(item: Item)
|
||||||
Parameters :
Returns :
void
|
Private _metaStore |
Type : MetaStore
|
Public activeStores |
Type : ItemStore[]
|
Default value : []
|
Protected destroy$ |
Default value : new Subject<void>()
|
Private query |
Type : string | null
|
Public selected |
Type : Item[] | null
|
Default value : null
|
metaStore | ||||||
getmetaStore()
|
||||||
setmetaStore(metaStore: MetaStore)
|
||||||
Parameters :
Returns :
void
|
selectInto | ||||||
setselectInto(items: Item[] | null)
|
||||||
Parameters :
Returns :
void
|
filterQuery | ||||||
setfilterQuery(query: string)
|
||||||
Parameters :
Returns :
void
|
import {
Component,
Input,
OnInit,
OnDestroy,
Optional,
Type,
ChangeDetectionStrategy,
ChangeDetectorRef,
} from '@angular/core'
import { FormControl } from '@angular/forms'
import {
Subject,
takeUntil,
startWith
} from 'rxjs'
import {
Item,
ItemStore
} from '@rcc/core'
import { MetaStore } from '../meta-store.class'
@Component({
selector: 'rcc-meta-store',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './meta-store.component.html',
styleUrls: ['./meta-store.component.scss'],
})
export class MetaStoreComponent implements OnInit, OnDestroy {
// @ContentChild(TemplateRef)
// public itemLabelTemplate! : TemplateRef<any>;
@Input()
public itemClass! : Type<Item>
@Input()
set metaStore(metaStore : MetaStore) { this.setMetaStore(metaStore) }
get metaStore(): MetaStore { return this._metaStore }
private _metaStore : MetaStore
@Optional() @Input()
public filterControl! : FormControl | null
@Optional() @Input()
public set selectInto(items: Item[] | null) {
this.selected = items || null
}
@Optional() @Input()
public set filterQuery(query: string){
this.query = query
}
public activeStores : ItemStore[] = []
public selected : Item[] | null = null
private query! : string | null
protected destroy$ = new Subject<void>()
constructor(
protected changeDetectorRef : ChangeDetectorRef
){
}
public setMetaStore(metaStore : MetaStore) : void {
this._metaStore = metaStore
if(!metaStore) return;
metaStore.change$
.pipe(
takeUntil(this.destroy$),
startWith(null)
)
.subscribe( () => {
this.activeStores = this.metaStore.stores?.filter( store => store.items?.length > 0 )
this.changeDetectorRef.markForCheck()
})
}
public filterItems(items: Item[]): Item[]{
if(!this.query) return items
return items.filter( (item:Item<unknown> ) => item.matches(this.query) )
}
public toggleSelect(item: Item): void {
if(! Array.isArray(this.selected) ) return;
const pos = this.selected.indexOf(item)
pos == -1
? this.selected.push(item)
: this.selected.splice(pos,1)
}
ngOnInit() : void {
if(this.filterControl){
this.filterControl.valueChanges
.pipe( takeUntil(this.destroy$) )
.subscribe( value => this.query = value )
}
}
ngOnDestroy() : void {
this.destroy$.next()
this.destroy$.complete()
}
}
<ion-list *ngFor ="let store of activeStores">
<ion-list-header
[id] = "store.name | toID: 'rcc-e2e'"
>
<h2>{{ store.name | translate }}</h2>
<p> ({{(store.items).length}})</p>
</ion-list-header>
<ion-item-group>
<rcc-item-tag
*ngFor = "let item of filterItems(store.items)"
[item] = "item"
[selected] = "selected && selected.includes(item)"
(click) = "toggleSelect(item)"
></rcc-item-tag>
</ion-item-group>
<ion-item
*ngIf = "filterItems(store.items).length == 0"
>
<ion-label
class = "ion-text-center"
[id] = "'META_STORE.NO_ITEMS' | toID: 'rcc-e2e'"
>
{{ 'META_STORE.NO_ITEMS' | translate }}
</ion-label>
</ion-item>
</ion-list>
<ion-text
*ngIf = "activeStores.length == 0"
class = "ion-text-center"
[id] = "'META_STORE.NO_ACTIVE_STORES' | toID: 'rcc-e2e'"
>
<h3>{{ 'META_STORE.NO_ACTIVE_STORES' | translate }}</h3>
</ion-text>
./meta-store.component.scss