RIFT-15726 - optimize download size -> lodash usage in UI
[osm/UI.git] / skyquake / plugins / composer / src / src / stores / CatalogDataStore.js
index 144332c..afc5009 100644 (file)
@@ -18,7 +18,9 @@
  */
 'use strict';
 
-import _ from 'lodash'
+import _pick from 'lodash/pick'
+import _isEqual from 'lodash/isEqual'
+import _cloneDeep from 'lodash/cloneDeep'
 import cc from 'change-case'
 import alt from '../alt'
 import UID from '../libraries/UniqueId'
@@ -44,11 +46,23 @@ const defaults = {
 
 const areCatalogItemsMetaDataEqual = function (a, b) {
        const metaProps = ['id', 'name', 'short-name', 'description', 'vendor', 'version'];
-       const aMetaData = _.pick(a, metaProps);
-       const bMetaData = _.pick(b, metaProps);
-       return _.isEqual(aMetaData, bMetaData);
+       const aMetaData = _pick(a, metaProps);
+       const bMetaData = _pick(b, metaProps);
+       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 +75,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 +94,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 +121,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 +147,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) {
@@ -168,7 +203,7 @@ class CatalogDataStore {
                                                        vnfd.uiState['instance-ref-count'] = instanceRefCount;
                                                }
                                                // create an instance of this vnfd to carry transient ui state properties
-                                               const instance = _.cloneDeep(vnfd);
+                                               const instance = _cloneDeep(vnfd);
                                                instance.uiState['member-vnf-index'] = d['member-vnf-index'];
                                                instance['vnf-configuration'] = d['vnf-configuration'];
                                                instance['start-by-default'] = d['start-by-default'];
@@ -290,7 +325,7 @@ class CatalogDataStore {
                                                                ComposerAppActions.showError.defer({
                                                                        errorMessage: 'Cannot edit NSD/VNFD with references to instantiated Network Services'
                                                                });
-                                                               return _.cloneDeep(d);
+                                                               return _cloneDeep(d);
                                                        } else {
                                                                item.uiState.modified = modified;
                                                                requiresSave = true;
@@ -324,7 +359,7 @@ class CatalogDataStore {
                                                        ComposerAppActions.showError.defer({
                                                                errorMessage: 'Cannot edit NSD/VNFD with references to instantiated Network Services'
                                                        });
-                                                       return _.cloneDeep(d);
+                                                       return _cloneDeep(d);
                                                } else {
                                                        itemDescriptor.model.uiState.modified = true;
                                                        this.addSnapshot(itemDescriptor.model);
@@ -421,25 +456,17 @@ 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() {
                const item = this.getFirstSelectedCatalogItem();
                if (item) {
-                       const newItem = _.cloneDeep(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 +540,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 +554,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);
                }
        }