1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
'use strict';
import {Component, IMainCategory, IGroup, IConfigStatuses, IAppMenu, IAppConfigurtaion, IUserProperties, ISubCategory} from "app/models";
import {EntityService, IUserResourceClass, CacheService} from "app/services";
import {ComponentFactory, ResourceType, MenuHandler, ChangeLifecycleStateHandler} from "app/utils";
interface Checkboxes {
componentTypes:Array<string>;
resourceSubTypes:Array<string>;
}
interface CheckboxesFilter {
// Types
selectedComponentTypes:Array<string>;
selectedResourceSubTypes:Array<string>;
// Categories
selectedCategoriesModel:Array<string>;
// Statuses
selectedStatuses:Array<string>;
}
interface Gui {
isLoading:boolean;
onResourceSubTypesClick:Function;
onComponentTypeClick:Function;
onCategoryClick:Function;
onSubcategoryClick:Function;
onGroupClick:Function;
}
export interface ICatalogViewModelScope extends ng.IScope {
checkboxes:Checkboxes;
checkboxesFilter:CheckboxesFilter;
gui:Gui;
categories:Array<IMainCategory>;
confStatus:IConfigStatuses;
sdcMenu:IAppMenu;
catalogFilterdItems:Array<Component>;
expandedSection:Array<string>;
actionStrategy:any;
user:IUserProperties;
catalogMenuItem:any;
version:string;
sortBy:string;
reverse:boolean;
vfcmtType:string;
//this is for UI paging
numberOfItemToDisplay:number;
isAllItemDisplay:boolean;
changeLifecycleState(entity:any, state:string):void;
sectionClick (section:string):void;
order(sortBy:string):void;
getNumOfElements(num:number):string;
goToComponent(component:Component):void;
raiseNumberOfElementToDisplay():void;
}
export class CatalogViewModel {
static '$inject' = [
'$scope',
'$filter',
'Sdc.Services.EntityService',
'sdcConfig',
'sdcMenu',
'$state',
'$q',
'Sdc.Services.UserResourceService',
'Sdc.Services.CacheService',
'ComponentFactory',
'ChangeLifecycleStateHandler',
'MenuHandler'
];
constructor(private $scope:ICatalogViewModelScope,
private $filter:ng.IFilterService,
private EntityService:EntityService,
private sdcConfig:IAppConfigurtaion,
private sdcMenu:IAppMenu,
private $state:ng.ui.IStateService,
private $q:ng.IQService,
private userResourceService:IUserResourceClass,
private cacheService:CacheService,
private ComponentFactory:ComponentFactory,
private ChangeLifecycleStateHandler:ChangeLifecycleStateHandler,
private MenuHandler:MenuHandler) {
this.initScopeMembers();
this.initCatalogData(); // Async task to get catalog from server.
this.initScopeMethods();
}
private initCatalogData = ():void => {
let onSuccess = (followedResponse:Array<Component>):void => {
this.$scope.catalogFilterdItems = followedResponse;
this.$scope.isAllItemDisplay = this.$scope.numberOfItemToDisplay >= this.$scope.catalogFilterdItems.length;
this.$scope.categories = this.cacheService.get('serviceCategories').concat(this.cacheService.get('resourceCategories')).concat(this.cacheService.get('productCategories'));
this.$scope.gui.isLoading = false;
};
let onError = ():void => {
console.info('Failed to load catalog CatalogViewModel::initCatalog');
this.$scope.gui.isLoading = false;
};
this.EntityService.getCatalog().then(onSuccess, onError);
};
private initScopeMembers = ():void => {
// Gui init
this.$scope.gui = <Gui>{};
this.$scope.gui.isLoading = true;
this.$scope.numberOfItemToDisplay = 0;
//this.$scope.categories = this.cacheService.get('categoriesMap');
this.$scope.sdcMenu = this.sdcMenu;
this.$scope.confStatus = this.sdcMenu.statuses;
this.$scope.expandedSection = ["type", "category", "product-category", "status"];
this.$scope.user = this.userResourceService.getLoggedinUser();
this.$scope.catalogMenuItem = this.sdcMenu.catalogMenuItem;
this.$scope.version = this.cacheService.get('version');
this.$scope.sortBy = 'lastUpdateDate';
this.$scope.reverse = true;
// Checklist init
this.$scope.checkboxes = <Checkboxes>{};
this.$scope.checkboxes.componentTypes = ['Resource', 'Service', 'Product'];
this.$scope.checkboxes.resourceSubTypes = ['VF', 'VFC', 'CP', 'VL'];
// Checkboxes filter init
this.$scope.checkboxesFilter = <CheckboxesFilter>{};
this.$scope.checkboxesFilter.selectedComponentTypes = [];
this.$scope.checkboxesFilter.selectedResourceSubTypes = [];
this.$scope.checkboxesFilter.selectedCategoriesModel = [];
this.$scope.checkboxesFilter.selectedStatuses = [];
// this.$scope.isAllItemDisplay = this.$scope.numberOfItemToDisplay >= this.$scope.catalogFilterdItems.length;
this.$scope.vfcmtType = ResourceType.VFCMT;
};
private initScopeMethods = ():void => {
this.$scope.sectionClick = (section:string):void => {
let index:number = this.$scope.expandedSection.indexOf(section);
if (index !== -1) {
this.$scope.expandedSection.splice(index, 1);
} else {
this.$scope.expandedSection.push(section);
}
};
this.$scope.order = (sortBy:string):void => {//default sort by descending last update. default for alphabetical = ascending
this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : (sortBy === 'lastUpdateDate') ? true : false;
this.$scope.sortBy = sortBy;
};
this.$scope.goToComponent = (component:Component):void => {
this.$scope.gui.isLoading = true;
this.$state.go('workspace.general', {id: component.uniqueId, type: component.componentType.toLowerCase()});
};
// Will print the number of elements found in catalog
this.$scope.getNumOfElements = (num:number):string => {
if (!num || num === 0) {
return "No Elements found";
} else if (num === 1) {
return "1 Element found";
} else {
return num + " Elements found";
}
};
/**
* Select | unselect sub resource when resource is clicked | unclicked.
* @param type
*/
this.$scope.gui.onComponentTypeClick = (type:string):void => {
if (type === 'Resource') {
if (this.$scope.checkboxesFilter.selectedComponentTypes.indexOf('Resource') === -1) {
// If the resource was not selected, unselect all childs.
this.$scope.checkboxesFilter.selectedResourceSubTypes = [];
} else {
// If the resource was selected, select all childs
this.$scope.checkboxesFilter.selectedResourceSubTypes = angular.copy(this.$scope.checkboxes.resourceSubTypes);
}
}
};
/**
* Selecting | unselect resources when sub resource is clicked | unclicked.
*/
this.$scope.gui.onResourceSubTypesClick = ():void => {
if (this.$scope.checkboxesFilter.selectedResourceSubTypes && this.$scope.checkboxesFilter.selectedResourceSubTypes.length === this.$scope.checkboxes.resourceSubTypes.length) {
this.$scope.checkboxesFilter.selectedComponentTypes.push('Resource');
} else {
this.$scope.checkboxesFilter.selectedComponentTypes = _.without(this.$scope.checkboxesFilter.selectedComponentTypes, 'Resource');
}
};
this.$scope.gui.onCategoryClick = (category:IMainCategory):void => {
// Select | Unselect all childs
if (this.isCategorySelected(category.uniqueId)) {
this.$scope.checkboxesFilter.selectedCategoriesModel = this.$scope.checkboxesFilter.selectedCategoriesModel.concat(angular.copy(_.map(category.subcategories, (item) => {
return item.uniqueId;
})));
if (category.subcategories) {
category.subcategories.forEach((sub:ISubCategory)=> { // Loop on all selected subcategories and mark the childrens
this.$scope.checkboxesFilter.selectedCategoriesModel = this.$scope.checkboxesFilter.selectedCategoriesModel.concat(angular.copy(_.map(sub.groupings, (item) => {
return item.uniqueId;
})));
});
}
} else {
this.$scope.checkboxesFilter.selectedCategoriesModel = _.difference(this.$scope.checkboxesFilter.selectedCategoriesModel, _.map(category.subcategories, (item) => {
return item.uniqueId;
}));
if (category.subcategories) {
category.subcategories.forEach((sub:ISubCategory)=> { // Loop on all selected subcategories and un mark the childrens
this.$scope.checkboxesFilter.selectedCategoriesModel = _.difference(this.$scope.checkboxesFilter.selectedCategoriesModel, _.map(sub.groupings, (item) => {
return item.uniqueId;
}));
});
}
}
};
this.$scope.gui.onSubcategoryClick = (category:IMainCategory, subCategory:ISubCategory):void => {
// Select | Unselect all childs
if (this.isCategorySelected(subCategory.uniqueId)) {
this.$scope.checkboxesFilter.selectedCategoriesModel = this.$scope.checkboxesFilter.selectedCategoriesModel.concat(angular.copy(_.map(subCategory.groupings, (item) => {
return item.uniqueId;
})));
} else {
this.$scope.checkboxesFilter.selectedCategoriesModel = _.difference(this.$scope.checkboxesFilter.selectedCategoriesModel, _.map(subCategory.groupings, (item) => {
return item.uniqueId;
}));
}
// Mark | Un mark the parent when all childs selected.
if (this.areAllCategoryChildsSelected(category)) {
// Add the category to checkboxesFilter.selectedCategoriesModel
this.$scope.checkboxesFilter.selectedCategoriesModel.push(category.uniqueId);
} else {
this.$scope.checkboxesFilter.selectedCategoriesModel = _.without(this.$scope.checkboxesFilter.selectedCategoriesModel, category.uniqueId);
}
};
this.$scope.raiseNumberOfElementToDisplay = ():void => {
this.$scope.numberOfItemToDisplay = this.$scope.numberOfItemToDisplay + 35;
if (this.$scope.catalogFilterdItems) {
this.$scope.isAllItemDisplay = this.$scope.numberOfItemToDisplay >= this.$scope.catalogFilterdItems.length;
}
};
this.$scope.gui.onGroupClick = (subCategory:ISubCategory):void => {
// Mark | Un mark the parent when all childs selected.
if (this.areAllSubCategoryChildsSelected(subCategory)) {
// Add the category to checkboxesFilter.selectedCategoriesModel
this.$scope.checkboxesFilter.selectedCategoriesModel.push(subCategory.uniqueId);
} else {
this.$scope.checkboxesFilter.selectedCategoriesModel = _.without(this.$scope.checkboxesFilter.selectedCategoriesModel, subCategory.uniqueId);
}
};
};
private areAllCategoryChildsSelected = (category:IMainCategory):boolean => {
if (!category.subcategories) {
return false;
}
let allIds = _.map(category.subcategories, (sub:ISubCategory)=> {
return sub.uniqueId;
});
let total = _.intersection(this.$scope.checkboxesFilter.selectedCategoriesModel, allIds);
return total.length === category.subcategories.length ? true : false;
};
private areAllSubCategoryChildsSelected = (subCategory:ISubCategory):boolean => {
if (!subCategory.groupings) {
return false;
}
let allIds = _.map(subCategory.groupings, (group:IGroup)=> {
return group.uniqueId;
});
let total = _.intersection(this.$scope.checkboxesFilter.selectedCategoriesModel, allIds);
return total.length === subCategory.groupings.length ? true : false;
};
private isCategorySelected = (uniqueId:string):boolean => {
if (this.$scope.checkboxesFilter.selectedCategoriesModel.indexOf(uniqueId) !== -1) {
return true;
}
return false;
};
}
|