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 324
/
Copy pathjoin-via-external-app-dialog.js
123 lines (106 loc) · 3.44 KB
/
join-via-external-app-dialog.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
const etch = require('etch')
const $ = etch.dom
module.exports =
class JoinViaExternalAppDialog {
constructor ({config, commandRegistry, workspace}) {
this.commandRegistry = commandRegistry
this.workspace = workspace
this.confirm = this.confirm.bind(this)
this.cancel = this.cancel.bind(this)
this.handleBlur = this.handleBlur.bind(this)
this.props = {uri: ''}
etch.initialize(this)
this.disposables = this.commandRegistry.add(this.element, {
'core:confirm': this.confirm,
'core:cancel': this.cancel
})
}
destroy () {
if (this.panel) this.panel.destroy()
this.disposables.dispose()
etch.destroy(this)
}
async show (uri) {
await this.update({uri})
// This dialog could be opened before Atom's workaround for window focus is
// triggered (see https://git.io/vxWDa), so we delay focusing it to prevent
// such workaround from stealing focus from the dialog.
await timeout(5)
// We explicitly add the modal as hidden because of a bug in the auto-focus
// feature that prevents it from working correctly when using visible: true.
this.panel = this.workspace.addModalPanel({item: this, visible: false, autoFocus: true})
this.panel.show()
this.element.focus()
this.element.addEventListener('blur', this.handleBlur)
return new Promise((resolve) => {
this.resolveWithExitStatus = resolve
this.panel.onDidDestroy(() => {
this.panel = null
this.element.removeEventListener('blur', this.handleBlur)
})
})
}
confirm () {
if (this.refs.joinWithoutAskingCheckbox.checked) {
this.confirmAlways()
} else {
this.confirmOnce()
}
}
confirmOnce () {
this.resolveWithExitStatus(this.constructor.EXIT_STATUS.CONFIRM_ONCE)
this.panel.destroy()
}
confirmAlways () {
this.resolveWithExitStatus(this.constructor.EXIT_STATUS.CONFIRM_ALWAYS)
this.panel.destroy()
}
cancel () {
this.resolveWithExitStatus(this.constructor.EXIT_STATUS.CANCEL)
this.panel.destroy()
}
render () {
return $.div({className: 'JoinViaExternalAppDialog', tabIndex: -1},
$.div(null,
$.h1(null, 'Join this portal?'),
$.a({className: 'JoinViaExternalAppDialog-cancel icon icon-x', onClick: this.cancel})
),
$.p({className: 'JoinViaExternalAppDialog-uri'}, this.props.uri),
$.p(null, 'By joining this portal, the other collaborators will see your GitHub username, your avatar, and any edits that you perform inside the portal.'),
$.footer({className: 'JoinViaExternalAppDialog-footer'},
$.label({className: 'input-label'},
$.input({
ref: 'joinWithoutAskingCheckbox',
className: 'input-checkbox',
type: 'checkbox'
}),
$.span(null, 'Always join without asking. I only open URLs from people I trust.')
),
$.button(
{className: 'btn btn-lg btn-primary', onClick: this.confirm},
'Join portal'
)
)
)
}
update (props) {
this.props = props
return etch.update(this)
}
writeAfterUpdate () {
this.refs.joinWithoutAskingCheckbox.checked = false
}
handleBlur (event) {
if (document.hasFocus() && !this.element.contains(event.relatedTarget)) {
this.cancel()
}
}
}
module.exports.EXIT_STATUS = {
CONFIRM_ALWAYS: 0,
CONFIRM_ONCE: 1,
CANCEL: 2
}
function timeout (ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}