/
leekTreeItem.ts
214 lines (203 loc) · 7.17 KB
/
leekTreeItem.ts
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { join } from 'path';
import { ExtensionContext, TreeItem, TreeItemCollapsibleState } from 'vscode';
import globalState from '../globalState';
import { DEFAULT_LABEL_FORMAT } from './constant';
import { FundInfo, IconType, TreeItemType } from './typed';
import { formatLabelString, formatTreeText } from './utils';
export class LeekTreeItem extends TreeItem {
info: FundInfo;
type: string | undefined;
isCategory: boolean;
contextValue: string | undefined;
_itemType?: TreeItemType;
constructor(info: FundInfo, context: ExtensionContext | undefined, isCategory = false) {
super('', TreeItemCollapsibleState.None);
this.info = info;
this.isCategory = isCategory;
const {
showLabel,
isStock,
name,
code,
type,
symbol,
percent,
price,
open,
yestclose,
high,
low,
updown,
volume,
amount = 0,
earnings,
priceDate,
time,
isStop,
t2,
contextValue,
_itemType,
} = info;
if (_itemType) {
this._itemType = _itemType;
} else {
this._itemType = isStock ? TreeItemType.STOCK : TreeItemType.FUND;
}
this.type = type;
this.contextValue = contextValue;
let _percent: number | string = Math.abs(percent);
if (isNaN(_percent)) {
_percent = '--';
} else {
_percent = _percent.toFixed(2);
}
let icon = 'up';
const grow = percent.indexOf('-') === 0 ? false : true;
const val = Math.abs(percent);
if (grow) {
if (IconType.ARROW === globalState.iconType) {
icon = val >= 2 ? 'up' : 'up1';
} else if (IconType.FOOD1 === globalState.iconType) {
icon = 'meat2';
} else if (IconType.FOOD2 === globalState.iconType) {
icon = 'kabob';
} else if (IconType.FOOD3 === globalState.iconType) {
icon = 'wine';
} else if (IconType.ICON_FOOD === globalState.iconType) {
icon = '🍗';
}
_percent = '+' + _percent;
} else {
if (IconType.ARROW === globalState.iconType) {
icon = val >= 2 ? 'down' : 'down1';
} else if (IconType.FOOD1 === globalState.iconType) {
icon = 'noodles';
} else if (IconType.FOOD2 === globalState.iconType) {
icon = 'bakeleek';
} else if (IconType.FOOD3 === globalState.iconType) {
icon = 'noodles';
} else if (IconType.ICON_FOOD === globalState.iconType) {
icon = '🍜';
}
_percent = '-' + _percent;
}
if (isStop) {
icon = 'stop';
}
let iconPath: string | undefined = '';
if (showLabel) {
iconPath =
globalState.iconType !== IconType.ICON_FOOD
? context?.asAbsolutePath(join('resources', `${icon}.svg`))
: icon;
}
const isIconPath = iconPath?.lastIndexOf('.svg') !== -1;
if (isIconPath && type !== 'nodata') {
this.iconPath = iconPath;
}
let text = '';
if (showLabel) {
/* `showLabel: true` */
if (this._itemType === TreeItemType.STOCK) {
const risePercent = isStop ? '停牌' : `${_percent}%`;
if (type === 'nodata') {
text = info.name;
} else {
/* text = `${!isIconPath ? iconPath : ''}${risePercent}${formatTreeText(
price,
15
)}「${name}」`; */
text = formatLabelString(
globalState.labelFormat?.['sidebarStockLabelFormat'] ??
DEFAULT_LABEL_FORMAT.sidebarStockLabelFormat,
{
...info,
icon: !isIconPath ? iconPath : '',
percent: risePercent,
}
);
}
} else if (this._itemType === TreeItemType.FUND) {
/* text =
`${!isIconPath ? iconPath : ''}${formatTreeText(`${_percent}%`)}「${name}」${
t2 || !(globalState.showEarnings && amount > 0)
? ''
: `(${grow ? '盈' : '亏'}:${grow ? '+' : ''}${earnings})`
}` + `${t2 ? `(${time})` : ''}`; */
text = formatLabelString(
globalState.labelFormat?.['sidebarFundLabelFormat'] ??
DEFAULT_LABEL_FORMAT.sidebarFundLabelFormat,
{
...info,
icon: !isIconPath ? iconPath : '',
percent: `${_percent}%`,
earnings:
t2 || !(globalState.showEarnings && amount > 0)
? ''
: `(${grow ? '盈' : '亏'}:${grow ? '+' : ''}${earnings})`,
time: t2 ? `(${time})` : '',
}
);
// ${earningPercent !== 0 ? ',率:' + earningPercent + '%' : ''}
} else if (this._itemType === TreeItemType.BINANCE) {
text = formatLabelString(
globalState.labelFormat?.['sidebarBinanceLabelFormat'] ??
DEFAULT_LABEL_FORMAT.sidebarBinanceLabelFormat,
{
...info,
icon: !isIconPath ? iconPath : '',
percent: `${_percent}%`,
}
);
}
} else {
/* `showLabel: false` */
text =
this._itemType === TreeItemType.STOCK
? `${formatTreeText(`${_percent}%`, 11)}${formatTreeText(price, 15)} 「${code}」`
: `${formatTreeText(`${_percent}%`)}「${code}」`;
}
if (globalState.enableHighlights === 0) {
this.label = text;
} else if (globalState.enableHighlights >= 1) {
this.label = grow ? { label: text, highlights: [[0, text.length]] } : text;
} else if (globalState.enableHighlights <= -1) {
this.label = grow ? text : { label: text, highlights: [[0, text.length]] };
}
this.id = info.id || code;
if (this._itemType === TreeItemType.STOCK || this._itemType === TreeItemType.FUND) {
this.command = {
title: name, // 标题
command:
this._itemType === TreeItemType.STOCK
? 'leek-fund.stockItemClick'
: 'leek-fund.fundItemClick', // 命令 ID
arguments: [
this._itemType === TreeItemType.STOCK ? '0' + symbol : code, // 基金/股票编码
name, // 基金/股票名称
text,
`${type}${symbol}`,
],
};
if (type === 'nodata') {
this.command.command = '';
}
}
if (this._itemType === TreeItemType.STOCK) {
const labelText = !showLabel ? name : '';
const isFuture = type === 'cnf_';
// type字段:期货没有前缀,去掉自定义的 `cnf_`。股票的 type 是交易所 (sz,sh)
const typeText = isFuture ? symbol?.replace('cnf_', '') : type;
const symbolText = isFuture ? '' : symbol;
if (type === 'nodata') {
this.tooltip = '接口不支持,右键删除关注';
} else {
this.tooltip = `【今日行情】${labelText}${typeText}${symbolText}\n 涨跌:${updown} 百分比:${_percent}%\n 最高:${high} 最低:${low}\n 今开:${open} 昨收:${yestclose}\n 成交量:${volume} 成交额:${amount}`;
}
} else if (this._itemType === TreeItemType.BINANCE) {
this.tooltip = `【今日行情】${name}\n 涨跌:${updown} 百分比:${_percent}%\n 最高:${high} 最低:${low}\n 今开:${open} 昨收:${yestclose}\n 成交量:${volume} 成交额:${amount}`;
} else {
this.tooltip = `「${name}」(${code})`;
}
}
}