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
diff --git a/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.ts b/vid-webpack-master/src/app/shared/components/formControls/component/multiselect/multiselect.formControl.service.ts
new file mode 100644 (file)
index 0000000..4a95805
--- /dev/null
@@ -0,0 +1,50 @@
+import {Injectable} from "@angular/core";
+import {MultiselectFormControl} from "../../../../models/formControlModels/multiselectFormControl.model";
+import {MultiSelectItem} from "./multiselect.model";
+import * as _ from "lodash";
+
+
+@Injectable()
+export class MultiselectFormControlService {
+
+  convertOriginalItems = (data : MultiselectFormControl) : Promise<MultiSelectItem[]> => {
+    return new Promise<MultiSelectItem[]>((resolve) =>{
+      let result: MultiSelectItem[] = [];
+      if(data.options$) {
+        let index: number = 1;
+        data.options$.map((originalItems: any) => {
+          result.push(new MultiSelectItem(index, originalItems[data.ngValue], originalItems[data.selectedFieldName]));
+          index++;
+        });
+      }
+      resolve(result);
+    })
+  };
+
+
+  convertOptionsToHashMap = (config : MultiselectFormControl) => {
+    let index = 1;
+    return _.reduce(config.options$ , (obj, param: any ) => {
+      param.index = index;
+      index++;
+      obj[param[config.ngValue]] = param;
+      return obj;
+    }, {});
+  };
+
+  convertSelectedItems(data : MultiselectFormControl) : Promise<MultiSelectItem[]>{
+    return new Promise<MultiSelectItem[]>((resolve) =>{
+      let result: MultiSelectItem[] = [];
+      const hashMap = this.convertOptionsToHashMap(data);
+
+      if(data.options$ && data.value) {
+        const convertArray = data.convertOriginalDataToArray ? data.convertOriginalDataToArray(data.value) : data.value;
+        convertArray.map((itemId) => {
+          const uniqueIdentifier = itemId.trim();
+          result.push(new MultiSelectItem(hashMap[uniqueIdentifier].index, hashMap[uniqueIdentifier][data.ngValue], hashMap[uniqueIdentifier][data.selectedFieldName]));
+        });
+      }
+      resolve(result);
+    });
+  }
+}