2015-08-10 18:43:49 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
|
|
|
export default Ember.Component.extend({
|
|
|
|
tagName: 'button',
|
|
|
|
buttonText: '',
|
|
|
|
submitting: false,
|
2015-08-20 06:39:07 +03:00
|
|
|
showSpinner: false,
|
2015-08-27 20:11:23 +03:00
|
|
|
showSpinnerTimeout: null,
|
2015-08-10 18:43:49 +03:00
|
|
|
autoWidth: true,
|
|
|
|
|
|
|
|
// Disable Button when isLoading equals true
|
2015-08-25 22:36:40 +03:00
|
|
|
attributeBindings: ['disabled', 'type', 'tabindex'],
|
2015-08-10 18:43:49 +03:00
|
|
|
|
|
|
|
// Must be set on the controller
|
2015-08-27 20:11:23 +03:00
|
|
|
disabled: Ember.computed.equal('showSpinner', true),
|
2015-08-10 18:43:49 +03:00
|
|
|
|
|
|
|
click: function () {
|
|
|
|
if (this.get('action')) {
|
|
|
|
this.sendAction('action');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2015-08-27 20:11:23 +03:00
|
|
|
toggleSpinner: Ember.observer('submitting', function () {
|
|
|
|
var submitting = this.get('submitting'),
|
|
|
|
timeout = this.get('showSpinnerTimeout');
|
|
|
|
|
|
|
|
if (submitting) {
|
|
|
|
this.set('showSpinner', true);
|
|
|
|
this.set('showSpinnerTimeout', Ember.run.later(this, function () {
|
|
|
|
if (!this.get('submitting')) {
|
|
|
|
this.set('showSpinner', false);
|
|
|
|
}
|
2015-08-31 15:27:23 +03:00
|
|
|
this.set('showSpinnerTimeout', null);
|
2015-08-27 20:11:23 +03:00
|
|
|
}, 1000));
|
|
|
|
} else if (!submitting && timeout === null) {
|
|
|
|
this.set('showSpinner', false);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
setSize: Ember.observer('showSpinner', function () {
|
|
|
|
if (this.get('showSpinner') && this.get('autoWidth')) {
|
2015-08-20 06:39:07 +03:00
|
|
|
this.$().width(this.$().width());
|
|
|
|
this.$().height(this.$().height());
|
|
|
|
} else {
|
|
|
|
this.$().width('');
|
|
|
|
this.$().height('');
|
2015-08-10 18:43:49 +03:00
|
|
|
}
|
2015-08-27 20:11:23 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
willDestroy: function () {
|
|
|
|
Ember.run.cancel(this.get('showSpinnerTimeout'));
|
|
|
|
}
|
2015-08-10 18:43:49 +03:00
|
|
|
});
|