-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathlibrary-list-widget.ts
280 lines (266 loc) · 8.25 KB
/
library-list-widget.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import {
injectable,
postConstruct,
inject,
} from '@theia/core/shared/inversify';
import { Message } from '@theia/core/shared/@phosphor/messaging';
import { addEventListener } from '@theia/core/lib/browser/widgets/widget';
import { DialogProps } from '@theia/core/lib/browser/dialogs';
import { AbstractDialog } from '../theia/dialogs/dialogs';
import {
LibraryPackage,
LibrarySearch,
LibraryService,
} from '../../common/protocol/library-service';
import {
ListWidget,
UserAbortError,
} from '../widgets/component-list/list-widget';
import { Installable } from '../../common/protocol';
import { ListItemRenderer } from '../widgets/component-list/list-item-renderer';
import { nls } from '@theia/core/lib/common';
import { LibraryFilterRenderer } from '../widgets/component-list/filter-renderer';
import { findChildTheiaButton, splitByBoldTag } from '../utils/dom';
@injectable()
export class LibraryListWidget extends ListWidget<
LibraryPackage,
LibrarySearch
> {
static WIDGET_ID = 'library-list-widget';
static WIDGET_LABEL = nls.localize(
'arduino/library/title',
'Library Manager'
);
constructor(
@inject(LibraryService) private service: LibraryService,
@inject(ListItemRenderer) itemRenderer: ListItemRenderer<LibraryPackage>,
@inject(LibraryFilterRenderer) filterRenderer: LibraryFilterRenderer
) {
super({
id: LibraryListWidget.WIDGET_ID,
label: LibraryListWidget.WIDGET_LABEL,
iconClass: 'fa fa-arduino-library',
searchable: service,
installable: service,
itemLabel: (item: LibraryPackage) => item.name,
itemRenderer,
filterRenderer,
defaultSearchOptions: { query: '', type: 'All', topic: 'All' },
});
}
@postConstruct()
protected override init(): void {
super.init();
this.toDispose.pushAll([
this.notificationCenter.onLibraryDidInstall(() =>
this.refresh(undefined)
),
this.notificationCenter.onLibraryDidUninstall(() =>
this.refresh(undefined)
),
]);
}
protected override async install({
item,
progressId,
version,
}: {
item: LibraryPackage;
progressId: string;
version: Installable.Version;
}): Promise<void> {
const dependencies = await this.service.listDependencies({
item,
version,
filterSelf: true,
});
let installDependencies: boolean | undefined = undefined;
if (dependencies.length) {
const message = document.createElement('div');
const textContent =
dependencies.length === 1
? nls.localize(
'arduino/library/needsOneDependency',
'The library <b>{0}:{1}</b> needs another dependency currently not installed:',
item.name,
version
)
: nls.localize(
'arduino/library/needsMultipleDependencies',
'The library <b>{0}:{1}</b> needs some other dependencies currently not installed:',
item.name,
version
);
const segments = splitByBoldTag(textContent);
if (!segments) {
message.textContent = textContent;
} else {
segments.map((segment) => {
const span = document.createElement('span');
if (typeof segment === 'string') {
span.textContent = segment;
} else {
const bold = document.createElement('b');
bold.textContent = segment.textContent;
span.appendChild(bold);
}
message.appendChild(span);
});
}
const listContainer = document.createElement('div');
listContainer.style.maxHeight = '300px';
listContainer.style.overflowY = 'auto';
const list = document.createElement('ul');
list.style.listStyleType = 'none';
for (const { name } of dependencies) {
const listItem = document.createElement('li');
listItem.textContent = ` - ${name}`;
listItem.style.fontWeight = 'bold';
list.appendChild(listItem);
}
listContainer.appendChild(list);
message.appendChild(listContainer);
const question = document.createElement('div');
question.textContent =
dependencies.length === 1
? nls.localize(
'arduino/library/installOneMissingDependency',
'Would you like to install the missing dependency?'
)
: nls.localize(
'arduino/library/installMissingDependencies',
'Would you like to install all the missing dependencies?'
);
message.appendChild(question);
const result = await new MessageBoxDialog({
title: nls.localize(
'arduino/library/installLibraryDependencies',
'Install library dependencies'
),
message,
buttons: [
nls.localize(
'arduino/library/installWithoutDependencies',
'Install without dependencies'
),
nls.localize('arduino/library/installAll', 'Install All'),
],
maxWidth: 740, // Aligned with `settings-dialog.css`.
}).open();
if (result) {
const { response } = result;
if (response === 0) {
// Current only
installDependencies = false;
} else if (response === 1) {
// All
installDependencies = true;
}
} else {
throw new UserAbortError();
}
} else {
// The lib does not have any dependencies.
installDependencies = false;
}
if (typeof installDependencies === 'boolean') {
await this.service.install({
item,
version,
progressId,
installDependencies,
});
this.messageService.info(
nls.localize(
'arduino/library/installedSuccessfully',
'Successfully installed library {0}:{1}',
item.name,
version
),
{ timeout: 3000 }
);
}
}
protected override async uninstall({
item,
progressId,
}: {
item: LibraryPackage;
progressId: string;
}): Promise<void> {
await super.uninstall({ item, progressId });
this.messageService.info(
nls.localize(
'arduino/library/uninstalledSuccessfully',
'Successfully uninstalled library {0}:{1}',
item.name,
item.installedVersion!
),
{ timeout: 3000 }
);
}
}
class MessageBoxDialog extends AbstractDialog<MessageBoxDialog.Result> {
protected response: number;
constructor(protected readonly options: MessageBoxDialog.Options) {
super(options);
this.contentNode.appendChild(this.createMessageNode(this.options.message));
(
options.buttons || [nls.localize('vscode/issueMainService/ok', 'OK')]
).forEach((text, index) => {
const button = this.createButton(text);
const isPrimaryButton =
index === (options.buttons ? options.buttons.length - 1 : 0);
button.title = text;
button.classList.add(
isPrimaryButton ? 'main' : 'secondary',
'message-box-dialog-button'
);
this.controlPanel.appendChild(button);
this.toDisposeOnDetach.push(
addEventListener(button, 'click', () => {
this.response = index;
this.accept();
})
);
});
}
protected override onCloseRequest(message: Message): void {
super.onCloseRequest(message);
this.accept();
}
get value(): MessageBoxDialog.Result {
return { response: this.response };
}
protected createMessageNode(message: string | HTMLElement): HTMLElement {
if (typeof message === 'string') {
const messageNode = document.createElement('div');
messageNode.textContent = message;
return messageNode;
}
return message;
}
protected override handleEnter(event: KeyboardEvent): boolean | void {
this.response = 0;
super.handleEnter(event);
}
protected override onAfterAttach(message: Message): void {
super.onAfterAttach(message);
findChildTheiaButton(this.controlPanel)?.focus();
}
}
export namespace MessageBoxDialog {
export interface Options extends DialogProps {
/**
* When empty, `['OK']` will be inferred.
*/
buttons?: string[];
message: string | HTMLElement;
}
export interface Result {
/**
* The index of `buttons` that was clicked.
*/
readonly response: number;
}
}