search.js 6.53 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
Patrick's avatar
Patrick committed
13
    keys: ["item.title", "item.keywords", "item.authors.lastName", "item.authors.firstName"
Joe TS Dell's avatar
update    
Joe TS Dell committed
14
    ]
Patrick's avatar
Patrick committed
15
}
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
16
17
18

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

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
24
25
26
27
28
29
30
31
32
33
34

    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
35
36

function search_paper() {
Patrick's avatar
Patrick committed
37
    const fuse = new Fuse(stuff_paper, options_paper);
Joe TS Dell's avatar
update    
Joe TS Dell committed
38
    searchanswer_paper = fuse.search(document.getElementById("search-input_paper").value)
Patrick's avatar
Patrick committed
39
    document.getElementById("nextblock").style.display = "none";
Patrick's avatar
Patrick committed
40
41
42
43
44
45
46
47
48
49
    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
50
51
52
//get json
// --- can be adapted if we load the content from the json
var stuff = []
Patrick's avatar
Patrick committed
53
var stuff_paper = []
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
54
55
56
57
58
59
60
61
62
63
64
// $.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
65
66
67
68




Joe TS Dell's avatar
update    
Joe TS Dell committed
69
70
$(document).ready(function () {
    $.getJSON("./content/content.json", function (result) {
Joe TS Dell's avatar
update    
Joe TS Dell committed
71
        console.log("content.json ==> HTML")
Joe TS Dell's avatar
update    
Joe TS Dell committed
72
73
74
75
76
77
78
79
        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
        }
Patrick's avatar
Patrick committed
80

Joe TS Dell's avatar
update    
Joe TS Dell committed
81
82
83
84
85
86
87
88
89
90
91
        $.getJSON("./content/team.json", function(result){ 
            console.log("team.json ==> HTML")
            team = result;
            var new_row = document.getElementById("team-section")
            new_row.innerHTML = "";
            var arrayLength = team.length;
            for (var i = 0; i < arrayLength; i++) {
                addTeam(team[i]);
                //Do something
            }
        });
Patrick's avatar
Patrick committed
92
        changeLang();
Joe TS Dell's avatar
update    
Joe TS Dell committed
93
    });
Joe TS Dell's avatar
up    
Joe TS Dell committed
94
95
    var currentYear = new Date().getFullYear();
    document.getElementById('current-year').innerHTML = currentYear;   
Joe TS Dell's avatar
update    
Joe TS Dell committed
96

Joe TS Dell's avatar
update    
Joe TS Dell committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    // 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
    //     }
    // });

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

Joe TS Dell's avatar
up    
Joe TS Dell committed
116
    
Patrick's avatar
Patrick committed
117
118
119

});
function changeLang () {
Joe TS Dell's avatar
update    
Joe TS Dell committed
120
    var userLang = navigator.language || navigator.userLanguage;
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
121
    console.log("The language is: " + userLang);
Joe TS Dell's avatar
update    
Joe TS Dell committed
122
    if (userLang.includes("de")) {
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
123
124
125
126
127
        console.log(true)
        var select1 = document.getElementById('selectpicker1');
        console.log(select1)
        select1.value = "2";
        select1.dispatchEvent(new Event('change'));
Patrick's avatar
Patrick committed
128
129
130
131
132
    } else {
        var select1 = document.getElementById('selectpicker1');
        console.log(select1)
        select1.value = "1";
        select1.dispatchEvent(new Event('change'));
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
133
    }
Patrick's avatar
Patrick committed
134
}
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
135
136
137
138
139
140
141
142
143

// 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
144
function searchToggle(obj, evt) {
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
145
146
    console.log("arrive")
    var container = $(obj).closest('.search-wrapper');
Joe TS Dell's avatar
update    
Joe TS Dell committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    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
161
        }
Joe TS Dell's avatar
update    
Joe TS Dell committed
162
163
164
165
166
167
        container.find('.search-input').val('');
    } else {
        console.log("search")
        search();
        // addcontent();
    }
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
168
}
Joe TS Dell's avatar
update    
Joe TS Dell committed
169
170

function searchToggle_paper(obj, evt) {
Patrick's avatar
Patrick committed
171
172
    console.log("arrive")
    var container = $(obj).closest('.search-wrapper');
Joe TS Dell's avatar
update    
Joe TS Dell committed
173
174
175
176
177
178
179
180
181
182
183
184
    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++) {
Patrick's avatar
Patrick committed
185
186
            document.getElementById("nextblock").style.display = "block";
            if (i <= max_paper_list) {
Joe TS Dell's avatar
update    
Joe TS Dell committed
187
                addpaper(stuff_paper[i]);
Patrick's avatar
Patrick committed
188
            }
Joe TS Dell's avatar
update    
Joe TS Dell committed
189
190

            //Do something
Patrick's avatar
Patrick committed
191
        }
Joe TS Dell's avatar
update    
Joe TS Dell committed
192
193
194
195
196
197
        container.find('#search-input_paper').val('');
    } else {
        console.log("search")
        search_paper();
        // addcontent();
    }
Patrick's avatar
Patrick committed
198
}
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
199
200
201

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

Joe TS Dell's avatar
update    
Joe TS Dell committed
206
207
        document.getElementById("search-button").click();
    }
Patrick's avatar
Patrick committed
208
209
});

Patrick's avatar
Patrick committed
210
211
var input2 = document.getElementById("search-input_paper");
input2.addEventListener("keyup", function (event) {
Joe TS Dell's avatar
update    
Joe TS Dell committed
212
213
    if (event.keyCode === 13) {
        event.preventDefault();
Patrick's avatar
Patrick committed
214

Joe TS Dell's avatar
update    
Joe TS Dell committed
215
216
217
        document.getElementById("search-button_paper").click();
    }
});