search.js 9.34 KB
Newer Older
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
1
2
3
4
5
6
7
8
// 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.keywords",
    "item.author.firstName", "item.author.lastName"]
}
Patrick's avatar
Patrick committed
9
10
11
12
13
14
const options_paper = {
    includeScore: true,
    // Search in `author` and in `tags` array
    keys: ["item.title","item.keywords",
    "item.author.firstName", "item.author.lastName"]
}
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
15
16
17

// start search
var searchanswer
Patrick's avatar
Patrick committed
18
var searchanswer_paper
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function search(){
    const fuse  = new Fuse(stuff, options);
    searchanswer = fuse.search(document.getElementById("search-input").value) 

    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);
}
Patrick's avatar
Patrick committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function search_paper(){
    const fuse  = new Fuse(stuff_paper, options);
    searchanswer_paper = fuse.search(document.getElementById("search-input_paper").value) 

    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
47
48
49
//get json
// --- can be adapted if we load the content from the json
var stuff = []
Patrick's avatar
Patrick committed
50
var stuff_paper = []
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
51
52
53
54
55
56
57
58
59
60
61
// $.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162


  //
 // begining of xlsx to json coversion
//

var jsonContent = [];

/* set up XMLHttpRequest */
var url = "./content/CitationSheet.xlsx";
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", url, true);
xmlHttpRequest.responseType = "arraybuffer";

xmlHttpRequest.onload = function(e) {
    var arraybuffer = xmlHttpRequest.response;

    // convert data to binary string //
    var data = new Uint8Array(arraybuffer);
    var arr = new Array();
    for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
            var bstr = arr.join("");

            // Call XLSX //
            var workbook = XLSX.read(bstr, {
                type: "binary"
            });

            // DO SOMETHING WITH workbook HERE //
            var first_sheet_name = workbook.SheetNames[0];
            // Get worksheet //
            var worksheet = workbook.Sheets[first_sheet_name];

            var jsonOutput = XLSX.utils.sheet_to_json(worksheet);

            for (i=0; i < jsonOutput.length; i++){
                var item = {
                    "item": {"title": "",
                    "project": "",
                    "authors": [],
                    "imageLink":"",
                    "keywords":"",
                    "journal": {"name":"journal","volume":"","year":"","pages":""},
                    "DOI":""},
                    "links": {"pdf":"","url": "","demo": ""}
                  }
                var authors = jsonOutput[i].Authors.replace("and " ,"").trim().split(",");

                item["item"].title = jsonOutput[i].Titel;
                item["item"].project = jsonOutput[i].Project;
                for (j=0; j < authors.length; j++){
                    var author = {
                        "firstName": "",
                        "lastName": ""
                    }
                    if (authors[j].trim().split(" ").length == 1){
                        author["firstName"] = authors[j].trim().split(".")[0];
                        author["lastName"] = authors[j].trim().split(".")[1];
                        item["item"].authors.push(author);
                    }
                    else if (authors[j].trim().split(" ").length == 2){
                        author["firstName"] = authors[j].trim().split(" ")[0];
                        author["lastName"] = authors[j].trim().split(" ")[1];
                        item["item"].authors.push(author);
                    }
                    else if (authors[j].trim().split(" ").length == 3) {
                        author["firstName"] = authors[j].trim().split(" ")[0];
                        author["lastName"] = authors[j].trim().split(" ")[2];
                        item["item"].authors.push(author);
                    }
                    
                }
                item["item"].imageLink = jsonOutput[i]["Path to demo image"]
                if (jsonOutput[i].Keywords != undefined){
                    item["item"].keywords = jsonOutput[i].Keywords.split(",");
                }
                else {
                    item["item"].keywords = jsonOutput[i].Keywords;
                }
                item["item"].journal.name = jsonOutput[i].Journal;
                item["item"].journal.volume = jsonOutput[i].Volume;
                item["item"].journal.year = jsonOutput[i].Year;
                item["item"].journal.pages = jsonOutput[i].Pages;
                item["item"].DOI = jsonOutput[i].DOI;
                item["links"].pdf = jsonOutput[i].URL;
                item["links"].url = jsonOutput[i].URL;
                item["links"].demo = jsonOutput[i].URL;

                jsonContent.push(item);

            }
            console.log(jsonContent);

}
xmlHttpRequest.send();

  //
 // ending of xlsx to json coversion
//


JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
163
164
165
166
167
168
169
170
171
172
173
$(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
    }
    });
Joe TS Dell's avatar
up    
Joe TS Dell committed
174
    $.getJSON("./content/paper2.json", function(result){
Patrick's avatar
Patrick committed
175
176
177
178
179
180
        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++) {

Joe TS Dell's avatar
up    
Joe TS Dell committed
181
182
183
184
            if(Math.abs(arrayLength - i) <= max_paper_list){
                console.log("close to " + i)
                addpaper(stuff_paper[i]);
            }
Patrick's avatar
Patrick committed
185
186
187
            //Do something
        }
        });
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    var userLang = navigator.language || navigator.userLanguage; 
    console.log("The language is: " + userLang);
    if (userLang.includes("de")){
        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
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")
            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
            }
            container.find('.search-input').val('');
        } else {
            console.log("search")
            search();
            // addcontent();
        }
}
Patrick's avatar
Patrick committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
function searchToggle_paper(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")
            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]);
                }

                //Do something
            }
            container.find('#search-input_paper').val('');
        } else {
            console.log("search")
            search_paper();
            // addcontent();
        }
}
JOE Thunyathep S's avatar
up  
JOE Thunyathep S committed
263
264
265
266
267
268
269
270
271

// 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();
  }
Patrick's avatar
Patrick committed
272
273
274
275
276
277
278
279
280
});

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

   document.getElementById("search-button_paper").click();
  }
Sini's avatar
Sini committed
281
});