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';
|
2020-06-10 13:49:16 +00:00
|
|
|
import { classNames } from '@ember-decorators/component';
|
|
|
|
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')
|
|
|
|
export default class TwoStepButton extends Component {
|
|
|
|
idleText = '';
|
|
|
|
cancelText = '';
|
|
|
|
confirmText = '';
|
|
|
|
confirmationMessage = '';
|
|
|
|
awaitingConfirmation = false;
|
|
|
|
disabled = false;
|
|
|
|
alignRight = false;
|
|
|
|
isInfoAction = false;
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|