-
Notifications
You must be signed in to change notification settings - Fork 404
/
scrollable_text_panel.js
125 lines (118 loc) · 3.54 KB
/
scrollable_text_panel.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
'use strict';
var View = require('./view');
var splitLines = require('../../utils/strutils').splitLines;
var Screen = require('./screen');
// This is a generic scrollable text viewer widget. Should be refactored
// out to another file or npm module at some point.
module.exports = View.extend({
defaults: {
visible: true,
text: '',
textLines: [],
scrollOffset: 0
},
// expect the attributes to have
// -----------------------------
//
// * line and col (top left coordinates)
// * height and width
initialize: function() {
var self = this;
if (!this.get('screen')) {
this.set('screen', new Screen());
}
this.updateTextLines();
this.observe(this, 'change:text change:width', function() {
self.updateTextLines();
});
this.observe(this, 'change:visible change:textLines change:height', function() {
self.render();
});
this.render();
},
updateTextLines: function() {
var text = this.get('text');
var lines = splitLines(text, this.get('width'));
this.set('textLines', lines);
},
scrollUp: function() {
var scrollOffset = this.get('scrollOffset');
if (scrollOffset > 0) {
scrollOffset--;
this.set('scrollOffset', scrollOffset, {silent: true});
this.render();
}
},
scrollDown: function() {
var height = this.get('height');
var scrollOffset = this.get('scrollOffset');
var textLines = this.get('textLines');
if (textLines.length > height + scrollOffset) {
scrollOffset++;
this.set('scrollOffset', scrollOffset, {silent: true});
this.render();
}
},
pageUp: function() {
var height = this.get('height');
var scrollOffset = this.get('scrollOffset');
this.scrollTo(Math.max(0, scrollOffset - height));
},
pageDown: function() {
var height = this.get('height');
var scrollOffset = this.get('scrollOffset');
var textLines = this.get('textLines');
this.scrollTo(Math.min(scrollOffset + height, textLines.length - height));
},
halfPageUp: function() {
var height = this.get('height');
var scrollOffset = this.get('scrollOffset');
this.scrollTo(Math.max(0, scrollOffset - Math.ceil(height / 2)));
},
halfPageDown: function() {
var height = this.get('height');
var scrollOffset = this.get('scrollOffset');
var textLines = this.get('textLines');
this.scrollTo(Math.min(scrollOffset + Math.ceil(height / 2), textLines.length - height));
},
scrollTo: function(newOffset) {
var scrollOffset = this.get('scrollOffset');
if (scrollOffset !== newOffset) {
this.set('scrollOffset', newOffset, {slient: true});
this.render();
}
},
render: function(firstOrLast) {
if (!this.get('visible')) {
return;
}
var screen = this.get('screen');
var startLine = this.get('line');
var col = this.get('col');
var width = this.get('width');
var height = this.get('height');
var textLines = this.get('textLines');
var scrollOffset = this.get('scrollOffset');
function renderLine(i) {
var idx = i + scrollOffset;
var textLine = textLines[idx] || '';
var output = textLine.toString();
screen
.position(col, startLine + i + 1)
.write(output);
if (textLine.length < width) {
screen.erase('end');
}
}
if (!firstOrLast) {
for (var i = 0; i < height; i++) {
renderLine(i);
}
} else if (firstOrLast === 'first') {
renderLine(0);
} else if (firstOrLast === 'last') {
renderLine(height - 1);
}
screen.display('reset');
}
});