-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·128 lines (104 loc) · 2.53 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
<html>
<head>
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
<script src="/socket.io/socket.io.js"></script>
<script src="/jquery-min.js"></script>
<LINK href="/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<script>
var items, currentId=-1;
function buildList() {
var list = "";
for (var i=0; i < items.length; i++) {
if (items[i].id == currentId){
list = list + "<li class='current'>";
} else {
list = list + "<li >";
}
list = list + items[i].text;
if (items[i].remove){
list = list + " <a class='removeItem' href='/remove/" + items[i].id + "'> X </a>"; //
}
list = list + "</li>";
}
return list;
}
var socket = io.connect('http://10.7.7.1');
socket.on('current', function (data) {
currentId = data.id;
console.log("Current item: " + data.id);
$("#list").html(buildList());
});
socket.on('add', function (data) {
items.push(data.item);
console.log("Adding item: " + data.item.id);
$("#list").html(buildList());
});
socket.on('remove', function (data) {
for (var i=0; i < items.length; i++){
if (items[i].id == data.id){
items.splice(i,1);
console.log("Removed item: " + data.id);
break;
}
}
$("#list").html(buildList());
});
socket.on('update', function (data) {
for (var i=0; i < items.length; i++){
if (items[i].id == data.item.id){
items[i] = data.item;
console.log("Updated item: " + data.item.id);
break;
}
}
$("#list").html(buildList());
});
socket.on('items', function (data) {
items = data.items;
$("#list").html(buildList());
console.log("Recieved " + items.length + " items.");
});
$(document).ready(function(){
$("#myForm").submit(function(){
$.ajax({
type: "POST",
url: "/add",
data: $("#myForm").serialize(),
dataType: "json",
success: function(msg){
$("#message").val('');
},
error: function(){
}
});
//make sure the form doesn't post
return false;
});
$("a.removeItem").live('click',function(e){
e.preventDefault();
$.ajax({
type: "GET",
url: e.target.href,
data: $("#myForm").serialize(),
dataType: "json",
success: function(msg){
$("#message").val('');
},
error: function(){
}
});
//make sure the form doesn't post
return false;
});
})
</script>
<form id="myForm">
<p>Add a Message:</p>
<input type="text" id="message" name="message" />
<input type="submit" value="Submit" />
</form>
<ul id="list"></ul>
</body>
</html