import { NgModule, Component, Pipe, OnInit, DoCheck, HostListener, Input, ElementRef, Output, EventEmitter, forwardRef, IterableDiffers } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Observable } from 'rxjs/Rx'; import { FormsModule, NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms'; import { Input as Field, Option } from '../wizard/model'; const MULTISELECT_VALUE_ACCESSOR: any = { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => MultiselectList), multi: true }; @Pipe({ name: 'searchFilter' }) export class MultiSelectSearchFilter { transform(options: Array, args: any): Array { return options.filter((option: Option) => option[args.field].toLowerCase().indexOf((args.text || '').toLowerCase()) > -1); } } @Component({ selector: 'ob-multiselect-list', providers: [MULTISELECT_VALUE_ACCESSOR], styles: [` a { outline: none !important; } `], template: ` {{searchField[0].toUpperCase() + searchField.substring(1)}} Name Description 0"> {{value}} {{option.id}} {{option.description}} {{option.stability[0] + option.stability.substring(1).toLowerCase()}} {{normalize(option.tags)}} ` }) export class MultiselectList implements DoCheck, ControlValueAccessor { @Input() input: Field; onModelChange: Function = (_: any) => {}; onModelTouched: Function = () => {}; differ: any; model: string[]; searchFilterText: string = ''; searchField: string = 'name'; isVisible: boolean; constructor( private element: ElementRef, private differs: IterableDiffers ) { this.differ = differs.find([]).create(null); } normalize(name: string): string { return name.replace(/,/g, ", "); } writeValue(value: any) : void { if (value !== undefined) { this.model = value; } } registerOnChange(fn: Function): void { this.onModelChange = fn; } registerOnTouched(fn: Function): void { this.onModelTouched = fn; } ngDoCheck() { let changes = this.differ.diff(this.model); if (changes) { // this.updateNumSelected(); // this.updateTitle(); } } clearSearch() { this.searchFilterText = ''; } toggleDropdown() { this.isVisible = !this.isVisible; } isSelected(option: Option): boolean { return this.model && this.model.indexOf(option.id) > -1; } setSelected(option: Option) { if (!this.model) this.model = []; var index = this.model.indexOf(option.id); if (index > -1) { this.model.splice(index, 1); } else { this.model.push(option.id); } this.onModelChange(this.model); } } @NgModule({ imports: [CommonModule, FormsModule], exports: [MultiselectList], declarations: [MultiselectList, MultiSelectSearchFilter], }) export class MultiselectListModule { }