a3ad7a27143be3c30130ea2af4302da806348b77
[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 /// <reference path="../../../references"/>
21 module Sdc.ViewModels {
22     'use strict';
23
24     interface ICategoryManagementViewModelScope extends ng.IScope {
25         SERVICE:string;
26         RESOURCE:string;
27         categoriesToShow: Array<Sdc.Services.ICategoryResource>;
28         serviceCategories: Array<Sdc.Services.ICategoryResource>;
29         resourceCategories: Array<Sdc.Services.ICategoryResource>;
30         selectedCategory: Sdc.Services.ICategoryResource;
31         selectedSubCategory: Sdc.Services.ICategoryResource;
32         modalInstance:ng.ui.bootstrap.IModalServiceInstance;
33         isLoading:boolean;
34         type:string;
35         namePattern:RegExp;
36
37         selectCategory(category:Sdc.Services.ICategoryResource) :void;
38         selectSubCategory(subcategory:Sdc.Services.ICategoryResource) :void;
39         selectType(type:string) :void;
40         deleteCategory(category:Sdc.Services.ICategoryResource, subCategory:Sdc.Services.ICategoryResource) :void;
41         createCategoryModal(parentCategory:Sdc.Services.ICategoryResource) :void;
42     }
43
44     export class CategoryManagementViewModel {
45         static '$inject' = [
46             '$scope',
47             'sdcConfig',
48             'Sdc.Services.CacheService',
49             '$templateCache',
50             '$modal',
51             '$filter',
52             'ValidationUtils',
53             'ModalsHandler'
54         ];
55
56         constructor(private $scope:ICategoryManagementViewModelScope,
57                     private sdcConfig:Models.IAppConfigurtaion,
58                     private cacheService:Services.CacheService,
59                     private $templateCache:ng.ITemplateCacheService,
60                     private $modal:ng.ui.bootstrap.IModalService,
61                     private $filter:ng.IFilterService,
62                     private ValidationUtils: Sdc.Utils.ValidationUtils,
63                     private ModalsHandler: Utils.ModalsHandler
64         ) {
65
66             this.initScope();
67             this.$scope.selectType(Sdc.Utils.Constants.ComponentType.SERVICE.toLocaleLowerCase());
68
69         }
70
71         private initScope = ():void => {
72             let scope:ICategoryManagementViewModelScope = this.$scope;
73             scope.SERVICE = Sdc.Utils.Constants.ComponentType.SERVICE.toLocaleLowerCase();
74             scope.RESOURCE = Sdc.Utils.Constants.ComponentType.RESOURCE.toLocaleLowerCase();
75
76             scope.namePattern = this.ValidationUtils.getValidationPattern('cssClasses');
77
78             scope.selectCategory = (category :Sdc.Services.ICategoryResource) => {
79                 if(scope.selectedCategory !== category) {
80                     scope.selectedSubCategory = null;
81                 }
82                 scope.selectedCategory = category;
83             };
84             scope.selectSubCategory = (subcategory :Sdc.Services.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:Sdc.Services.ICategoryResource):void => {
98                 //can't create a sub category for service
99                 if(parentCategory && scope.type === Sdc.Utils.Constants.ComponentType.SERVICE.toLowerCase()) {
100                     return;
101                 }
102
103                 let type:string = scope.type;
104
105                 let onOk = (newCategory :Sdc.Services.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                     template: this.$templateCache.get('/app/scripts/view-models/admin-dashboard/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.$modal.open(modalOptions);
137                 scope.modalInstance.result.then(onOk, onCancel);
138
139             };
140
141             scope.deleteCategory = (category: Sdc.Services.ICategoryResource, subCategory: Sdc.Services.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<Sdc.Services.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' : 'cssClasses';
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     }
197 }