2017-01-19 14:20:33 +03:00
|
|
|
import Component from 'ember-component';
|
|
|
|
import observer from 'ember-metal/observer';
|
2016-08-11 09:58:38 +03:00
|
|
|
import {reads} from 'ember-computed';
|
|
|
|
import {invokeAction} from 'ember-invoke-action';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-01-19 14:20:33 +03:00
|
|
|
export default Component.extend({
|
|
|
|
tagName: 'button',
|
|
|
|
classNameBindings: ['isRunning:appear-disabled'],
|
|
|
|
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,
|
2016-08-11 09:58:38 +03:00
|
|
|
|
2017-01-19 14:20:33 +03:00
|
|
|
isRunning: reads('task.last.isRunning'),
|
|
|
|
|
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');
|
|
|
|
|
2016-08-24 16:14:36 +03:00
|
|
|
return task.perform();
|
2017-01-19 14:20:33 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
setSize: observer('isRunning', function () {
|
|
|
|
if (this.get('isRunning')) {
|
|
|
|
this.$().width(this.$().width());
|
|
|
|
this.$().height(this.$().height());
|
|
|
|
} else {
|
|
|
|
this.$().width('');
|
|
|
|
this.$().height('');
|
|
|
|
}
|
|
|
|
})
|
2016-08-11 09:58:38 +03:00
|
|
|
});
|