pm-controls
Version:
ProModel Controls
149 lines (128 loc) • 4.33 kB
text/typescript
import {
ChangeDetectorRef,
ChangeDetectionStrategy,
Component,
Input,
OnInit,
Output,
EventEmitter,
ViewContainerRef,
ViewChild,
TemplateRef,
SimpleChanges
} from '@angular/core';
import { VirtualPanelComponent } from '../../panels/virtual-panel/virtual-panel-component';
export class CheckBoxListBoxComponent implements OnInit {
constructor(
public changeDetectorRef: ChangeDetectorRef) {
this.changeDetectorRef.detach();
}
ListBoxClass: string = "list-box-container-border";
ListBoxItemClass: string = "list-box-item";
LastListBoxItemClass: string = "last-list-box-item";
ListBoxSelectedItemClass: string = "list-box-list-item-selected";
ListBoxItemBackgroundClass: string;
ListBoxBorderClass: string = "list-box-border";
ItemHeight: number = 30;
ControlHeight: any;
ControlWidth: any;
SelectedItem: any;
DisplayMemberPath: any; // should be property name or function.
ItemTemplate: TemplateRef<any>;
SelectionMode: string = "SelectionMode.Single";
SelectedItemsChange: EventEmitter<any> = new EventEmitter<any>();
SelectedItemChange: EventEmitter<any> = new EventEmitter<any>();
virtualPanel: VirtualPanelComponent;
moveItemIntoViewFunc: Function;
filteredItemSource: any;
private highlightedItem: any;
private itemsSource: Array<any> = [];
get ItemsSource(): Array<any> {
return this.itemsSource;
}
set ItemsSource(value: Array<any>) {
this.itemsSource = value;
}
private selectedItems: Array<any> = [];
get SelectedItems(): Array<any> {
return this.selectedItems;
}
set SelectedItems(value: Array<any>) {
this.selectedItems = value;
}
ngOnInit() {
this.filterItemSource();
if (this.SelectedItem)
this.SelectItem(this.SelectedItem);
this.changeDetectorRef.detectChanges();
}
ngOnChanges(changes: SimpleChanges) {
if (changes['ItemsSource']) {
this.filterItemSource();
}
}
filterItemSource() {
this.filteredItemSource = this.ItemsSource;
this.changeDetectorRef.detectChanges();
}
UpdateSelectedItem() {
this.SelectedItem = this.SelectedItems.length>0? this.SelectedItems[0]:null;
this.SelectedItemChange.emit(this.SelectedItem);
}
isLastItem(item: any) : boolean {
return item === this.filteredItemSource[this.filteredItemSource.length - 1];
}
getItemDisplay(item) :string {
if (!item)
return;
if (!this.DisplayMemberPath)
return item;
if(typeof this.DisplayMemberPath === "function")
return this.DisplayMemberPath(item);
else
return <string>item[this.DisplayMemberPath];
}
SelectItem(item: any) {
if (this.SelectionMode == "SelectionMode.Single")
this.SelectedItems.length = 0;
// only include unique items.
if(this.SelectedItems.indexOf(item)==-1)
this.SelectedItems.push(item);
else {
// remove the item if it's already selected
this.removeSelectedItem(item);
this.UpdateSelectedItem();
return;
}
this.UpdateSelectedItem();
this.SelectedItemsChange.emit(this.SelectedItems);
this.virtualPanel.RaiseChange();
}
removeSelectedItem(item:any) {
var idx = this.SelectedItems.indexOf(item);
if(idx<0) {
console.error("Could not find selected item to remove.");
console.trace();
return;
}
this.SelectedItems.splice(idx,1);
this.UpdateSelectedItem();
this.SelectedItemsChange.emit(this.SelectedItems);
}
IsCheckedChange() {
this.changeDetectorRef.detectChanges();
}
IsChecked(item: any) {
if (this.SelectedItems && this.SelectedItems.includes(item))
return true;
return false;
}
}