From 0e9a8f33daa9fe3db5632f73cecfbe0b878d5084 Mon Sep 17 00:00:00 2001
From: Yves Noirjean <yves.noirjean@id.ethz.ch>
Date: Fri, 9 Nov 2018 15:34:54 +0100
Subject: [PATCH] SSDM-5305: added button for clearing saved search selection;
 handling hidden field hack properly; handling multiple rules correctly

---
 .../AdvancedSearchController.js               | 12 ++-
 .../AdvancedSearch/AdvancedSearchModel.js     | 12 ++-
 .../AdvancedSearch/AdvancedSearchView.js      | 86 +++++++++++++------
 3 files changed, 81 insertions(+), 29 deletions(-)

diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchController.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchController.js
index 25b7daad6b4..1b08ef8f112 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchController.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchController.js
@@ -227,7 +227,7 @@ function AdvancedSearchController(mainController, forceSearch) {
 	this.selectSavedSearch = function(selcetedSavedSearchIndex) {
 		var savedSearch = this._advancedSearchModel.savedSearches[selcetedSavedSearchIndex];
 		this._advancedSearchModel.selcetedSavedSearchIndex = selcetedSavedSearchIndex;
-		this._advancedSearchModel.criteria = savedSearch.criteria;
+		this._advancedSearchModel.criteria = this._clone(savedSearch.criteria);
 		this._advancedSearchModel.forceLoadCriteria = true;
 		this._advancedSearchView.repaintContent();
 	}
@@ -271,6 +271,7 @@ function AdvancedSearchController(mainController, forceSearch) {
 			}, function(success) {
 				if (success) {
 					Util.showSuccess('Search updated.');
+					savedSearch.criteria = _this._clone(_this._advancedSearchModel.criteria);
 				}
 				callback();
 			});
@@ -296,6 +297,11 @@ function AdvancedSearchController(mainController, forceSearch) {
 		);
 	}
 
+	this.clearSelection = function() {
+		this._advancedSearchModel.selcetedSavedSearchIndex = -1;
+		this._advancedSearchView.repaintContent();
+	}
+
 	this._loadSavedSearches = function(callback) {
 		var _this = this;
 		this._mainController.serverFacade.searchSamplesV3('SEARCH_QUERY', function(result) {
@@ -399,4 +405,8 @@ function AdvancedSearchController(mainController, forceSearch) {
 			}
 		});
 	}
+
+	this._clone = function(object) {
+		return JSON.parse(JSON.stringify(object));
+	}
 }
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchModel.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchModel.js
index 0bc09d82001..e75a6608e04 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchModel.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchModel.js
@@ -47,7 +47,17 @@ function AdvancedSearchModel(forceSearch) {
 	this.setEntityKind = function(entityKind) {
 		this.criteria.entityKind = entityKind;
 	}
-	
+
+	// a hidden object type rule is used for the "Search For" dropdown
+	this.getHiddenRule = function() {
+		for(var ruleKey in this.criteria.rules) {
+			if (this.criteria.rules[ruleKey].hidden) {
+				return this.criteria.rules[ruleKey];
+			}
+		}
+		return null;
+	}
+
 	this.resetModel = function(entityKind) {
 		this.criteria.entityKind = entityKind;
 		this.criteria.rules = {};
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchView.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchView.js
index 643c801d6f8..15505da7ca0 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchView.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/views/AdvancedSearch/AdvancedSearchView.js
@@ -21,6 +21,7 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 	this._$andOrDropdownComponent = null;
 	this._$menuPanelContainer = null;
 	this._$searchCriteriaPanelContainer = null;
+	this._$rulesPanelContainer = null;
 	this._$tbody = null;
 	this._$dataGridContainer = null;
 	this._$saveLoadContainer = null;
@@ -67,19 +68,28 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 		//Triggers Layout refresh
 		$container.append($mainPanel);
 		
-		if(this._advancedSearchModel.forceLoadCriteria) {
-			this._$entityTypeDropdown.val(this._advancedSearchModel.criteria.entityKind);
-			this._$andOrDropdownComponent.val(this._advancedSearchModel.criteria.logicalOperator);
-			this._advancedSearchModel.forceLoadCriteria = undefined;
-		}
-		
+		this._updateEntityTypeAndUsingDropdownValues();
 	}
 
 	this.repaintContent = function() {
 		this._paintSaveLoadPanel(this._$saveLoadContainer);
 		this._paintCriteriaPanel(this._$searchCriteriaPanelContainer);
+		this._updateEntityTypeAndUsingDropdownValues();
 	}	
 
+	this._updateEntityTypeAndUsingDropdownValues = function() {
+		if(this._advancedSearchModel.forceLoadCriteria) {
+			var hiddenRule = this._advancedSearchModel.getHiddenRule();
+			if (hiddenRule) {
+				this._$entityTypeDropdown.val('SAMPLE$' + hiddenRule.value);
+			} else {
+				this._$entityTypeDropdown.val(this._advancedSearchModel.criteria.entityKind);
+			}
+			this._$andOrDropdownComponent.val(this._advancedSearchModel.criteria.logicalOperator);
+			this._advancedSearchModel.forceLoadCriteria = undefined;
+		}
+	}
+
 	//
 	// Repaint Panels Methods
 	//
@@ -176,6 +186,13 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 			theme: "bootstrap"
 		});
 
+		if (_this._advancedSearchModel.selcetedSavedSearchIndex > -1) {
+			var $buttonClear = FormUtil.getButtonWithIcon('glyphicon-remove', function() {
+				_this._advancedSearchController.clearSelection();
+			}, null, 'Clear selection');
+			$container.append($buttonClear);	
+		}
+
 		var $buttonSave = FormUtil.getButtonWithIcon('glyphicon-floppy-disk', function() {
 			_this._save();
 		}, 'Save');
@@ -227,6 +244,15 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 
 		this._paintTypeSelectionPanel(this._$searchCriteriaPanelContainer);
 
+		// TODO put the rest in separate panel
+		this._$rulesPanelContainer = $("<div>");
+		this._paintRulesPanel(this._$rulesPanelContainer);
+		$searchCriteriaPanelContainer.append(this._$rulesPanelContainer);
+	}
+
+
+	this._paintRulesPanel = function($container) {
+		$container.empty();
 		var _this = this;
 		var $table = $("<table>", { class : "table table-bordered"});
 		$table.css({ 'margin-top': '10px' });
@@ -250,24 +276,31 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 						.append($("<th>").text("Field Value"))
 						.append($("<th>", { "style" : "width : 56px !important;" }).append(this._$addButton))
 					);
-		
-		this._paintInputRow();
-		
-		$searchCriteriaPanelContainer.append($table);
+
+		if ($.isEmptyObject(this._advancedSearchModel.criteria.rules)) {
+				this._paintInputRow();
+		} else {
+			for(var ruleKey in this._advancedSearchModel.criteria.rules) {
+				if ( ! this._advancedSearchModel.criteria.rules[ruleKey].hidden) {
+					this._paintInputRow(ruleKey);
+				}
+			}
+		}
+
+		$container.append($table);
 	}
 	
 	//
 	// Auxiliar Components Methods
 	//
-	
-	this._paintInputRow = function() {
+
+	// paints a row for the given ruleKey or a new empty row if no ruleKey is given
+	this._paintInputRow = function(ruleKey) {
 		var _this = this;
-		
-		if(this._advancedSearchModel.forceLoadCriteria) {
-			var uuidValue = null;
-			for(var ruleKey in this._advancedSearchModel.criteria.rules) {
-				uuidValue = ruleKey;
-			}
+
+		var uuidValue = null;
+		if (ruleKey) {
+			uuidValue = ruleKey;
 		} else {
 			var uuidValue = Util.guid();
 			this._advancedSearchModel.criteria.rules[uuidValue] = { };
@@ -313,13 +346,11 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 		}
 		
 		if(this._advancedSearchModel.forceLoadCriteria) {
-			for(var ruleKey in this._advancedSearchModel.criteria.rules) {
-				var rule = this._advancedSearchModel.criteria.rules[ruleKey];
-				$fieldTypeDropdown.val(rule.type).change();
-				$fieldValue.val(rule.value);
-				var $fieldNameDropdown = $($newFieldNameContainer.children()[0]);
-				$fieldNameDropdown.val(rule.name);
-			}
+			var rule = this._advancedSearchModel.criteria.rules[uuidValue];
+			$fieldTypeDropdown.val(rule.type).change();
+			$fieldValue.val(rule.value);
+			var $fieldNameDropdown = $($newFieldNameContainer.children()[0]);
+			$fieldNameDropdown.val(rule.name);
 		}
 	}
 	
@@ -631,7 +662,7 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 				}				
 			} else {
 				_this._advancedSearchModel.resetModel(kindAndType[0]); //Restart model
-				_this._paintCriteriaPanel(_this._$searchCriteriaPanelContainer); //Restart view	
+				_this._paintRulesPanel(_this._$rulesPanelContainer);
 			}
 			
 			if(kindAndType.length === 2) {
@@ -640,6 +671,7 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 				_this._advancedSearchModel.criteria.rules[uuidValue].type = 'Attribute';
 				_this._advancedSearchModel.criteria.rules[uuidValue].name = 'ATTR.SAMPLE_TYPE';
 				_this._advancedSearchModel.criteria.rules[uuidValue].value = kindAndType[1];
+				_this._advancedSearchModel.criteria.rules[uuidValue].hidden = true;
 				_this._advancedSearchModel.isSampleTypeForced = true;
 			} else {
 				_this._advancedSearchModel.isSampleTypeForced = false;
@@ -897,4 +929,4 @@ function AdvancedSearchView(advancedSearchController, advancedSearchModel) {
 			return dataGrid;
 	}
 	
-}
\ No newline at end of file
+}
-- 
GitLab