-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
306 lines (258 loc) · 9.66 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="src/NodeNotFoundException.js"></script>
<script src="src/ToFewParametersPassedAsArgument.js"></script>
<script src="src/Node.js"></script>
<script src="src/Graph.js"></script>
<script src="lib/jquery-1.7.2.js"></script>
<script src="lib/bootstrap/js/bootstrap.js"></script>
<link rel="stylesheet" href="lib/bootstrap/css/bootstrap.css"/>
<link rel="stylesheet" href="lib/bootstrap/css/bootstrap-responsive.css"/>
</head>
<body>
<div class="container">
<form>
<label for="child">Input command (add, find):</label>
<span id="operation" class="label" style="display: none;"></span>
<input type="text" id="child" autocomplete="off"/>
<span id="prefix" class="label" style="display: none;">to</span>
<input type="text" id="parent" autocomplete="off" style="display: none;"/>
</form>
<span id="caption"></span>
<ul id="tree"></ul>
</div>
<script>
$(document).ready(function () {
$(window).keydown(function (event) {
if (event.keyCode == 13) {
console.log("#" + event.target.id);
$("#" + event.target.id).change();
event.preventDefault();
return false;
}
return true;
});
});
var graph = new Graph();
graph.addNode("apple devices");
graph.addNode("apple devices", "iphone");
graph.addNode("apple devices", "ipad");
graph.addNode("apple devices", "ipod");
graph.addNode("apple devices", "mac");
graph.addNode("iphone", "iphone classic");
graph.addNode("iphone", "iphone 3");
graph.addNode("iphone", "iphone 4");
graph.addNode("iphone", "iphone 5");
graph.addNode("ipad", "ipad classic");
graph.addNode("ipad", "ipad 2");
graph.addNode("ipad", "ipad 3");
graph.addNode("ipod", "ipod classic");
graph.addNode("ipod", "ipod hdd");
graph.addNode("ipod", "ipod 4");
graph.addNode("ipod", "ipod 5");
graph.addNode("mac", "mac book");
graph.addNode("mac", "mac mini");
graph.addNode("iphone 3", "iphone 3 classic");
graph.addNode("iphone 3", "iphone 3 retina");
graph.addNode("iphone 4", "iphone 4 classic");
graph.addNode("iphone 4", "iphone 4 s");
graph.addNode("ipad 2", "ipad 2 classic");
graph.addNode("ipad 2", "ipad 2 retina");
graph.addNode("ipod classic", "ipod classic mini");
graph.addNode("ipod classic", "ipod classic nano");
graph.addNode("ipod classic", "ipod classic full");
graph.addNode("ipod hdd", "ipod hdd mini");
graph.addNode("ipod hdd", "ipod hdd nano");
graph.addNode("ipod hdd", "ipod hdd full");
graph.addNode("ipod 4", "ipod 4 mini");
graph.addNode("ipod 4", "ipod 4 nano");
graph.addNode("ipod 4", "ipod 4 full");
graph.addNode("ipod 5", "ipod 5 mini");
graph.addNode("ipod 5", "ipod 5 nano");
graph.addNode("ipod 5", "ipod 5 full");
graph.addNode("mac book", "mac book pro");
graph.addNode("mac book", "mac book air");
graph.addNode("mac book pro", "mac book pro 13");
graph.addNode("mac book pro", "mac book pro 15");
graph.addNode("mac book pro", "mac book pro 17");
graph.addNode("mac book air", "mac book air 13");
graph.addNode("mac book air", "mac book air 15");
graph.addNode("mac book air", "mac book air 17");
graph.addNode("mac mini", "mac mini $500");
graph.addNode("mac mini", "mac mini $700");
graph.addNode("mac book pro 13", "mac book pro 13 X48UFOWJDHFN3RWEJH");
graph.addNode("mac book pro 13", "mac book pro 13 X48UFOSDLKFJSDLKDS");
graph.addNode("mac book pro 13", "mac book pro 13 X48UFOWSADHASKJDHA");
graph.addNode("mac book pro 13", "mac book pro 13 X48UFOWJASRLDKSADK");
graph.addNode("mac book pro 15", "mac book pro 15 X57JDOWJDHFN3RWEJH");
graph.addNode("mac book pro 15", "mac book pro 15 X57JDOSDLKFJSDLKDS");
graph.addNode("mac book pro 15", "mac book pro 15 X57JDOWSADHASKJDHA");
graph.addNode("mac book pro 15", "mac book pro 15 X57JDOWJASRLDKSADK");
graph.addNode("mac book pro 17", "mac book pro 17 X77SDHWJDHFN3RWEJH");
graph.addNode("mac book pro 17", "mac book pro 17 X77SDHSDLKFJSDLKDS");
graph.addNode("mac book pro 17", "mac book pro 17 X77SDHWSADHASKJDHA");
graph.addNode("mac book pro 17", "mac book pro 17 X77SDHWJASRLDKSADK");
graph.addNode("mac book air 13", "mac book air 13 A84SDLKFJWJDHFN3RWEJH");
graph.addNode("mac book air 13", "mac book air 13 A84SDLKFJSDLKFJSDLKDS");
graph.addNode("mac book air 13", "mac book air 13 A84SDLKFJWSADHASKJDHA");
graph.addNode("mac book air 13", "mac book air 13 A84SDLKFJWJASRLDKSADK");
graph.addNode("mac book air 15", "mac book air 15 A56SDLKFJ57DHFN3RWEJH");
graph.addNode("mac book air 15", "mac book air 15 A56SDLKFJSDLKFJSDLKDS");
graph.addNode("mac book air 15", "mac book air 15 A56SDLKFJWSADHASKJDHA");
graph.addNode("mac book air 15", "mac book air 15 A56SDLKFJWJASRLDKSADK");
var selectChildNode = function (query) {
var result = [];
(function () {
try {
var search = graph.search(query);
} catch (e) {
if (e.name === "NodeNotFoundException") {
result = [];
return;
}
}
for (var i in search) {
result.push(search[i].getName());
}
})();
console.log(result);
return result;
}
$('#child').change(function () {
var request = $('#child').val();
console.log(request);
var search = request.search("<");
console.log(search);
if (currentCommand == "find") {
showChildren(request);
$("#operation").hide();
$("#child").val("");
currentCommand = "";
return true;
}
if (currentCommand == "remove") {
removeNode(request);
$('#child').val("");
$('#caption').val("");
$('#tree').html("");
currentCommand = "";
return true;
}
//determine operation type
if (request[0] == "a") {
$('#operation').html("add");
$('#operation').show();
$('#parent').show();
$('#prefix').show();
$('#child').val("");
currentCommand = "add";
} else if (request[0] == "f") {
$('#operation').html("find");
$('#operation').show();
var request = $('#child').val();
$('#child').val("");
currentCommand = "find";
} else if (request[0] == "r") {
$('#operation').html("remove");
$('#operation').show();
$('#child').val("");
currentCommand = "remove";
}
});
$('#parent').change(function () {
var parent= $("#child").val();
var child = $("#parent").val();
console.log(parent);
try {
if (parent === "") {
graph.addNode(child);
} else {
console.log(">>>>"+parent);
graph.addNode(parent, child);
}
} catch (e) {
if (e.name === NodeNotFoundException.name) {
$("#caption").html("<span class='label label-important'>Node with this name does not exists!</span>");
}
}
if (parent === "") {
$("#caption").html("<span class='label label-info'>Node <" + child + "> was added as \"root\" node</span>");
} else {
$("#caption").html("<span class='label label-info'>Node <" + child + "> was added to <" + parent + "></span>");
}
$('#child').val("");
$('#tree').html("");
$('#parent').val("");
$('#operation').hide();
$('#prefix').hide();
$('#parent').hide();
currentCommand = "";
});
var currentCommand = "";
$('#child').typeahead({source:function (query) {
return (function () {
if (currentCommand === "") {
return ["add <node> <parent>", "remove <node>", "find <node>"];
} else {
return selectChildNode(query);
}
}());
}});
$('#parent').typeahead({source:selectChildNode});
var showChildren = function (query) {
console.log(1);
$("#tree").html("");
// try {
var search = graph.search(query);
console.log(search);
if (search && !(search instanceof Array)) {
var childNames = search.getChildNames();
if (childNames.length > 0) {
$("#caption").html(
"Child of " +
"<span class='label label-success'>" + query + "</span>" +
" is :"
);
for (var k in childNames)
$("#tree").append("<li> <span class='label label-warning'>" + childNames[k] + "</span></li>");
} else {
var parentNodes = search.getParentNames();
if (parentNodes[0] === Graph.prototype.rootNodeName) {
$("#caption").html(
"<span class='label label-warning'>" + query + "</span>" +
" is root node"
);
} else {
$("#caption").html(
"Parent of " +
"<span class='label label-warning'>" + query + "</span>" +
" is : " +
"<span class='label label-success'>" + parentNodes[0] + "</span>"
);
}
}
} else {
$("#caption").html("<span class='label label-important'>Node name " + query + " is incorrect!</span>");
}
/*} catch (e) {
if (e.name === NodeNotFoundException.name) {
$("#caption").html("<span class='label label-important'>Node with this name does not exists!</span>");
}
throw e;
}*/
};
var removeNode = function (nodeName) {
try {
graph.removeNode(nodeName);
$("#caption").html("<span class='label label-info'>Node with name " + nodeName + " succesfully delted!</span>");
} catch (e) {
if (e.name === NodeNotFoundException.name) {
$("#caption").html("<span class='label label-important'>Node with this name does not exists!</span>");
}
}
}
</script>
</body>
</html>