-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.html
208 lines (181 loc) · 7.45 KB
/
index.html
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
58
59
60
61
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="My Bookshelf">
<meta name="keywords" content="bookshelf">
<meta name="author" content="Marco Favorito">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Bookshelf</title>
<link rel="stylesheet" type="text/css" href="https://rawgit.com/Pixabay/jQuery-tagEditor/master/jquery.tag-editor.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="css/index.css">
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.2/jquery-ui.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script src="https://rawgit.com/Pixabay/jQuery-tagEditor/master/jquery.tag-editor.min.js"></script>
<script src="https://rawgit.com/Pixabay/jQuery-tagEditor/master/jquery.caret.min.js"></script>
<script src="js/classes.js"></script>
</head>
<body>
<a href="https://github.com/MarcoFavorito/my-bookshelf"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a>
<div class="container">
<div class="row">
<div class="col">
<h1>My bookshelf</h1>
</div>
</div>
<div class="row">
<div class="col" id="available-tags">
Available tags:
</div>
</div>
<div class="row">
<div class="col">
<textarea id="tagEditor"></textarea>
</div>
</div>
<div class="row">
<div class="col">
<div id="booksList"></div>
</div>
</div>
</div>
<script>
var bookshelf;
fetch('./bookshelf.tsv')
.then(function(response) {
return response.text();
}).then(function(text){
main(text);
});
function getItemFromLine(line){
var tokens = line.split("\t");
var name = tokens[0];
var link = tokens[1];
var b = new Item(name, link);
return b;
}
function getTagsFromLine(line){
var tags = line.split("\t").slice(2);
tags = new Set(tags);
tags.delete("");
return tags
}
function parseBookshelfFile(text){
var tag2items = new Map();
var item2tags = new Map();
lines = text.split("\n");
for (var i=1; i<lines.length-1; i++){
var cur_line = lines[i];
var b = getItemFromLine(cur_line);
var tags = getTagsFromLine(cur_line);
item2tags.set(b, tags);
for (var cur_tag of tags){
if (!(tag2items.has(cur_tag))){
tag2items.set(cur_tag, new Set());
}
tag2items.get(cur_tag).add(b);
}
}
return new Bookshelf(item2tags, tag2items);
}
function buildItemList(items, bookshelf){
var list = "";
for (var item of items){
var item_tags = bookshelf.item2tags.get(item);
var item_array = Array.from(item_tags);
var tag_string = item_array.join(", ");
var dot = "<li><a href='" + item.link +
"' title='" + tag_string + "'>" + item.name + "</a></li>";
list = list + "\n" + dot;
}
return "<ul>" + list + "</ul>\n";
}
function findItemsFromTagListAND(tags, bookshelf){
var temp_items = new Set();
var items = new Set();
for (var i=0; i<tags.length; i++){
var items_from_tag = bookshelf.tag2items.get(tags[i]);
if (i==0){
temp_items = new Set(items_from_tag);
}
else{
temp_items = new Set([...items_from_tag]
.filter(x => items.has(x)));
}
items = new Set(temp_items);
temp_items = new Set();
}
return items;
}
function onTagChange(field, editor, tags) {
$("#booksList").empty();
if (tags.length==0 || field==null){
$("#booksList").append("<h2>All tags</h2>");
tags = bookshelf.tag2items.keys();
}
else if (tags.length > 1) {
items = findItemsFromTagListAND(tags, bookshelf);
$("#booksList").append("<h2>" + new Array(tags).join(" & ") + "</h2>");
var list = buildItemList(items, bookshelf);
$("#booksList").append(list);
}
for (var tag of tags){
var items_from_tag = bookshelf.tag2items.get(tag);
var list = buildItemList(items_from_tag, bookshelf);
$("#booksList").append("<h2>" + tag + "</h2>");
$("#booksList").append(list);
}
}
function setup_tag_editor(tags){
$('#tagEditor').tagEditor({
autocomplete: {
autoFocus: true,
source: tags,
minLength: 1,
delay: 0
},
placeholder: 'Enter tags, e.g. machine-learning, algorithms, computer-programming, papers, textbooks, websites, ...',
onChange: onTagChange,
});
onTagChange(null, null, tags);
}
function setup_tag_list(tags){
var container = $('#available-tags');
for (var tag of tags){
var tag_id = `#tag-${tag}`;
var code_tag = $(`<code id=\"${tag_id}\">${tag}</code>`);
code_tag.click(onclick_tag(tag));
code_tag.addClass("tag_code tag_off")
container.append(code_tag);
container.append(" ");
}
}
function onclick_tag(tag){
var f = function(){
var current_tag = tag;
var clicked = false;
return function(){
if (clicked == false){
$('#tagEditor').tagEditor('addTag', current_tag);
clicked = true;
}
else{
$('#tagEditor').tagEditor('removeTag', current_tag);
clicked = false;
}
};
}();
return f;
}
function main(text){
bookshelf = parseBookshelfFile(text);
var tags = bookshelf.tag2items.keys();
var tag_array = Array.from(tags);
setup_tag_editor(tag_array);
setup_tag_list(tag_array);
}
</script>
</body>
</html>