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