93c1dac1743cc3d27b1ea5bcd36e011473fcd0f3
[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 IAddCategoryModalViewModelScope extends ng.IScope {
25         category:Sdc.Services.ICategoryResource;
26         modelType:string;
27         footerButtons: Array<any>;
28         forms:any;
29
30         save():void;
31         close():void;
32     }
33
34     export class AddCategoryModalViewModel {
35
36         static '$inject' = [
37             '$scope',
38             'Sdc.Services.CategoryResourceService',
39             '$modalInstance',
40             'parentCategory',
41             'type'
42         ];
43
44         constructor(
45             private $scope:IAddCategoryModalViewModelScope,
46             private categoryResourceService:Sdc.Services.ICategoryResourceClass,
47             private $modalInstance:ng.ui.bootstrap.IModalServiceInstance,
48             private parentCategory:Sdc.Services.ICategoryResource,
49             private type:string
50         ){
51             this.initScope();
52         }
53
54         private initScope = ():void => {
55             this.$scope.forms = {};
56             this.$scope.modelType = this.parentCategory ? 'sub category' : 'category';
57             this.$scope.category = new this.categoryResourceService();
58
59             this.$scope.close = ():void => {
60                 this.$modalInstance.dismiss();
61             };
62
63             this.$scope.save = ():void => {
64
65                 let onOk = (newCategory :Sdc.Services.ICategoryResource):void => {
66                     this.$modalInstance.close(newCategory);
67                 };
68
69                 let onCancel = ():void => {
70                     //error
71                 };
72
73                 if(!this.parentCategory) {
74                     this.$scope.category.$save({types: this.type+"s"}, onOk, onCancel);
75                 }else{
76                     this.$scope.category.$saveSubCategory({types: this.type+"s", categoryId: this.parentCategory.uniqueId}, onOk, onCancel);
77                 }
78
79             };
80
81             this.$scope.footerButtons = [
82                 {'name': 'OK', 'css': 'blue', 'callback': this.$scope.save, 'disabled': true},
83                 {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
84             ];
85
86             this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
87                 this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
88             });
89
90         }
91
92
93     }
94 }