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