This repository has been archived by the owner on May 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArticlesList.js
146 lines (128 loc) · 3.52 KB
/
ArticlesList.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
const UI = require('./UI').default;
const Entities = require('html-entities').XmlEntities;
const entities = new Entities();
const _ = require('lodash');
/**
* ArticlesList Class
*/
module.exports.default = class ArticlesList extends UI {
/**
* Constructs the ArticlesList object.
*
* @param {Object} args The arguments object
*/
constructor(args) {
super(args);
this._widget = this.driver.listtable({
'parent': this.screen,
'top': 0,
'left': 0,
'border': 'line',
'align': 'left',
'tags': true,
'keys': true,
'width': this.screen.width - 4,
'height': '100%',
'vi': false,
'mouse': false,
'noCellBorders': true,
'style': {
'border': {
'fg': 'white'
},
'header': {
'fg': 'cyan',
'bold': true
},
'cell': {
'fg': 'white',
'selected': {
'bg': 'red'
}
}
}
});
this._widget.focus();
}
/**
* Set raw data and process it
*
* @param {Object} raw The raw data
*/
set raw(raw) {
this._raw = raw;
this._data = this.buildDataFromRaw(raw);
this._widget.setData(this._data);
this.render();
}
/**
* Get raw data
*
* @return {Object} The raw data
*/
get raw() {
return this._raw;
}
/**
* Get the data
*
* @return {Object} The data
*/
get data() {
return this._data;
}
/**
* Builds data from raw data
*
* @param {Object} raw The raw data
* @return {Array} The data
*/
buildDataFromRaw(raw) {
let data = [
[
'{bold}Score{/bold}',
'{bold}Subreddit{/bold}',
'{bold}Title{/bold}'
]
];
_.forEach(raw, (item) => {
let title = entities.decode(item.data.title);
const subreddit = item.data.subreddit;
let scoreString = this.buildScoreString(item.data.score);
if(item.data.clicked === true || item.data.visited === true) {
title = `{gray-fg}${title}{/gray-fg}`;
}
if(item.data.over_18 === true) {
title = `{red-fg}[18+]{/red-fg} ${title}`;
}
if(item.data.saved === true) {
title = `{green-fg}[saved]{/green-fg} ${title}`;
}
data.push([
scoreString,
subreddit,
title
]);
});
return data;
}
/**
* Build the score string
*
* @param {number} score The score
* @return {string} The score string
*/
buildScoreString(score) {
let scoreString = score.toString();
if(score > 999) {
scoreString = (score / 1000).toFixed(1) + 'k';
} else if(score >= 15000 && score < 25000) {
scoreString = `{green-fg}${scoreString}{/green-fg}`;
} else if(score >= 25000 && score < 35000) {
scoreString = `{yellow-fg}${scoreString}{/yellow-fg}`;
} else if(score >= 35000) {
scoreString = `{red-fg}${scoreString}{/red-fg}`;
}
return scoreString;
}
};