Mercurial > hg > LGSearch
annotate 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 |
rev | line source |
---|---|
6
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
1 |
0
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
2 function initSection(){ //Hide sections if there are more than 5 |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
3 $("td div.section").hide(); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
4 for(var i=1; i<=5; i++){ |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
5 $("td div.section:nth-child("+i+")").show(); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
6 } |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
7 $("td:has(div)").each(function(){ |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
8 console.log($(this).children().length); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
9 if($(this).children().length<=5){ |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
10 return; |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
11 } |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
12 var last=$(this).children(".section").last(); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
13 $("<div/>").addClass("visibilityControl").html("+").insertAfter(last); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
14 }); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
15 $("td div.visibilityControl").click(function(){ |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
16 if($(this).html()=="+"){ |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
17 $(this).parent().children(".section").show(); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
18 $(this).html("-"); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
19 return; |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
20 } |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
21 if($(this).html()=="-"){ |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
22 $(this).parent().children(".section").hide(); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
23 for(var i=1; i<=5; i++){ |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
24 $(this).parent().children(".section:nth-child("+i+")").show(); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
25 } |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
26 $(this).html("+"); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
27 } |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
28 }); |
c9363a90b8b5
first commit to development server
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
29 } |
6
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
30 |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
31 function submitenter(e) |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
32 { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
33 var keycode; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
34 if (window.event) keycode = window.event.keyCode; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
35 else if (e) keycode = e.which; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
36 else return true; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
37 |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
38 if (keycode == 13) { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
39 checkPassword(); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
40 return false; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
41 } else { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
42 return true; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
43 } |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
44 } |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
45 |
7
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
46 |
6
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
47 function checkPassword() { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
48 var password = document.getElementById("pass").value; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
49 $.ajax({ |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
50 url : 'search_function.php', |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
51 async : false, |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
52 type : 'POST', |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
53 data : 'func=checkPassword'+'&password='+password, |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
54 success: function (e) { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
55 }, |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
56 error: function (e) { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
57 alert("Internal error when checking password."); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
58 } |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
59 }).done(function(result) { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
60 //console.log("password checking result: "+result); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
61 if (result == 1) { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
62 closePopup(); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
63 $("#search").show(); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
64 } else { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
65 // still showing popup |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
66 alert("Wrong password!"); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
67 showPopup(); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
68 $("#search").hide(); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
69 } |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
70 }); |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
71 }; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
72 |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
73 function showPopup() { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
74 // clear previous input |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
75 if (document.getElementById("pass")) { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
76 document.getElementById("pass").value = ""; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
77 document.getElementById("popup").style.display = "block"; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
78 } |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
79 } |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
80 function closePopup() { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
81 if (document.getElementById("pass")) { |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
82 document.getElementById("popup").style.display = "none"; |
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
83 } |
7
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
84 } |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
85 |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
86 |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
87 $(document).ready(function(){ |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
88 highlightKeywords(); |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
89 }) |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
90 |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
91 function highlightKeywords() // highlight keywords in content column, with class="content" |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
92 { |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
93 if(!$("#keywords")[0]) |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
94 return; |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
95 |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
96 var keywords = $("#keywords")[0].innerHTML; |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
97 var keywordsArray = keywords.split(", "); |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
98 //console.log("keywordsArray: "+keywordsArray); |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
99 |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
100 var content = $(".content"); |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
101 for (var i = 0; i < content.length; i++) { |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
102 // find keywords in content[i] |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
103 var text = content[i].innerHTML; |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
104 for (var j = 0; j < keywordsArray.length; j++) { |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
105 var index = text.indexOf(keywordsArray[j]); |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
106 if (index >= 0) { |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
107 text = text.substring(0,index) + "<span class='highlight'>" + text.substring(index, index+keywordsArray[j].length) + "</span>" + text.substring(index+keywordsArray[j].length); |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
108 content[i].innerHTML = text; |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
109 } |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
110 }; |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
111 |
23dcd1b5e9c4
add keywords highlighting on searching result
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
112 }; |
6
4b9ae7d500f9
add example page, modify password popup
Zoe Hong <zhong@mpiwg-berlin.mpg.de>
parents:
0
diff
changeset
|
113 } |