forked from dgchurchill/nanowaspjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tapeview.js
189 lines (157 loc) · 6.32 KB
/
tapeview.js
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
/* NanoWasp - A MicroBee emulator
* Copyright (C) 2007, 2011 David G. Churchill
*
* This file is part of NanoWasp.
*
* NanoWasp is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* NanoWasp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var nanowasp = nanowasp || {};
nanowasp.TapeView = function (tape, parentBlockElement, onTapeSelected, onTapeEdited) {
this._tape = tape;
this._parentBlockElement = parentBlockElement;
this._form = null;
this._buildView(onTapeSelected, onTapeEdited);
};
nanowasp.TapeView.prototype = {
_buildView: function (onTapeSelected, onTapeEdited) {
var this_ = this; // For closures
this._nameSpan = document.createElement("span");
this._nameSpan.className = "link";
this._nameSpan.onclick = function () {
onTapeSelected(this_._tape);
};
this._nameSpan.appendChild(document.createTextNode(this._tape.title));
var editDiv = document.createElement("div");
editDiv.className = "link right";
editDiv.onclick = function () {
if (this_._form == null) {
this_._insertForm(onTapeEdited);
editDiv.innerHTML = "done";
} else {
this_._removeForm();
editDiv.innerHTML = "edit";
}
};
editDiv.appendChild(document.createTextNode("edit"));
this._parentBlockElement.appendChild(editDiv);
this._parentBlockElement.appendChild(this._nameSpan);
},
_insertForm: function (onTapeEdited) {
var this_ = this;
var toHex = function (v) { return "0x" + v.toString(16); };
var nameValidator = function (v) {
this_._nameSpan.innerHTML = "";
this_._nameSpan.appendChild(document.createTextNode(v));
return v;
};
this._form = this._createForm(
this._tape,
[
{ property: 'title', label: 'Name', validator: nameValidator },
{ property: 'typeCode', label: 'Type code' },
{ property: 'extra', label: 'Spare byte', validator: this._integerValidator(0, 0xFF), renderer: toHex },
{ property: 'startAddress', label: 'Load address', validator: this._integerValidator(0, 0xFFFF), renderer: toHex },
{ property: 'autoStartAddress', label: 'Start address', validator: this._integerValidator(0, 0xFFFF), renderer: toHex },
{ property: 'isAutoStart', label: 'Auto started', type: 'checkbox' }
],
2,
function () { onTapeEdited(this_._tape); });
this._parentBlockElement.appendChild(this._form);
},
_removeForm: function () {
if (this._form != null) {
this._form.parentNode.removeChild(this._form);
this._form = null;
}
},
_integerValidator: function(min, max) {
var validator = function (value) {
var base = null;
if (/^[0-9]+$/.test(value)) {
base = 10;
} else if (/^0x[0-9a-f]+$/i.test(value)) {
base = 16;
}
if (base == null) {
return undefined;
}
var result = parseInt(value, base);
if (result >= min && result <= max) {
return result;
}
return undefined;
};
validator.message = "Enter a number between " + min + " and " + max + ".";
return validator;
},
_createForm: function(data, fields, width, onChange) {
var form = document.createElement('form');
var have_width = typeof(width) != 'undefined';
var parent = form;
for (var i = 0; i < fields.length; ++i) {
if (have_width && i % width == 0) {
var parent = document.createElement('p');
form.appendChild(parent);
}
parent.appendChild(this._createInput(data, fields[i], onChange));
}
return form;
},
_createInput: function (data, field, onChange) {
var type = field.type;
if (type == undefined) {
type = 'text';
}
var input_el = document.createElement('input');
input_el.type = type;
var renderer = field.renderer;
if (renderer == undefined) {
renderer = function (v) { return v; };
}
if (type == 'checkbox') {
input_el.checked = renderer(data[field.property]);
} else {
input_el.value = renderer(data[field.property]);
}
var validator = field.validator;
if (validator == undefined) {
validator = function (v) { return v; };
}
input_el.onchange = function () {
var new_value;
if (type == 'checkbox') {
new_value = validator(input_el.checked);
} else {
new_value = validator(input_el.value);
}
if (new_value != undefined) {
data[field.property] = new_value;
label_el.className = "";
onChange();
} else {
label_el.className = "invalid";
}
};
var label_el = document.createElement('label');
var label_first = type != 'checkbox';
if (label_first) {
label_el.appendChild(document.createTextNode(field.label));
label_el.appendChild(input_el);
} else {
label_el.appendChild(input_el);
label_el.appendChild(document.createTextNode(field.label));
}
return label_el;
}
};