-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders.js
202 lines (177 loc) · 5.46 KB
/
providers.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
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
const vscode = require('vscode');
const path = require('path');
/**
* The provider for the todo list, which is a TreeView
*/
class TodoListProvider {
constructor(){
if (TodoListProvider.instance) {
return TodoListProvider.instance;
}
TodoListProvider.instance = this;
this._onDidChangeTreeData = new vscode.EventEmitter();
this.todoItems = [];
return this;
}
get onDidChangeTreeData() {
return this._onDidChangeTreeData.event;
}
/**
* Refresh the tree view of the todo list
*/
refresh() {
this._onDidChangeTreeData.fire();
}
/**
* Get a single item in the tree view
* @param {TodoItem} element The element to get
* @returns {TodoItem}
*/
getTreeItem(element) {
return element;
}
/**
* Helper function to get the children of an element in the TodoListProvider
* If no element is passed in, it returns the root elements
* @param {TodoItem} element The element to get the children of
* @returns {Promise<TodoItem[]>}
*/
getChildren(element) {
if(element) {
return Promise.resolve(element.getChildren());
} else {
return Promise.resolve(this.todoItems);
}
}
/**
* Creates a new todo item and adds it to the top level of the TodoListProvider
* @param {String} todoItemString Label for the new todo item
*/
addTodoItem(todoItemString) {
this.todoItems.push(new TodoItem(todoItemString, null));
//console.log(this.todoItems);
this._onDidChangeTreeData.fire();
}
/**
* Removes a top-level todo item from the TodoListProvider, should only be called by TodoItem.removeSelf()
* @param {TodoItem} todoItem The todo item to remove
*/
removeTodoItem(todoItem) {
this.todoItems = this.todoItems.filter(item => item !== todoItem);
this._onDidChangeTreeData.fire();
}
/**
* Clears all todo items from the TodoListProvider
*/
clearAll(){
this.todoItems = [];
this._onDidChangeTreeData.fire();
}
/**
* Marks all todo items as done
*/
doneAll(){
this.todoItems.forEach(item => {
item.recursiveDone();
});
}
}
/**
* TodoItem class to represent a single todo item in the tree view of TodoListProvider
* @extends vscode.TreeItem
*/
class TodoItem extends vscode.TreeItem {
constructor(
label,
parent
){
super(label, 0);
this.label = label;
this.parent = parent;
this.children = [];
this.contextValue = 'todoItem';
this.iconPath = {
light: path.join(__dirname, 'resources', 'light', 'open.svg'), // path for light theme icon
dark: path.join(__dirname, 'resources', 'dark', 'open.svg') // path for dark theme icon
};
}
/**
* @returns {boolean} True if the todo item has children, false otherwise
*/
hasChildren() {
return this.children.length > 0;
}
/**
* @returns {Promise<TodoItem[]>} The children of the todo item
*/
getChildren() {
return Promise.resolve(this.children);
}
/**
* Adds the specified TodoItem as a child of this TodoItem
* @param {TodoItem} childItemString
*/
addChildItem(childItemString) {
this.children.push(new TodoItem(childItemString, this));
this.collapsibleState = vscode.TreeItemCollapsibleState.Expanded;
TodoListProvider.instance.refresh();
}
/**
* Removes itself from the children list of its parent TodoItem
* If it has no parent, it removes itself from the TodoListProvider
*/
removeSelf() {
if(this.parent) {
this.parent.children = this.parent.children.filter(item => item !== this);
if(this.parent.children.length === 0) {
this.parent.collapsibleState = vscode.TreeItemCollapsibleState.None;
}
TodoListProvider.instance.refresh();
} else{
TodoListProvider.instance.removeTodoItem(this);
}
}
/**
* Edits the label of the todo item
* @param {String} newLabel
*/
editItem(newLabel) {
this.label = newLabel;
TodoListProvider.instance.refresh();
}
/**
* Marks the todo item as done
*/
doneItem() {
this.iconPath = {
light: path.join(__dirname, 'resources', 'light', 'complete.svg'), // path for light theme icon
dark: path.join(__dirname, 'resources', 'dark', 'complete.svg') // path for dark theme icon
};
this.contextValue = 'doneItem';
TodoListProvider.instance.refresh();
this.children.forEach(child => {
child.doneItem();
});
}
/**
* Recursively marks the todo item and all of its children as done
*/
recursiveDone(){
this.doneItem();
this.children.forEach(child => {
child.recursiveDone();
});
}
reopenItem() {
this.iconPath = {
light: path.join(__dirname, 'resources', 'light', 'open.svg'), // path for light theme icon
dark: path.join(__dirname, 'resources', 'dark', 'open.svg') // path for dark theme icon
};
this.contextValue = 'todoItem';
TodoListProvider.instance.refresh();
if(this.parent) {
this.parent.reopenItem();
}
}
}
module.exports = {TodoListProvider, TodoItem};