-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsourceWindow.js
56 lines (52 loc) · 1.51 KB
/
sourceWindow.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
// TI calculator simulator
// Ken Shirriff, http://righto.com/ti
// Based on patent US3934233
//
// This file displays the source code in the source window.
var SourceWindow = function(elem, model, sourceCode) {
var height = 336 / 12; // height in lines
var top = 0;
var highlighted = null;
var addrToLine = [];
// Copy code into source window
(function init() {
var html = [];
var regex = /^([\d ]{3}) (.*)/;
for (var i = 0; i < sourceCode.length; i++) {
var comment = sourceCode[i].replace('; ', '');
var line = '<div id="s' + i + '">';
var m = sourceCode[i].match(regex);
if (m) {
addrToLine[parseInt(m[1], 10)] = i;
var addr = ('000' + m[1]).substr(-3);
var parts = m[2].split('; ');
var instr = parts[0];
instr = (instr + ' ').slice(0, 25);
var comment = parts.length > 1 ? parts[1] : null
line += '<span class="instr">' + instr + '</span>';
}
if (comment) {
line += '<span class="comment">' + comment + '</span>';
}
line += '</div>';
html.push(line);
}
elem.html(html.join('\n'));
}());
this.update = function() {
$('#s' + highlighted).removeClass('highlight');
if (!model.power) {
return;
}
highlighted = addrToLine[model.address];
$('#s' + highlighted).addClass('highlight');
top = elem.scrollTop() / 12;
if (highlighted >= top + height || highlighted < top) {
top = highlighted - 3;
if (top < 0) {
top = 0;
}
elem.scrollTop(top * 12);
}
}
};