|
| 1 | +const etch = require('etch') |
| 2 | +const $ = etch.dom |
| 3 | + |
| 4 | +module.exports = |
| 5 | +class JoinViaExternalAppDialog { |
| 6 | + constructor ({config, commandRegistry, workspace}) { |
| 7 | + this.commandRegistry = commandRegistry |
| 8 | + this.workspace = workspace |
| 9 | + this.confirm = this.confirm.bind(this) |
| 10 | + this.cancel = this.cancel.bind(this) |
| 11 | + this.handleBlur = this.handleBlur.bind(this) |
| 12 | + this.props = {uri: ''} |
| 13 | + etch.initialize(this) |
| 14 | + this.disposables = this.commandRegistry.add(this.element, { |
| 15 | + 'core:confirm': this.confirm, |
| 16 | + 'core:cancel': this.cancel |
| 17 | + }) |
| 18 | + } |
| 19 | + |
| 20 | + destroy () { |
| 21 | + if (this.panel) this.panel.destroy() |
| 22 | + |
| 23 | + this.disposables.dispose() |
| 24 | + etch.destroy(this) |
| 25 | + } |
| 26 | + |
| 27 | + async show (uri) { |
| 28 | + await this.update({uri}) |
| 29 | + |
| 30 | + // This dialog could be opened before Atom's workaround for window focus is |
| 31 | + // triggered (see https://git.io/vxWDa), so we delay focusing it to prevent |
| 32 | + // such workaround from stealing focus from the dialog. |
| 33 | + await timeout(5) |
| 34 | + |
| 35 | + // We explicitly add the modal as hidden because of a bug in the auto-focus |
| 36 | + // feature that prevents it from working correctly when using visible: true. |
| 37 | + this.panel = this.workspace.addModalPanel({item: this, visible: false, autoFocus: true}) |
| 38 | + this.panel.show() |
| 39 | + this.element.focus() |
| 40 | + this.element.addEventListener('blur', this.handleBlur) |
| 41 | + |
| 42 | + return new Promise((resolve) => { |
| 43 | + this.resolveWithExitStatus = resolve |
| 44 | + this.panel.onDidDestroy(() => { |
| 45 | + this.panel = null |
| 46 | + this.element.removeEventListener('blur', this.handleBlur) |
| 47 | + }) |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + confirm () { |
| 52 | + if (this.refs.joinWithoutAskingCheckbox.checked) { |
| 53 | + this.confirmAlways() |
| 54 | + } else { |
| 55 | + this.confirmOnce() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + confirmOnce () { |
| 60 | + this.resolveWithExitStatus(this.constructor.EXIT_STATUS.CONFIRM_ONCE) |
| 61 | + this.panel.destroy() |
| 62 | + } |
| 63 | + |
| 64 | + confirmAlways () { |
| 65 | + this.resolveWithExitStatus(this.constructor.EXIT_STATUS.CONFIRM_ALWAYS) |
| 66 | + this.panel.destroy() |
| 67 | + } |
| 68 | + |
| 69 | + cancel () { |
| 70 | + this.resolveWithExitStatus(this.constructor.EXIT_STATUS.CANCEL) |
| 71 | + this.panel.destroy() |
| 72 | + } |
| 73 | + |
| 74 | + render () { |
| 75 | + return $.div({className: 'JoinViaExternalAppDialog', tabIndex: -1}, |
| 76 | + $.div(null, |
| 77 | + $.h1(null, 'Join this portal?'), |
| 78 | + $.a({className: 'JoinViaExternalAppDialog-cancel icon icon-x', onClick: this.cancel}) |
| 79 | + ), |
| 80 | + $.p({className: 'JoinViaExternalAppDialog-uri'}, this.props.uri), |
| 81 | + $.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.'), |
| 82 | + $.footer({className: 'JoinViaExternalAppDialog-footer'}, |
| 83 | + $.label({className: 'input-label'}, |
| 84 | + $.input({ |
| 85 | + ref: 'joinWithoutAskingCheckbox', |
| 86 | + className: 'input-checkbox', |
| 87 | + type: 'checkbox' |
| 88 | + }), |
| 89 | + $.span(null, 'Always join without asking. I only open URLs from people I trust.') |
| 90 | + ), |
| 91 | + $.button( |
| 92 | + {className: 'btn btn-lg btn-primary', onClick: this.confirm}, |
| 93 | + 'Join portal' |
| 94 | + ) |
| 95 | + ) |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + update (props) { |
| 100 | + this.props = props |
| 101 | + return etch.update(this) |
| 102 | + } |
| 103 | + |
| 104 | + writeAfterUpdate () { |
| 105 | + this.refs.joinWithoutAskingCheckbox.checked = false |
| 106 | + } |
| 107 | + |
| 108 | + handleBlur (event) { |
| 109 | + if (document.hasFocus() && !this.element.contains(event.relatedTarget)) { |
| 110 | + this.cancel() |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +module.exports.EXIT_STATUS = { |
| 116 | + CONFIRM_ALWAYS: 0, |
| 117 | + CONFIRM_ONCE: 1, |
| 118 | + CANCEL: 2 |
| 119 | +} |
| 120 | + |
| 121 | +function timeout (ms) { |
| 122 | + return new Promise((resolve) => setTimeout(resolve, ms)) |
| 123 | +} |
0 commit comments