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 {Component, IMainCategory, IGroup, IConfigStatuses, IAppMenu, IAppConfigurtaion, IUserProperties, ISubCategory} from "app/models";
23 import {EntityService, IUserResourceClass, CacheService} from "app/services";
24 import {ComponentFactory, ResourceType, MenuHandler, ChangeLifecycleStateHandler} from "app/utils";
27 interface Checkboxes {
28 componentTypes:Array<string>;
29 resourceSubTypes:Array<string>;
32 interface CheckboxesFilter {
34 selectedComponentTypes:Array<string>;
35 selectedResourceSubTypes:Array<string>;
37 selectedCategoriesModel:Array<string>;
39 selectedStatuses:Array<string>;
44 onResourceSubTypesClick:Function;
45 onComponentTypeClick:Function;
46 onCategoryClick:Function;
47 onSubcategoryClick:Function;
48 onGroupClick:Function;
51 export interface ICatalogViewModelScope extends ng.IScope {
52 checkboxes:Checkboxes;
53 checkboxesFilter:CheckboxesFilter;
56 categories:Array<IMainCategory>;
57 confStatus:IConfigStatuses;
59 catalogFilterdItems:Array<Component>;
60 expandedSection:Array<string>;
69 //this is for UI paging
70 numberOfItemToDisplay:number;
71 isAllItemDisplay:boolean;
73 changeLifecycleState(entity:any, state:string):void;
74 sectionClick (section:string):void;
75 order(sortBy:string):void;
76 getNumOfElements(num:number):string;
77 goToComponent(component:Component):void;
78 raiseNumberOfElementToDisplay():void;
81 export class CatalogViewModel {
85 'Sdc.Services.EntityService',
90 'Sdc.Services.UserResourceService',
91 'Sdc.Services.CacheService',
93 'ChangeLifecycleStateHandler',
97 constructor(private $scope:ICatalogViewModelScope,
98 private $filter:ng.IFilterService,
99 private EntityService:EntityService,
100 private sdcConfig:IAppConfigurtaion,
101 private sdcMenu:IAppMenu,
102 private $state:ng.ui.IStateService,
103 private $q:ng.IQService,
104 private userResourceService:IUserResourceClass,
105 private cacheService:CacheService,
106 private ComponentFactory:ComponentFactory,
107 private ChangeLifecycleStateHandler:ChangeLifecycleStateHandler,
108 private MenuHandler:MenuHandler) {
111 this.initScopeMembers();
112 this.initCatalogData(); // Async task to get catalog from server.
113 this.initScopeMethods();
116 private initCatalogData = ():void => {
117 let onSuccess = (followedResponse:Array<Component>):void => {
118 this.$scope.catalogFilterdItems = followedResponse;
119 this.$scope.isAllItemDisplay = this.$scope.numberOfItemToDisplay >= this.$scope.catalogFilterdItems.length;
120 this.$scope.categories = this.cacheService.get('serviceCategories').concat(this.cacheService.get('resourceCategories'));
121 this.$scope.gui.isLoading = false;
124 let onError = ():void => {
125 console.info('Failed to load catalog CatalogViewModel::initCatalog');
126 this.$scope.gui.isLoading = false;
128 this.EntityService.getCatalog().then(onSuccess, onError);
132 private initScopeMembers = ():void => {
134 this.$scope.gui = <Gui>{};
135 this.$scope.gui.isLoading = true;
136 this.$scope.numberOfItemToDisplay = 0;
137 //this.$scope.categories = this.cacheService.get('categoriesMap');
138 this.$scope.sdcMenu = this.sdcMenu;
139 this.$scope.confStatus = this.sdcMenu.statuses;
140 this.$scope.expandedSection = ["type", "category", "status"];
141 this.$scope.user = this.userResourceService.getLoggedinUser();
142 this.$scope.catalogMenuItem = this.sdcMenu.catalogMenuItem;
143 this.$scope.version = this.cacheService.get('version');
144 this.$scope.sortBy = 'lastUpdateDate';
145 this.$scope.reverse = true;
149 this.$scope.checkboxes = <Checkboxes>{};
150 this.$scope.checkboxes.componentTypes = ['Resource', 'Service'];
151 this.$scope.checkboxes.resourceSubTypes = ['VF', 'VFC', 'PNF', 'CP', 'VL'];
153 // Checkboxes filter init
154 this.$scope.checkboxesFilter = <CheckboxesFilter>{};
155 this.$scope.checkboxesFilter.selectedComponentTypes = [];
156 this.$scope.checkboxesFilter.selectedResourceSubTypes = [];
157 this.$scope.checkboxesFilter.selectedCategoriesModel = [];
158 this.$scope.checkboxesFilter.selectedStatuses = [];
160 // this.$scope.isAllItemDisplay = this.$scope.numberOfItemToDisplay >= this.$scope.catalogFilterdItems.length;
162 this.$scope.vfcmtType = ResourceType.VFCMT;
165 private initScopeMethods = ():void => {
166 this.$scope.sectionClick = (section:string):void => {
167 let index:number = this.$scope.expandedSection.indexOf(section);
169 this.$scope.expandedSection.splice(index, 1);
171 this.$scope.expandedSection.push(section);
176 this.$scope.order = (sortBy:string):void => {//default sort by descending last update. default for alphabetical = ascending
177 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : (sortBy === 'lastUpdateDate') ? true : false;
178 this.$scope.sortBy = sortBy;
182 this.$scope.goToComponent = (component:Component):void => {
183 this.$scope.gui.isLoading = true;
184 this.$state.go('workspace.general', {id: component.uniqueId, type: component.componentType.toLowerCase()});
188 // Will print the number of elements found in catalog
189 this.$scope.getNumOfElements = (num:number):string => {
190 if (!num || num === 0) {
191 return "No Elements found";
192 } else if (num === 1) {
193 return "1 Element found";
195 return num + " Elements found";
200 * Select | unselect sub resource when resource is clicked | unclicked.
203 this.$scope.gui.onComponentTypeClick = (type:string):void => {
204 if (type === 'Resource') {
205 if (this.$scope.checkboxesFilter.selectedComponentTypes.indexOf('Resource') === -1) {
206 // If the resource was not selected, unselect all childs.
207 this.$scope.checkboxesFilter.selectedResourceSubTypes = [];
209 // If the resource was selected, select all childs
210 this.$scope.checkboxesFilter.selectedResourceSubTypes = angular.copy(this.$scope.checkboxes.resourceSubTypes);
216 * Selecting | unselect resources when sub resource is clicked | unclicked.
218 this.$scope.gui.onResourceSubTypesClick = ():void => {
219 if (this.$scope.checkboxesFilter.selectedResourceSubTypes && this.$scope.checkboxesFilter.selectedResourceSubTypes.length === this.$scope.checkboxes.resourceSubTypes.length) {
220 this.$scope.checkboxesFilter.selectedComponentTypes.push('Resource');
222 this.$scope.checkboxesFilter.selectedComponentTypes = _.without(this.$scope.checkboxesFilter.selectedComponentTypes, 'Resource');
226 this.$scope.gui.onCategoryClick = (category:IMainCategory):void => {
227 // Select | Unselect all childs
228 if (this.isCategorySelected(category.uniqueId)) {
229 this.$scope.checkboxesFilter.selectedCategoriesModel = this.$scope.checkboxesFilter.selectedCategoriesModel.concat(angular.copy(_.map(category.subcategories, (item) => {
230 return item.uniqueId;
232 if (category.subcategories) {
233 category.subcategories.forEach((sub:ISubCategory)=> { // Loop on all selected subcategories and mark the childrens
234 this.$scope.checkboxesFilter.selectedCategoriesModel = this.$scope.checkboxesFilter.selectedCategoriesModel.concat(angular.copy(_.map(sub.groupings, (item) => {
235 return item.uniqueId;
240 this.$scope.checkboxesFilter.selectedCategoriesModel = _.difference(this.$scope.checkboxesFilter.selectedCategoriesModel, _.map(category.subcategories, (item) => {
241 return item.uniqueId;
243 if (category.subcategories) {
244 category.subcategories.forEach((sub:ISubCategory)=> { // Loop on all selected subcategories and un mark the childrens
245 this.$scope.checkboxesFilter.selectedCategoriesModel = _.difference(this.$scope.checkboxesFilter.selectedCategoriesModel, _.map(sub.groupings, (item) => {
246 return item.uniqueId;
253 this.$scope.gui.onSubcategoryClick = (category:IMainCategory, subCategory:ISubCategory):void => {
254 // Select | Unselect all childs
255 if (this.isCategorySelected(subCategory.uniqueId)) {
256 this.$scope.checkboxesFilter.selectedCategoriesModel = this.$scope.checkboxesFilter.selectedCategoriesModel.concat(angular.copy(_.map(subCategory.groupings, (item) => {
257 return item.uniqueId;
260 this.$scope.checkboxesFilter.selectedCategoriesModel = _.difference(this.$scope.checkboxesFilter.selectedCategoriesModel, _.map(subCategory.groupings, (item) => {
261 return item.uniqueId;
265 // Mark | Un mark the parent when all childs selected.
266 if (this.areAllCategoryChildsSelected(category)) {
267 // Add the category to checkboxesFilter.selectedCategoriesModel
268 this.$scope.checkboxesFilter.selectedCategoriesModel.push(category.uniqueId);
270 this.$scope.checkboxesFilter.selectedCategoriesModel = _.without(this.$scope.checkboxesFilter.selectedCategoriesModel, category.uniqueId);
275 this.$scope.raiseNumberOfElementToDisplay = ():void => {
276 this.$scope.numberOfItemToDisplay = this.$scope.numberOfItemToDisplay + 35;
277 if (this.$scope.catalogFilterdItems) {
278 this.$scope.isAllItemDisplay = this.$scope.numberOfItemToDisplay >= this.$scope.catalogFilterdItems.length;
282 this.$scope.gui.onGroupClick = (subCategory:ISubCategory):void => {
283 // Mark | Un mark the parent when all childs selected.
284 if (this.areAllSubCategoryChildsSelected(subCategory)) {
285 // Add the category to checkboxesFilter.selectedCategoriesModel
286 this.$scope.checkboxesFilter.selectedCategoriesModel.push(subCategory.uniqueId);
288 this.$scope.checkboxesFilter.selectedCategoriesModel = _.without(this.$scope.checkboxesFilter.selectedCategoriesModel, subCategory.uniqueId);
295 private areAllCategoryChildsSelected = (category:IMainCategory):boolean => {
296 if (!category.subcategories) {
299 let allIds = _.map(category.subcategories, (sub:ISubCategory)=> {
302 let total = _.intersection(this.$scope.checkboxesFilter.selectedCategoriesModel, allIds);
303 return total.length === category.subcategories.length ? true : false;
306 private areAllSubCategoryChildsSelected = (subCategory:ISubCategory):boolean => {
307 if (!subCategory.groupings) {
310 let allIds = _.map(subCategory.groupings, (group:IGroup)=> {
311 return group.uniqueId;
313 let total = _.intersection(this.$scope.checkboxesFilter.selectedCategoriesModel, allIds);
314 return total.length === subCategory.groupings.length ? true : false;
317 private isCategorySelected = (uniqueId:string):boolean => {
318 if (this.$scope.checkboxesFilter.selectedCategoriesModel.indexOf(uniqueId) !== -1) {