This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathinstalled-packages-panel.js
348 lines (291 loc) · 13.1 KB
/
installed-packages-panel.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/** @babel */
/** @jsx etch.dom */
import {CompositeDisposable, TextEditor} from 'atom'
import etch from 'etch'
import fuzzaldrin from 'fuzzaldrin'
import CollapsibleSectionPanel from './collapsible-section-panel'
import PackageCard from './package-card'
import ErrorView from './error-view'
import List from './list'
import ListView from './list-view'
import {ownerFromRepository, packageComparatorAscending} from './utils'
export default class InstalledPackagesPanel extends CollapsibleSectionPanel {
static loadPackagesDelay () {
return 300
}
constructor (settingsView, packageManager) {
super()
etch.initialize(this)
this.settingsView = settingsView
this.packageManager = packageManager
this.items = {
dev: new List('name'),
core: new List('name'),
user: new List('name'),
git: new List('name'),
deprecated: new List('name')
}
this.itemViews = {
dev: new ListView(this.items.dev, this.refs.devPackages, this.createPackageCard.bind(this)),
core: new ListView(this.items.core, this.refs.corePackages, this.createPackageCard.bind(this)),
user: new ListView(this.items.user, this.refs.communityPackages, this.createPackageCard.bind(this)),
git: new ListView(this.items.git, this.refs.gitPackages, this.createPackageCard.bind(this)),
deprecated: new ListView(this.items.deprecated, this.refs.deprecatedPackages, this.createPackageCard.bind(this))
}
this.subscriptions = new CompositeDisposable()
this.subscriptions.add(
this.refs.filterEditor.onDidStopChanging(() => { this.matchPackages() })
)
this.subscriptions.add(
this.packageManager.on('package-install-failed theme-install-failed package-uninstall-failed theme-uninstall-failed package-update-failed theme-update-failed', ({pack, error}) => {
this.refs.updateErrors.appendChild(new ErrorView(this.packageManager, error).element)
})
)
let loadPackagesTimeout
this.subscriptions.add(
this.packageManager.on('package-updated package-installed package-uninstalled package-installed-alternative', () => {
clearTimeout(loadPackagesTimeout)
loadPackagesTimeout = setTimeout(this.loadPackages.bind(this), InstalledPackagesPanel.loadPackagesDelay())
})
)
this.subscriptions.add(this.handleEvents())
this.subscriptions.add(atom.commands.add(this.element, {
'core:move-up': () => { this.scrollUp() },
'core:move-down': () => { this.scrollDown() },
'core:page-up': () => { this.pageUp() },
'core:page-down': () => { this.pageDown() },
'core:move-to-top': () => { this.scrollToTop() },
'core:move-to-bottom': () => { this.scrollToBottom() }
}))
this.loadPackages()
}
focus () {
this.refs.filterEditor.element.focus()
}
show () {
this.element.style.display = ''
}
destroy () {
this.subscriptions.dispose()
return etch.destroy(this)
}
update () {}
render () {
return (
<div className='panels-item' tabIndex='-1'>
<section className='section'>
<div className='section-container'>
<div className='section-heading icon icon-package'>
Installed Packages
<span ref='totalPackages' className='section-heading-count badge badge-flexible'>…</span>
</div>
<div className='editor-container'>
<TextEditor ref='filterEditor' mini placeholderText='Filter packages by name' />
</div>
<div ref='updateErrors' />
<section ref='deprecatedSection' className='sub-section deprecated-packages'>
<h3 ref='deprecatedPackagesHeader' className='sub-section-heading icon icon-package'>
Deprecated Packages
<span ref='deprecatedCount' className='section-heading-count badge badge-flexible'>…</span>
</h3>
<p>Atom does not load deprecated packages. These packages may have updates available.</p>
<div ref='deprecatedPackages' className='container package-container'>
<div ref='deprecatedLoadingArea' className='alert alert-info loading-area icon icon-hourglass'>Loading packages…</div>
</div>
</section>
<section className='sub-section installed-packages'>
<h3 ref='communityPackagesHeader' className='sub-section-heading icon icon-package'>
Community Packages
<span ref='communityCount' className='section-heading-count badge badge-flexible'>…</span>
</h3>
<div ref='communityPackages' className='container package-container'>
<div ref='communityLoadingArea' className='alert alert-info loading-area icon icon-hourglass'>Loading packages…</div>
</div>
</section>
<section className='sub-section core-packages'>
<h3 ref='corePackagesHeader' className='sub-section-heading icon icon-package'>
Core Packages
<span ref='coreCount' className='section-heading-count badge badge-flexible'>…</span>
</h3>
<div ref='corePackages' className='container package-container'>
<div ref='coreLoadingArea' className='alert alert-info loading-area icon icon-hourglass'>Loading packages…</div>
</div>
</section>
<section className='sub-section dev-packages'>
<h3 ref='devPackagesHeader' className='sub-section-heading icon icon-package'>
Development Packages
<span ref='devCount' className='section-heading-count badge badge-flexible'>…</span>
</h3>
<div ref='devPackages' className='container package-container'>
<div ref='devLoadingArea' className='alert alert-info loading-area icon icon-hourglass'>Loading packages…</div>
</div>
</section>
<section className='sub-section git-packages'>
<h3 ref='gitPackagesHeader' className='sub-section-heading icon icon-package'>
Git Packages
<span ref='gitCount' className='section-heading-count badge badge-flexible'>…</span>
</h3>
<div ref='gitPackages' className='container package-container'>
<div ref='gitLoadingArea' className='alert alert-info loading-area icon icon-hourglass'>Loading packages…</div>
</div>
</section>
</div>
</section>
</div>
)
}
filterPackages (packages) {
packages.dev = packages.dev.filter(({theme}) => !theme)
packages.user = packages.user.filter(({theme}) => !theme)
packages.deprecated = packages.user.filter(({name, version}) => atom.packages.isDeprecatedPackage(name, version))
packages.core = packages.core.filter(({theme}) => !theme)
packages.git = (packages.git || []).filter(({theme}) => !theme)
for (let pack of packages.core) {
if (pack.repository == null) {
pack.repository = `https://github.com/atom/${pack.name}`
}
}
for (let packageType of ['dev', 'core', 'user', 'git', 'deprecated']) {
for (let pack of packages[packageType]) {
pack.owner = ownerFromRepository(pack.repository)
}
}
return packages
}
sortPackages (packages) {
packages.dev.sort(packageComparatorAscending)
packages.core.sort(packageComparatorAscending)
packages.user.sort(packageComparatorAscending)
packages.git.sort(packageComparatorAscending)
packages.deprecated.sort(packageComparatorAscending)
return packages
}
loadPackages () {
const packagesWithUpdates = {}
this.packageManager.getOutdated().then((packages) => {
for (let {name, latestVersion} of packages) {
packagesWithUpdates[name] = latestVersion
}
this.displayPackageUpdates(packagesWithUpdates)
})
this.packageManager.getInstalled().then((packages) => {
this.packages = this.sortPackages(this.filterPackages(packages))
this.refs.devLoadingArea.remove()
this.items.dev.setItems(this.packages.dev)
this.refs.coreLoadingArea.remove()
this.items.core.setItems(this.packages.core)
this.refs.communityLoadingArea.remove()
this.items.user.setItems(this.packages.user)
this.refs.gitLoadingArea.remove()
this.items.git.setItems(this.packages.git)
if (this.packages.deprecated.length) {
this.refs.deprecatedSection.style.display = ''
} else {
this.refs.deprecatedSection.style.display = 'none'
}
this.refs.deprecatedLoadingArea.remove()
this.items.deprecated.setItems(this.packages.deprecated)
// TODO show empty mesage per section
this.updateSectionCounts()
this.displayPackageUpdates(packagesWithUpdates)
this.matchPackages()
}).catch((error) => {
console.error(error.message, error.stack)
})
}
displayPackageUpdates (packagesWithUpdates) {
for (const packageType of ['dev', 'core', 'user', 'git', 'deprecated']) {
for (const packageCard of this.itemViews[packageType].getViews()) {
const newVersion = packagesWithUpdates[packageCard.pack.name]
if (newVersion) {
packageCard.displayAvailableUpdate(newVersion)
}
}
}
}
createPackageCard (pack) {
return new PackageCard(pack, this.settingsView, this.packageManager, {back: 'Packages'})
}
filterPackageListByText (text) {
if (!this.packages) {
return
}
for (let packageType of ['dev', 'core', 'user', 'git', 'deprecated']) {
const allViews = this.itemViews[packageType].getViews()
const activeViews = this.itemViews[packageType].filterViews((pack) => {
if (text === '') {
return true
} else {
const owner = pack.owner != null ? pack.owner : ownerFromRepository(pack.repository)
const filterText = `${pack.name} ${owner}`
return fuzzaldrin.score(filterText, text) > 0
}
})
for (const view of allViews) {
if (view) {
view.element.style.display = 'none'
view.element.classList.add('hidden')
}
}
for (const view of activeViews) {
if (view) {
view.element.style.display = ''
view.element.classList.remove('hidden')
}
}
}
this.updateSectionCounts()
}
updateUnfilteredSectionCounts () {
this.updateSectionCount(this.refs.deprecatedPackagesHeader, this.refs.deprecatedCount, this.packages.deprecated.length)
this.updateSectionCount(this.refs.communityPackagesHeader, this.refs.communityCount, this.packages.user.length)
this.updateSectionCount(this.refs.corePackagesHeader, this.refs.coreCount, this.packages.core.length)
this.updateSectionCount(this.refs.devPackagesHeader, this.refs.devCount, this.packages.dev.length)
this.updateSectionCount(this.refs.gitPackagesHeader, this.refs.gitCount, this.packages.git.length)
const totalPackages =
this.packages.user.length +
this.packages.core.length +
this.packages.dev.length +
this.packages.git.length
this.refs.totalPackages.textContent = totalPackages.toString()
}
updateFilteredSectionCounts () {
const deprecated = this.notHiddenCardsLength(this.refs.deprecatedPackages)
this.updateSectionCount(this.refs.deprecatedPackagesHeader, this.refs.deprecatedCount, deprecated, this.packages.deprecated.length)
const community = this.notHiddenCardsLength(this.refs.communityPackages)
this.updateSectionCount(this.refs.communityPackagesHeader, this.refs.communityCount, community, this.packages.user.length)
const core = this.notHiddenCardsLength(this.refs.corePackages)
this.updateSectionCount(this.refs.corePackagesHeader, this.refs.coreCount, core, this.packages.core.length)
const dev = this.notHiddenCardsLength(this.refs.devPackages)
this.updateSectionCount(this.refs.devPackagesHeader, this.refs.devCount, dev, this.packages.dev.length)
const git = this.notHiddenCardsLength(this.refs.gitPackages)
this.updateSectionCount(this.refs.gitPackagesHeader, this.refs.gitCount, git, this.packages.git.length)
const shownPackages = dev + core + community + git
const totalPackages = this.packages.user.length + this.packages.core.length + this.packages.dev.length + this.packages.git.length
this.refs.totalPackages.textContent = `${shownPackages}/${totalPackages}`
}
resetSectionHasItems () {
this.resetCollapsibleSections([this.refs.deprecatedPackagesHeader, this.refs.communityPackagesHeader, this.refs.corePackagesHeader, this.refs.devPackagesHeader, this.refs.gitPackagesHeader])
}
matchPackages () {
this.filterPackageListByText(this.refs.filterEditor.getText())
}
scrollUp () {
this.element.scrollTop -= document.body.offsetHeight / 20
}
scrollDown () {
this.element.scrollTop += document.body.offsetHeight / 20
}
pageUp () {
this.element.scrollTop -= this.element.offsetHeight
}
pageDown () {
this.element.scrollTop += this.element.offsetHeight
}
scrollToTop () {
this.element.scrollTop = 0
}
scrollToBottom () {
this.element.scrollTop = this.element.scrollHeight
}
}