1 import {Injectable} from "@angular/core";
2 import {MultiselectFormControl} from "../../../../models/formControlModels/multiselectFormControl.model";
3 import {MultiSelectItem} from "./multiselect.model";
4 import * as _ from "lodash";
8 export class MultiselectFormControlService {
10 convertOriginalItems = (data : MultiselectFormControl) : Promise<MultiSelectItem[]> => {
11 return new Promise<MultiSelectItem[]>((resolve) =>{
12 let result: MultiSelectItem[] = [];
14 let index: number = 1;
15 data.options$.map((originalItems: any) => {
16 result.push(new MultiSelectItem(index, originalItems[data.ngValue], originalItems[data.selectedFieldName]));
25 convertOptionsToHashMap = (config : MultiselectFormControl) => {
27 return _.reduce(config.options$ , (obj, param: any ) => {
30 obj[param[config.ngValue]] = param;
35 convertSelectedItems(data : MultiselectFormControl) : Promise<MultiSelectItem[]>{
36 return new Promise<MultiSelectItem[]>((resolve) =>{
37 let result: MultiSelectItem[] = [];
38 const hashMap = this.convertOptionsToHashMap(data);
40 if(data.options$ && data.value) {
41 const convertArray = data.convertOriginalDataToArray ? data.convertOriginalDataToArray(data.value) : data.value;
42 convertArray.map((itemId) => {
43 const uniqueIdentifier = itemId.trim();
44 result.push(new MultiSelectItem(hashMap[uniqueIdentifier].index, hashMap[uniqueIdentifier][data.ngValue], hashMap[uniqueIdentifier][data.selectedFieldName]));