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";
7 export interface IProductHierarchyScope extends IWorkspaceViewModelScope {
9 categoriesOptions:Array<IMainCategory>;
14 onInputTextClicked():void;
15 onGroupSelected(category:IMainCategory, subcategory:ISubCategory, group:IGroup):void;
17 deleteGroup(uniqueId:string):void;
20 export class ProductHierarchyViewModel {
24 'Sdc.Services.CacheService',
29 constructor(private $scope:IProductHierarchyScope,
30 private cacheService:CacheService,
31 private ComponentFactory:ComponentFactory,
32 private $state:ng.ui.IStateService) {
35 this.$scope.product = <Product>this.$scope.getComponent();
36 this.$scope.setValidState(true);
38 this.$scope.updateSelectedMenuItem();
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);
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;
56 group.isDisabled = true;
63 private setFormValidation = ():void => {
65 this.$scope.setValidState(true);
69 private initScope = ():void => {
70 this.$scope.isLoading = false;
71 this.$scope.showDropDown = false;
72 this.initCategories();
73 this.setFormValidation();
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();
83 this.$scope.onInputTextClicked = ():void => {//just edit the component in place, no pop up nor server update ?
84 this.$scope.showDropDown = !this.$scope.showDropDown;
87 this.$scope.clickOutside = ():any => {
88 this.$scope.showDropDown = false;
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();
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;
103 groupObj.isDisabled = false;