-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.js
93 lines (74 loc) · 2.02 KB
/
view.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
var blessed = require('blessed');
var Screen = require('./component/Screen');
var LoadingBox = require('./component/LoadingBox');
var List = require('./component/List');
var TitleBar = require('./component/TitleBar');
var screen, list, loadingBox, titleBar;
var prevIndex = 0;
exports.init = function(options){
options.title = options.title || 'TechBridge';
screen = Screen(blessed);
screen.title = options.title;
screen.key(['escape', 'q', 'C-c'], function(ch, key) {
return process.exit(0);
});
}
exports.loading = function(){
loadingBox = LoadingBox(blessed);
screen.append(loadingBox);
screen.render();
}
exports.render = function(options){
var length = options.items.length;
var items = options.items.map(function(item){
return options.getTitle(item);
});
if(list){
screen.remove(list);
list.setItems([])
list.destroy();
list = null;
}
titleBar = TitleBar(blessed);
list = List(blessed, items);
if(loadingBox){
loadingBox.destroy();
}
screen.append(titleBar);
screen.append(list);
showLineNumber(1, length);
if(options.type=='main'){
list.select(prevIndex);
showLineNumber(prevIndex+1, length);
}
list.focus();
screen.render();
list.on('select', function(item){
var index = item.index - item.parent.index - 1;
if(options.type=='main') prevIndex = index;
options.select(index);
})
list.key('right', function(){
if(options.type=='main') prevIndex = this.selected;
options.select(this.selected);
})
list.key('up', function(){
showLineNumber(this.selected+1, length);
});
list.key('down', function(){
showLineNumber(this.selected+1, length);
});
list.key('pagedown', function(){
list.move(10);
showLineNumber(this.selected+1, length);
})
list.key('pageup', function(){
list.move(-10);
showLineNumber(this.selected+1, length);
})
list.key('left', options.left || function(){});
}
function showLineNumber(now, total){
titleBar.setLine(3, '目前文章:' + now + '/' + total);
screen.render();
}