-
Notifications
You must be signed in to change notification settings - Fork 1
/
program-10.html
358 lines (330 loc) · 8.64 KB
/
program-10.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html>
<html>
<head>
<title>Program 10 - Small multiples</title>
<style>
button.insert-button { width: 30em; height: 4em; }
ul { display: block; margin: 0; padding: 0; width: 30em; }
li { display: block; width: 30em; }
li > * { display: inline-block; }
</style>
</head>
<body>
</body>
<script type="text/javascript">
/*
Program 10 - Small multiples
Most nontrivial interfaces show many instances of the same view,
each displaying different data. This could be a table or a list
or a series of plots. We need a generic way of dealing with these
small multiples.
Our example will be a vertical list of input text boxes with
validation. The validation will ensure that all the contents
are integers. We will also add a button to each row that deletes
that row. A single button at the top will insert a new text box
at the beginning of the list.
Our model's sole slot is the array of integers corresponding
to the text boxes.
*/
var Observable = {
addObserver: function(observer) {
this.observers.add(observer);
},
deleteObserver: function(observer) {
this.observers.delete(observer);
},
notify: function() {
this.observers.forEach(function(observer) {
observer.update();
});
}
};
var Model = Object.create(Observable, {
observers: { writable: true, value: new Set() },
numbers: { writable: true, value: [] },
});
Model.addNumber = function(n) {
this.numbers.unshift(n);
this.notify();
};
Model.removeNumberAt = function(i) {
this.numbers.splice(i, 1);
this.notify();
};
Model.setNumberAt = function(i, n) {
this.numbers[i] = n;
this.notify();
};
function writeSubviewsIn(el, subviews) {
subviews.forEach(function(subview) {
var subEl = document.createElement('div');
el.appendChild(subEl);
subview.writeOver(subEl);
}, this);
}
/*
We use the same body view for our root as program 9.
*/
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);
}
};
function Button(model, controller) {
this.model = model;
this.controller = controller;
controller.initialize(model, this);
this.model.addObserver(this);
this.writeOver = function(el) {
var button = document.createElement('button');
button.textContent = this.controller.getText();
if (this.controller.getClass) {
button.setAttribute(
'class',
this.controller.getClass()
);
}
if (this.controller.isEnabled) {
button.disabled = !this.controller.isEnabled();
}
button.addEventListener(
'click',
this.controller.onClick.bind(this.controller)
);
this.button = button;
el.parentNode.replaceChild(button, el);
};
this.update = function(msg) {
if (this.button) {
this.button.textContent = this.controller.getText(msg);
if (this.controller.isEnabled) {
this.button.disabled = !this.controller.isEnabled();
}
}
};
this.release = function() {
this.model.deleteObserver(this);
this.model = null;
this.controller = null;
this.button = null;
};
}
/*
First, the button that adds a new row. It inserts a random
integer between 0 and 100 at the beginning of the list.
*/
var AddButton = new Button(Model, {
initialize: function() {},
getText: function() { return 'Insert row'; },
getClass: function() { return 'insert-button'; },
onClick: function() {
Model.addNumber(Math.floor(100*Math.random()));
}
});
Body.addSubview(AddButton);
/*
Next we need the view to construct an individual row. We will use
the number picker from program 7.
*/
function NumberPicker(model, controller) {
this.model = model;
this.controller = controller;
controller.initialize(model, this);
this.writeOver = function(el) {
this.textBox = new TextBox(model, {
initialize: function(model) {
this.model = model
},
isValid: function(s) {
var r = /\s*[0-9]+\s*/;
if (!r.test(s)) {
return false;
}
if (controller.range) {
var n = parseInt(s);
if (n < controller.range[0] || n >= controller.range[1]) {
return false;
}
}
return true;
},
onChanged: function(s) {
controller.setValue(parseInt(s))
},
getPlaceholder: function() {
return '(Integer)';
},
getValue: function() {
return controller.getValue();
}
});
this.decrementButton = new Button(model, {
initialize: function(model) {
this.model = model;
},
getText: function() {
return '--';
},
onClick: function() {
controller.setValue(controller.getValue()-1);
},
isEnabled: function() {
if (controller.range) {
var v = controller.getValue();
return v > controller.range[0];
} else {
return true;
}
}
});
this.incrementButton = new Button(model, {
initialize: function(model) {
this.model = model;
},
getText: function() {
return '++';
},
onClick: function() {
controller.setValue(controller.getValue()+1);
},
isEnabled: function() {
if (controller.range) {
var v = controller.getValue();
return v < controller.range[1]-1;
} else {
return true;
}
}
});
var span = document.createElement('div');
var views = [this.textBox,
this.decrementButton,
this.incrementButton];
views.forEach(function(subview) {
var subEl = document.createElement('div');
span.appendChild(subEl);
subview.writeOver(subEl);
});
el.parentNode.replaceChild(span, el);
};
this.update = function() {
if (this.textBox) {
this.textBox.update();
this.decrementButton.update();
this.incrementButton.update();
}
};
}
function TextBox(model, controller) {
this.model = model;
this.controller = controller;
controller.initialize(model, this);
this.model.addObserver(this);
this.writeOver = function(el) {
this.input = document.createElement('input');
this.input.setAttribute('type', 'text');
if (this.controller.getClass) {
this.input.setAttribute(
'class', this.controller.getClass()
);
}
if (this.controller.getPlaceholder) {
this.input.setAttribute(
'placeholder', this.controller.getPlaceholder()
);
}
this.input.setAttribute('value', this.controller.getValue());
this.input.addEventListener('change', function() {
var s = this.input.value;
if (s === '') {
this.input.setAttribute(
'placeholder',
this.controller.getPlaceholder()
);
if (this.controller.onCleared) {
this.controller.onCleared();
}
} else if (this.controller.isValid && !this.controller.isValid(s)) {
this.input.classList.add('invalid');
} else {
this.input.classList.remove('invalid');
this.controller.onChanged(s);
}
}.bind(this));
el.parentNode.replaceChild(this.input, el);
};
this.update = function() {
if (this.input) {
this.input.value = this.controller.getValue();
if (!this.controller.isValid) {
return;
} else if (this.controller.isValid(this.input.value)) {
this.input.classList.remove('invalid');
} else {
this.input.classList.add('invalid');
}
}
};
}
/*
Then we have a list view. It uses a NumberPicker plus a delete
button as each row.
*/
var List = {
writeOver: function(el) {
var ul = document.createElement('ul');
var li, div, button, picker;
Model.numbers.forEach(function(key, i) {
li = document.createElement('li');
picker = new NumberPicker(Model, {
index: i,
initialize: function(model, view) {
this.model = model;
},
range: [0, 100],
setValue: function(n) {
Model.setNumberAt(this.index, n);
},
getValue: function() {
return Model.numbers[this.index];
}
});
button = new Button(Model, {
initialize: function() {},
getText: function() { return 'Delete'; },
onClick: function() {
Model.removeNumberAt(this.controller.index);
}.bind(picker)
});
writeSubviewsIn(li, [picker, button]);
ul.appendChild(li);
}, this);
el.parentNode.replaceChild(ul, el);
this.ul = ul;
},
release: function() {
this.ul = null;
Model.deleteObserver(this);
},
update: function() {
if (this.ul) {
this.writeOver(this.ul);
}
}
};
Model.addObserver(List);
Body.addSubview(List);
document.addEventListener('DOMContentLoaded', function() {
Body.show();
});
</script>
</html>