2018-04-18 19:19:35 +00:00
|
|
|
import Component from '@ember/component';
|
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';
|
2018-04-18 19:19:35 +00:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
classNames: ['two-step-button'],
|
|
|
|
|
|
|
|
idleText: '',
|
|
|
|
cancelText: '',
|
|
|
|
confirmText: '',
|
|
|
|
confirmationMessage: '',
|
2018-08-24 20:41:14 +00:00
|
|
|
awaitingConfirmation: false,
|
2019-05-17 00:41:40 +00:00
|
|
|
disabled: false,
|
2018-04-18 19:19:35 +00:00
|
|
|
onConfirm() {},
|
|
|
|
onCancel() {},
|
|
|
|
|
|
|
|
state: 'idle',
|
|
|
|
isIdle: equal('state', 'idle'),
|
|
|
|
isPendingConfirmation: equal('state', 'prompt'),
|
|
|
|
|
2019-05-17 00:41:40 +00:00
|
|
|
cancelOnClickOutside: task(function*() {
|
|
|
|
while (true) {
|
|
|
|
let ev = yield waitForEvent(document.body, 'click');
|
|
|
|
if (!this.element.contains(ev.target) && !this.awaitingConfirmation) {
|
|
|
|
this.send('setToIdle');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2018-04-18 19:19:35 +00:00
|
|
|
actions: {
|
|
|
|
setToIdle() {
|
|
|
|
this.set('state', 'idle');
|
2019-05-17 00:41:40 +00:00
|
|
|
this.cancelOnClickOutside.cancelAll();
|
2018-04-18 19:19:35 +00:00
|
|
|
},
|
|
|
|
promptForConfirmation() {
|
|
|
|
this.set('state', 'prompt');
|
2019-05-17 00:41:40 +00:00
|
|
|
next(() => {
|
|
|
|
this.cancelOnClickOutside.perform();
|
|
|
|
});
|
2018-04-18 19:19:35 +00:00
|
|
|
},
|
2018-08-24 21:02:13 +00:00
|
|
|
confirm() {
|
2019-03-26 07:46:44 +00:00
|
|
|
RSVP.resolve(this.onConfirm()).then(() => {
|
2018-08-24 21:02:13 +00:00
|
|
|
this.send('setToIdle');
|
|
|
|
});
|
|
|
|
},
|
2018-04-18 19:19:35 +00:00
|
|
|
},
|
|
|
|
});
|