Merge "[sdc] - merged 1707_build into sdc LF"
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / product-hierarchy / product-hierarchy-view-model.ts
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
21 'use strict';
22 import {ComponentFactory} from "app/utils";
23 import {Product, IGroup, ISubCategory, IMainCategory} from "app/models";
24 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
25 import {CacheService} from "app/services";
26
27 export interface IProductHierarchyScope extends IWorkspaceViewModelScope {
28
29     categoriesOptions:Array<IMainCategory>;
30     product:Product;
31     isLoading:boolean;
32     showDropDown:boolean;
33
34     onInputTextClicked():void;
35     onGroupSelected(category:IMainCategory, subcategory:ISubCategory, group:IGroup):void;
36     clickOutside():void;
37     deleteGroup(uniqueId:string):void;
38 }
39
40 export class ProductHierarchyViewModel {
41
42     static '$inject' = [
43         '$scope',
44         'Sdc.Services.CacheService',
45         'ComponentFactory',
46         '$state'
47     ];
48
49     constructor(private $scope:IProductHierarchyScope,
50                 private cacheService:CacheService,
51                 private ComponentFactory:ComponentFactory,
52                 private $state:ng.ui.IStateService) {
53
54
55         this.$scope.product = <Product>this.$scope.getComponent();
56         this.$scope.setValidState(true);
57         this.initScope();
58         this.$scope.updateSelectedMenuItem();
59     }
60
61     private initCategories = () => {
62         this.$scope.categoriesOptions = angular.copy(this.cacheService.get('productCategories'));
63         let selectedGroup:Array<IGroup> = [];
64         _.forEach(this.$scope.product.categories, (category:IMainCategory) => {
65             _.forEach(category.subcategories, (subcategory:ISubCategory) => {
66                 selectedGroup = selectedGroup.concat(subcategory.groupings);
67             });
68         });
69         _.forEach(this.$scope.categoriesOptions, (category:IMainCategory) => {
70             _.forEach(category.subcategories, (subcategory:ISubCategory) => {
71                 _.forEach(subcategory.groupings, (group:ISubCategory) => {
72                     let componentGroup:IGroup = _.find(selectedGroup, (componentGroupObj) => {
73                         return componentGroupObj.uniqueId == group.uniqueId;
74                     });
75                     if (componentGroup) {
76                         group.isDisabled = true;
77                     }
78                 });
79             });
80         });
81     };
82
83     private setFormValidation = ():void => {
84
85         this.$scope.setValidState(true);
86
87     };
88
89     private initScope = ():void => {
90         this.$scope.isLoading = false;
91         this.$scope.showDropDown = false;
92         this.initCategories();
93         this.setFormValidation();
94
95         this.$scope.onGroupSelected = (category:IMainCategory, subcategory:ISubCategory, group:IGroup):void => {
96             this.$scope.product.addGroup(category, subcategory, group);
97             this.$state.current.data.unsavedChanges = !this.$scope.isViewMode();
98             group.isDisabled = true;
99             this.$scope.showDropDown = false;
100             this.setFormValidation();
101         };
102
103         this.$scope.onInputTextClicked = ():void => {//just edit the component in place, no pop up nor server update ?
104             this.$scope.showDropDown = !this.$scope.showDropDown;
105         };
106
107         this.$scope.clickOutside = ():any => {
108             this.$scope.showDropDown = false;
109         };
110
111         this.$scope.deleteGroup = (uniqueId:string):void => {
112             //delete group from component
113             this.$scope.product.deleteGroup(uniqueId);
114             this.$state.current.data.unsavedChanges = !this.$scope.isViewMode();
115             this.setFormValidation();
116             //enabled group
117             _.forEach(this.$scope.categoriesOptions, (category:IMainCategory) => {
118                 _.forEach(category.subcategories, (subcategory:ISubCategory) => {
119                     let groupObj:IGroup = _.find(subcategory.groupings, (group) => {
120                         return group.uniqueId === uniqueId;
121                     });
122                     if (groupObj) {
123                         groupObj.isDisabled = false;
124                     }
125                 });
126             });
127         }
128     };
129 }