-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
297 lines (240 loc) · 7.19 KB
/
Copy pathindex.html
File metadata and controls
297 lines (240 loc) · 7.19 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>D3 Key Function Demo</title>
<!-- Author: Bo Ericsson, bo@boe.net -->
<link rel=stylesheet type=text/css href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" media="all">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<style>
body {
margin: 10px;
}
h4 {
margin-bottom: 20px;
}
pre {
font-size: 10px;
line-height: 11px;
}
label {
margin-top: 10px;
margin-bottom: 10px;
}
</style>
<body>
<script>
'use strict';
var func;
var keyFunc = null;
var data;
var idCounter = 0;
// get reference to container
var body = d3.select("body").append("div")
.attr("id", "container");
body.append("h4").text("D3 Key Function Demo")
// create UI elements
var ctrlDiv = body.append("div")
.style("position", "relative");
// add checkbox key function
var useKeyFunction = false;
ctrlDiv.append("label")
.attr("class", "checkbox")
.text("Use key function")
.append("input")
.attr("type", "checkbox")
.property("checked", useKeyFunction)
.on("click", function() {
useKeyFunction = d3.select(this).property("checked")
})
// add checkbox for selection ordering
var reOrderSelection = false;
ctrlDiv.append("label")
.attr("class", "checkbox")
.text("Reorder DOM elements to match selection")
.append("input")
.attr("type", "checkbox")
.property("checked", reOrderSelection)
.on("click", function() {
reOrderSelection = d3.select(this).property("checked")
if (reOrderSelection && data != undefined) d3.selectAll(".testItem")
.data(data, keyFunc)
.order();
})
// add button for code execution
var btn = ctrlDiv.append("button")
.attr("class", "btn btn-small")
.style("width", "300px")
.text("Step 0: Create data")
.on("click", function() {
this.blur();
if (func != undefined) func();
})
// add comment field
var comment = ctrlDiv.append("label")
.html(" ");
// add elem to hold current data structure
var currData = ctrlDiv.append("pre")
.style("position", "absolute")
.style("width", "200px")
.style("top", "30px")
.style("left", "410px")
.style("display", "none")
.text("")
// add elem to hold code to be executed
var code = ctrlDiv.append("pre")
.style("position", "absolute")
.style("width", "600px")
.style("top", "570px")
.style("left", "10px")
.style("height", "200px")
.style("overflow", "scroll")
.text("")
// create div to hold the dynamically created labels
var div = body.append("div")
.style("margin-top", "30px")
// introspect this code
var source = d3.select("body").select("script").node().innerText;
code.text(source);
var lines = source.split("\n");
var lineNumbers = [];
lines.forEach(function(d, i) {
if (d.indexOf("function step") == 0 || d.indexOf("function start") == 0)
lineNumbers.push({ func: d.substring(0, 14), lineNumber: i})
})
function scrollCode(codeSegment) {
var found;
lineNumbers.some(function(d) {
if (d.func.indexOf("function " + codeSegment) == 0) {
found = d;
return true;
}
})
var lineNumber = found.lineNumber
code.node().scrollTop = lineNumber * 11;
}
// master data
var initialData = [
{ item: 0, name: "zero", value: 0, initial: true },
{ item: 1, name: "one", value: 1, initial: true },
{ item: 2, name: "two", value: 2, initial: true },
{ item: 3, name: "three", value: 3, initial: true },
{ item: 4, name: "four", value: 4, initial: true }
];
// called from each step
function processData(data) {
keyFunc = null;
if (useKeyFunction) keyFunc = function(d, i) { return d.item }
var updateSel = div.selectAll(".testItem")
.data(data, keyFunc)
// process exit selection
updateSel.exit().remove();
// process enter selection; add items
updateSel.enter().append("label")
.attr("id", function(d) { return idCounter++} )
.attr("class", "testItem")
.style("color", function(d) {
if (d.initial) return "red";
else return "black";
})
.style("display", "block");
// process update election (which now includes the enter selection)
updateSel.text(function(d, i) { return d.name + " (" + d.value + ")"; });
// un-comment this to see the order of items in the selection
updateSel[0].forEach(function(d) {
//console.log(d3.select(d).attr("id"));
})
// optionally reorder DOM elements to match selection order
// only relevant if our key function is used
if (reOrderSelection) updateSel.order();
}
// set initial function pointer, and code scroll position
func = step0;
scrollCode("step0");
function step0() {
// clear DOM
d3.selectAll(".testItem").remove();
comment.html(" ");
// clone data and reset counter
data = JSON.parse(JSON.stringify(initialData, null, 1));
//console.log('data.bla', data.bla())
idCounter = 0;
// update UI
currData.text(JSON.stringify(data, null, 1));
currData.style("display", "block");
// next
btn.text("Step 1: Load DOM...");
scrollCode("step1");
func = step1;
}
function step1() {
// inital data bind
processData(data);
// update UI
currData.text(JSON.stringify(data, null, 1));
comment.text(data.length + " items were added...");
// next
btn.text("Step 2: Remove 2nd item...");
scrollCode("step2");
func = step2;
}
function step2() {
// remove the 2nd item
data.splice(1, 1);
processData(data);
// update UI
currData.text(JSON.stringify(data, null, 1));
comment.text("The 2nd item was removed...");
// next
btn.text("Step 3: Move last item to 2nd position");
scrollCode("step3");
func = step3;
}
function step3() {
// move last item to 2nd position
var lastItem = data.pop();
lastItem.name = lastItem.name + "-modified";
lastItem.value = lastItem.value * 10;
data.splice(1, 0, lastItem);
processData(data);
// update UI
currData.text(JSON.stringify(data, null, 1));
comment.text("Was the new item moved to 2nd position?");
// next
btn.text("Step 4: Add three items to end of array...");
scrollCode("step4");
func = step4;
}
function step4() {
// add items
var initialLength = data.length;
data.push({ item: 4, name: "four", value: 400 });
data.push({ item: 5, name: "five", value: 500 });
data.push({ item: 6, name: "six", value: 600 });
var dataLength = data.length;
var itemsAdded = dataLength - initialLength;
processData(data);
// update UI
currData.text(JSON.stringify(data, null, 1));
var domLength = d3.selectAll(".testItem")[0].length;
var text = (domLength == dataLength) ?
"All " + itemsAdded + " new items were added to DOM" :
"Only " + (domLength - dataLength + itemsAdded) + " items were added to DOM!";
comment.text(text);
// next
btn.text("Step 5: Add one item at the beginning of array...");
scrollCode("step5");
func = step5;
}
function step5() {
data.unshift({ item: 7, name: "seven", value: 7 });
processData(data);
// update UI
currData.text(JSON.stringify(data, null, 1));
comment.text("Pay attention where the new item was added to the DOM");
// next
btn.text("Repeat Step 0: Create data")
scrollCode("step0");
func = step0;
}
</script>