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 277
/
Copy pathpackage-updates-status-view.js
167 lines (138 loc) · 5.5 KB
/
package-updates-status-view.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
/** @babel */
import _ from 'underscore-plus'
import {CompositeDisposable, Disposable} from 'atom'
export default class PackageUpdatesStatusView {
initialize (statusBar, packageManager, updates) {
this.statusBar = statusBar
this.updates = updates
this.destroyed = true
this.updatingPackages = []
this.failedUpdates = []
this.disposables = new CompositeDisposable()
this.element = document.createElement('div')
this.element.classList.add('package-updates-status-view', 'inline-block', 'text', 'text-info')
const iconPackage = document.createElement('span')
iconPackage.classList.add('icon', 'icon-package')
this.element.appendChild(iconPackage)
this.countLabel = document.createElement('span')
this.countLabel.classList.add('available-updates-status')
this.element.appendChild(this.countLabel)
this.disposables.add(packageManager.on('package-update-available theme-update-available', ({pack, error}) => { this.onPackageUpdateAvailable(pack) }))
this.disposables.add(packageManager.on('package-updating theme-updating', ({pack, error}) => { this.onPackageUpdating(pack) }))
this.disposables.add(packageManager.on('package-updated theme-updated package-uninstalled theme-uninstalled', ({pack, error}) => { this.onPackageUpdated(pack) }))
this.disposables.add(packageManager.on('package-update-failed theme-update-failed', ({pack, error}) => { this.onPackageUpdateFailed(pack) }))
this.disposables.add(atom.config.onDidChange('core.disabledPackages', () => { this.updateTile() }))
const clickHandler = () => {
atom.commands.dispatch(atom.views.getView(atom.workspace), 'settings-view:check-for-package-updates')
}
this.element.addEventListener('click', clickHandler)
this.disposables.add(new Disposable(() => { this.element.removeEventListener('click', clickHandler) }))
this.updateTile()
}
destroy () {
this.disposables.dispose()
this.element.remove()
if (this.tile) {
this.tile.destroy()
this.tile = null
}
if (this.tooltip) {
this.tooltip.dispose()
this.tooltip = null
}
}
onPackageUpdateAvailable (pack) {
for (const update of this.updates) {
if (update.name === pack.name) {
return
}
}
this.updates.push(pack)
this.updateTile()
}
onPackageUpdating (pack) {
// Wipe failed status when an update is retried
for (let index = 0; index < this.failedUpdates.length; index++) {
const update = this.failedUpdates[index]
if (update.name === pack.name) {
this.failedUpdates.splice(index, 1)
}
}
this.updatingPackages.push(pack)
this.updateTile()
}
onPackageUpdated (pack) {
for (let index = 0; index < this.updates.length; index++) {
const update = this.updates[index]
if (update.name === pack.name) {
this.updates.splice(index, 1)
}
}
for (let index = 0; index < this.updatingPackages.length; index++) {
const update = this.updatingPackages[index]
if (update.name === pack.name) {
this.updatingPackages.splice(index, 1)
}
}
for (let index = 0; index < this.failedUpdates.length; index++) {
const update = this.failedUpdates[index]
if (update.name === pack.name) {
this.failedUpdates.splice(index, 1)
}
}
this.updateTile()
}
onPackageUpdateFailed (pack) {
for (const update of this.failedUpdates) {
if (update.name === pack.name) {
return
}
}
for (let index = 0; index < this.updatingPackages.length; index++) {
const update = this.updatingPackages[index]
if (update.name === pack.name) {
this.updatingPackages.splice(index, 1)
}
}
this.failedUpdates.push(pack)
this.updateTile()
}
updateTile () {
const disabledPackages = []
if (this.updates.length){
const updatesWithoutDisabled = this.updates.filter(update => atom.packages.isPackageDisabled(update.name))
updatesWithoutDisabled.filter(update => this.updates.splice(this.updates.indexOf(update),1))
updatesWithoutDisabled.filter(update => disabledPackages.push(update))
}
if (this.updates.length) {
if (this.tooltip) {
this.tooltip.dispose()
this.tooltip = null
}
if (this.destroyed) {
// Priority of -99 should put us just to the left of the Squirrel icon, which displays when Atom has updates available
this.tile = this.statusBar.addRightTile({item: this, priority: -99})
this.destroyed = false
}
let labelText = `${_.pluralize(this.updates.length, 'update')}` // 5 updates
let tooltipText = `${_.pluralize(this.updates.length, 'package update')} available`
if (this.updatingPackages.length) {
labelText = `${this.updatingPackages.length}/${this.updates.length} updating` // 3/5 updating
tooltipText += `, ${_.pluralize(this.updatingPackages.length, 'package')} currently updating`
}
if (this.failedUpdates.length) {
labelText += ` (${this.failedUpdates.length} failed)` // 1 update (1 failed), or 3/5 updating (1 failed)
tooltipText += `, ${_.pluralize(this.failedUpdates.length, 'failed update')}`
}
this.countLabel.textContent = labelText
this.tooltip = atom.tooltips.add(this.element, {title: tooltipText})
} else if (!this.destroyed) {
this.tile.destroy()
this.tile = null
this.destroyed = true
}
if (disabledPackages.length){
disabledPackages.filter(update => this.updates.push(update))
}
}
}