Browse Source

second test and example of how to use lunr.js in ratchet

better style und better marking of found list-items
pull/160/head
Simon Waldherr 12 years ago
parent
commit
3bbc3feba3
  1. 12
      dist/ratchet.css
  2. 47
      dist/ratchet.js
  3. 12
      lib/css/bars.css
  4. 47
      lib/js/lunr.js

12
dist/ratchet.css vendored

@ -489,6 +489,18 @@ a {
.bar-standard input[type=search] { .bar-standard input[type=search] {
height: 32px; height: 32px;
margin: 0; margin: 0;
outline: none;
-webkit-transition: -webkit-box-shadow 0.3s;
-moz-transition: -moz-box-shadow 0.3s;
-o-transition: -o-box-shadow 0.3s;
transition: box-shadow 0.3s;
}
.bar-standard input[type=search]:focus,
.bar-standard input[type=search]:active {
-webkit-box-shadow: 0px 0px 5px 1px #4B8CF7;
-moz-box-shadow: 0px 0px 5px 1px #4B8CF7;
-o-box-shadow: 0px 0px 5px 1px #4B8CF7;
box-shadow: 0px 0px 5px 1px #4B8CF7;
}/* Lists }/* Lists
-------------------------------------------------- */ -------------------------------------------------- */

47
dist/ratchet.js vendored

@ -1573,6 +1573,10 @@ lunr.TokenStore.prototype.expand = function (token, memo) {
return memo; return memo;
} }
/*!
* lunr.ratchetintegration
* Copyright (C) 2013 Simon Waldherr
*/
var searchindexes = new Array(); var searchindexes = new Array();
@ -1593,7 +1597,6 @@ function indexList(listid, searchid) {
if(typeof listeles[i].innerText !== 'undefined') { if(typeof listeles[i].innerText !== 'undefined') {
if(listeles[i].innerText.length > 2) { if(listeles[i].innerText.length > 2) {
text = {"text" : listeles[i].innerText, "id" : i} text = {"text" : listeles[i].innerText, "id" : i}
//console.log(listeles[i].innerText);
searchindexes[lunrid].add(text); searchindexes[lunrid].add(text);
} }
} }
@ -1606,25 +1609,37 @@ function searchList(listid, searchid, searchstring) {
var lunrid = document.getElementById(searchid).getAttribute('data-lid'), var lunrid = document.getElementById(searchid).getAttribute('data-lid'),
listeles = document.getElementById(listid).getElementsByTagName('li'), listeles = document.getElementById(listid).getElementsByTagName('li'),
found = searchindexes[lunrid].search(searchstring), found = searchindexes[lunrid].search(searchstring),
foundbool; foundbool, score;
for(var i = 0; i < listeles.length; i++) { for(var i = 0; i < listeles.length; i++) {
foundbool = false; if(searchstring.length > 1) {
for(var j = 0; j < found.length; j++) { foundbool = false;
if(parseInt(found[j].ref) === i) { score = 0;
foundbool = true; for(var j = 0; j < found.length; j++) {
if(parseInt(found[j].ref) === i) {
foundbool = true;
score = found[j].score;
}
} }
} if(listeles[i].hasAttribute('data-noindex') !== true) {
if(listeles[i].hasAttribute('data-noindex') !== true) { if(foundbool) {
if(foundbool) { if(score > 0.3) {
listeles[i].style.backgroundColor = '#e1dac7'; listeles[i].style.opacity = 1;
//listeles[i].style.opacity = 1; } else {
listeles[i].style.display = 'list-item'; listeles[i].style.opacity = 0.5;
} else { }
listeles[i].style.backgroundColor = '#f5bca3'; //listeles[i].style.backgroundColor = '#e1dac7';
//listeles[i].style.opacity = 0.5; //listeles[i].style.opacity = 1;
listeles[i].style.display = 'none'; listeles[i].style.display = 'list-item';
} else {
//listeles[i].style.backgroundColor = '#f5bca3';
//listeles[i].style.opacity = 0.5;
listeles[i].style.display = 'none';
}
} }
} else {
listeles[i].style.opacity = 1;
listeles[i].style.display = 'list-item';
} }
} }
} }

12
lib/css/bars.css

@ -329,4 +329,16 @@
.bar-standard input[type=search] { .bar-standard input[type=search] {
height: 32px; height: 32px;
margin: 0; margin: 0;
outline: none;
-webkit-transition: -webkit-box-shadow 0.3s;
-moz-transition: -moz-box-shadow 0.3s;
-o-transition: -o-box-shadow 0.3s;
transition: box-shadow 0.3s;
}
.bar-standard input[type=search]:focus,
.bar-standard input[type=search]:active {
-webkit-box-shadow: 0px 0px 5px 1px #4B8CF7;
-moz-box-shadow: 0px 0px 5px 1px #4B8CF7;
-o-box-shadow: 0px 0px 5px 1px #4B8CF7;
box-shadow: 0px 0px 5px 1px #4B8CF7;
} }

47
lib/js/lunr.js

@ -1245,6 +1245,10 @@ lunr.TokenStore.prototype.expand = function (token, memo) {
return memo return memo
} }
/*!
* lunr.ratchetintegration
* Copyright (C) 2013 Simon Waldherr
*/
var searchindexes = new Array(); var searchindexes = new Array();
@ -1265,7 +1269,6 @@ function indexList(listid, searchid) {
if(typeof listeles[i].innerText !== 'undefined') { if(typeof listeles[i].innerText !== 'undefined') {
if(listeles[i].innerText.length > 2) { if(listeles[i].innerText.length > 2) {
text = {"text" : listeles[i].innerText, "id" : i} text = {"text" : listeles[i].innerText, "id" : i}
//console.log(listeles[i].innerText);
searchindexes[lunrid].add(text); searchindexes[lunrid].add(text);
} }
} }
@ -1278,25 +1281,37 @@ function searchList(listid, searchid, searchstring) {
var lunrid = document.getElementById(searchid).getAttribute('data-lid'), var lunrid = document.getElementById(searchid).getAttribute('data-lid'),
listeles = document.getElementById(listid).getElementsByTagName('li'), listeles = document.getElementById(listid).getElementsByTagName('li'),
found = searchindexes[lunrid].search(searchstring), found = searchindexes[lunrid].search(searchstring),
foundbool; foundbool, score;
for(var i = 0; i < listeles.length; i++) { for(var i = 0; i < listeles.length; i++) {
foundbool = false; if(searchstring.length > 1) {
for(var j = 0; j < found.length; j++) { foundbool = false;
if(parseInt(found[j].ref) === i) { score = 0;
foundbool = true; for(var j = 0; j < found.length; j++) {
if(parseInt(found[j].ref) === i) {
foundbool = true;
score = found[j].score;
}
} }
} if(listeles[i].hasAttribute('data-noindex') !== true) {
if(listeles[i].hasAttribute('data-noindex') !== true) { if(foundbool) {
if(foundbool) { if(score > 0.3) {
listeles[i].style.backgroundColor = '#e1dac7'; listeles[i].style.opacity = 1;
//listeles[i].style.opacity = 1; } else {
listeles[i].style.display = 'list-item'; listeles[i].style.opacity = 0.5;
} else { }
listeles[i].style.backgroundColor = '#f5bca3'; //listeles[i].style.backgroundColor = '#e1dac7';
//listeles[i].style.opacity = 0.5; //listeles[i].style.opacity = 1;
listeles[i].style.display = 'none'; listeles[i].style.display = 'list-item';
} else {
//listeles[i].style.backgroundColor = '#f5bca3';
//listeles[i].style.opacity = 0.5;
listeles[i].style.display = 'none';
}
} }
} else {
listeles[i].style.opacity = 1;
listeles[i].style.display = 'list-item';
} }
} }
} }

Loading…
Cancel
Save