view search.js @ 7:23dcd1b5e9c4 default tip

add keywords highlighting on searching result
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Mon, 16 Nov 2015 16:58:39 +0100
parents 4b9ae7d500f9
children
line wrap: on
line source


function initSection(){ //Hide sections if there are more than 5
	$("td div.section").hide();
	for(var i=1; i<=5; i++){
		$("td div.section:nth-child("+i+")").show();
	}
	$("td:has(div)").each(function(){
		console.log($(this).children().length);
		if($(this).children().length<=5){
			return;
		}
		var last=$(this).children(".section").last();
		$("<div/>").addClass("visibilityControl").html("+").insertAfter(last);
	});
	$("td div.visibilityControl").click(function(){
		if($(this).html()=="+"){
			$(this).parent().children(".section").show();
			$(this).html("-");
			return;
		}
		if($(this).html()=="-"){
			$(this).parent().children(".section").hide();
			for(var i=1; i<=5; i++){
				$(this).parent().children(".section:nth-child("+i+")").show();
			}
			$(this).html("+");
		}
	});
}

function submitenter(e)
{
	var keycode; 
	if (window.event) keycode = window.event.keyCode; 
	else if (e) keycode = e.which; 
	else return true;

	if (keycode == 13) { 
		checkPassword();
		return false; 
	} else {
		return true; 
	}
}


function checkPassword() { 
	var password = document.getElementById("pass").value;
	$.ajax({
		url : 'search_function.php',
        async : false,
	    type : 'POST',
        data : 'func=checkPassword'+'&password='+password,
        success: function (e) {
        },
        error: function (e) {
            alert("Internal error when checking password.");
        }
    }).done(function(result) {
    	//console.log("password checking result: "+result);
    	if (result == 1) {
    		closePopup();
    		$("#search").show();
    	} else {
    		// still showing popup
    		alert("Wrong password!");
    		showPopup();
    		$("#search").hide();
    	}
    });
};

function showPopup() {
	// clear previous input
	if (document.getElementById("pass")) {
		document.getElementById("pass").value = "";	
		document.getElementById("popup").style.display = "block";
	}
}
function closePopup() {
	if (document.getElementById("pass")) {
		document.getElementById("popup").style.display = "none";
	}
}


$(document).ready(function(){
	highlightKeywords();
})

function highlightKeywords()	// highlight keywords in content column, with class="content"
{	
	if(!$("#keywords")[0])
		return;

	var keywords = $("#keywords")[0].innerHTML;
	var keywordsArray = keywords.split(", ");
	//console.log("keywordsArray: "+keywordsArray);

	var content = $(".content");
	for (var i = 0; i < content.length; i++) {
		// find keywords in content[i]
		var text = content[i].innerHTML;
		for (var j = 0; j < keywordsArray.length; j++) {
			var index = text.indexOf(keywordsArray[j]);
			if (index >= 0) {
				text = text.substring(0,index) + "<span class='highlight'>" + text.substring(index, index+keywordsArray[j].length) + "</span>" + text.substring(index+keywordsArray[j].length);
				content[i].innerHTML = text;
			}		
		};

	};
}