/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
///
module Sdc.ViewModels {
'use strict';
import ResourceType = Sdc.Utils.Constants.ResourceType;
export interface IDashboardViewModelScope extends ng.IScope {
isLoading: boolean;
components: Array;
folders: FoldersMenu;
roles: Models.IConfigRoles;
user: Models.IUserProperties;
sdcConfig:Models.IAppConfigurtaion;
sdcMenu:Models.IAppMenu;
sharingService:Sdc.Services.SharingService;
showTutorial:boolean;
isFirstTime:boolean;
version:string;
checkboxesFilter:CheckboxesFilter;
onImportVfc(file:any):void;
onImportVf(file:any):void;
openCreateModal(componentType: Utils.Constants.ComponentType, importedFile:any): void;
openWhatsNewModal(version:string):void;
openDesignerModal(isResource:boolean, uniqueId:string): void;
openViewerModal(entity:any) : void;
setSelectedFolder(folderItem: FoldersItemsMenu): void;
entitiesCount(folderItem: FoldersItemsMenu): number;
getCurrentFolderDistributed(): Array;
changeLifecycleState(entity:any, data:any): void;
goToComponent(component:Models.Components.Component):void;
wizardDebugEdit:Function;
notificationIconCallback:Function;
}
interface CheckboxesFilter {
// Statuses
selectedStatuses:Array;
// distributed
distributed:Array;
}
export interface IItemMenu {
}
export interface IMenuItemProperties {
text: string;
group: string;
state: string;
dist: string;
groupname: string;
states: Array;
}
export class FoldersMenu {
private _folders: Array = [];
constructor(folders: Array) {
let self = this;
folders.forEach(function(folder: IMenuItemProperties) {
if (folder.groupname){
self._folders.push(new FoldersItemsMenuGroup(folder));
} else {
self._folders.push(new FoldersItemsMenu(folder));
}
});
self._folders[0].setSelected(true);
}
public getFolders = (): Array => {
return this._folders;
};
public getCurrentFolder = (): FoldersItemsMenu => {
let menuItem: FoldersItemsMenu = undefined;
this.getFolders().forEach(function(tmpFolder: FoldersItemsMenu) {
if (tmpFolder.isSelected()){
menuItem = tmpFolder;
}
});
return menuItem;
};
public setSelected = (folder: FoldersItemsMenu):void => {
this.getFolders().forEach(function(tmpFolder: FoldersItemsMenu) {
tmpFolder.setSelected(false);
});
folder.setSelected(true);
}
}
export class FoldersItemsMenu implements IItemMenu {
public text:string;
public group: string;
public state: string;
public dist: string;
public states: Array;
private selected: boolean = false;
constructor(menuProperties: IMenuItemProperties) {
this.text = menuProperties.text;
this.group = menuProperties.group;
this.state = menuProperties.state;
this.states = menuProperties.states;
this.dist = menuProperties.dist;
}
public isSelected = ():boolean => {
return this.selected;
};
public setSelected = (value: boolean):void => {
this.selected = value;
};
public isGroup = ():boolean => {
return false;
}
}
export class FoldersItemsMenuGroup extends FoldersItemsMenu {
public groupname:string;
constructor(menuProperties: IMenuItemProperties) {
super(menuProperties);
this.groupname = menuProperties.groupname;
}
public isGroup = ():boolean => {
return true;
}
}
export class DashboardViewModel {
static '$inject' = [
'$scope',
'$filter',
'Sdc.Services.EntityService',
'$http',
'sdcConfig',
'sdcMenu',
'$modal',
'$templateCache',
'$state',
'$stateParams',
'Sdc.Services.UserResourceService',
'Sdc.Services.SharingService',
'Sdc.Services.CacheService',
'$q',
'ComponentFactory',
'ChangeLifecycleStateHandler',
'ModalsHandler',
'MenuHandler'
];
private components: Array;
constructor(private $scope:IDashboardViewModelScope,
private $filter:ng.IFilterService,
private entityService:Services.EntityService,
private $http:ng.IHttpService,
private sdcConfig:Models.IAppConfigurtaion,
private sdcMenu:Models.IAppMenu,
private $modal:ng.ui.bootstrap.IModalService,
private $templateCache:ng.ITemplateCacheService,
private $state:any,
private $stateParams:any,
private userResourceService:Sdc.Services.IUserResourceClass,
private sharingService:Services.SharingService,
private cacheService:Services.CacheService,
private $q:ng.IQService,
private ComponentFactory: Sdc.Utils.ComponentFactory,
private ChangeLifecycleStateHandler: Sdc.Utils.ChangeLifecycleStateHandler,
private ModalsHandler: Sdc.Utils.ModalsHandler,
private MenuHandler: Utils.MenuHandler
) {
this.initScope();
this.initFolders();
this.initEntities(true);
if (this.$stateParams){
if (this.$state.params.folder){
let self = this;
let folderName = this.$state.params.folder.replaceAll("_"," ");
this.$scope.folders.getFolders().forEach(function(tmpFolder: FoldersItemsMenu) {
if (tmpFolder.text === folderName){
self.$scope.setSelectedFolder(tmpFolder);
}
});
}
// Show the tutorial if needed when the dashboard page is opened.
// This is called from the welcome page.
else if (this.$stateParams.show==='tutorial'){
this.$scope.showTutorial = true;
this.$scope.isFirstTime = true;
}
}
}
private initFolders = ():void => {
if (this.$scope.user) {
this.$scope.folders = new FoldersMenu(this.$scope.roles[this.$scope.user.role].folder);
}
};
private initScope = ():void => {
let self = this;
this.$scope.version = this.cacheService.get('version');
this.$scope.sharingService = this.sharingService;
this.$scope.isLoading = false;
this.$scope.sdcConfig = this.sdcConfig;
this.$scope.sdcMenu = this.sdcMenu;
this.$scope.user = this.userResourceService.getLoggedinUser();
this.$scope.roles = this.sdcMenu.roles;
this.$scope.showTutorial = false;
this.$scope.isFirstTime = false;
// Open onboarding modal
this.$scope.notificationIconCallback = ():void => {
this.ModalsHandler.openOnboadrdingModal('Import').then(()=>{
// OK
}, ()=>{
// ERROR
});
};
// Checkboxes filter init
this.$scope.checkboxesFilter = {};
this.$scope.checkboxesFilter.selectedStatuses = [];
this.$scope.checkboxesFilter.distributed = [];
let appendTemplateAndControllerForProduct:Function = (modalOptions:ng.ui.bootstrap.IModalSettings, isViewer:boolean):void => {
let viewModelsHtmlBasePath:string = '/app/scripts/view-models/';
if (isViewer) {
modalOptions.template = this.$templateCache.get(viewModelsHtmlBasePath + 'entity-viewer/product-viewer-view.html');
modalOptions.controller = 'Sdc.ViewModels.ResourceViewerViewModel';
} else {
modalOptions.template = this.$templateCache.get(viewModelsHtmlBasePath + 'entity-handler/product-form/product-form-view.html');
modalOptions.controller = 'Sdc.ViewModels.ProductFormViewModel';
}
};
this.$scope.onImportVf = (file:any):void => {
if(file && file.filename) {
// Check that the file has valid extension.
let fileExtension:string = file.filename.split(".").pop();
if (this.sdcConfig.csarFileExtension.indexOf(fileExtension.toLowerCase()) !== -1){
this.$state.go('workspace.general', {type:Utils.Constants.ComponentType.RESOURCE.toLowerCase(), importedFile: file, resourceType: ResourceType.VF});
}else {
let data:Sdc.ViewModels.IClientMessageModalModel = {
title: self.$filter('translate')("NEW_SERVICE_RESOURCE_ERROR_VALID_CSAR_EXTENSIONS_TITLE"),
message: self.$filter('translate')("NEW_SERVICE_RESOURCE_ERROR_VALID_CSAR_EXTENSIONS", "{'extensions': '" + this.sdcConfig.csarFileExtension + "'}"),
severity: Utils.Constants.SEVERITY.ERROR
};
this.ModalsHandler.openClientMessageModal(data);
}
}
};
this.$scope.onImportVfc = (file:any):void => {
if(file && file.filename) {
// Check that the file has valid extension.
let fileExtension:string = file.filename.split(".").pop();
if (this.sdcConfig.toscaFileExtension.indexOf(fileExtension.toLowerCase()) !== -1){
this.$state.go('workspace.general', {type:Utils.Constants.ComponentType.RESOURCE.toLowerCase(), importedFile: file, resourceType: ResourceType.VFC});
}else {
let data:Sdc.ViewModels.IClientMessageModalModel = {
title: self.$filter('translate')("NEW_SERVICE_RESOURCE_ERROR_VALID_TOSCA_EXTENSIONS_TITLE"),
message: self.$filter('translate')("NEW_SERVICE_RESOURCE_ERROR_VALID_TOSCA_EXTENSIONS", "{'extensions': '" + this.sdcConfig.toscaFileExtension + "'}"),
severity: Utils.Constants.SEVERITY.ERROR
};
this.ModalsHandler.openClientMessageModal(data);
}
}
};
this.$scope.openCreateModal = (componentType: string, importedFile:any):void => {
if (importedFile){
this.initEntities(true); // Return from import
} else {
this.$state.go('workspace.general', {type:componentType.toLowerCase()});
}
};
this.$scope.entitiesCount = (folderItem: FoldersItemsMenu): any => {
let self = this;
let total: number = 0;
if (folderItem.isGroup()){
this.$scope.folders.getFolders().forEach(function(tmpFolder: FoldersItemsMenu){
if (tmpFolder.group && tmpFolder.group===(folderItem).groupname){
total = total + self._getTotalCounts(tmpFolder, self);
}
});
} else {
total = total + self._getTotalCounts(folderItem, self);
}
return total;
};
this.$scope.getCurrentFolderDistributed = (): Array => {
let self = this;
let states = [];
if (this.$scope.folders) {
let folderItem:FoldersItemsMenu = this.$scope.folders.getCurrentFolder();
if (folderItem.isGroup()) {
this.$scope.folders.getFolders().forEach(function (tmpFolder:FoldersItemsMenu) {
if (tmpFolder.group && tmpFolder.group === (folderItem).groupname) {
self._setStates(tmpFolder, states);
}
});
} else {
self._setStates(folderItem, states);
}
}
return states;
};
this.$scope.setSelectedFolder = (folderItem: FoldersItemsMenu):void => {
this.$scope.folders.setSelected(folderItem);
};
this.$scope.goToComponent = (component:Models.Components.Component):void => {
this.$scope.isLoading=true;
this.$state.go('workspace.general', {id: component.uniqueId, type:component.componentType.toLowerCase() });
};
};
private _getTotalCounts(tmpFolder, self): number {
let total: number = 0;
if (tmpFolder.dist !== undefined) {
let distributions = tmpFolder.dist.split(',');
distributions.forEach((item:any) => {
total = total + self.getEntitiesByStateDist(tmpFolder.state, item).length;
});
}
else {
total = total + self.getEntitiesByStateDist(tmpFolder.state, tmpFolder.dist).length;
}
return total;
}
private _setStates(tmpFolder, states) {
if (tmpFolder.states !== undefined) {
tmpFolder.states.forEach(function (item:any) {
states.push({"state": item.state, "dist": item.dist});
});
} else {
states.push({"state": tmpFolder.state, "dist": tmpFolder.dist});
}
}
private initEntities = (reload:boolean):void => {
this.$scope.isLoading = reload;
this.entityService.getAllComponents().then(
(components: Array) => {
this.components = components;
this.$scope.components = components;
this.$scope.isLoading = false;
});
};
private getEntitiesByStateDist = (state: string, dist: string) : Array => {
let gObj:Array;
if (this.components && (state || dist)) {
gObj = this.components.filter(function (obj:Models.Components.Component) {
if (dist !== undefined && obj.distributionStatus === dist && obj.lifecycleState === state){
return true;
} else if (dist === undefined && obj.lifecycleState === state) {
return true;
}
return false;
});
} else {
gObj = [];
}
return gObj;
}
}
}