Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagehtml/xml
titleadvanced.jsp
collapsetrue
...
<%
 //Add this code on top section
 //Generate JSPUI Solr path url
 String scheme = request.getScheme();             // http
 String serverName = request.getServerName();     //request.getServerName();     // hostname.com
 int serverPort = request.getServerPort();        // 80
 String parentContextPath = "";                   //Change this value if dspace not installed on html root folder
 String contextPath = "/jspui";
 String servletPath = "/searchJSON";
 // Reconstruct original requesting URL
 StringBuffer url =  new StringBuffer();
 url.append(scheme).append("://").append(serverName);
 if (serverPort != 80)
 url.append(":").append(serverPort);
 url.append(parenContextPath).append(contextPath).append(servletPath);
 String solrPath = url.toString();

%>
...
<!-- Add this lines after <dspace:layout> tag-->
<link type="text/css" href="<%=request.getContextPath()%>/static/js/jqueryui/css/smoothness/jquery-ui-1.8.20.custom.css" rel="stylesheet" />
<script type="text/javascript" src="<%=request.getContextPath()%>/static/js/jqueryui/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/js/jqueryui/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/static/js/autocomplete.js"></script>
...
<!-- Add onchange event call for every search textbox -->
<select name="field2" id="tfield2" onchange="JavaScript:<script type="text/javascript">
window.onload = function() {
   monkeyPatchAutocomplete();
   replaceSAXExpression('<%=solrPath%>solrPath %>', 10 ,'tfield2tfield1', 'tquery2tquery1');">
<select name="field1" id="tfield1" onchange="JavaScript:   replaceSAXExpression('<%=solrPath %>', 10,'tfield2', 'tquery2');
   replaceSAXExpression('<%=solrPath%>solrPath %>', 10 ,'tfield1tfield3', 'tquery1tquery3');">

Adding javascript functions


}
</script>
...
<!-- Add onchange event call for every search combobox -->
<select name="field1" id="tfield1" onchange="JavaScript:replaceSAXExpression('<%=solrPath%>', 10 ,'tfield1', 'tquery1');">
<select name="field2" id="tfield2" onchange="JavaScript:replaceSAXExpression('<%=solrPath%>', 10 ,'tfield2', 'tquery2');">
<select name="field3" id="tfield3" onchange="JavaScript:replaceSAXExpression('<%=solrPath%>', 10 ,'tfield3', 'tquery3');">

Adding javascript functions

Wiki Markup
It's time to create a javascript library file that will contain a pair of functions. &nbsp;First one will patch autocomplete default behavior and will bold every part of result text that match our typed words. Second one will replace SAX expression used in autocomplete textbox everytime we change select value (we must search by other index). It will reside in \[dspace-source\]/dspace-jspui/dspace-jspui-webapp/src/main/webapp/static/js/autocomplete.js
It's time to create a javascript library file that will contain a pair of functions.  First one will patch autocomplete default behavior and will bold every part of result text that match our typed words. Second one will replaceSAX expression on our texbox everytime a keyword it's typed in it, giving the autocomplete effect.

Code Block
languagejavascript
titleautocomplete.js
linenumberstrue
collapsetrue
/**
 * Functions used on advanced search page
 */
function monkeyPatchAutocomplete() {
    // don't really need this, but in case I did, I could store it and chain
    var oldFn = $.ui.autocomplete.prototype._renderItem;

    $.ui.autocomplete.prototype._renderItem = function( ul, item){
    	var term = this.term.split(' ').join('|');
    	var re = new RegExp("(" + term + ")", "gi") ;
    	var t = item.label.replace(re,"<b>$1</b>");
    	return $( "<li></li>" )
    	.data( "item.autocomplete", item )
    	.append( "<a>" + t + "</a>" )
    	.appendTo( ul );
    };

}

function replaceSAXExpression(baseUrl , limit ,fieldName, fieldText){
	var field = $('#'+ fieldName).val();
	$('#' + fieldText).val('');
	$('#' + fieldText).autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: baseUrl +"/terms?terms=true&terms.limit=" + limit + "&terms.sort=count&terms.regex.flag=case_insensitive&terms.fl=" + field + "&terms.regex=.*" + request.term + ".*&wt=json",
				dataType: "json",
				data: {
					style: "full",
					maxRows: 5,
					name_startsWith: request.term,
				},
				success: function( data ) {
 					response( $.map( data.terms[field], function( item ) {
						if(!$.isNumeric(item)){
							return{
    							label: item,
    							value: "\"" + item + "\"",
         					}
         				}else {
         					return null;
         				}
         			}
				));}
			});
		},
		minLength: 1,
		select: function( event, ui ) {

		},
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});
}

...

Code Block
languagehtml/xml
titleweb.xml
collapsetrue
<servlet>
        <servlet-name>searchJSON</servlet-namename>
        <servlet-class>org.dspace.app.webui.servlet.SearchJSONServlet</servlet-class>
</servlet>
<servlet-mapping>
        <servlet-name>searchJSON</servlet-name>
        <url-pattern>/searchJSON/*</url-pattern>
</servlet-mapping>