2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
27 interface ICategoryManagementViewModelScope extends ng.IScope {
30 categoriesToShow:Array<ICategoryResource>;
31 serviceCategories:Array<ICategoryResource>;
32 resourceCategories:Array<ICategoryResource>;
33 selectedCategory:ICategoryResource;
34 selectedSubCategory:ICategoryResource;
35 modalInstance:ng.ui.bootstrap.IModalServiceInstance;
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;
47 export class CategoryManagementViewModel {
51 'Sdc.Services.CacheService',
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) {
67 this.$scope.selectType(ComponentType.SERVICE.toLocaleLowerCase());
71 private initScope = ():void => {
72 let scope:ICategoryManagementViewModelScope = this.$scope;
73 scope.SERVICE = ComponentType.SERVICE.toLocaleLowerCase();
74 scope.RESOURCE = ComponentType.RESOURCE.toLocaleLowerCase();
76 scope.namePattern = this.ValidationUtils.getValidationPattern('category');
78 scope.selectCategory = (category:ICategoryResource) => {
79 if (scope.selectedCategory !== category) {
80 scope.selectedSubCategory = null;
82 scope.selectedCategory = category;
84 scope.selectSubCategory = (subcategory:ICategoryResource) => {
85 scope.selectedSubCategory = subcategory;
87 scope.selectType = (type:string):void => {
88 if (scope.type !== type) {
89 scope.selectedCategory = null;
90 scope.selectedSubCategory = null;
94 scope.categoriesToShow = scope[type + 'Categories'];
97 scope.createCategoryModal = (parentCategory:ICategoryResource):void => {
98 //can't create a sub category for service
99 if (parentCategory && scope.type === ComponentType.SERVICE.toLowerCase()) {
103 let type:string = scope.type;
105 let onOk = (newCategory:ICategoryResource):void => {
106 if (!parentCategory) {
107 scope[type + 'Categories'].push(newCategory);
109 if (!parentCategory.subcategories) {
110 parentCategory.subcategories = [];
112 parentCategory.subcategories.push(newCategory);
116 let onCancel = ():void => {
120 let modalOptions:ng.ui.bootstrap.IModalSettings = {
121 templateUrl: '../add-category-modal/add-category-modal-view.html',
122 controller: 'Sdc.ViewModels.AddCategoryModalViewModel',
127 parentCategory: function () {
128 return parentCategory;
136 scope.modalInstance = this.$uibModal.open(modalOptions);
137 scope.modalInstance.result.then(onOk, onCancel);
141 scope.deleteCategory = (category:ICategoryResource, subCategory:ICategoryResource):void => {
143 let onOk = ():void => {
145 scope.isLoading = true;
146 let type:string = scope.type;
148 let onError = (response):void => {
149 scope.isLoading = false;
150 console.info('onFaild', response);
153 let onSuccess = (response:any):void => {
154 let arr:Array<ICategoryResource>;
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;
164 arr = category.subcategories;
165 arr.splice(arr.indexOf(subCategory), 1);
168 scope.isLoading = false;
174 categoryId: category.uniqueId
176 , onSuccess, onError);
178 category.$deleteSubCategory({
180 categoryId: category.uniqueId,
181 subCategoryId: subCategory.uniqueId,
183 , onSuccess, onError);
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 + "' }");
190 this.ModalsHandler.openConfirmationModal(title, message, false, 'sdc-xsm').then(onOk);
193 this.$scope.serviceCategories = this.cacheService.get('serviceCategories');
194 this.$scope.resourceCategories = this.cacheService.get('resourceCategories');