Sync Integ to Master
[sdc.git] / catalog-ui / src / app / view-models / modals / icons-modal / icons-modal-view.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 /**
22  * Created by rc2122 on 7/4/2017.
23  */
24 'use strict';
25 import * as _ from "lodash";
26 import {ComponentFactory} from "app/utils";
27 import {AvailableIconsService} from "app/services";
28 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
29 import {IMainCategory, ISubCategory} from "app/models";
30 import {Component} from "app/models";
31 import {ResourceType} from "app/utils/constants";
32
33 interface IIconsModalViewModelScope {
34     modalIcons:ng.ui.bootstrap.IModalServiceInstance;
35     icons:Array<string>,
36     iconSprite:string,
37     selectedIcon:string,
38     changeIcon(icon:string):void,
39     cancel():void
40     updateIcon():void;
41 }
42
43 export class IconsModalViewModel {
44     static '$inject' = [
45         '$scope',
46         'Sdc.Services.AvailableIconsService',
47         'ComponentFactory',
48         '$state',
49         '$uibModalInstance',
50         'component'
51     ];
52
53     constructor(private $scope:IIconsModalViewModelScope,
54                 private availableIconsService:AvailableIconsService,
55                 private ComponentFactory:ComponentFactory,
56                 private $state:ng.ui.IStateService,
57                 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
58                 private component: Component) {
59         this.initScope();
60         this._initIcons();
61         this.$scope.iconSprite = this.component.iconSprite;
62         this.$scope.selectedIcon = this.component.icon;
63
64         if (this.component.isResource()) {
65             this.initVendor();
66         }
67
68     }
69
70     private _initIcons = ():void => {
71
72         // For subcategories that where created by admin, there is no icons
73         this.$scope.icons = new Array<string>();
74         if (this.component.categories && this.component.categories.length > 0) {
75
76             _.forEach(this.component.categories, (category:IMainCategory):void => {
77                 if (category.icons) {
78                     this.$scope.icons = this.$scope.icons.concat(category.icons);
79                 }
80                 if (category.subcategories) {
81                     _.forEach(category.subcategories, (subcategory:ISubCategory):void => {
82                         if (subcategory.icons) {
83                             this.$scope.icons = this.$scope.icons.concat(subcategory.icons);
84                         }
85                     });
86                 }
87             });
88         }
89
90         if (this.component.isResource()) {
91             let resourceType:string = this.component.getComponentSubType();
92             if (resourceType === ResourceType.VL) {
93                 this.$scope.icons = ['vl'];
94             }
95             if (resourceType === ResourceType.CP) {
96                 this.$scope.icons = ['cp'];
97             }
98         }
99
100         if (this.$scope.icons.length === 0) {
101             this.$scope.icons = this.availableIconsService.getIcons(this.component.componentType);
102         }
103         //we always add the defual icon to the list
104         this.$scope.icons.push('defaulticon');
105     };
106
107     private initVendor = ():void => {
108         let vendors:Array<string> = this.availableIconsService.getIcons(this.component.componentType).slice(5, 19);
109         let vendorName = this.component.vendorName.toLowerCase();
110         if ('at&t' === vendorName) {
111             vendorName = 'att';
112         }
113         if ('nokia' === vendorName) {
114             vendorName = 'nokiasiemens';
115         }
116         let vendor:string = _.find(vendors, (vendor:string)=> {
117             return vendor.replace(/[_]/g, '').toLowerCase() === vendorName;
118         });
119
120         if (vendor && this.$scope.icons.indexOf(vendor) === -1) {
121             this.$scope.icons.push(vendor);
122         }
123     };
124
125     private initScope():void {
126         this.$scope.modalIcons = this.$uibModalInstance;
127         this.$scope.icons = [];
128         this.$scope.changeIcon = (icon:string):void => {
129             this.$scope.selectedIcon = icon;
130         };
131         this.$scope.cancel = ():void => {
132             this.$uibModalInstance.dismiss();
133         };
134         this.$scope.updateIcon = ():void => {
135             let isDirty:boolean = this.component.icon != this.$scope.selectedIcon;
136             this.component.icon = this.$scope.selectedIcon;
137             this.$uibModalInstance.close(isDirty);
138         }
139     }
140
141 }
142
143
144