novo-elements
Version:
Bullhorn's NOVO Element Repository for Angular 2
122 lines (115 loc) • 4.26 kB
text/typescript
// NG2
import { Component, ElementRef } from '@angular/core';
// APP
import { BasePickerResults } from '../base-picker-results/BasePickerResults';
import { Helpers } from '../../../../utils/Helpers';
import { NovoLabelService } from '../../../../services/novo-label-service';
// Vendor
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
/**
* @name: ChecklistPickerResults
*
* @description This is the actual list of matches that gets injected into the DOM.
*/
export class ChecklistPickerResults extends BasePickerResults {
filteredMatches: any;
constructor(element: ElementRef, public labels: NovoLabelService) {
super(element);
}
search() {
let options = this.config.options;
//only set this the first time
return Observable.fromPromise(new Promise((resolve, reject) => {
// Check if there is match data
if (options) {
// Resolve the data
if (Array.isArray(options)) {
this.isStatic = true;
// Arrays are returned immediately
resolve(options);
} else {
// All other kinds of data are rejected
reject('The data provided is not an array or a promise');
throw new Error('The data provided is not an array or a promise');
}
} else {
// No data gets rejected
reject('error');
}
}));
}
/**
* @name filterData=
* @param matches - Collection of objects=
*
* @description This function loops through the picker options and creates a filtered list of objects that contain
* the newSearch.
*/
filterData(matches): any {
if (this.term && matches) {
this.filteredMatches = matches.map(section => {
let items = section.originalData.filter((match) => {
return ~String(match.label).toLowerCase().indexOf(this.term.toLowerCase());
});
section.data = items;
return section;
}, this);
return this.filteredMatches;
} else if (this.term === '') {
matches.forEach(section => {
section.data = section.originalData;
});
return matches;
}
// Show no recent results template
return matches;
}
/**
* @name selectMatch
* @param event
* @param item
*
* @description
*/
selectMatch(event, item) {
Helpers.swallowEvent(event);
if (item.indeterminate) {
item.indeterminate = false;
item.checked = true;
} else {
item.checked = !item.checked;
}
let selected = this.activeMatch;
if (selected) {
this.parent.value = selected;
}
return false;
}
}