2018-04-18 19:19:35 +00:00
|
|
|
import Component from '@ember/component';
|
2020-06-10 13:49:16 +00:00
|
|
|
import { action } from '@ember/object';
|
2019-05-17 00:41:40 +00:00
|
|
|
import { next } from '@ember/runloop';
|
2018-04-18 19:19:35 +00:00
|
|
|
import { equal } from '@ember/object/computed';
|
2019-05-17 00:41:40 +00:00
|
|
|
import { task, waitForEvent } from 'ember-concurrency';
|
2018-08-24 21:02:13 +00:00
|
|
|
import RSVP from 'rsvp';
|
2021-02-10 14:38:37 +00:00
|
|
|
import { classNames, classNameBindings } from '@ember-decorators/component';
|
2020-06-10 13:49:16 +00:00
|
|
|
import classic from 'ember-classic-decorator';
|
2018-04-18 19:19:35 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@classic
|
|
|
|
@classNames('two-step-button')
|
2021-02-10 14:38:37 +00:00
|
|
|
@classNameBindings('inlineText:has-inline-text')
|
2020-06-10 13:49:16 +00:00
|
|
|
export default class TwoStepButton extends Component {
|
|
|
|
idleText = '';
|
|
|
|
cancelText = '';
|
|
|
|
confirmText = '';
|
|
|
|
confirmationMessage = '';
|
|
|
|
awaitingConfirmation = false;
|
|
|
|
disabled = false;
|
|
|
|
alignRight = false;
|
2021-02-10 14:38:37 +00:00
|
|
|
inlineText = false;
|
2020-06-10 13:49:16 +00:00
|
|
|
onConfirm() {}
|
|
|
|
onCancel() {}
|
2018-04-18 19:19:35 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
state = 'idle';
|
|
|
|
@equal('state', 'idle') isIdle;
|
|
|
|
@equal('state', 'prompt') isPendingConfirmation;
|
2018-04-18 19:19:35 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@task(function*() {
|
2019-05-17 00:41:40 +00:00
|
|
|
while (true) {
|
|
|
|
let ev = yield waitForEvent(document.body, 'click');
|
|
|
|
if (!this.element.contains(ev.target) && !this.awaitingConfirmation) {
|
|
|
|
this.send('setToIdle');
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 13:49:16 +00:00
|
|
|
})
|
|
|
|
cancelOnClickOutside;
|
2019-05-17 00:41:40 +00:00
|
|
|
|
2020-06-10 13:49:16 +00:00
|
|
|
@action
|
|
|
|
setToIdle() {
|
|
|
|
this.set('state', 'idle');
|
|
|
|
this.cancelOnClickOutside.cancelAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
promptForConfirmation() {
|
|
|
|
this.set('state', 'prompt');
|
|
|
|
next(() => {
|
|
|
|
this.cancelOnClickOutside.perform();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
confirm() {
|
|
|
|
RSVP.resolve(this.onConfirm()).then(() => {
|
|
|
|
this.send('setToIdle');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|