[SDC] rebase 1710 code
[sdc.git] / catalog-ui / src / app / view-models / modals / icons-modal / icons-modal-view.ts
  */
 
 /**
- * Created by obarda on 4/4/2016.
+ * Created by rc2122 on 7/4/2017.
  */
 'use strict';
 import {ComponentFactory} from "app/utils";
 import {AvailableIconsService} from "app/services";
 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
 import {IMainCategory, ISubCategory} from "app/models";
-
-export interface IIconsScope extends IWorkspaceViewModelScope {
-    icons:Array<string>;
-    iconSprite:string;
-    setComponentIcon(iconSrc:string):void;
+import {Component} from "app/models";
+import {ResourceType} from "app/utils/constants";
+
+interface IIconsModalViewModelScope {
+    modalIcons:ng.ui.bootstrap.IModalServiceInstance;
+    icons:Array<string>,
+    iconSprite:string,
+    selectedIcon:string,
+    changeIcon(icon:string):void,
+    cancel():void
+    updateIcon():void;
 }
 
-export class IconsViewModel {
-
+export class IconsModalViewModel {
     static '$inject' = [
         '$scope',
         'Sdc.Services.AvailableIconsService',
         'ComponentFactory',
-        '$state'
+        '$state',
+        '$uibModalInstance',
+        'component'
     ];
 
-    constructor(private $scope:IIconsScope,
+    constructor(private $scope:IIconsModalViewModelScope,
                 private availableIconsService:AvailableIconsService,
                 private ComponentFactory:ComponentFactory,
-                private $state:ng.ui.IStateService) {
-
-
+                private $state:ng.ui.IStateService,
+                private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
+                private component: Component) {
         this.initScope();
-        this.initIcons();
-        this.$scope.updateSelectedMenuItem();
-        this.$scope.iconSprite = this.$scope.component.iconSprite;
+        this._initIcons();
+        this.$scope.iconSprite = this.component.iconSprite;
+        this.$scope.selectedIcon = this.component.icon;
 
-        if (this.$scope.component.isResource()) {
+        if (this.component.isResource()) {
             this.initVendor();
         }
+
     }
 
-    private initialIcon:string = this.$scope.component.icon;
-    private initIcons = ():void => {
+    private _initIcons = ():void => {
 
         // For subcategories that where created by admin, there is no icons
         this.$scope.icons = new Array<string>();
-        if (this.$scope.component.categories && this.$scope.component.categories.length > 0) {
+        if (this.component.categories && this.component.categories.length > 0) {
 
-            _.forEach(this.$scope.component.categories, (category:IMainCategory):void => {
+            _.forEach(this.component.categories, (category:IMainCategory):void => {
                 if (category.icons) {
                     this.$scope.icons = this.$scope.icons.concat(category.icons);
                 }
@@ -79,26 +86,26 @@ export class IconsViewModel {
             });
         }
 
-        if (this.$scope.component.isResource()) {
-            let resourceType:string = this.$scope.component.getComponentSubType();
-            if (resourceType === 'VL') {
+        if (this.component.isResource()) {
+            let resourceType:string = this.component.getComponentSubType();
+            if (resourceType === ResourceType.VL) {
                 this.$scope.icons = ['vl'];
             }
-            if (resourceType === 'CP') {
+            if (resourceType === ResourceType.CP) {
                 this.$scope.icons = ['cp'];
             }
         }
 
         if (this.$scope.icons.length === 0) {
-            this.$scope.icons = this.availableIconsService.getIcons(this.$scope.component.componentType);
+            this.$scope.icons = this.availableIconsService.getIcons(this.component.componentType);
         }
         //we always add the defual icon to the list
         this.$scope.icons.push('defaulticon');
     };
 
     private initVendor = ():void => {
-        let vendors:Array<string> = this.availableIconsService.getIcons(this.$scope.component.componentType).slice(5, 19);
-        let vendorName = this.$scope.component.vendorName.toLowerCase();
+        let vendors:Array<string> = this.availableIconsService.getIcons(this.component.componentType).slice(5, 19);
+        let vendorName = this.component.vendorName.toLowerCase();
         if ('at&t' === vendorName) {
             vendorName = 'att';
         }
@@ -115,17 +122,22 @@ export class IconsViewModel {
     };
 
     private initScope():void {
+        this.$scope.modalIcons = this.$uibModalInstance;
         this.$scope.icons = [];
-        this.$scope.setValidState(true);
-        //if(this.$scope.component.icon === DEFAULT_ICON){
-        //    //this.$scope.setValidState(false);
-        //}
-
-        this.$scope.setComponentIcon = (iconSrc:string):void => {
-            this.$state.current.data.unsavedChanges = !this.$scope.isViewMode() && (iconSrc != this.initialIcon);
-            this.$scope.component.icon = iconSrc;
-            // this.$scope.setValidState(true);
+        this.$scope.changeIcon = (icon:string):void => {
+            this.$scope.selectedIcon = icon;
         };
-
+        this.$scope.cancel = ():void => {
+            this.$uibModalInstance.dismiss();
+        };
+        this.$scope.updateIcon = ():void => {
+            let isDirty:boolean = this.component.icon != this.$scope.selectedIcon;
+            this.component.icon = this.$scope.selectedIcon;
+            this.$uibModalInstance.close(isDirty);
+        }
     }
+
 }
+
+
+