From dfedca5843d4c47722e79cc03ed56d50453595d2 Mon Sep 17 00:00:00 2001
From: juanf <>
Date: Tue, 21 Apr 2020 10:27:39 +0200
Subject: [PATCH] SSDM-9524 : Read only spaces can now be edited

---
 .../html/js/config/SettingsManager.js         | 21 ++++++++++++--
 .../SettingsForm/SettingsFormController.js    |  1 +
 .../js/views/SettingsForm/SettingsFormView.js | 28 ++++++++++++++++++-
 3 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/SettingsManager.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/SettingsManager.js
index 0132e137bf2..0f26cd82cad 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/SettingsManager.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/SettingsManager.js
@@ -81,6 +81,10 @@ function SettingsManager(serverFacade) {
 		return profile.allSpaces;
 	}
 
+    this.getInventorySpacesReadOnlyOptions = function() {
+		return profile.allSpaces;
+	}
+
 	this.getSampleTypeOptions = function() {
 		return profile.getAllSampleTypes().map( function(_) { return _.code; } )
 	}
@@ -99,17 +103,21 @@ function SettingsManager(serverFacade) {
 			"hideSectionsByDefault"
 		];
 		for (var field of fieldsToOverride) {
-			if (settings[field] != null) {
+			if (settings[field] !== null && settings[field] !== undefined) {
 				targetProfile[field] = settings[field];
 			}
 		}
 		
 		// array fields to add/remove values to defaults
 		var fieldsToAdd = [
-			"inventorySpaces"
+			"inventorySpaces", "inventorySpacesReadOnly"
 		];
+
 		for (var field of fieldsToAdd) {
-			targetProfile[field] = settings[field];
+		    // We add fields only if they have been populated, to avoid overriding new ones
+		    if(settings[field] !== null && settings[field] !== undefined) {
+			    targetProfile[field] = settings[field];
+			}
 		}
 		
 		// main menu, checks menu items one by one to keep new ones
@@ -161,6 +169,13 @@ function SettingsManager(serverFacade) {
                 }
             }
         }
+        if(settings.inventorySpacesReadOnly) {
+            for(var idx = 0; idx < settings.inventorySpaces.length; idx++) {
+                if(!settings.inventorySpaces[idx]) {
+                    errors.push("Empty value found instead of a space, please delete it before save.");
+                }
+            }
+        }
     }
 
 	this._validateSampleTypeDefinitionsExtension = function(settings, errors) {
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormController.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormController.js
index 934e8c517e7..048a089388b 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormController.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormController.js
@@ -85,6 +85,7 @@ function SettingsFormController(mainController, settingsSample, mode) {
 	this.getForcedDisableRTFOptions = this._settingsManager.getForcedDisableRTFOptions;
 	this.getForcedMonospaceFontOptions = this._settingsManager.getForcedMonospaceFontOptions;
 	this.getInventorySpacesOptions = this._settingsManager.getInventorySpacesOptions;
+    this.getInventorySpacesReadOnlyOptions = this._settingsManager.getInventorySpacesReadOnlyOptions;
 	this.getSampleTypeOptions = this._settingsManager.getSampleTypeOptions;
 	this.getAnnotationPropertyTypeOptions = this._settingsManager.getAnnotationPropertyTypeOptions;
 
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormView.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormView.js
index 60fa6fe72ef..8e4c11c631a 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormView.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormView.js
@@ -25,6 +25,7 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 	this._customWidgetsTableModel = null;
 	this._forcedMonospaceTableModel = null;
 	this._inventorySpacesTableModel = null;
+	this._inventorySpacesReadOnlyTableModel = null;
 	this._sampleTypeDefinitionsMiscellaneousSettingsTableModels = {}; // key: sample type; value: table model
 	this._sampleTypeDefinitionsSettingsTableModels = {}; // key: sample type; value: table model
 	this._sampleTypeDefinitionsHintsTableModels = {}; // key: sample type; value: table model
@@ -113,6 +114,7 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 			forcedDisableRTF : this._forcedDisableRTFTableModel.getValues(),
 			forceMonospaceFont : this._forcedMonospaceTableModel.getValues(),
 			inventorySpaces : this._inventorySpacesTableModel.getValues(),
+			inventorySpacesReadOnly : this._inventorySpacesReadOnlyTableModel.getValues(),
 			sampleTypeDefinitionsExtension : this._getSampleTypeDefinitionsExtension(),
 			showDatasetArchivingButton : this._miscellaneousTableModel.getValues()["Show Dataset archiving button"],
 			hideSectionsByDefault : this._miscellaneousTableModel.getValues()["Hide sections by default"],
@@ -239,6 +241,8 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 		$fieldset.append(FormUtil.getInfoText(text.info));
 		this._inventorySpacesTableModel = this._getInventorySpacesTableModel();
 		$fieldset.append(this._getTable(this._inventorySpacesTableModel));
+		this._inventorySpacesReadOnlyTableModel = this._getInventorySpacesReadOnlyTableModel();
+        $fieldset.append(this._getTable(this._inventorySpacesReadOnlyTableModel));
 	}
 
 	this._getMainMenuItemsTableModel = function() {
@@ -340,7 +344,7 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 	}
 
 	this._getInventorySpacesTableModel = function() {
-		// Not accessible spaces are known on the config anyway, hidding them now will not increase security
+		// Not accessible spaces are known on the config anyway, hiding them now will not increase security
 		// Removing them can bring more issues so better keep them
 		
 		var spacesOptions = this._settingsFormController.getInventorySpacesOptions();
@@ -361,6 +365,28 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 		});
 	}
 
+    this._getInventorySpacesReadOnlyTableModel = function() {
+		// Not accessible spaces are known on the config anyway, hiding them now will not increase security
+		// Removing them can bring more issues so better keep them
+
+		var spacesOptions = this._settingsFormController.getInventorySpacesReadOnlyOptions();
+			spacesOptions = JSON.parse(JSON.stringify(spacesOptions));
+		var initialValues = this._profileToEdit.inventorySpacesReadOnly.filter(space => space != null);
+
+		for(var i = 0; i < initialValues.length; i++) {
+			if($.inArray(initialValues[i], spacesOptions) === -1) {
+				spacesOptions.push(initialValues[i]);
+			}
+		}
+
+		return this._getSingleColumnDropdownTableModel({
+			columnName : "Space Read only",
+			placeholder : "select space",
+			options : spacesOptions,
+			initialValues : initialValues,
+		});
+	}
+
 	//
 	// dataset types for filenames
 	//
-- 
GitLab