1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/components/gh-spin-button.js
Austin Burdine 2dff7edd3d convert remainder of components to use ember-cli-shims (#101)
follow up from #95
- converts components to use ember-cli-shims
2016-06-30 19:14:25 +01:00

64 lines
1.8 KiB
JavaScript

import Component from 'ember-component';
import Ember from 'ember';
import {equal} from 'ember-computed';
import observer from 'ember-metal/observer';
import run from 'ember-runloop';
// ember-cli-shims doesn't export Ember.Testing
const {testing} = Ember;
export default 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: equal('showSpinner', true),
click() {
if (this.get('action')) {
this.sendAction('action');
return false;
}
return true;
},
toggleSpinner: observer('submitting', function () {
let submitting = this.get('submitting');
let timeout = this.get('showSpinnerTimeout');
let delay = testing ? 10 : 1000;
if (submitting) {
this.set('showSpinner', true);
this.set('showSpinnerTimeout', run.later(this, function () {
if (!this.get('submitting')) {
this.set('showSpinner', false);
}
this.set('showSpinnerTimeout', null);
}, delay));
} else if (!submitting && timeout === null) {
this.set('showSpinner', false);
}
}),
setSize: observer('showSpinner', function () {
if (this.get('showSpinner') && this.get('autoWidth')) {
this.$().width(this.$().width());
this.$().height(this.$().height());
} else {
this.$().width('');
this.$().height('');
}
}),
willDestroy() {
this._super(...arguments);
run.cancel(this.get('showSpinnerTimeout'));
}
});