diff options
Diffstat (limited to 'catalog-ui/src/app/view-models')
-rw-r--r-- | catalog-ui/src/app/view-models/workspace/tabs/general/general-view-model.ts | 197 |
1 files changed, 111 insertions, 86 deletions
diff --git a/catalog-ui/src/app/view-models/workspace/tabs/general/general-view-model.ts b/catalog-ui/src/app/view-models/workspace/tabs/general/general-view-model.ts index 6c28fae508..e766e36899 100644 --- a/catalog-ui/src/app/view-models/workspace/tabs/general/general-view-model.ts +++ b/catalog-ui/src/app/view-models/workspace/tabs/general/general-view-model.ts @@ -36,7 +36,7 @@ import {EventListenerService, ProgressService} from "app/services"; import {CacheService, ElementService, ModelService, ImportVSPService, OnboardingService} from "app/services-ng2"; import {Component, IAppConfigurtaion, ICsarComponent, IMainCategory, IMetadataKey, ISubCategory, IValidate, Resource, Service} from "app/models"; import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model"; -import {CATEGORY_SERVICE_METADATA_KEYS, PREVIOUS_CSAR_COMPONENT} from "../../../../utils/constants"; +import {CATEGORY_SERVICE_METADATA_KEYS, PREVIOUS_CSAR_COMPONENT, DEFAULT_MODEL_NAME} from "../../../../utils/constants"; import {Observable} from "rxjs"; import {Model} from "../../../../models/model"; @@ -93,9 +93,10 @@ export interface IGeneralScope extends IWorkspaceViewModelScope { convertCategoryStringToOneArray(category:string, subcategory:string):Array<IMainCategory>; onCategoryChange():void; onEcompGeneratedNamingChange():void; + onModelChange():void; onBaseTypeChange():void; openOnBoardingModal():void; - initCategoreis():void; + initCategories():void; initEnvironmentContext():void; initInstantiationTypes():void; initBaseTypes():void; @@ -252,7 +253,7 @@ export class GeneralViewModel { this.$scope.component.tags = _.without(this.$scope.component.tags, this.$scope.component.name); // Init categories - this.$scope.initCategoreis(); + this.$scope.initCategories(); // Init Environment Context this.$scope.initEnvironmentContext(); @@ -298,7 +299,7 @@ export class GeneralViewModel { if (this.$scope.component.isResource() && (this.$scope.component as Resource).resourceType === ResourceType.VF || - (this.$scope.component as Resource).resourceType === ResourceType.PNF && (this.$scope.component as Resource).csarUUID) { + (this.$scope.component as Resource).resourceType === ResourceType.PNF && (this.$scope.component as Resource).csarUUID) { this.$scope.isShowOnboardingSelectionBrowse = true; this.setImportedFileText(); } else { @@ -413,11 +414,11 @@ export class GeneralViewModel { }); return tempCategories; - }; - + }; + private initScopeMethods = ():void => { - this.$scope.initCategoreis = ():void => { + this.$scope.initCategories = ():void => { if (this.$scope.componentType === ComponentType.RESOURCE) { this.$scope.categories = this.cacheService.get('resourceCategories'); @@ -431,7 +432,7 @@ export class GeneralViewModel { //Flag to disbale category if service is created through External API this.$scope.isHiddenCategorySelected = this.isHiddenCategory(this.$scope.component.selectedCategory); } - + } }; @@ -453,24 +454,27 @@ export class GeneralViewModel { this.$scope.initBaseTypes = ():void => { if (this.$scope.componentType === ComponentType.SERVICE && this.$scope.component && this.$scope.component.categories) { - let modelName = this.$scope.component.model ? this.$scope.component.model : null; - this.elementService.getCategoryBasetypes(this.$scope.component.categories[0].name, modelName).subscribe((data: BaseTypeResponse[]) => { - this.$scope.baseTypes = [] - this.$scope.baseTypeVersions = [] - data.forEach(baseType => { - this.$scope.baseTypes.push(baseType.toscaResourceName) - if (baseType.toscaResourceName === this.$scope.component.derivedFromGenericType){ - baseType.versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version)); - }}); - }) + let modelName = this.$scope.component.model ? this.$scope.component.model : null; + this.elementService.getCategoryBasetypes(this.$scope.component.categories[0].name, modelName).subscribe((data: BaseTypeResponse[]) => { + this.$scope.baseTypes = [] + this.$scope.baseTypeVersions = [] + data.forEach(baseType => { + this.$scope.baseTypes.push(baseType.toscaResourceName) + if (baseType.toscaResourceName === this.$scope.component.derivedFromGenericType){ + baseType.versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version)); + }}); + }) } }; this.$scope.initModel = ():void => { this.$scope.isModelRequired = false; this.$scope.models = []; - this.$scope.defaultModelOption = 'SDC AID'; + this.$scope.defaultModelOption = DEFAULT_MODEL_NAME; this.$scope.showDefaultModelOption = true; + if (this.$scope.componentType === ComponentType.SERVICE) { + this.filterCategoriesByModel(this.$scope.component.model); + } if (this.$scope.isCreateMode() && this.$scope.isVspImport()) { if (this.$scope.component.componentMetadata.models) { this.$scope.isModelRequired = true; @@ -542,16 +546,16 @@ export class GeneralViewModel { this.importVSPService.openOnboardingModal(csarUUID, csarVersion).subscribe((result) => { this.ComponentFactory.getComponentWithMetadataFromServer(result.type.toUpperCase(), result.previousComponent.uniqueId).then( (component:Component)=> { - if (result.componentCsar && component.isResource()){ - this.cacheService.set(PREVIOUS_CSAR_COMPONENT, angular.copy(component)); - component = this.ComponentFactory.updateComponentFromCsar(result.componentCsar, <Resource>component); - } - this.$scope.setComponent(component); - this.$scope.save(); - this.setImportedFileText(); - }, ()=> { - // ERROR - }); + if (result.componentCsar && component.isResource()){ + this.cacheService.set(PREVIOUS_CSAR_COMPONENT, angular.copy(component)); + component = this.ComponentFactory.updateComponentFromCsar(result.componentCsar, <Resource>component); + } + this.$scope.setComponent(component); + this.$scope.save(); + this.setImportedFileText(); + }, ()=> { + // ERROR + }); }) }; @@ -590,7 +594,7 @@ export class GeneralViewModel { return; } - + let subtype:string = ComponentType.RESOURCE == this.$scope.componentType ? this.$scope.component.getComponentSubType() : undefined; if (subtype == "SRVC") { subtype = "VF" @@ -712,42 +716,41 @@ export class GeneralViewModel { this.$scope.onCategoryChange = (): void => { this.$scope.component.selectedCategory = this.$scope.componentCategories.selectedCategory; - this.$scope.component.categories = this.convertCategoryStringToOneArray(); - this.$scope.component.icon = DEFAULT_ICON; - if (this.$scope.component.categories[0].metadataKeys) { - for (let metadataKey of this.$scope.component.categories[0].metadataKeys) { - if (!this.$scope.component.categorySpecificMetadata[metadataKey.name]) { - this.$scope.component.categorySpecificMetadata[metadataKey.name] = metadataKey.defaultValue ? metadataKey.defaultValue : ""; - } + if (this.$scope.component.selectedCategory) { + this.$scope.component.categories = this.convertCategoryStringToOneArray(); + this.$scope.component.icon = DEFAULT_ICON; + if (this.$scope.component.categories[0].metadataKeys) { + for (let metadataKey of this.$scope.component.categories[0].metadataKeys) { + if (!this.$scope.component.categorySpecificMetadata[metadataKey.name]) { + this.$scope.component.categorySpecificMetadata[metadataKey.name] = metadataKey.defaultValue ? metadataKey.defaultValue : ""; + } + } } - } - if (this.$scope.component.categories[0].subcategories && this.$scope.component.categories[0].subcategories[0].metadataKeys) { - for (let metadataKey of this.$scope.component.categories[0].subcategories[0].metadataKeys) { - if (!this.$scope.component.categorySpecificMetadata[metadataKey.name]) { - this.$scope.component.categorySpecificMetadata[metadataKey.name] = metadataKey.defaultValue ? metadataKey.defaultValue : ""; - } + if (this.$scope.component.categories[0].subcategories && this.$scope.component.categories[0].subcategories[0].metadataKeys) { + for (let metadataKey of this.$scope.component.categories[0].subcategories[0].metadataKeys) { + if (!this.$scope.component.categorySpecificMetadata[metadataKey.name]) { + this.$scope.component.categorySpecificMetadata[metadataKey.name] = metadataKey.defaultValue ? metadataKey.defaultValue : ""; + } + } + } + if (this.$scope.componentType === ComponentType.SERVICE && this.$scope.component.categories[0]) { + let modelName : string = this.$scope.component.model ? this.$scope.component.model : null; + this.elementService.getCategoryBasetypes(this.$scope.component.categories[0].name, modelName).subscribe((data: BaseTypeResponse[]) => { + + if(this.$scope.isCreateMode()){ + this.loadBaseTypes(data); + } else { + var isValidForBaseType:boolean = false; + data.forEach(baseType => {if (!this.$scope.component.derivedFromGenericType || baseType.toscaResourceName === this.$scope.component.derivedFromGenericType){ + isValidForBaseType = true; + };}); + this.$scope.editForm['category'].$setValidity('validForBaseType', isValidForBaseType); + } + }); } + } else { + this.$scope.baseTypes = []; } - if (this.$scope.componentType === ComponentType.SERVICE && this.$scope.component.categories[0]) { - let modelName : string = this.$scope.component.model ? this.$scope.component.model : null; - this.elementService.getCategoryBasetypes(this.$scope.component.categories[0].name, modelName).subscribe((data: BaseTypeResponse[]) => { - - if(this.$scope.isCreateMode()){ - this.$scope.baseTypes = [] - this.$scope.baseTypeVersions = [] - data.forEach(baseType => this.$scope.baseTypes.push(baseType.toscaResourceName)); - data[0].versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version)); - this.$scope.component.derivedFromGenericType = data[0].toscaResourceName; - this.$scope.component.derivedFromGenericVersion = data[0].versions[0]; - } else { - var isValidForBaseType:boolean = false; - data.forEach(baseType => {if (!this.$scope.component.derivedFromGenericType || baseType.toscaResourceName === this.$scope.component.derivedFromGenericType){ - isValidForBaseType = true; - };}); - this.$scope.editForm['category'].$setValidity('validForBaseType', isValidForBaseType); - } - }); - } }; this.$scope.onEcompGeneratedNamingChange = (): void => { @@ -757,29 +760,23 @@ export class GeneralViewModel { }; this.$scope.onBaseTypeChange = (): void => { - let modelName : string = this.$scope.component.model ? this.$scope.component.model : null; + let modelName : string = this.$scope.component.model ? this.$scope.component.model : null; this.elementService.getCategoryBasetypes(this.$scope.component.categories[0].name, modelName).subscribe((data: BaseTypeResponse[]) => { - this.$scope.baseTypeVersions = [] - data.forEach(baseType => { - if(baseType.toscaResourceName === this.$scope.component.derivedFromGenericType) { - baseType.versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version)); - this.$scope.component.derivedFromGenericVersion = baseType.versions[0]; - }; - }); - }) + this.$scope.baseTypeVersions = [] + data.forEach(baseType => { + if(baseType.toscaResourceName === this.$scope.component.derivedFromGenericType) { + baseType.versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version)); + this.$scope.component.derivedFromGenericVersion = baseType.versions[0]; + }; + }); + }) }; this.$scope.onModelChange = (): void => { - if (this.$scope.componentType === ComponentType.SERVICE && this.$scope.component && this.$scope.component.categories) { + if (this.$scope.componentType === ComponentType.SERVICE && this.$scope.component && this.$scope.categories) { let modelName = this.$scope.component.model ? this.$scope.component.model : null; - this.elementService.getCategoryBasetypes(this.$scope.component.categories[0].name, modelName).subscribe((data: BaseTypeResponse[]) => { - this.$scope.baseTypes = [] - this.$scope.baseTypeVersions = [] - data.forEach(baseType => this.$scope.baseTypes.push(baseType.toscaResourceName)); - data[0].versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version)); - this.$scope.component.derivedFromGenericType = data[0].toscaResourceName; - this.$scope.component.derivedFromGenericVersion = data[0].versions[0]; - }); + this.filterCategoriesByModel(modelName); + this.filterBaseTypesByModelAndCategory(modelName) } }; @@ -801,7 +798,7 @@ export class GeneralViewModel { if (metadataKey) { return metadataKey.validValues; } - return []; + return []; } this.$scope.isMetadataKeyForComponentCategory = (key: string): boolean => { @@ -818,7 +815,36 @@ export class GeneralViewModel { this.$scope.component[attribute] = metadatakey.defaultValue; } return metadatakey != null; - } + } + } + + private filterCategoriesByModel(modelName:string) { + // reload categories + this.$scope.initCategories(); + this.$scope.categories = this.$scope.categories.filter(category => + !modelName ? category.models.indexOf(DEFAULT_MODEL_NAME) !== -1 : category.models !== null && category.models.indexOf(modelName) !== -1); + } + + + private filterBaseTypesByModelAndCategory(modelName:string) { + let categories = this.$scope.component.categories; + if (categories) { + this.elementService.getCategoryBasetypes(categories[0].name, modelName).subscribe((data: BaseTypeResponse[]) => { + this.loadBaseTypes(data); + }); + } + } + + private loadBaseTypes(data:BaseTypeResponse[]) { + this.$scope.baseTypes = []; + this.$scope.baseTypeVersions = []; + data.forEach(baseType => this.$scope.baseTypes.push(baseType.toscaResourceName)); + let baseType = data[0]; + if (baseType) { + baseType.versions.reverse().forEach(version => this.$scope.baseTypeVersions.push(version)); + this.$scope.component.derivedFromGenericType = baseType.toscaResourceName; + this.$scope.component.derivedFromGenericVersion = baseType.versions[0]; + } } private setUnsavedChanges = (hasChanges: boolean): void => { @@ -834,14 +860,14 @@ export class GeneralViewModel { } private getSubcategoryMetadataKey(categories: IMainCategory[], key: string) : IMetadataKey { - if (categories[0].subcategories && categories[0].subcategories[0].metadataKeys && categories[0].subcategories[0].metadataKeys.some(metadataKey => metadataKey.name == key)) { + if (categories[0].subcategories && categories[0].subcategories[0].metadataKeys && categories[0].subcategories[0].metadataKeys.some(metadataKey => metadataKey.name == key)) { return categories[0].subcategories[0].metadataKeys.find(metadataKey => metadataKey.name == key); } return null; } private getCategoryMetadataKey(categories: IMainCategory[], key: string) : IMetadataKey { - if (categories[0].metadataKeys && categories[0].metadataKeys.some(metadataKey => metadataKey.name == key)) { + if (categories[0].metadataKeys && categories[0].metadataKeys.some(metadataKey => metadataKey.name == key)) { return categories[0].metadataKeys.find(metadataKey => metadataKey.name == key); } return null; @@ -852,4 +878,3 @@ export class GeneralViewModel { } } - |