Merge "[sdc] - merged 1707_build into sdc LF"
[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 {ComponentFactory} from "app/utils";
26 import {AvailableIconsService} from "app/services";
27 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
28 import {IMainCategory, ISubCategory} from "app/models";
29 import {Component} from "app/models";
30 import {ResourceType} from "app/utils/constants";
31
32 interface IIconsModalViewModelScope {
33     modalIcons:ng.ui.bootstrap.IModalServiceInstance;
34     icons:Array<string>,
35     iconSprite:string,
36     selectedIcon:string,
37     changeIcon(icon:string):void,
38     cancel():void
39     updateIcon():void;
40 }
41
42 export class IconsModalViewModel {
43     static '$inject' = [
44         '$scope',
45         'Sdc.Services.AvailableIconsService',
46         'ComponentFactory',
47         '$state',
48         '$uibModalInstance',
49         'component'
50     ];
51
52     constructor(private $scope:IIconsModalViewModelScope,
53                 private availableIconsService:AvailableIconsService,
54                 private ComponentFactory:ComponentFactory,
55                 private $state:ng.ui.IStateService,
56                 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
57                 private component: Component) {
58         this.initScope();
59         this._initIcons();
60         this.$scope.iconSprite = this.component.iconSprite;
61         this.$scope.selectedIcon = this.component.icon;
62
63         if (this.component.isResource()) {
64             this.initVendor();
65         }
66
67     }
68
69     private _initIcons = ():void => {
70
71         // For subcategories that where created by admin, there is no icons
72         this.$scope.icons = new Array<string>();
73         if (this.component.categories && this.component.categories.length > 0) {
74
75             _.forEach(this.component.categories, (category:IMainCategory):void => {
76                 if (category.icons) {
77                     this.$scope.icons = this.$scope.icons.concat(category.icons);
78                 }
79                 if (category.subcategories) {
80                     _.forEach(category.subcategories, (subcategory:ISubCategory):void => {
81                         if (subcategory.icons) {
82                             this.$scope.icons = this.$scope.icons.concat(subcategory.icons);
83                         }
84                     });
85                 }
86             });
87         }
88
89         if (this.component.isResource()) {
90             let resourceType:string = this.component.getComponentSubType();
91             if (resourceType === ResourceType.VL) {
92                 this.$scope.icons = ['vl'];
93             }
94             if (resourceType === ResourceType.CP) {
95                 this.$scope.icons = ['cp'];
96             }
97         }
98
99         if (this.$scope.icons.length === 0) {
100             this.$scope.icons = this.availableIconsService.getIcons(this.component.componentType);
101         }
102         //we always add the defual icon to the list
103         this.$scope.icons.push('defaulticon');
104     };
105
106     private initVendor = ():void => {
107         let vendors:Array<string> = this.availableIconsService.getIcons(this.component.componentType).slice(5, 19);
108         let vendorName = this.component.vendorName.toLowerCase();
109         if ('at&t' === vendorName) {
110             vendorName = 'att';
111         }
112         if ('nokia' === vendorName) {
113             vendorName = 'nokiasiemens';
114         }
115         let vendor:string = _.find(vendors, (vendor:string)=> {
116             return vendor.replace(/[_]/g, '').toLowerCase() === vendorName;
117         });
118
119         if (vendor && this.$scope.icons.indexOf(vendor) === -1) {
120             this.$scope.icons.push(vendor);
121         }
122     };
123
124     private initScope():void {
125         this.$scope.modalIcons = this.$uibModalInstance;
126         this.$scope.icons = [];
127         this.$scope.changeIcon = (icon:string):void => {
128             this.$scope.selectedIcon = icon;
129         };
130         this.$scope.cancel = ():void => {
131             this.$uibModalInstance.dismiss();
132         };
133         this.$scope.updateIcon = ():void => {
134             let isDirty:boolean = this.component.icon != this.$scope.selectedIcon;
135             this.component.icon = this.$scope.selectedIcon;
136             this.$uibModalInstance.close(isDirty);
137         }
138     }
139
140 }
141
142
143