import {NgModule,Component,ElementRef,OnInit,AfterContentInit,DoCheck,OnDestroy,Input,Output,SimpleChange,EventEmitter,ContentChild,ContentChildren,QueryList,TemplateRef} from '@angular/core'; import {CommonModule} from '@angular/common'; import {ObjectUtils} from '../utils/objectutils'; import {Header,Footer,PrimeTemplate,SharedModule} from '../common/shared'; import {PaginatorModule} from '../paginator/paginator'; import {BlockableUI} from '../common/blockableui'; import {SelectItem} from '../common/selectitem'; @Component({ selector: 'p-dataView', template: `
{{emptyMessage}}
`, providers: [ObjectUtils] }) export class DataView implements OnInit,AfterContentInit,BlockableUI { @Input() layout: string = 'list'; @Input() paginator: boolean; @Input() rows: number; @Input() totalRecords: number; @Input() pageLinks: number = 5; @Input() rowsPerPageOptions: number[]; @Input() paginatorPosition: string = 'bottom'; @Input() alwaysShowPaginator: boolean = true; @Input() paginatorDropdownAppendTo: any; @Input() lazy: boolean; @Input() emptyMessage: string = 'No records found'; @Output() onLazyLoad: EventEmitter = new EventEmitter(); @Input() style: any; @Input() styleClass: string; @Input() trackBy: Function = (index: number, item: any) => item; @Input() filterBy: string; @Output() onPage: EventEmitter = new EventEmitter(); @Output() onSort: EventEmitter = new EventEmitter(); @ContentChild(Header) header; @ContentChild(Footer) footer; @ContentChildren(PrimeTemplate) templates: QueryList; _value: any[]; listItemTemplate: TemplateRef; gridItemTemplate: TemplateRef; itemTemplate: TemplateRef; first: number = 0; filteredValue: any[]; _sortField: string; _sortOrder: number = 1; initialized: boolean; constructor(public el: ElementRef, public objectUtils: ObjectUtils) {} ngOnInit() { if(this.lazy) { this.onLazyLoad.emit(this.createLazyLoadMetadata()); } this.initialized = true; } @Input() get sortField(): string { return this._sortField; } set sortField(val: string) { this._sortField = val; //avoid triggering lazy load prior to lazy initialization at onInit if ( !this.lazy || this.initialized ) { this.sort(); } } @Input() get sortOrder(): number { return this._sortOrder; } set sortOrder(val: number) { this._sortOrder = val; //avoid triggering lazy load prior to lazy initialization at onInit if ( !this.lazy || this.initialized ) { this.sort(); } } ngAfterContentInit() { this.templates.forEach((item) => { switch(item.getType()) { case 'listItem': this.listItemTemplate = item.template; break; case 'gridItem': this.gridItemTemplate = item.template; break; } }); this.updateItemTemplate(); } updateItemTemplate() { switch(this.layout) { case 'list': this.itemTemplate = this.listItemTemplate; break; case 'grid': this.itemTemplate = this.gridItemTemplate; break; } } @Input() get value(): any[] { return this._value; } set value(val:any[]) { this._value = val; this.updateTotalRecords(); } changeLayout(layout: string) { this.layout = layout; this.updateItemTemplate(); } updateTotalRecords() { this.totalRecords = this.lazy ? this.totalRecords : (this._value ? this._value.length : 0); } paginate(event) { this.first = event.first; this.rows = event.rows; if (this.lazy) { this.onLazyLoad.emit(this.createLazyLoadMetadata()); } this.onPage.emit({ first: this.first, rows: this.rows }); } sort() { this.first = 0; if(this.lazy) { this.onLazyLoad.emit(this.createLazyLoadMetadata()); } else if (this.value) { this.value.sort((data1, data2) => { let value1 = this.objectUtils.resolveFieldData(data1, this.sortField); let value2 = this.objectUtils.resolveFieldData(data2, this.sortField); let result = null; if (value1 == null && value2 != null) result = -1; else if (value1 != null && value2 == null) result = 1; else if (value1 == null && value2 == null) result = 0; else if (typeof value1 === 'string' && typeof value2 === 'string') result = value1.localeCompare(value2); else result = (value1 < value2) ? -1 : (value1 > value2) ? 1 : 0; return (this.sortOrder * result); }); } this.onSort.emit({ sortField: this.sortField, sortOrder: this.sortOrder }); } isEmpty() { let data = this.filteredValue||this.value; return data == null || data.length == 0; } createLazyLoadMetadata(): any { return { first: this.first, rows: this.rows }; } getBlockableElement(): HTMLElement { return this.el.nativeElement.children[0]; } filter(value: string) { if (this.value && this.value.length) { let searchFields = this.filterBy.split(','); this.filteredValue = this.objectUtils.filter(this.value, searchFields, value); if (this.filteredValue.length === this.value.length ) { this.filteredValue = null; } if (this.paginator) { this.totalRecords = this.filteredValue ? this.filteredValue.length : this.value ? this.value.length : 0; } } } } @Component({ selector: 'p-dataViewLayoutOptions', template: ` ` }) export class DataViewLayoutOptions { @Input() style: any; @Input() styleClass: string; constructor(public dv: DataView) {} changeLayout(event: Event, layout: string) { this.dv.changeLayout(layout); event.preventDefault(); } } @NgModule({ imports: [CommonModule,SharedModule,PaginatorModule], exports: [DataView,SharedModule,DataViewLayoutOptions], declarations: [DataView,DataViewLayoutOptions] }) export class DataViewModule { }