mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-12 06:25:51 +03:00
fb239054a0
follow up from #95 - converts components to use ember-cli-shims
65 lines
1.8 KiB
JavaScript
65 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'));
|
|
}
|
|
});
|