c99f9e874dc33420f004b3f0355c6dbfbe71a690
[sdc.git] /
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 'use strict';
22 import {ModalsHandler, ValidationUtils} from "app/utils";
23 import {CacheService, ICategoryResource} from "app/services";
24 import {IAppConfigurtaion} from "app/models";
25 import {ComponentType} from "../../../utils/constants";
26
27 interface ICategoryManagementViewModelScope extends ng.IScope {
28     SERVICE:string;
29     RESOURCE:string;
30     categoriesToShow:Array<ICategoryResource>;
31     serviceCategories:Array<ICategoryResource>;
32     resourceCategories:Array<ICategoryResource>;
33     selectedCategory:ICategoryResource;
34     selectedSubCategory:ICategoryResource;
35     modalInstance:ng.ui.bootstrap.IModalServiceInstance;
36     isLoading:boolean;
37     type:string;
38     namePattern:RegExp;
39
40     selectCategory(category:ICategoryResource):void;
41     selectSubCategory(subcategory:ICategoryResource):void;
42     selectType(type:string):void;
43     deleteCategory(category:ICategoryResource, subCategory:ICategoryResource):void;
44     createCategoryModal(parentCategory:ICategoryResource):void;
45 }
46
47 export class CategoryManagementViewModel {
48     static '$inject' = [
49         '$scope',
50         'sdcConfig',
51         'Sdc.Services.CacheService',
52         '$uibModal',
53         '$filter',
54         'ValidationUtils',
55         'ModalsHandler'
56     ];
57
58     constructor(private $scope:ICategoryManagementViewModelScope,
59                 private sdcConfig:IAppConfigurtaion,
60                 private cacheService:CacheService,
61                 private $uibModal:ng.ui.bootstrap.IModalService,
62                 private $filter:ng.IFilterService,
63                 private ValidationUtils:ValidationUtils,
64                 private ModalsHandler:ModalsHandler) {
65
66         this.initScope();
67         this.$scope.selectType(ComponentType.SERVICE.toLocaleLowerCase());
68
69     }
70
71     private initScope = ():void => {
72         let scope:ICategoryManagementViewModelScope = this.$scope;
73         scope.SERVICE = ComponentType.SERVICE.toLocaleLowerCase();
74         scope.RESOURCE = ComponentType.RESOURCE.toLocaleLowerCase();
75
76         scope.namePattern = this.ValidationUtils.getValidationPattern('category');
77
78         scope.selectCategory = (category:ICategoryResource) => {
79             if (scope.selectedCategory !== category) {
80                 scope.selectedSubCategory = null;
81             }
82             scope.selectedCategory = category;
83         };
84         scope.selectSubCategory = (subcategory:ICategoryResource) => {
85             scope.selectedSubCategory = subcategory;
86         };
87         scope.selectType = (type:string):void => {
88             if (scope.type !== type) {
89                 scope.selectedCategory = null;
90                 scope.selectedSubCategory = null;
91             }
92
93             scope.type = type;
94             scope.categoriesToShow = scope[type + 'Categories'];
95         };
96
97         scope.createCategoryModal = (parentCategory:ICategoryResource):void => {
98             //can't create a sub category for service
99             if (parentCategory && scope.type === ComponentType.SERVICE.toLowerCase()) {
100                 return;
101             }
102
103             let type:string = scope.type;
104
105             let onOk = (newCategory:ICategoryResource):void => {
106                 if (!parentCategory) {
107                     scope[type + 'Categories'].push(newCategory);
108                 } else {
109                     if (!parentCategory.subcategories) {
110                         parentCategory.subcategories = [];
111                     }
112                     parentCategory.subcategories.push(newCategory);
113                 }
114             };
115
116             let onCancel = ():void => {
117
118             };
119
120             let modalOptions:ng.ui.bootstrap.IModalSettings = {
121                 templateUrl: '../add-category-modal/add-category-modal-view.html',
122                 controller: 'Sdc.ViewModels.AddCategoryModalViewModel',
123                 size: 'sdc-xsm',
124                 backdrop: 'static',
125                 scope: scope,
126                 resolve: {
127                     parentCategory: function () {
128                         return parentCategory;
129                     },
130                     type: function () {
131                         return type;
132                     }
133                 }
134             };
135
136             scope.modalInstance = this.$uibModal.open(modalOptions);
137             scope.modalInstance.result.then(onOk, onCancel);
138
139         };
140
141         scope.deleteCategory = (category:ICategoryResource, subCategory:ICategoryResource):void => {
142
143             let onOk = ():void => {
144
145                 scope.isLoading = true;
146                 let type:string = scope.type;
147
148                 let onError = (response):void => {
149                     scope.isLoading = false;
150                     console.info('onFaild', response);
151                 };
152
153                 let onSuccess = (response:any):void => {
154                     let arr:Array<ICategoryResource>;
155
156                     if (!subCategory) {
157                         arr = this.$scope[type + 'Categories'];
158                         arr.splice(arr.indexOf(category), 1);
159                         if (category === scope.selectedCategory) {
160                             scope.selectedCategory = null;
161                             scope.selectedSubCategory = null;
162                         }
163                     } else {
164                         arr = category.subcategories;
165                         arr.splice(arr.indexOf(subCategory), 1);
166                     }
167
168                     scope.isLoading = false;
169                 };
170
171                 if (!subCategory) {
172                     category.$delete({
173                             types: type + "s",
174                             categoryId: category.uniqueId
175                         }
176                         , onSuccess, onError);
177                 } else {
178                     category.$deleteSubCategory({
179                             types: type + "s",
180                             categoryId: category.uniqueId,
181                             subCategoryId: subCategory.uniqueId,
182                         }
183                         , onSuccess, onError);
184                 }
185             };
186             let modelType:string = subCategory ? 'sub category' : 'category';
187             let title:string = this.$filter('translate')("DELETE_CATEGORY_MODAL_HEADER", "{'modelType': '" + modelType + "' }");
188             let message:string = this.$filter('translate')("DELETE_CATEGORY_MODAL_CATEGORY_NAME", "{'modelType': '" + modelType + "' }");
189
190             this.ModalsHandler.openConfirmationModal(title, message, false, 'sdc-xsm').then(onOk);
191         };
192
193         this.$scope.serviceCategories = this.cacheService.get('serviceCategories');
194         this.$scope.resourceCategories = this.cacheService.get('resourceCategories');
195     }
196 }