From b95d056168c02f488bf039a8871300d1a42fad37 Mon Sep 17 00:00:00 2001
From: juanf <juanf>
Date: Wed, 1 Oct 2014 09:45:28 +0000
Subject: [PATCH] ELN UI - PAPER - Support Domain-specific searching

SVN: 32533
---
 .../newbrowser/html/js/config/Profile.js      | 21 +++++++++-
 .../newbrowser/html/js/server/ServerFacade.js |  7 ++++
 .../newbrowser/html/js/util/FormUtil.js       | 42 +++++++++++++++++++
 .../html/js/widgets/SideMenuWidget.js         | 41 ++++++++++++++++++
 4 files changed, 110 insertions(+), 1 deletion(-)

diff --git a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/config/Profile.js b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/config/Profile.js
index dd9a8ee4701..c7f6160d915 100644
--- a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/config/Profile.js
+++ b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/config/Profile.js
@@ -31,6 +31,7 @@ $.extend(DefaultProfile.prototype, {
 		// DEFAULTS, TYPICALLY DON'T TOUCH IF YOU DON'T KNOW WHAT YOU DO
 		//
 		
+		this.searchDomains = [ { "@id" : -1, "@type" : "GobalSearch", label : "Global", name : "global"}];
 		this.ELNExperiments = ["SYSTEM_EXPERIMENT"];
 		this.notShowTypes = ["SYSTEM_EXPERIMENT"];
 		this.inventorySpaces = [];
@@ -68,6 +69,10 @@ $.extend(DefaultProfile.prototype, {
 			"isEnabled" : false
 		};
 		
+		this.getSearchDomains = function() {
+			return this.searchDomains;
+		}
+		
 		this.getDefaultDataStoreCode = function() {
 			var dataStoreCode = null;
 			if(this.allDataStores.length > 0) {
@@ -493,6 +498,18 @@ $.extend(DefaultProfile.prototype, {
 			);
 		}
 		
+		this.initSearchDomains = function(callback) {
+			var _this = this;
+			this.serverFacade.listSearchDomains(function(data) {
+				if(data && data.result) {
+					for(var i = 0; i < data.result.length; i++) {
+						_this.searchDomains.push(data.result[i]);
+					}
+				}
+				callback();
+			});
+		}
+		
 		//
 		// Initializes
 		//
@@ -501,7 +518,9 @@ $.extend(DefaultProfile.prototype, {
 			
 			this.initPropertyTypes(function(){
 				_this.initVocabulariesForSampleTypes(function() {
-					callbackWhenDone();
+					_this.initSearchDomains(function() {
+						callbackWhenDone();
+					});
 				});
 			});
 		}
diff --git a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/server/ServerFacade.js b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/server/ServerFacade.js
index 835199ebf7b..72cb8f7ee08 100644
--- a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/server/ServerFacade.js
+++ b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/server/ServerFacade.js
@@ -154,6 +154,13 @@ function ServerFacade(openbisServer) {
 		}
 	}
 	
+	this.listSearchDomains = function(callbackFunction) {
+		if(this.openbisServer.listAvailableSearchDomains) {
+			this.openbisServer.listAvailableSearchDomains(callbackFunction);
+		} else {
+			callbackFunction();
+		}
+	}
 	//
 	// Others
 	//
diff --git a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/util/FormUtil.js b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/util/FormUtil.js
index 5f799655695..a201b85cd9b 100644
--- a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/util/FormUtil.js
+++ b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/util/FormUtil.js
@@ -20,6 +20,48 @@ var FormUtil = new function() {
 	// Standard Form Fields
 	//
 	
+	this.getDropDownToogleWithSelectedFeedback = function(prefixElement, labelWithEvents, isSelectedFeedback) {
+		var $dropDownToogle = $('<span>', { class : 'dropdown' });
+		if(prefixElement) {
+			$dropDownToogle.append(prefixElement);
+		}
+		$dropDownToogle.append($('<a>', { 'href' : '#', 'data-toggle' : 'dropdown', 'class' : 'dropdown-toggle btn btn-default'}).append($('<b>', { 'class' : 'caret' })));
+		
+		var $dropDownToogleOptions = $('<ul>', { class : 'dropdown-menu', 'role' : 'menu' });
+		$dropDownToogle.append($dropDownToogleOptions);
+		
+		for(var i = 0; i < labelWithEvents.length; i++) {
+			
+			var selectedFeedback = $('<span>', { 'id' : 'dropdown-' + labelWithEvents[i].id });
+			
+			if(isSelectedFeedback && i === 0) {
+				selectedFeedback.append("<span class='glyphicon glyphicon-ok'></span>");
+			}
+			
+			var $a = $('<a>', { class : '', 'title' : labelWithEvents[i].title }).append(selectedFeedback).append('&nbsp;').append(labelWithEvents[i].title);
+			
+			var clickFunction = function(labelWithEvents, selectedIndex, isSelectedFeedback) {
+				return function() {
+					if(isSelectedFeedback) {
+						for(var j = 0; j < labelWithEvents.length; j++) {
+							if(j === selectedIndex) {
+								$("#" + 'dropdown-' + labelWithEvents[j].id).append("<span class='glyphicon glyphicon-ok'></span>");
+							} else {
+								$("#" + 'dropdown-' + labelWithEvents[j].id).empty();
+							}
+						}
+					}
+					
+					labelWithEvents[selectedIndex].href();
+				};
+			}
+			
+			$a.click(clickFunction(labelWithEvents, i, isSelectedFeedback));
+			$dropDownToogleOptions.append($('<li>', { 'role' : 'presentation' }).append($a));
+		}	
+		return $dropDownToogle;
+	}
+	
 	this.getDefaultBenchDropDown = function(id, isRequired) {
 		var $storageDropDown = this.getDefaultStoragesDropDown(id, isRequired);
 		if(!$storageDropDown) {
diff --git a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/widgets/SideMenuWidget.js b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/widgets/SideMenuWidget.js
index 88034e3de67..3c14e8c493f 100644
--- a/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/widgets/SideMenuWidget.js
+++ b/plasmid/source/core-plugins/newbrowser/1/as/webapps/newbrowser/html/js/widgets/SideMenuWidget.js
@@ -393,9 +393,50 @@ function SideMenuWidget(mainController, containerId, serverFacade) {
 					.append($("<span>", { "class" : "glyphicon glyphicon-resize-horizontal" }))
 				);
 		
+		var dropDownSearch = "";
+		var searchDomains = profile.getSearchDomains();
+		
+		if(searchDomains.length > 1) {
+			//Default Selected for the prefix
+			var defaultSelected = "";
+			if(searchDomains[0].label.length > 3) {
+				defaultSelected = searchDomains[0].label.substring(0, 2) + ".";
+			} else {
+				defaultSelected = searchDomains[0].label.label;
+			}
+			
+			//Prefix function
+			var selectedFunction = function(selectedSearchDomain) {
+				return function() {
+					var $component = $("#prefix-selected-search-domain");
+					$component.empty();
+					if(selectedSearchDomain.label.length > 3) {
+						$component.append(selectedSearchDomain.label.substring(0, 2) + ".");
+					} else {
+						$component.append(selectedSearchDomain.label);
+					}
+					$component.attr('selected-name', selectedSearchDomain.name);
+				};
+			}
+			
+			//Dropdown elements
+			var dropDownComponents = [];
+			for(var i = 0; i < searchDomains.length; i++) {
+				dropDownComponents.push({
+					href : selectedFunction(searchDomains[i]),
+					title : searchDomains[i].label,
+					id : searchDomains[i].name
+				});
+			}
+			
+			dropDownSearch = FormUtil.getDropDownToogleWithSelectedFeedback($('<span>', { id : 'prefix-selected-search-domain', class : 'btn btn-default disabled', 'selected-name' :  searchDomains[0].name }).append(defaultSelected),dropDownComponents, true);
+		}
+		
 		var $searchForm = $("<li>")
 						.append($("<form>", { "class" : "navbar-form", "onsubmit" : "return false;"})
 									.append($("<input>", { "id" : "search", "type" : "text", "onkeyup" : "mainController.changeView(\"showSearchPage\", event.target.value);", "class" : "form-control search-query", "placeholder" : "Search"}))
+									.append('&nbsp;')
+									.append(dropDownSearch)
 								);
 		
 		var logoutButton = $("<a>", { "id" : "logout-button", "href" : "" }).append($("<span>", { "class" : "glyphicon glyphicon-off"}));
-- 
GitLab