X-Git-Url: https://osm.etsi.org/gitweb/?a=blobdiff_plain;f=skyquake%2Fplugins%2Fcomposer%2Fsrc%2Fsrc%2Fstores%2FCatalogDataStore.js;h=1098f0e2342132e0c3f5beb4ca2bd2f8c24d1a13;hb=7f02fe01671d62e6c3c9a523cf0d21b251960365;hp=144332cb61a1ccafc1541174b8e6e97077a42b4e;hpb=e29efc315df33d546237e270470916e26df391d6;p=osm%2FUI.git diff --git a/skyquake/plugins/composer/src/src/stores/CatalogDataStore.js b/skyquake/plugins/composer/src/src/stores/CatalogDataStore.js index 144332cb6..1098f0e23 100644 --- a/skyquake/plugins/composer/src/src/stores/CatalogDataStore.js +++ b/skyquake/plugins/composer/src/src/stores/CatalogDataStore.js @@ -49,6 +49,18 @@ const areCatalogItemsMetaDataEqual = function (a, b) { return _.isEqual(aMetaData, bMetaData); }; +function createItem (type) { + let newItem = DescriptorModelMetaFactory.createModelInstanceForType(type); + if (newItem){ + newItem.id = guid(); + UID.assignUniqueId(newItem.uiState); + newItem.uiState.isNew = true; + newItem.uiState.modified = true; + newItem.uiState['instance-ref-count'] = 0; + } + return newItem; +} + class CatalogDataStore { constructor() { @@ -61,6 +73,14 @@ class CatalogDataStore { this.registerAsync(CatalogDataSource); this.bindActions(CatalogDataSourceActions); this.bindActions(CatalogItemsActions); + this.exportPublicMethods({ + getCatalogs: this.getCatalogs, + getCatalogItemById: this.getCatalogItemById, + getCatalogItemByUid: this.getCatalogItemByUid, + getTransientCatalogs: this.getTransientCatalogs, + getTransientCatalogItemById: this.getTransientCatalogItemById, + getTransientCatalogItemByUid: this.getTransientCatalogItemByUid + }); } resetSelectionState = () => { @@ -72,6 +92,10 @@ class CatalogDataStore { return this.catalogs || (this.catalogs = []); } + getTransientCatalogs() { + return this.state.catalogs || (this.state.catalogs = []); + } + getAllSelectedCatalogItems() { return this.getCatalogs().reduce((r, d) => { d.descriptors.forEach(d => { @@ -95,12 +119,24 @@ class CatalogDataStore { }, [])[0]; } + getTransientCatalogItemById(id) { + return this.getTransientCatalogs().reduce((r, catalog) => { + return r.concat(catalog.descriptors.filter(d => d.id === id)); + }, [])[0]; + } + getCatalogItemByUid(uid) { return this.getCatalogs().reduce((r, catalog) => { return r.concat(catalog.descriptors.filter(d => UID.from(d) === uid)); }, [])[0]; } + getTransientCatalogItemByUid(uid) { + return this.getTransientCatalogs().reduce((r, catalog) => { + return r.concat(catalog.descriptors.filter(d => UID.from(d) === uid)); + }, [])[0]; + } + removeCatalogItem(deleteItem = {}) { this.getCatalogs().map(catalog => { catalog.descriptors = catalog.descriptors.filter(d => d.id !== deleteItem.id); @@ -109,16 +145,13 @@ class CatalogDataStore { } addNewItemToCatalog(newItem) { - const id = guid(); const type = newItem.uiState.type; - newItem.id = id; - UID.assignUniqueId(newItem.uiState); this.getCatalogs().filter(d => d.type === type).forEach(catalog => { catalog.descriptors.push(newItem); }); // update indexes and integrate new model into catalog this.updateCatalogIndexes(this.getCatalogs()); - return this.getCatalogItemById(id); + return this.getCatalogItemById(newItem.id); } updateCatalogIndexes(catalogs) { @@ -421,18 +454,8 @@ class CatalogDataStore { } createCatalogItem(type = 'nsd') { - const model = DescriptorModelMetaFactory.createModelInstanceForType(type); - if (model) { - const newItem = this.addNewItemToCatalog(model); - newItem.uiState.isNew = true; - newItem.uiState.modified = true; - newItem.uiState['instance-ref-count'] = 0; - // open the new model for editing in the canvas/details panels - setTimeout(() => { - this.selectCatalogItem(newItem); - CatalogItemsActions.editCatalogItem.defer(newItem); - }, 200); - } + const newItem = createItem(type); + this.saveItem(newItem) } duplicateSelectedCatalogItem() { @@ -440,6 +463,8 @@ class CatalogDataStore { if (item) { const newItem = _.cloneDeep(item); newItem.name = newItem.name + ' Copy'; + newItem.id = guid(); + UID.assignUniqueId(newItem.uiState); const nsd = this.addNewItemToCatalog(newItem); this.selectCatalogItem(nsd); nsd.uiState.isNew = true; @@ -513,7 +538,13 @@ class CatalogDataStore { saveCatalogItem() { const activeItem = ComposerAppStore.getState().item; if (activeItem) { - if (activeItem.uiState['instance-ref-count'] > 0) { + this.saveItem(activeItem); + } + } + + saveItem(item) { + if (item) { + if (item.uiState['instance-ref-count'] > 0) { console.log('cannot save NSD/VNFD with references to instantiated Network Services'); ComposerAppActions.showError.defer({ errorMessage: 'Cannot save NSD/VNFD with references to instantiated Network Services' @@ -521,25 +552,29 @@ class CatalogDataStore { return; } const success = () => { - delete activeItem.uiState.isNew; - delete activeItem.uiState.modified; - this.updateCatalogItem(activeItem); + delete item.uiState.modified; + if (item.uiState.isNew) { + this.addNewItemToCatalog(item); + delete item.uiState.isNew; + } else { + this.updateCatalogItem(item); + } // TODO should the save action clear the undo/redo stack back to the beginning? - this.resetSnapshots(activeItem); + this.resetSnapshots(item); ModalOverlayActions.hideModalOverlay.defer(); - CatalogItemsActions.editCatalogItem.defer(activeItem); + CatalogItemsActions.editCatalogItem.defer(item); }; const failure = () => { ModalOverlayActions.hideModalOverlay.defer(); - CatalogItemsActions.editCatalogItem.defer(activeItem); + CatalogItemsActions.editCatalogItem.defer(item); }; const exception = () => { - console.warn('unable to save catalog item', activeItem); + console.warn('unable to save catalog item', item); ModalOverlayActions.hideModalOverlay.defer(); - CatalogItemsActions.editCatalogItem.defer(activeItem); + CatalogItemsActions.editCatalogItem.defer(item); }; ModalOverlayActions.showModalOverlay.defer(); - this.getInstance().saveCatalogItem(activeItem).then(success, failure).catch(exception); + this.getInstance().saveCatalogItem(item).then(success, failure).catch(exception); } }