From f828653ad4b50adb4b388cd971ea213337bfb65b Mon Sep 17 00:00:00 2001
From: Juan Fuentes <juanf@bs-mbpr28.d.ethz.ch>
Date: Fri, 9 Aug 2019 13:01:14 +0200
Subject: [PATCH] SSDM-8587 : Custom widget settings now shown on the settings
 page

---
 .../eln-lims/html/js/config/ELNDictionary.js  |  4 ++
 .../eln-lims/html/js/server/ServerFacade.js   | 26 +++++++-
 .../SettingsForm/SettingsFormController.js    | 26 ++++----
 .../views/SettingsForm/SettingsFormModel.js   |  1 +
 .../js/views/SettingsForm/SettingsFormView.js | 61 ++++++++++++++++++-
 5 files changed, 104 insertions(+), 14 deletions(-)

diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/ELNDictionary.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/ELNDictionary.js
index ad6abe53af5..46bd617cda7 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/ELNDictionary.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/ELNDictionary.js
@@ -36,6 +36,10 @@ ELNDictionary.settingsView = {
 			title : "Main Menu",
 			info : "These options give the opportunity to the administrator to show/hide different sections of the user interface from the main menu.",
 		},
+		customWidgets : {
+		    title : "Custom Widgets",
+            info : "Assign custom widgets to properties!",
+		},
 		forcedDisableRTF : {
 			title : "Forced Disable RTF",
 			info : "By default all MULTILINE_VARCHAR properties have RTF. Use this section to disable the RTF on specific properties.",
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
index 66e2a02e0ed..54e465edf47 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/server/ServerFacade.js
@@ -67,7 +67,31 @@ function ServerFacade(openbisServer) {
 	}
 	
 	this.openbisServer.setResponseInterceptor(responseInterceptor);
-	
+
+	//
+	// Custom Widget Settings
+	//
+	this.getCustomWidgetSettings = function(doneCallback) {
+        require([ "openbis", "as/dto/property/search/PropertyTypeSearchCriteria", "as/dto/property/fetchoptions/PropertyTypeFetchOptions" ],
+        function(openbis, PropertyTypeSearchCriteria, PropertyTypeFetchOptions) {
+            var ptsc = new PropertyTypeSearchCriteria();
+            var ptfo = new PropertyTypeFetchOptions();
+            mainController.openbisV3.searchPropertyTypes(ptsc, ptfo).done(function(searchResult) {
+                var customWidgetProperties = [];
+                for(var oIdx = 0; oIdx < searchResult.totalCount; oIdx++) {
+                    var propertyType = searchResult.objects[oIdx];
+                    if(propertyType.metaData["custom_widget"]) {
+                        customWidgetProperties.push({
+                            "Property Type" : propertyType.code,
+                            "Widget" : propertyType.metaData["custom_widget"]
+                        });
+                    }
+                }
+                doneCallback(customWidgetProperties);
+            });
+        });
+    }
+
 	//
 	// Display Settings
 	//
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 526ebc24ddd..df11a741831 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
@@ -21,17 +21,21 @@ function SettingsFormController(mainController, settingsSample, mode) {
 	this._settingsManager = new SettingsManager(this._mainController.serverFacade);
 
 	this.init = function(views) {
-		var runningProfile = jQuery.extend(true, {}, profile);
-		var profileToEdit = null;
-		
-		if(settingsSample.properties["$ELN_SETTINGS"]) {
-			profileToEdit = JSON.parse(settingsSample.properties["$ELN_SETTINGS"]);
-		} else {
-			profileToEdit = runningProfile;
-		}
-		
-		this._settingsManager.applySettingsToProfile(profileToEdit, runningProfile);
-		this._settingsFormView.repaint(views, runningProfile);
+	    var _this = this;
+	    mainController.serverFacade.getCustomWidgetSettings(function(customWidgetSettings) {
+	        var runningProfile = jQuery.extend(true, {}, profile);
+            var profileToEdit = null;
+
+            if(settingsSample.properties["$ELN_SETTINGS"]) {
+                profileToEdit = JSON.parse(settingsSample.properties["$ELN_SETTINGS"]);
+            } else {
+                profileToEdit = runningProfile;
+            }
+
+            _this._settingsManager.applySettingsToProfile(profileToEdit, runningProfile);
+            _this._settingsFormView.repaint(views, runningProfile);
+            _this._settingsFormModel.customWidgetSettings = customWidgetSettings;
+	    });
 	}
 
 	this.save = function(settings) {
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormModel.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormModel.js
index 26ec6ab3a9d..455d82cd9db 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormModel.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/SettingsForm/SettingsFormModel.js
@@ -17,4 +17,5 @@
 function SettingsFormModel(settingsSample, mode) {
 	this.settingsSample = settingsSample;
 	this.mode = mode;
+	this.customWidgetSettings = [];
 }
\ No newline at end of file
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 d620792fc50..f4681e457ff 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
@@ -22,6 +22,7 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 
 	this._mainMenuItemsTableModel = null;
 	this._forcedDisableRTFTableModel = null;
+	this._customWidgetsTableModel = null;
 	this._forcedMonospaceTableModel = null;
 	this._inventorySpacesTableModel = null;
 	this._sampleTypeDefinitionsMiscellaneousSettingsTableModels = {}; // key: sample type; value: table model
@@ -81,6 +82,7 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 			this._paintMainMenuSection($formColumn, texts.mainMenu);
 			this._paintStoragesSection($formColumn, texts.storages);
 			this._paintTemplateSection($formColumn, texts.templates);
+			this._paintCustomWidgetsSection($formColumn, texts.customWidgets);
 			this._paintForcedDisableRtfSection($formColumn, texts.forcedDisableRTF);
 			this._paintForcedMonospaceSection($formColumn, texts.forceMonospaceFont);
 			this._paintInventorySpacesSection($formColumn, texts.inventorySpaces);
@@ -170,10 +172,11 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 		var $fieldset = this._getFieldset($container, text.title, "settings-section-templates");
 		$fieldset.append(FormUtil.getInfoText(text.info));
 
-		var $gridContainer = $("<div>");
+		var $dropdownContainer = $("<p>");
 	    var $dropDownMenu = $("<span>", { class : 'dropdown' });
         var $caret = $("<a>", { 'href' : '#', 'data-toggle' : 'dropdown', class : 'dropdown-toggle btn btn-default'}).append("Operations ").append($("<b>", { class : 'caret' }));
         var $list = $("<ul>", { class : 'dropdown-menu', 'role' : 'menu', 'aria-labelledby' :'sampleTableDropdown' });
+        $dropdownContainer.append($dropDownMenu);
         $dropDownMenu.append($caret);
         $dropDownMenu.append($list);
 
@@ -185,7 +188,9 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
         });
         $list.append($createSampleOption);
 
-        $fieldset.append($dropDownMenu);
+        $fieldset.append($dropdownContainer);
+
+        var $gridContainer = $("<div>");
 		$fieldset.append($gridContainer);
 
 		var advancedSampleSearchCriteria = {
@@ -217,6 +222,14 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 		$fieldset.append(this._getTable(this._forcedDisableRTFTableModel));
 	}
 
+    this._paintCustomWidgetsSection = function($container, text) {
+    		var $fieldset = this._getFieldset($container, text.title, "settings-section-custom-widgets");
+    		$fieldset.append(FormUtil.getInfoText(text.info));
+    		this._customWidgetsTableModel = this._getCustomWidgetsTableModel();
+    		$fieldset.append(this._getTable(this._customWidgetsTableModel));
+    }
+
+
 	this._paintForcedMonospaceSection = function($container, text) {
 		var $fieldset = this._getFieldset($container, text.title, "settings-section-monospace");
 		$fieldset.append(FormUtil.getInfoText(text.info));
@@ -277,6 +290,50 @@ function SettingsFormView(settingsFormController, settingsFormModel) {
 		});
 	}
 
+	this._getCustomWidgetsTableModel = function() {
+        return this._getDoubleColumnDropdownTableModel({
+            columnName : "Property Type",
+    	    placeholder : "select property type",
+    	    options : profile.allPropertyTypes.map(function(_) { return _.code; })
+        }, {
+    	    columnName : "Widget",
+            placeholder : "select widget",
+            options : ["Default", "Word Processor", "Spreadsheet"]
+        }, this._settingsFormModel.customWidgetSettings);
+        // [{ "Property Type" : "$NAME", "Widget" : "Word Processor" }]
+    }
+
+	this._getDoubleColumnDropdownTableModel = function(params, paramsB, initialValues) {
+		var tableModel = this._getTableModel();
+		tableModel.dynamicRows = true;
+		// define columns
+		tableModel.columns = [{ label : params.columnName }, { label : paramsB.columnName }];
+		tableModel.rowBuilders[params.columnName] = function(rowData) {
+			return FormUtil.getDropdown(params.options.map(function(option) {
+				return {
+					label : Util.getDisplayNameFromCode(option),
+					value : option,
+					selected : option === rowData[params.columnName],
+				};
+			}), params.placeholder);
+		}
+		tableModel.rowBuilders[paramsB.columnName] = function(rowData) {
+            return FormUtil.getDropdown(paramsB.options.map(function(option) {
+        	    return {
+        		    label : option,
+        		    value : option,
+        		    selected : option === rowData[paramsB.columnName],
+                };
+            }), paramsB.placeholder);
+        }
+		// add data
+		for (var item of initialValues) {
+			tableModel.addRow(item);
+		}
+
+		return tableModel;
+	}
+
 	this._getForcedMonospaceTableModel = function() {
 		return this._getSingleColumnDropdownTableModel({
 			columnName : "Property Type",
-- 
GitLab