Merge "[sdc] - merged 1707_build into sdc LF"
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / icons / icons-view-model.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 obarda on 4/4/2016.
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
30 export interface IIconsScope extends IWorkspaceViewModelScope {
31     icons:Array<string>;
32     iconSprite:string;
33     setComponentIcon(iconSrc:string):void;
34 }
35
36 export class IconsViewModel {
37
38     static '$inject' = [
39         '$scope',
40         'Sdc.Services.AvailableIconsService',
41         'ComponentFactory',
42         '$state'
43     ];
44
45     constructor(private $scope:IIconsScope,
46                 private availableIconsService:AvailableIconsService,
47                 private ComponentFactory:ComponentFactory,
48                 private $state:ng.ui.IStateService) {
49
50
51         this.initScope();
52         this.initIcons();
53         this.$scope.updateSelectedMenuItem();
54         this.$scope.iconSprite = this.$scope.component.iconSprite;
55
56         if (this.$scope.component.isResource()) {
57             this.initVendor();
58         }
59     }
60
61     private initialIcon:string = this.$scope.component.icon;
62     private initIcons = ():void => {
63
64         // For subcategories that where created by admin, there is no icons
65         this.$scope.icons = new Array<string>();
66         if (this.$scope.component.categories && this.$scope.component.categories.length > 0) {
67
68             _.forEach(this.$scope.component.categories, (category:IMainCategory):void => {
69                 if (category.icons) {
70                     this.$scope.icons = this.$scope.icons.concat(category.icons);
71                 }
72                 if (category.subcategories) {
73                     _.forEach(category.subcategories, (subcategory:ISubCategory):void => {
74                         if (subcategory.icons) {
75                             this.$scope.icons = this.$scope.icons.concat(subcategory.icons);
76                         }
77                     });
78                 }
79             });
80         }
81
82         if (this.$scope.component.isResource()) {
83             let resourceType:string = this.$scope.component.getComponentSubType();
84             if (resourceType === 'VL') {
85                 this.$scope.icons = ['vl'];
86             }
87             if (resourceType === 'CP') {
88                 this.$scope.icons = ['cp'];
89             }
90         }
91
92         if (this.$scope.icons.length === 0) {
93             this.$scope.icons = this.availableIconsService.getIcons(this.$scope.component.componentType);
94         }
95         //we always add the defual icon to the list
96         this.$scope.icons.push('defaulticon');
97     };
98
99     private initVendor = ():void => {
100         let vendors:Array<string> = this.availableIconsService.getIcons(this.$scope.component.componentType).slice(5, 19);
101         let vendorName = this.$scope.component.vendorName.toLowerCase();
102         if ('at&t' === vendorName) {
103             vendorName = 'att';
104         }
105         if ('nokia' === vendorName) {
106             vendorName = 'nokiasiemens';
107         }
108         let vendor:string = _.find(vendors, (vendor:string)=> {
109             return vendor.replace(/[_]/g, '').toLowerCase() === vendorName;
110         });
111
112         if (vendor && this.$scope.icons.indexOf(vendor) === -1) {
113             this.$scope.icons.push(vendor);
114         }
115     };
116
117     private initScope():void {
118         this.$scope.icons = [];
119         this.$scope.setValidState(true);
120         //if(this.$scope.component.icon === DEFAULT_ICON){
121         //    //this.$scope.setValidState(false);
122         //}
123
124         this.$scope.setComponentIcon = (iconSrc:string):void => {
125             this.$state.current.data.unsavedChanges = !this.$scope.isViewMode() && (iconSrc != this.initialIcon);
126             this.$scope.component.icon = iconSrc;
127             // this.$scope.setValidState(true);
128         };
129
130     }
131 }