-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic.html
148 lines (112 loc) · 2.7 KB
/
dynamic.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
<html>
<style>
body
{
font-size:10px;
font-family: helvetica;
}
.note
{
width:200px;
background-color: gray;
border-style: solid;
border-width: 1px;
border-color: black;
padding:10px;
margin: 10px;
}
</style>
<body>
<h2>Dynamic Interface - Welcome to the 21st Century!</h3>
<div id="dynamicInterface">
<p>Name<input id="name" name="name" type="text"></input></p>
<p>Note<input id="note" name="note" type="text"></input></p>
</div>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$( document ).ready(function()
{
var dynamicInterface = $( "#dynamicInterface" )
notesField = $("input#note")
// When user presses inter in the notes box, post the data
notesField.bind("enterKey",postData);
// Define the enterKey event.
notesField.keyup(function(e)
{
if(e.keyCode == 13)
{
$(this).trigger("enterKey");
}
});
dynamicInterface.append("<h4>Raw Data</h4>" )
pre = $("<pre id='data'>").appendTo(dynamicInterface)
dynamicInterface.append("<h4>Cooked Data</h4>" )
pre = $("<div id='cooked'>").appendTo(dynamicInterface)
getData()
// Poll for new data every second.
setInterval( getData, 1000 )
})
function getData()
{
var url = "/data"
$.get( url, undefined, onDataReceived )
}
function onDataReceived( data )
{
// display pretty-printed json in HTML pre tag.
$("#data").text( JSON.stringify(data, undefined, 4) )
// display one div per entry in the data
cookedDiv = $("#cooked")
cookedDiv.empty()
if ( data )
{
for ( var i=0; i<data.length; i++ )
{
// Create a div containing the 'name' and 'note' fields
elementDiv = $("<div class='note'>").appendTo( cookedDiv )
elementDiv.html( data[i].name + "<br>" + data[i].note )
elementDiv.append("<hr>")
// add button to this div for removing this specific data entry.
var button = $("<button>").appendTo(elementDiv)
button.text("remove")
button.click( removeData(data[i].name, data[i].id) )
}
}
}
// GUID generator.
// Thanks broofa
// http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function createUniqueID()
{
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
return guid;
}
function postData()
{
var data = {}
data["name"] = $("#name").val();
data["note"] = $("#note").val();
data["id"] = createUniqueID();
var url = "/post"
$.post( url, data )
}
function removeData( name, id )
{
return function()
{
var data = {}
if ( id != undefined )
{
data["id"] = id
}
data["name"] = name;
var url = "/remove"
$.post( url, data )
}
}
</script>
</html>