import { Component, ElementRef, OnInit, Input, Output, EventEmitter, ContentChildren, QueryList } from '@angular/core';
import { CfCoreComponent } from '../core/core.component';
import { ListModel } from '../../models/list/list.model';
import { ListStylingModel } from '../../models/list/list-styling.model';
import { CfItemComponent } from '../item/item.component';
@Component({
selector: 'cf-list',
template: "
{{cfList.title}}
",
styles: [":host md-nav-list { width: 100%; } :host md-nav-list md-list-item { position: relative; z-index: 0; } :host md-nav-list .cf-list-item-action { width: 100%; display: flex; flex-direction: row; align-items: top; font-family: 'Avenir', \"Helvetica Neue\", sans-serif; box-sizing: border-box; font-size: 16px; padding: 8px 0; } :host md-nav-list .cf-list-item-trigger { transition: all .5s; position: absolute; z-index: 100; bottom: 0px; right: 0px; } :host md-nav-list .cf-list-item-trigger.cf-expanded { transform: rotate(-90deg); } :host md-nav-list .cf-list-item-details-container { width: 100%; font-family: 'Avenir', \"Helvetica Neue\", sans-serif; } :host md-nav-list cf-list-item-details-template { display: none; transition: all .5s; } :host md-nav-list cf-list-item-details-template.cf-expanded { display: block; } :host /deep/ .mat-list-item-content { height: auto !important; flex-direction: column !important; } [hidden] { display: none !important; } "]
})
export class CfListComponent extends CfCoreComponent implements OnInit {
cfList: ListModel;
@Input()
public properties: ListModel;
@Input()
public styling: ListStylingModel;
@Input() title:string;
@Input() disable: boolean;
@Input() display: boolean;
@Input() selectable: boolean = false;
@Input() withCheckbox: boolean = true;
selectedItems: number[] = [];
@Output() onSelect = new EventEmitter();
randomId: number = 0;
constructor(public elementRef: ElementRef) { super(elementRef); }
/**
* It will be generated cfList object and rendered inside component template. Also cfListToggleClasses array will be initialized to build each item details toggle icon.
*/
ngOnInit(): void {
if(this.properties==null)
this.properties = new ListModel();
if(this.styling==null)
this.styling = new ListStylingModel();
if(this.disable!=null) this.properties.disable = this.disable;
if(this.display!=null) this.properties.display = this.display;
if(this.title!=null) this.properties.title = this.title;
this.cfList = this.properties;
}
ngOnChanges(changes: any) {
if(this.properties!=null && this.styling!=null)
{
if(changes.disable!=null) this.properties.disable = changes.disable.currentValue;
if(changes.display!=null) this.properties.display = changes.display.currentValue;
if(changes.title!=null) this.properties.title = changes.title.currentValue;
}
}
@ContentChildren(CfItemComponent) items: QueryList;
ngAfterContentInit() {
if(this.items!=null && this.items.length>0)
{
if(this.selectable!=false)
{
for(let item of this.items.toArray())
{
item.internalId = this.randomId;
this.randomId++;
if(item.selected==true)
{
this.selectedItems.push(item.internalId);
}
console.log(this.withCheckbox);
setTimeout(_ =>
item.selectable = true,
item.itemClicked.subscribe(data=> this.updateValues(data))
);
if(this.withCheckbox!=true)
{
item.withCheckbox = false;
}
}
}
}
}
updateValues(data)
{
if(data["value"]==true)
{
this.selectedItems.push(data["id"]);
this.selectedItems.sort();
this.onSelect.emit(this.selectedItems);
}
else
{
this.selectedItems.splice(this.selectedItems.indexOf(data["id"]),1);
this.onSelect.emit(this.selectedItems);
}
}
}