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-detail-view.js
517 lines (437 loc) · 16.2 KB
/
package-detail-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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/** @babel */
/** @jsx etch.dom */
import path from 'path'
import url from 'url'
import _ from 'underscore-plus'
import fs from 'fs-plus'
import {shell} from 'electron'
import {CompositeDisposable, Disposable} from 'atom'
import etch from 'etch'
import PackageCard from './package-card'
import PackageGrammarsView from './package-grammars-view'
import PackageKeymapView from './package-keymap-view'
import PackageReadmeView from './package-readme-view'
import PackageSnippetsView from './package-snippets-view'
import SettingsPanel from './settings-panel'
const NORMALIZE_PACKAGE_DATA_README_ERROR = 'ERROR: No README data found!'
export default class PackageDetailView {
constructor (pack, settingsView, packageManager, snippetsProvider) {
this.pack = pack
this.settingsView = settingsView
this.packageManager = packageManager
this.snippetsProvider = snippetsProvider
this.disposables = new CompositeDisposable()
etch.initialize(this)
this.loadPackage()
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() }
}))
const packageRepoClickHandler = (event) => {
event.preventDefault()
const repoUrl = this.packageManager.getRepositoryUrl(this.pack)
if (typeof repoUrl === 'string') {
if (url.parse(repoUrl).pathname === '/atom/atom') {
shell.openExternal(`${repoUrl}/tree/master/packages/${this.pack.name}`)
} else {
shell.openExternal(repoUrl)
}
}
}
this.refs.packageRepo.addEventListener('click', packageRepoClickHandler)
this.disposables.add(new Disposable(() => { this.refs.packageRepo.removeEventListener('click', packageRepoClickHandler) }))
const issueButtonClickHandler = (event) => {
event.preventDefault()
let bugUri = this.packageManager.getRepositoryBugUri(this.pack)
if (bugUri) {
shell.openExternal(bugUri)
}
}
this.refs.issueButton.addEventListener('click', issueButtonClickHandler)
this.disposables.add(new Disposable(() => { this.refs.issueButton.removeEventListener('click', issueButtonClickHandler) }))
const changelogButtonClickHandler = (event) => {
event.preventDefault()
if (this.changelogPath) {
this.openMarkdownFile(this.changelogPath)
}
}
this.refs.changelogButton.addEventListener('click', changelogButtonClickHandler)
this.disposables.add(new Disposable(() => { this.refs.changelogButton.removeEventListener('click', changelogButtonClickHandler) }))
const licenseButtonClickHandler = (event) => {
event.preventDefault()
if (this.licensePath) {
this.openMarkdownFile(this.licensePath)
}
}
this.refs.licenseButton.addEventListener('click', licenseButtonClickHandler)
this.disposables.add(new Disposable(() => { this.refs.licenseButton.removeEventListener('click', licenseButtonClickHandler) }))
const openButtonClickHandler = (event) => {
event.preventDefault()
if (fs.existsSync(this.pack.path)) {
atom.open({pathsToOpen: [this.pack.path]})
}
}
this.refs.openButton.addEventListener('click', openButtonClickHandler)
this.disposables.add(new Disposable(() => { this.refs.openButton.removeEventListener('click', openButtonClickHandler) }))
const learnMoreButtonClickHandler = (event) => {
event.preventDefault()
shell.openExternal(`https://atom.io/packages/${this.pack.name}`)
}
this.refs.learnMoreButton.addEventListener('click', learnMoreButtonClickHandler)
this.disposables.add(new Disposable(() => { this.refs.learnMoreButton.removeEventListener('click', learnMoreButtonClickHandler) }))
const breadcrumbClickHandler = (event) => {
event.preventDefault()
this.settingsView.showPanel(this.breadcrumbBackPanel)
}
this.refs.breadcrumb.addEventListener('click', breadcrumbClickHandler)
this.disposables.add(new Disposable(() => { this.refs.breadcrumb.removeEventListener('click', breadcrumbClickHandler) }))
}
completeInitialization () {
if (this.refs.packageCard) {
this.packageCard = this.refs.packageCard.packageCard
} else if (!this.packageCard) { // Had to load this from the network
this.packageCard = new PackageCard(this.pack.metadata, this.settingsView, this.packageManager, {onSettingsView: true})
this.refs.packageCardParent.replaceChild(this.packageCard.element, this.refs.loadingMessage)
}
this.refs.packageRepo.classList.remove('hidden')
this.refs.startupTime.classList.remove('hidden')
this.refs.buttons.classList.remove('hidden')
this.activateConfig()
this.populate()
this.updateFileButtons()
this.subscribeToPackageManager()
this.renderReadme()
}
loadPackage () {
const loadedPackage = atom.packages.getLoadedPackage(this.pack.name)
if (loadedPackage) {
this.pack = loadedPackage
this.completeInitialization()
} else {
// If the package metadata in `@pack` isn't complete, hit the network.
if (!this.pack.metadata || !this.pack.metadata.owner) {
this.fetchPackage()
} else {
this.completeInitialization()
}
}
}
fetchPackage () {
this.showLoadingMessage()
this.packageManager.getClient().package(this.pack.name, (err, packageData) => {
if (err || !packageData || !packageData.name) {
this.hideLoadingMessage()
this.showErrorMessage()
} else {
this.pack = packageData
// TODO: this should match Package.loadMetadata from core, but this is
// an acceptable hacky workaround
this.pack.metadata = _.extend(this.pack.metadata != null ? this.pack.metadata : {}, this.pack)
this.completeInitialization()
}
})
}
showLoadingMessage () {
this.refs.loadingMessage.classList.remove('hidden')
}
hideLoadingMessage () {
this.refs.loadingMessage.classList.add('hidden')
}
showErrorMessage () {
this.refs.errorMessage.classList.remove('hidden')
}
hideErrorMessage () {
this.refs.errorMessage.classList.add('hidden')
}
activateConfig () {
// Package.activateConfig() is part of the Private package API and should not be used outside of core.
if (atom.packages.isPackageLoaded(this.pack.name) && !atom.packages.isPackageActive(this.pack.name)) {
this.pack.activateConfig()
}
}
destroy () {
if (this.settingsPanel) {
this.settingsPanel.destroy()
this.settingsPanel = null
}
if (this.keymapView) {
this.keymapView.destroy()
this.keymapView = null
}
if (this.grammarsView) {
this.grammarsView.destroy()
this.grammarsView = null
}
if (this.snippetsView) {
this.snippetsView.destroy()
this.snippetsView = null
}
if (this.readmeView) {
this.readmeView.destroy()
this.readmeView = null
}
if (this.packageCard) {
this.packageCard.destroy()
this.packageCard = null
}
this.disposables.dispose()
return etch.destroy(this)
}
update () {}
beforeShow (opts) {
if (opts.back == null) {
opts.back = 'Install'
}
this.breadcrumbBackPanel = opts.back
this.refs.breadcrumb.textContent = this.breadcrumbBackPanel
}
show () {
this.element.style.display = ''
}
focus () {
this.element.focus()
}
render () {
let packageCardView
if (this.pack && this.pack.metadata && this.pack.metadata.owner) {
packageCardView = (
<div ref='packageCardParent' className='row'>
<PackageCardComponent
ref='packageCard'
settingsView={this.settingsView}
packageManager={this.packageManager}
metadata={this.pack.metadata}
options={{onSettingsView: true}} />
</div>
)
} else {
packageCardView = (
<div ref='packageCardParent' className='row'>
<div ref='loadingMessage' className='alert alert-info icon icon-hourglass'>{`Loading ${this.pack.name}\u2026`}</div>
<div ref='errorMessage' className='alert alert-danger icon icon-hourglass hidden'>Failed to load {this.pack.name} - try again later.</div>
</div>
)
}
return (
<div tabIndex='0' className='package-detail'>
<ol ref='breadcrumbContainer' className='native-key-bindings breadcrumb' tabIndex='-1'>
<li>
<a ref='breadcrumb' />
</li>
<li className='active'>
<a ref='title' />
</li>
</ol>
<div className='panels-item'>
<section className='section'>
<form className='section-container package-detail-view'>
<div className='container package-container'>
{packageCardView}
</div>
<p ref='packageRepo' className='link icon icon-repo repo-link hidden' />
<p ref='startupTime' className='text icon icon-dashboard hidden' tabIndex='-1' />
<div ref='buttons' className='btn-wrap-group hidden'>
<button ref='learnMoreButton' className='btn btn-default icon icon-link'>View on Atom.io</button>
<button ref='issueButton' className='btn btn-default icon icon-bug'>Report Issue</button>
<button ref='changelogButton' className='btn btn-default icon icon-squirrel'>CHANGELOG</button>
<button ref='licenseButton' className='btn btn-default icon icon-law'>LICENSE</button>
<button ref='openButton' className='btn btn-default icon icon-link-external'>View Code</button>
</div>
<div ref='errors' />
</form>
</section>
<div ref='sections' />
</div>
</div>
)
}
populate () {
this.refs.title.textContent = `${_.undasherize(_.uncamelcase(this.pack.name))}`
this.type = this.pack.metadata.theme ? 'theme' : 'package'
const repoUrl = this.packageManager.getRepositoryUrl(this.pack)
if (repoUrl) {
const repoName = url.parse(repoUrl).pathname
this.refs.packageRepo.textContent = repoName.substring(1)
this.refs.packageRepo.style.display = ''
} else {
this.refs.packageRepo.style.display = 'none'
}
this.updateInstalledState()
}
updateInstalledState () {
if (this.settingsPanel) {
this.settingsPanel.destroy()
this.settingsPanel = null
}
if (this.keymapView) {
this.keymapView.destroy()
this.keymapView = null
}
if (this.grammarsView) {
this.grammarsView.destroy()
this.grammarsView = null
}
if (this.snippetsView) {
this.snippetsView.destroy()
this.snippetsView = null
}
if (this.readmeView) {
this.readmeView.destroy()
this.readmeView = null
}
this.updateFileButtons()
this.activateConfig()
this.refs.startupTime.style.display = 'none'
if (atom.packages.isPackageLoaded(this.pack.name)) {
if (!atom.packages.isPackageDisabled(this.pack.name)) {
this.settingsPanel = new SettingsPanel({namespace: this.pack.name, includeTitle: false})
this.keymapView = new PackageKeymapView(this.pack)
this.refs.sections.appendChild(this.settingsPanel.element)
this.refs.sections.appendChild(this.keymapView.element)
if (this.pack.path) {
this.grammarsView = new PackageGrammarsView(this.pack.path)
this.snippetsView = new PackageSnippetsView(this.pack, this.snippetsProvider)
this.refs.sections.appendChild(this.grammarsView.element)
this.refs.sections.appendChild(this.snippetsView.element)
}
this.refs.startupTime.innerHTML =
`This ${this.type} added <span class='highlight'>${this.getStartupTime()}ms</span> to startup time.`
this.refs.startupTime.style.display = ''
}
}
const sourceIsAvailable = this.packageManager.isPackageInstalled(this.pack.name) && !atom.packages.isBundledPackage(this.pack.name)
if (sourceIsAvailable) {
this.refs.openButton.style.display = ''
} else {
this.refs.openButton.style.display = 'none'
}
this.renderReadme()
}
renderReadme () {
let readme
if (this.pack.metadata.readme && this.pack.metadata.readme.trim() !== NORMALIZE_PACKAGE_DATA_README_ERROR) {
readme = this.pack.metadata.readme
} else {
readme = null
}
if (this.readmePath && fs.existsSync(this.readmePath) && fs.statSync(this.readmePath).isFile() && !readme) {
readme = fs.readFileSync(this.readmePath, {encoding: 'utf8'})
}
let readmeSrc
if (this.pack.path) {
// If package is installed, use installed path
readmeSrc = this.pack.path
} else {
// If package isn't installed, use url path
let repoUrl = this.packageManager.getRepositoryUrl(this.pack)
// Check if URL is undefined (i.e. package is unpublished)
if (repoUrl) {
readmeSrc = repoUrl + `/blob/master/`
}
}
const readmeView = new PackageReadmeView(readme, readmeSrc)
if (this.readmeView) {
this.readmeView.element.parentElement.replaceChild(readmeView.element, this.readmeView.element)
this.readmeView.destroy()
} else {
this.refs.sections.appendChild(readmeView.element)
}
this.readmeView = readmeView
}
subscribeToPackageManager () {
this.disposables.add(this.packageManager.on('theme-installed package-installed', ({pack}) => {
if (this.pack.name === pack.name) {
this.loadPackage()
this.updateInstalledState()
}
}))
this.disposables.add(this.packageManager.on('theme-uninstalled package-uninstalled', ({pack}) => {
if (this.pack.name === pack.name) {
return this.updateInstalledState()
}
}))
this.disposables.add(this.packageManager.on('theme-updated package-updated', ({pack}) => {
if (this.pack.name === pack.name) {
this.loadPackage()
this.updateFileButtons()
this.populate()
}
}))
}
openMarkdownFile (path) {
if (atom.packages.isPackageActive('markdown-preview')) {
atom.workspace.open(encodeURI(`markdown-preview://${path}`))
} else {
atom.workspace.open(path)
}
}
updateFileButtons () {
this.changelogPath = null
this.licensePath = null
this.readmePath = null
const packagePath = this.pack.path != null ? this.pack.path : atom.packages.resolvePackagePath(this.pack.name)
for (const child of fs.listSync(packagePath)) {
switch (path.basename(child, path.extname(child)).toLowerCase()) {
case 'changelog':
case 'history':
this.changelogPath = child
break
case 'license':
case 'licence':
this.licensePath = child
break
case 'readme':
this.readmePath = child
break
}
if (this.readmePath && this.changelogPath && this.licensePath) {
break
}
}
if (this.changelogPath) {
this.refs.changelogButton.style.display = ''
} else {
this.refs.changelogButton.style.display = 'none'
}
if (this.licensePath) {
this.refs.licenseButton.style.display = ''
} else {
this.refs.licenseButton.style.display = 'none'
}
}
getStartupTime () {
const loadTime = this.pack.loadTime != null ? this.pack.loadTime : 0
const activateTime = this.pack.activateTime != null ? this.pack.activateTime : 0
return loadTime + activateTime
}
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
}
}
class PackageCardComponent {
constructor (props) {
this.packageCard = new PackageCard(props.metadata, props.settingsView, props.packageManager, props.options)
this.element = this.packageCard.element
}
update () {}
destroy () {}
}