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=========================================================
20 /// <reference path="../../../references"/>
21 module Sdc.ViewModels {
24 interface ICategoryManagementViewModelScope extends ng.IScope {
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;
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;
44 export class CategoryManagementViewModel {
48 'Sdc.Services.CacheService',
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
67 this.$scope.selectType(Sdc.Utils.Constants.ComponentType.SERVICE.toLocaleLowerCase());
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();
76 scope.namePattern = this.ValidationUtils.getValidationPattern('cssClasses');
78 scope.selectCategory = (category :Sdc.Services.ICategoryResource) => {
79 if(scope.selectedCategory !== category) {
80 scope.selectedSubCategory = null;
82 scope.selectedCategory = category;
84 scope.selectSubCategory = (subcategory :Sdc.Services.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:Sdc.Services.ICategoryResource):void => {
98 //can't create a sub category for service
99 if(parentCategory && scope.type === Sdc.Utils.Constants.ComponentType.SERVICE.toLowerCase()) {
103 let type:string = scope.type;
105 let onOk = (newCategory :Sdc.Services.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 template: this.$templateCache.get('/app/scripts/view-models/admin-dashboard/add-category-modal/add-category-modal-view.html'),
122 controller: 'Sdc.ViewModels.AddCategoryModalViewModel',
127 parentCategory: function () {
128 return parentCategory;
136 scope.modalInstance = this.$modal.open(modalOptions);
137 scope.modalInstance.result.then(onOk, onCancel);
141 scope.deleteCategory = (category: Sdc.Services.ICategoryResource, subCategory: Sdc.Services.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<Sdc.Services.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' : '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 +"' }");
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');