search.js 6.04 KB
Newer Older
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
1
2
3
4
5
// search options
// --- needs to be adapted to the form of our content
const options = {
    includeScore: true,
    // Search in `author` and in `tags` array
Joe TS Dell's avatar
update    
Joe TS Dell committed
6
7
8
    keys: ["item.title", "item.keywords",
        "item.author.firstName", "item.author.lastName"
    ]
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
9
}
Patrick's avatar
Patrick committed
10
11
12
const options_paper = {
    includeScore: true,
    // Search in `author` and in `tags` array
Joe TS Dell's avatar
update    
Joe TS Dell committed
13
14
15
    keys: ["item.title", "item.keywords",
        "item.author.firstName", "item.author.lastName"
    ]
Patrick's avatar
Patrick committed
16
}
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
17
18
19

// start search
var searchanswer
Patrick's avatar
Patrick committed
20
var searchanswer_paper
Joe TS Dell's avatar
update    
Joe TS Dell committed
21
22
23
24

function search() {
    const fuse = new Fuse(stuff, options);
    searchanswer = fuse.search(document.getElementById("search-input").value)
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
25
26
27
28
29
30
31
32
33
34
35

    var new_row = document.getElementById("row_main")
    new_row.innerHTML = "";
    var searchLength = searchanswer.length;
    for (var i = 0; i < searchLength; i++) {
        addcontent(searchanswer[i].item);
        //Do something
    }
    // alert(searchanswer);
    console.log(searchanswer);
}
Joe TS Dell's avatar
update    
Joe TS Dell committed
36
37
38
39

function search_paper() {
    const fuse = new Fuse(stuff_paper, options);
    searchanswer_paper = fuse.search(document.getElementById("search-input_paper").value)
Patrick's avatar
Patrick committed
40
41
42
43
44
45
46
47
48
49
50

    var new_row = document.getElementById("cont_paper")
    new_row.innerHTML = "";
    var searchLength = searchanswer_paper.length;
    for (var i = 0; i < searchLength; i++) {
        addpaper(searchanswer_paper[i].item);
        //Do something
    }
    // alert(searchanswer);
    console.log(searchanswer_paper);
}
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
51
52
53
//get json
// --- can be adapted if we load the content from the json
var stuff = []
Patrick's avatar
Patrick committed
54
var stuff_paper = []
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
55
56
57
58
59
60
61
62
63
64
65
// $.getJSON("./content/content.json", function(result){
//     stuff = result;
//     var new_row = document.getElementById("row_main")
//     new_row.innerHTML = "";
//     var arrayLength = stuff.length;
//     for (var i = 0; i < arrayLength; i++) {
//         addcontent(stuff[i]);
//         //Do something
//     }
//     // stuff.forEach(addcontent());
//     });
Sini's avatar
Sini committed
66
67
68
69




Joe TS Dell's avatar
update    
Joe TS Dell committed
70
71
72
73
74
75
76
77
78
79
80
$(document).ready(function () {
    $.getJSON("./content/content.json", function (result) {
        stuff = result;
        var new_row = document.getElementById("row_main")
        new_row.innerHTML = "";
        var arrayLength = stuff.length;
        for (var i = 0; i < arrayLength; i++) {
            addcontent(stuff[i]);
            //Do something
        }
    });
Sini's avatar
Sini committed
81

Joe TS Dell's avatar
update    
Joe TS Dell committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    // get paper content is commented out for back up
    // $.getJSON("./content/paper2.json", function (result) {
    //     stuff_paper = result;
    //     var new_row = document.getElementById("cont_paper")
    //     new_row.innerHTML = "";
    //     var arrayLength = stuff_paper.length;
    //     for (var i = 0; i < arrayLength; i++) {

    //         if (Math.abs(arrayLength - i) <= max_paper_list) {
    //             console.log("close to " + i)
    //             addpaper(stuff_paper[i]);
    //         }
    //         //Do something
    //     }
    // });

    // get paper from the Sabo result
    stuff_paper = jsonContent;
    var new_row = document.getElementById("cont_paper")
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
101
    new_row.innerHTML = "";
Joe TS Dell's avatar
update    
Joe TS Dell committed
102
    var arrayLength = stuff_paper.length;
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
103
    for (var i = 0; i < arrayLength; i++) {
Joe TS Dell's avatar
update    
Joe TS Dell committed
104
105
106
107
108

        if (Math.abs(arrayLength - i) <= max_paper_list) {
            console.log("close to " + i)
            addpaper(stuff_paper[i]);
        }
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
109
110
        //Do something
    }
Patrick's avatar
Patrick committed
111

Joe TS Dell's avatar
update    
Joe TS Dell committed
112
113
114


    var userLang = navigator.language || navigator.userLanguage;
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
115
    console.log("The language is: " + userLang);
Joe TS Dell's avatar
update    
Joe TS Dell committed
116
    if (userLang.includes("de")) {
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        console.log(true)
        var select1 = document.getElementById('selectpicker1');
        console.log(select1)
        select1.value = "2";
        select1.dispatchEvent(new Event('change'));
    }
});

// window.onload = function () {
//     var e = document.getElementById("selectpicker1");
//     e.value = 2;
//     e.dispatchEvent(new Event('change'));
// };


// searchbar and start search
Joe TS Dell's avatar
update    
Joe TS Dell committed
133
function searchToggle(obj, evt) {
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
134
135
    console.log("arrive")
    var container = $(obj).closest('.search-wrapper');
Joe TS Dell's avatar
update    
Joe TS Dell committed
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    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")
        var new_row = document.getElementById("row_main")
        new_row.innerHTML = "";
        var arrayLength = stuff.length;
        for (var i = 0; i < arrayLength; i++) {
            addcontent(stuff[i]);
            //Do something
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
150
        }
Joe TS Dell's avatar
update    
Joe TS Dell committed
151
152
153
154
155
156
        container.find('.search-input').val('');
    } else {
        console.log("search")
        search();
        // addcontent();
    }
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
157
}
Joe TS Dell's avatar
update    
Joe TS Dell committed
158
159

function searchToggle_paper(obj, evt) {
Patrick's avatar
Patrick committed
160
161
    console.log("arrive")
    var container = $(obj).closest('.search-wrapper');
Joe TS Dell's avatar
update    
Joe TS Dell committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
    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")
        var new_row = document.getElementById("cont_paper")
        new_row.innerHTML = "";
        var arrayLength = stuff_paper.length;
        for (var i = 0; i < arrayLength; i++) {
            if (Math.abs(arrayLength - i) <= 2) {
                console.log("close to " + i)
                addpaper(stuff_paper[i]);
Patrick's avatar
Patrick committed
177
            }
Joe TS Dell's avatar
update    
Joe TS Dell committed
178
179

            //Do something
Patrick's avatar
Patrick committed
180
        }
Joe TS Dell's avatar
update    
Joe TS Dell committed
181
182
183
184
185
186
        container.find('#search-input_paper').val('');
    } else {
        console.log("search")
        search_paper();
        // addcontent();
    }
Patrick's avatar
Patrick committed
187
}
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
188
189
190

// search on enter
var input = document.getElementById("search-input");
Joe TS Dell's avatar
update    
Joe TS Dell committed
191
192
193
input.addEventListener("keyup", function (event) {
    if (event.keyCode === 13) {
        event.preventDefault();
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
194

Joe TS Dell's avatar
update    
Joe TS Dell committed
195
196
        document.getElementById("search-button").click();
    }
Patrick's avatar
Patrick committed
197
198
199
});

var input = document.getElementById("search-input_paper");
Joe TS Dell's avatar
update    
Joe TS Dell committed
200
201
202
input.addEventListener("keyup", function (event) {
    if (event.keyCode === 13) {
        event.preventDefault();
Patrick's avatar
Patrick committed
203

Joe TS Dell's avatar
update    
Joe TS Dell committed
204
205
206
        document.getElementById("search-button_paper").click();
    }
});