2017-08-22 10:53:26 +03:00
|
|
|
import Component from '@ember/component';
|
|
|
|
import {computed} from '@ember/object';
|
2016-08-11 09:58:38 +03:00
|
|
|
import {invokeAction} from 'ember-invoke-action';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {isBlank} from '@ember/utils';
|
|
|
|
import {observer} from '@ember/object';
|
|
|
|
import {reads} from '@ember/object/computed';
|
2017-04-14 18:16:52 +03:00
|
|
|
import {task, timeout} from 'ember-concurrency';
|
2016-08-11 09:58:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Task Button works exactly like Spin button, but with one major difference:
|
|
|
|
*
|
|
|
|
* Instead of passing a "submitting" parameter (which is bound to the parent object),
|
|
|
|
* you pass an ember-concurrency task. All of the "submitting" behavior is handled automatically.
|
|
|
|
*
|
|
|
|
* As another bonus, there's no need to handle canceling the promises when something
|
|
|
|
* like a controller changes. Because the only task running is handled through this
|
|
|
|
* component, all running promises will automatically be cancelled when this
|
|
|
|
* component is removed from the DOM
|
|
|
|
*/
|
2017-03-07 20:28:52 +03:00
|
|
|
const GhTaskButton = Component.extend({
|
2017-01-19 14:20:33 +03:00
|
|
|
tagName: 'button',
|
2017-04-19 19:21:47 +03:00
|
|
|
classNameBindings: [
|
|
|
|
'isRunning:appear-disabled',
|
|
|
|
'isIdleClass',
|
|
|
|
'isRunningClass',
|
|
|
|
'isSuccessClass',
|
|
|
|
'isFailureClass'
|
|
|
|
],
|
2017-01-19 14:20:33 +03:00
|
|
|
attributeBindings: ['disabled', 'type', 'tabindex'],
|
2016-08-24 16:14:36 +03:00
|
|
|
|
2016-08-11 09:58:38 +03:00
|
|
|
task: null,
|
2016-08-24 16:14:36 +03:00
|
|
|
disabled: false,
|
2017-03-07 20:28:52 +03:00
|
|
|
buttonText: 'Save',
|
|
|
|
runningText: reads('buttonText'),
|
2017-04-19 19:21:47 +03:00
|
|
|
idleClass: '',
|
|
|
|
runningClass: '',
|
2017-03-07 20:28:52 +03:00
|
|
|
successText: 'Saved',
|
2017-03-10 00:48:54 +03:00
|
|
|
successClass: 'gh-btn-green',
|
2017-03-07 20:28:52 +03:00
|
|
|
failureText: 'Retry',
|
2017-03-10 00:48:54 +03:00
|
|
|
failureClass: 'gh-btn-red',
|
2016-08-11 09:58:38 +03:00
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
// hasRun is needed so that a newly rendered button does not show the last
|
|
|
|
// state of the associated task
|
|
|
|
hasRun: false,
|
2017-01-19 14:20:33 +03:00
|
|
|
isRunning: reads('task.last.isRunning'),
|
|
|
|
|
2017-04-19 19:21:47 +03:00
|
|
|
isIdleClass: computed('isIdle', function () {
|
|
|
|
if (this.get('isIdle')) {
|
|
|
|
return this.get('idleClass');
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
isRunningClass: computed('isRunning', function () {
|
|
|
|
if (this.get('isRunning')) {
|
|
|
|
return this.get('runningClass') || this.get('idleClass');
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
isSuccess: computed('hasRun', 'isRunning', 'task.last.value', function () {
|
|
|
|
if (!this.get('hasRun') || this.get('isRunning')) {
|
2017-03-07 20:28:52 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = this.get('task.last.value');
|
|
|
|
return !isBlank(value) && value !== false;
|
|
|
|
}),
|
|
|
|
|
2017-03-10 00:48:54 +03:00
|
|
|
isSuccessClass: computed('isSuccess', function () {
|
|
|
|
if (this.get('isSuccess')) {
|
|
|
|
return this.get('successClass');
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2017-04-11 16:39:45 +03:00
|
|
|
isFailure: computed('hasRun', 'isRunning', 'isSuccess', 'task.last.error', function () {
|
|
|
|
if (!this.get('hasRun') || this.get('isRunning') || this.get('isSuccess')) {
|
2017-03-07 20:28:52 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.get('task.last.error') !== undefined;
|
|
|
|
}),
|
|
|
|
|
2017-03-10 00:48:54 +03:00
|
|
|
isFailureClass: computed('isFailure', function () {
|
|
|
|
if (this.get('isFailure')) {
|
|
|
|
return this.get('failureClass');
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2017-03-07 20:28:52 +03:00
|
|
|
isIdle: computed('isRunning', 'isSuccess', 'isFailure', function () {
|
|
|
|
return !this.get('isRunning') && !this.get('isSuccess') && !this.get('isFailure');
|
|
|
|
}),
|
|
|
|
|
2016-08-11 09:58:38 +03:00
|
|
|
click() {
|
2017-01-19 14:20:33 +03:00
|
|
|
// do nothing if disabled externally
|
|
|
|
if (this.get('disabled')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-24 16:14:36 +03:00
|
|
|
let task = this.get('task');
|
|
|
|
let taskName = this.get('task.name');
|
|
|
|
let lastTaskName = this.get('task.last.task.name');
|
|
|
|
|
2017-01-19 14:20:33 +03:00
|
|
|
// task-buttons are never disabled whilst running so that clicks when a
|
|
|
|
// taskGroup is running don't get dropped BUT that means we need to check
|
|
|
|
// here to avoid spamming actions from multiple clicks
|
|
|
|
if (this.get('isRunning') && taskName === lastTaskName) {
|
2016-08-24 16:14:36 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-11 09:58:38 +03:00
|
|
|
invokeAction(this, 'action');
|
2017-03-10 00:48:54 +03:00
|
|
|
task.perform();
|
2016-08-11 09:58:38 +03:00
|
|
|
|
2017-04-14 18:16:52 +03:00
|
|
|
this.get('_restartAnimation').perform();
|
|
|
|
|
2017-03-10 00:48:54 +03:00
|
|
|
// prevent the click from bubbling and triggering form actions
|
|
|
|
return false;
|
2017-01-19 14:20:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setSize: observer('isRunning', function () {
|
|
|
|
if (this.get('isRunning')) {
|
2017-04-11 16:39:45 +03:00
|
|
|
this.set('hasRun', true);
|
2017-04-11 18:44:37 +03:00
|
|
|
// this.$().width(this.$().width());
|
|
|
|
// this.$().height(this.$().height());
|
2017-01-19 14:20:33 +03:00
|
|
|
} else {
|
2017-04-11 18:44:37 +03:00
|
|
|
// this.$().width('');
|
|
|
|
// this.$().height('');
|
2017-01-19 14:20:33 +03:00
|
|
|
}
|
2017-04-14 18:16:52 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
// when local validation fails there's no transition from failed->running
|
|
|
|
// so we want to restart the retry spinner animation to show something
|
|
|
|
// has happened when the button is clicked
|
|
|
|
_restartAnimation: task(function* () {
|
|
|
|
if (this.$('.retry-animated').length) {
|
2017-04-14 18:33:46 +03:00
|
|
|
// eslint-disable-next-line
|
2017-04-14 18:16:52 +03:00
|
|
|
let elem = this.$('.retry-animated')[0];
|
|
|
|
elem.classList.remove('retry-animated');
|
|
|
|
yield timeout(10);
|
|
|
|
elem.classList.add('retry-animated');
|
|
|
|
}
|
2017-01-19 14:20:33 +03:00
|
|
|
})
|
2016-08-11 09:58:38 +03:00
|
|
|
});
|
2017-03-07 20:28:52 +03:00
|
|
|
|
|
|
|
GhTaskButton.reopenClass({
|
|
|
|
positionalParams: ['buttonText']
|
|
|
|
});
|
|
|
|
|
|
|
|
export default GhTaskButton;
|