-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
217 lines (166 loc) · 4.14 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
209
210
211
212
213
214
215
216
217
<html>
<head>
<title> Image Tagging Applet </title>
<style>
div#galleryView{
float:left;
width:75%;
text-align: left;
border:2px solid black;
margin:auto;
}
div#filterMenu{
width:10%;
float:left;
border:2px solid black;
text-align:left;
padding:10px;
margin-right:40px;
margin-left:40px;
}
.imgLib {
height:100px;
width:170px;
margin:10px 27px 10px 27px;
border:3px solid white;
}
.selected {
border:3px solid blue;
}
label {
margin-left:5px;
margin-top:10px;
}
input {
margin-top:10px;
}
</style>
</head>
<div id="header" style="display:block;margin-bottom:10px">
<body style="text-align:center">
<button onclick = "tagSelected()"> Tag Selected </button>
</div>
<div id="filterMenu"><h3>Filter by:</h3></div>
<div id="galleryView"></div>
<script src="./ImageManager.js"></script>
<script>
var imgMan = new ImageManager('./test2.json');
for (el of imgMan.imgLib){
el.addEventListener("click",
function (event){
toggleClass('selected',event.target);
});
}
bindGallery('galleryView');
populateGallery()
function filterGallery(){
//This approach of populating then removing seems ineffecient for large galleries.
//Better to populate with the right tags in the first palce?
clearSelected();
if (selectedTags.length>0){
populateGallery();
for (i=galleryDIV.children.length -1 ; i >= 0 ; i--){
var passing = 0;
for (tag of selectedTags){
if (imgMan.hasTag(galleryDIV.children[i], tag)){
passing = 1;
}
}
if (passing ==0) {
galleryDIV.removeChild(galleryDIV.children[i]);
}
}
}
else populateGallery();
}
function tagSelected(){
var oldTagSet = imgMan.tagSet().slice(0);
var selected = document.getElementsByClassName('selected');
if (tags = prompt("Tag Name:")){
tags = tags.toLowerCase();
tags = tags.split(' '); //split multiple words into multiple tags
imgMan.tag(selected, tags);
}
clearSelected();
if (!arraysAreEqual(imgMan.tagSet(),oldTagSet)) populateFilterMenu();
}
function bindGallery(gallery){
window.galleryDIV = document.getElementById(gallery);
console.log('creating gallery');
}
function populateGallery(){
for (let el of imgMan.imgLib){
galleryDIV.append(el);
}
}
function clearSelected(){
var selected = document.getElementsByClassName('selected');
for (i=selected.length-1; i >= 0; i--){
selected[i].classList.remove('selected');
}
}
//FilterMenu stuff
var selectedTags = [];
//Changed from creating everything dynamically to referencing existing DOM elements
var filterDIV = document.getElementById("filterMenu");
//Create Form to hold checkboxes
var filterForm = document.createElement("FORM");
filterDIV.appendChild(filterForm);
populateFilterMenu();
function populateFilterMenu(){
for (el of imgMan.tagSet()){
if (!filterForm.querySelector('#'+el)){
//make checkbox
let newBox = document.createElement("INPUT");
newBox.type = "checkbox";
newBox.id = el;
//Match current tagging settings:
if (selectedTags.indexOf(el.toLowerCase()) != -1)
newBox.checked = true;
//make label
let newLab = document.createElement("LABEL");
newLab.textContent = el;
newLab.for = el;
//Add event listener to update form each time a box is checked:
newBox.addEventListener("change",()=>{
updateTagFilter();
filterGallery();
});
filterForm.appendChild(newBox);
filterForm.appendChild(newLab);
filterForm.appendChild(document.createElement("BR"));
}
}
}
function updateTagFilter(){
if (event.target.checked==true){
selectedTags.push(event.target.id.toLowerCase());
}
else{
remFromAr(selectedTags, event.target.id.toLowerCase());
}
}
//Helper functions ---------------------------------------------------------------
function toggleClass(className, target) {
if (target.classList.contains(className)){
target.classList.remove(className);
}
else{
target.classList.add(className);
}
}
function remFromAr(ar, value){
var loc = ar.indexOf(value);
if (loc == -1) return 0;
ar.splice(loc,1);
}
function arraysAreEqual(ar1, ar2){
if (ar1.length!=ar2.length) return false;
for (i=0; i<ar1.length;i++){
if (ar1[i]!=ar2[i]) return false;
}
return true;
}
</script>
</body>
</html>