Allow platform multi-selection for VNF in modern UI
[vid.git] / vid-webpack-master / src / app / shared / components / formControls / component / multiselect / multiselect.formControl.service.ts
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";
5
6
7 @Injectable()
8 export class MultiselectFormControlService {
9
10   convertOriginalItems = (data : MultiselectFormControl) : Promise<MultiSelectItem[]> => {
11     return new Promise<MultiSelectItem[]>((resolve) =>{
12       let result: MultiSelectItem[] = [];
13       if(data.options$) {
14         let index: number = 1;
15         data.options$.map((originalItems: any) => {
16           result.push(new MultiSelectItem(index, originalItems[data.ngValue], originalItems[data.selectedFieldName]));
17           index++;
18         });
19       }
20       resolve(result);
21     })
22   };
23
24
25   convertOptionsToHashMap = (config : MultiselectFormControl) => {
26     let index = 1;
27     return _.reduce(config.options$ , (obj, param: any ) => {
28       param.index = index;
29       index++;
30       obj[param[config.ngValue]] = param;
31       return obj;
32     }, {});
33   };
34
35   convertSelectedItems(data : MultiselectFormControl) : Promise<MultiSelectItem[]>{
36     return new Promise<MultiSelectItem[]>((resolve) =>{
37       let result: MultiSelectItem[] = [];
38       const hashMap = this.convertOptionsToHashMap(data);
39
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]));
45         });
46       }
47       resolve(result);
48     });
49   }
50 }