2019-11-01 15:53:24 +00:00
|
|
|
import { Component, Input, Injectable, Inject, Optional,ComponentFactoryResolver, ViewContainerRef, QueryList, ComponentFactory, ViewChildren,AfterViewInit } from '@angular/core';
|
2019-10-28 13:18:25 +00:00
|
|
|
import { Location } from '@angular/common';
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
import * as mapReducers from '../../reducers/map.reducer';
|
|
|
|
import { commonReducers,ItemTypeService, IItem, Item,IListItem } from '@farmmaps/common';
|
|
|
|
import { AbstractItemWidgetComponent } from '../item-list-item/item-list-item.component';
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
@Component({
|
|
|
|
selector: 'item-widget-list',
|
|
|
|
templateUrl: './item-widget-list.component.html',
|
|
|
|
styleUrls: ['./item-widget-list.component.scss']
|
|
|
|
})
|
|
|
|
export class ItemWidgetListComponent implements AfterViewInit {
|
|
|
|
|
|
|
|
@Input() item: IListItem;
|
|
|
|
public widgets: AbstractItemWidgetComponent[];
|
|
|
|
@ViewChildren('widgetTemplate', { read: ViewContainerRef }) private widgetTargets: QueryList<ViewContainerRef>;
|
|
|
|
|
2019-11-01 15:53:24 +00:00
|
|
|
constructor(public store: Store<mapReducers.State | commonReducers.State>, public itemTypeService: ItemTypeService, public location: Location, private componentFactoryResolver: ComponentFactoryResolver, @Optional() @Inject(AbstractItemWidgetComponent) itemWidgetComponentList: AbstractItemWidgetComponent[]) {
|
2019-10-28 13:18:25 +00:00
|
|
|
this.widgets = itemWidgetComponentList; //todo filter this list on widgets available for user
|
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
let targets = this.widgetTargets.toArray();
|
2019-11-01 15:53:24 +00:00
|
|
|
if(this.widgets) {
|
|
|
|
for (var i = 0; i < this.widgets.length; i++) {
|
|
|
|
var componentFactory: ComponentFactory<AbstractItemWidgetComponent> = this.componentFactoryResolver.resolveComponentFactory(this.widgets[i]['constructor'] as any);
|
|
|
|
const viewContainerRef = targets[i];
|
|
|
|
viewContainerRef.clear();
|
|
|
|
|
|
|
|
const componentRef = viewContainerRef.createComponent(componentFactory);
|
|
|
|
(<AbstractItemWidgetComponent>componentRef.instance).item = this.item;
|
|
|
|
}
|
2019-10-28 13:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|