search.js 1.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// search options
// --- needs to be adapted to the form of our content
const options = {
    includeScore: true,
    // Search in `author` and in `tags` array
    keys: ["item.title",
    "item.author.firstName", "item.author.lastName"]
}

// start search
var searchanswer
function search(){
    const fuse  = new Fuse(stuff, options);
    searchanswer = fuse.search(document.getElementById("search-input").value) 

    alert(searchanswer);
}
 
//get json
// --- can be adapted if we load the content from the json
var stuff = []
$(document).ready(function(){
    $.getJSON("./content/content.json", function(result){
    stuff = result;
    });
});


// searchbar and start search
function searchToggle(obj, evt){
    console.log("arrive")
    var container = $(obj).closest('.search-wrapper');
        if(!container.hasClass('active')){
            container.addClass('active');
            evt.preventDefault();
            console.log("first")
        }
        else if(container.hasClass('active') && $(obj).closest('.input-holder').length == 0){
            container.removeClass('active');
            // clear input
            console.log("second")
            container.find('.search-input').val('');
        } else {
            console.log("search")
            search();
        }
}

// search on enter
var input = document.getElementById("search-input");
input.addEventListener("keyup", function(event) {
  if (event.keyCode === 13) {
   event.preventDefault();

   document.getElementById("search-button").click();
  }
});