0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-13 22:41:32 -05:00
ghost/core/client/app/components/gh-spin-button.js
Kevin Ansfield 624893456e Set a minimum spin time of 1 second for gh-spin-button
refs #5652, #5719
- adds a timeout to `gh-spin-button` so the spinner is always shown for at least 1 second

As a stopgap solution before #5719 can be implemented it was decided to keep the button spinning for a minimum time, even if the associated action completes quickly. Discussion can be found at https://ghost.slack.com/archives/dev/p1440670418004358
2015-08-27 22:16:01 +01:00

55 lines
1.6 KiB
JavaScript

import Ember from 'ember';
export default Ember.Component.extend({
tagName: 'button',
buttonText: '',
submitting: false,
showSpinner: false,
showSpinnerTimeout: null,
autoWidth: true,
// Disable Button when isLoading equals true
attributeBindings: ['disabled', 'type', 'tabindex'],
// Must be set on the controller
disabled: Ember.computed.equal('showSpinner', true),
click: function () {
if (this.get('action')) {
this.sendAction('action');
return false;
}
return true;
},
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);
this.set('showSpinnerTimeout', null);
}
}, 1000));
} else if (!submitting && timeout === null) {
this.set('showSpinner', false);
}
}),
setSize: Ember.observer('showSpinner', function () {
if (this.get('showSpinner') && this.get('autoWidth')) {
this.$().width(this.$().width());
this.$().height(this.$().height());
} else {
this.$().width('');
this.$().height('');
}
}),
willDestroy: function () {
Ember.run.cancel(this.get('showSpinnerTimeout'));
}
});