-
Notifications
You must be signed in to change notification settings - Fork 1
/
program-11.html
306 lines (271 loc) · 7.48 KB
/
program-11.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>Program 11 - Sorting and filtering small multiples</title>
<style>
span.editable-label.label {
padding: 0.2em;
}
span.editable-label.label:hover {
padding: 0.1em;
border: 0.1em solid #aaa;
cursor: hand;
}
span.editable-label > button.check {
color: green;
}
span.editable-label > button.x {
color: red;
}
</style>
</head>
<body>
</body>
<script type="text/javascript">
/*
Program 11 - Sorting and filtering small multiples
The list we displayed in program 10 was rather static. In most real
interfaces displaying small multiples we want to be able to sort
them on different criteria and filter what is actually displayed.
Our example for this will be a table of records, each record having
the fields first name, last name, and phone number. The user will
be able to insert and edit new records, sort the table based on each
column, and put in filter text to filter the table.
We will have a button at the top to insert a new row in the table,
followed by a table with four columns. The first three columns
show the first name, last name, and phone number. The last column
has a button to delete the row.
*/
function Observable() {
this.observers = new Set();
this.addObserver = function(observer) {
this.observers.add(observer);
};
this.deleteObserver = function(observer) {
this.observers.delete(observer);
};
this.notify = function() {
this.observers.forEach(function(observer) {
observer.update();
});
};
}
var People = new Observable();
People.people = [];
People.insertPerson = function(person) {
this.people.unshift(person);
this.notify();
};
People.removePersonAt = function(i) {
this.people.splice(i, 1);
this.notify();
};
People.updatePersonAt = function(i, newPerson) {
this.people[i] = newPerson;
this.notify();
};
function writeSubviewsIn(el, subviews) {
subviews.forEach(function(subview) {
var subEl = document.createElement('div');
el.appendChild(subEl);
subview.writeOver(subEl);
}, this);
}
/*
The fields of our table will be labels that turn into text inputs
when clicked. Clicking a green check mark at the right of the box
or typing enter will commit changes to the text box. Clicking a
red X at the right of the box or typing escape will cancel the
changes. After commiting or canceling, the text box returns to
being a label.
Its controller protocol is:
Required:
- getText() -> string
- setText(string)
Optional:
- getClass() -> string
- getPlaceholder() -> string
*/
function EditableLabel(model, controller) {
model.addObserver(this);
this.editing = false;
this.writeOver = function(el) {
this.writeLabelOver(el);
};
this.writeLabelOver = function(el) {
this.editing = false;
var span = document.createElement('span');
span.classList.add('editable-label');
span.classList.add('label');
if (controller.getClass) {
div.classList.add(controller.getClass());
}
span.textContent = controller.getText();
span.addEventListener('click', function() {
this.writeInputOver(span);
}.bind(this));
el.parentNode.replaceChild(span, el);
this.el = span;
};
this.writeInputOver = function(el) {
this.editing = true;
var span = document.createElement('span');
span.classList.add('editable-label');
span.classList.add('editing');
var input = document.createElement('input');
if (controller.getClass) {
input.classList.add(controller.getClass());
}
if (controller.getPlaceholder) {
input.setAttribute('placeholder',
controller.getPlaceholder());
}
input.setAttribute('value', controller.getText());
span.appendChild(input);
var confirm = document.createElement('button');
confirm.textContent = '\u2713'; // Check mark
confirm.classList.add('check');
confirm.addEventListener('click', function() {
if (input.value !== controller.getText()) {
controller.setText(input.value);
}
this.writeLabelOver(this.el);
}.bind(this));
span.appendChild(confirm);
var cancel = document.createElement('button');
cancel.textContent = '\u2717'; // X mark
cancel.classList.add('x');
cancel.addEventListener('click', function() {
this.writeLabelOver(this.el);
}.bind(this));
span.appendChild(cancel);
this.el = span;
el.parentNode.replaceChild(span, el);
};
this.release = function() {
this.el = null;
this.model.deleteObserver(this);
};
this.update = function() {
if (!this.editing && this.el) {
this.el.textContent = controller.getText();
}
};
}
/*
Controller protocol:
- getColumnOrder() -> [key]
- getColumnLabel(key) -> string
- writeCellOver(row, column_key, el) -> subview
*/
function Table(model, controller) {
this.state = new Observable();
model.addObserver(this);
/*
To add:
- column order
- row order
- filter
*/
this.writeOver = function(el) {
var columns = controller.getColumnOrder();
var row, cell, div;
var table = document.createElement('table');
var thead = document.createElement('thead');
table.appendChild(thead);
row = document.createElement('tr');
thead.appendChild(row);
for (var i = 0; i < columns.length; i++) {
cell = document.createElement('th');
cell.textContent = controller.getColumnLabel(
columns[i]
);
row.appendChild(cell);
}
var tbody = document.createElement('tbody');
table.appendChild(tbody);
var N = controller.getNItems();
for (var i = 0; i < N; i++) {
row = document.createElement('tr');
tbody.appendChild(row);
item = controller.getItem(i);
for (var j = 0; j < columns.length; j++) {
cell = document.createElement('td');
div = document.createElement('div');
cell.appendChild(div);
controller.writeCellOver(i, columns[j], div);
row.appendChild(cell);
}
}
this.table = table;
el.parentNode.replaceChild(table, el);
};
this.update = function() {
if (this.table) {
this.writeOver(this.table);
}
};
this.release = function() {
this.table = null;
model.deleteObserver(this);
this.state.deleteObserver(this);
this.state = null;
}
}
var Body = {
subviews: new Set(),
addSubview: function(subview) {
this.subviews.add(subview);
},
show: function() {
currentScreen: { writeable: true }
var fragment = document.createDocumentFragment();
var div = document.createElement('div');
fragment.appendChild(div);
writeSubviewsIn(div, this.subviews);
document.body.appendChild(fragment);
}
};
Body.addSubview(new Table(People, {
getColumnOrder: function() {
return ['first', 'last', 'phone'];
},
columnLabels: {
'first': 'First name',
'last': 'Last name',
'phone': 'Phone number'
},
getColumnLabel: function(key) {
return this.columnLabels[key];
},
getNItems: function() {
return People.people.length;
},
getItem: function(i) {
return People.people[i];
},
writeCellOver: function(row, column_key, el) {
var label = new EditableLabel(People, {
getText: function() {
return People.people[row][column_key];
},
setText: function(s) {
p = People.people[row];
p[column_key] = s;
People.updatePersonAt(row, p);
}
});
label.writeOver(el);
return label;
}
}));
People.insertPerson({
first: 'Boris',
last: 'Gudenov',
phone: '206-484-4377'
});
document.addEventListener('DOMContentLoaded', function() {
Body.show();
});
</script>
</html>