This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 275
/
settings-view.js
381 lines (322 loc) · 11.8 KB
/
settings-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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/** @babel */
/** @jsx etch.dom */
import path from 'path'
import etch from 'etch'
import _ from 'underscore-plus'
import {CompositeDisposable, Disposable} from 'atom'
import GeneralPanel from './general-panel'
import EditorPanel from './editor-panel'
import PackageDetailView from './package-detail-view'
import KeybindingsPanel from './keybindings-panel'
import InstallPanel from './install-panel'
import ThemesPanel from './themes-panel'
import InstalledPackagesPanel from './installed-packages-panel'
import UpdatesPanel from './updates-panel'
import UriHandlerPanel from './uri-handler-panel'
export default class SettingsView {
constructor ({uri, packageManager, snippetsProvider, activePanel} = {}) {
this.uri = uri
this.packageManager = packageManager
this.snippetsProvider = snippetsProvider
this.deferredPanel = activePanel
this.destroyed = false
this.panelsByName = {}
this.panelCreateCallbacks = {}
etch.initialize(this)
this.disposables = new CompositeDisposable()
this.disposables.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.disposables.add(atom.packages.onDidActivateInitialPackages(() => {
this.disposables.add(
atom.packages.onDidActivatePackage(pack => this.removePanelCache(pack.name)),
atom.packages.onDidDeactivatePackage(pack => this.removePanelCache(pack.name))
)
}))
process.nextTick(() => this.initializePanels())
}
removePanelCache (name) {
delete this.panelsByName[name]
}
update () {}
destroy () {
this.destroyed = true
this.disposables.dispose()
for (let name in this.panelsByName) {
const panel = this.panelsByName[name]
panel.destroy()
}
return etch.destroy(this)
}
render () {
return (
<div className='settings-view pane-item' tabIndex='-1'>
<div className='config-menu' ref='sidebar'>
<ul className='panels-menu nav nav-pills nav-stacked' ref='panelMenu'>
<div className='panel-menu-separator' ref='menuSeparator' />
</ul>
<div className='button-area'>
<button className='btn btn-default icon icon-link-external' ref='openDotAtom'>Open Config Folder</button>
</div>
</div>
{/* The tabindex attr below ensures that clicks in a panel item won't
cause this view to gain focus. This is important because when this view
gains focus (e.g. immediately after atom displays it), it focuses the
currently active panel item. If that focusing causes the active panel to
scroll (e.g. because the active panel itself passes focus on to a search
box at the top of a scrolled panel), then the browser will not fire the
click event on the element within the panel on which the user originally
clicked (e.g. a package card). This would prevent us from showing a
package detail view when clicking on a package card. Phew! */}
<div className='panels' tabIndex='-1' ref='panels' />
</div>
)
}
// This prevents the view being actually disposed when closed
// If you remove it you will need to ensure the cached settingsView
// in main.coffee is correctly released on close as well...
onDidChangeTitle () { return new Disposable() }
initializePanels () {
if (this.refs.panels.children.length > 1) {
return
}
const clickHandler = (event) => {
const target = event.target.closest('.panels-menu li a, .panels-packages li a')
if (target) {
this.showPanel(target.closest('li').name)
}
}
this.element.addEventListener('click', clickHandler)
this.disposables.add(new Disposable(() => this.element.removeEventListener('click', clickHandler)))
const focusHandler = () => {
this.focusActivePanel()
}
this.element.addEventListener('focus', focusHandler)
this.disposables.add(new Disposable(() => this.element.removeEventListener('focus', focusHandler)))
const openDotAtomClickHandler = () => {
atom.open({pathsToOpen: [atom.getConfigDirPath()]})
}
this.refs.openDotAtom.addEventListener('click', openDotAtomClickHandler)
this.disposables.add(new Disposable(() => this.refs.openDotAtom.removeEventListener('click', openDotAtomClickHandler)))
this.addCorePanel('Core', 'settings', () => new GeneralPanel())
this.addCorePanel('Editor', 'code', () => new EditorPanel())
if (atom.config.getSchema('core.uriHandlerRegistration').type !== 'any') {
// "feature flag" based on core support for URI handling
this.addCorePanel('URI Handling', 'link', () => new UriHandlerPanel())
}
if ((process.platform === 'win32') && (require('atom').WinShell != null)) {
const SystemPanel = require('./system-windows-panel')
this.addCorePanel('System', 'device-desktop', () => new SystemPanel())
}
this.addCorePanel('Keybindings', 'keyboard', () => new KeybindingsPanel())
this.addCorePanel('Packages', 'package', () => new InstalledPackagesPanel(this, this.packageManager))
this.addCorePanel('Themes', 'paintcan', () => new ThemesPanel(this, this.packageManager))
this.addCorePanel('Updates', 'cloud-download', () => new UpdatesPanel(this, this.packageManager))
this.addCorePanel('Install', 'plus', () => new InstallPanel(this, this.packageManager))
this.showDeferredPanel()
if (!this.activePanel) {
this.showPanel('Core')
}
if (document.body.contains(this.element)) {
this.refs.sidebar.style.width = this.refs.sidebar.offsetWidth
}
}
serialize () {
return {
deserializer: 'SettingsView',
version: 2,
activePanel: this.activePanel != null ? this.activePanel : this.deferredPanel,
uri: this.uri
}
}
getPackages () {
let bundledPackageMetadataCache
if (this.packages != null) { return this.packages }
this.packages = atom.packages.getLoadedPackages()
try {
const packageMetadata = require(path.join(atom.getLoadSettings().resourcePath, 'package.json'))
bundledPackageMetadataCache = packageMetadata ? packageMetadata._atomPackages : null
} catch (error) {}
// Include disabled packages so they can be re-enabled from the UI
const disabledPackages = atom.config.get('core.disabledPackages') || []
for (const packageName of disabledPackages) {
var metadata
const packagePath = atom.packages.resolvePackagePath(packageName)
if (!packagePath) {
continue
}
try {
metadata = require(path.join(packagePath, 'package.json'))
} catch (error) {
if (bundledPackageMetadataCache && bundledPackageMetadataCache[packageName]) {
metadata = bundledPackageMetadataCache[packageName].metadata
}
}
if (metadata == null) {
continue
}
const name = metadata.name != null ? metadata.name : packageName
if (!_.findWhere(this.packages, {name})) {
this.packages.push({name, metadata, path: packagePath})
}
}
this.packages.sort((pack1, pack2) => {
const title1 = this.packageManager.getPackageTitle(pack1)
const title2 = this.packageManager.getPackageTitle(pack2)
return title1.localeCompare(title2)
})
return this.packages
}
addCorePanel (name, iconName, panelCreateCallback) {
const panelMenuItem = document.createElement('li')
panelMenuItem.name = name
panelMenuItem.setAttribute('name', name)
const a = document.createElement('a')
a.classList.add('icon', `icon-${iconName}`)
a.textContent = name
panelMenuItem.appendChild(a)
this.refs.menuSeparator.parentElement.insertBefore(panelMenuItem, this.refs.menuSeparator)
this.addPanel(name, panelCreateCallback)
}
addPanel (name, panelCreateCallback) {
this.panelCreateCallbacks[name] = panelCreateCallback
if (this.deferredPanel && this.deferredPanel.name === name) {
this.showDeferredPanel()
}
}
getOrCreatePanel (name, options) {
let panel = this.panelsByName[name]
if (panel) return panel
if (name in this.panelCreateCallbacks) {
panel = this.panelCreateCallbacks[name]()
delete this.panelCreateCallbacks[name]
} else if (options && options.pack) {
if (!options.pack.metadata) {
options.pack.metadata = _.clone(options.pack)
}
panel = new PackageDetailView(options.pack, this, this.packageManager, this.snippetsProvider)
}
if (panel) {
this.panelsByName[name] = panel
}
return panel
}
makePanelMenuActive (name) {
const previouslyActivePanel = this.refs.sidebar.querySelector('.active')
if (previouslyActivePanel) {
previouslyActivePanel.classList.remove('active')
}
const newActivePanel = this.refs.sidebar.querySelector(`[name='${name}']`)
if (newActivePanel) {
newActivePanel.classList.add('active')
}
}
focusActivePanel () {
// Pass focus to panel that is currently visible
for (let i = 0; i < this.refs.panels.children.length; i++) {
const child = this.refs.panels.children[i]
if (child.offsetWidth > 0) {
child.focus()
}
}
}
showDeferredPanel () {
if (this.deferredPanel) {
const {name, options} = this.deferredPanel
this.showPanel(name, options)
}
}
// Public: show a panel.
//
// * `name` {String} the name of the panel to show
// * `options` {Object} an options hash. Will be passed to `beforeShow()` on
// the panel. Options may include (but are not limited to):
// * `uri` the URI the panel was launched from
showPanel (name, options) {
const panel = this.getOrCreatePanel(name, options)
if (panel) {
this.appendPanel(panel, options)
this.makePanelMenuActive(name)
this.setActivePanel(name, options)
this.deferredPanel = null
} else {
this.deferredPanel = {name, options}
}
}
showPanelForURI (uri) {
const regex = /config\/([a-z]+)\/?([a-zA-Z0-9_-]+)?/i
const match = regex.exec(uri)
if (match) {
const path1 = match[1]
const path2 = match[2]
if (path1 === 'packages' && path2 != null) {
this.showPanel(path2, {
uri: uri,
pack: {name: path2},
back: atom.packages.getLoadedPackage(path2) ? 'Packages' : null
})
} else {
const panelName = path1[0].toUpperCase() + path1.slice(1)
this.showPanel(panelName, {uri})
}
}
}
appendPanel (panel, options) {
for (let i = 0; i < this.refs.panels.children.length; i++) {
this.refs.panels.children[i].style.display = 'none'
}
if (!this.refs.panels.contains(panel.element)) {
this.refs.panels.appendChild(panel.element)
}
if (panel.beforeShow) {
panel.beforeShow(options)
}
panel.show()
panel.focus()
}
setActivePanel (name, options = {}) {
this.activePanel = {name, options}
}
removePanel (name) {
const panel = this.panelsByName[name]
if (panel) {
panel.destroy()
delete this.panelsByName[name]
}
}
getTitle () {
return 'Settings'
}
getIconName () {
return 'tools'
}
getURI () {
return this.uri
}
isEqual (other) {
return other instanceof SettingsView
}
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
}
}