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=========================================================
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";
27 export interface IProductHierarchyScope extends IWorkspaceViewModelScope {
29 categoriesOptions:Array<IMainCategory>;
34 onInputTextClicked():void;
35 onGroupSelected(category:IMainCategory, subcategory:ISubCategory, group:IGroup):void;
37 deleteGroup(uniqueId:string):void;
40 export class ProductHierarchyViewModel {
44 'Sdc.Services.CacheService',
49 constructor(private $scope:IProductHierarchyScope,
50 private cacheService:CacheService,
51 private ComponentFactory:ComponentFactory,
52 private $state:ng.ui.IStateService) {
55 this.$scope.product = <Product>this.$scope.getComponent();
56 this.$scope.setValidState(true);
58 this.$scope.updateSelectedMenuItem();
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);
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;
76 group.isDisabled = true;
83 private setFormValidation = ():void => {
85 this.$scope.setValidState(true);
89 private initScope = ():void => {
90 this.$scope.isLoading = false;
91 this.$scope.showDropDown = false;
92 this.initCategories();
93 this.setFormValidation();
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();
103 this.$scope.onInputTextClicked = ():void => {//just edit the component in place, no pop up nor server update ?
104 this.$scope.showDropDown = !this.$scope.showDropDown;
107 this.$scope.clickOutside = ():any => {
108 this.$scope.showDropDown = false;
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();
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;
123 groupObj.isDisabled = false;