Ghost/core/client/app/components/gh-dropdown.js

93 lines
2.6 KiB
JavaScript
Raw Normal View History

2015-02-13 07:22:32 +03:00
import Ember from 'ember';
2014-09-28 19:39:25 +04:00
import DropdownMixin from 'ghost/mixins/dropdown-mixin';
export default Ember.Component.extend(DropdownMixin, {
classNames: 'dropdown',
classNameBindings: ['fadeIn:fade-in-scale:fade-out', 'isOpen:open:closed'],
name: null,
closeOnClick: false,
// Helps track the user re-opening the menu while it's fading out.
closing: false,
// Helps track whether the dropdown is open or closes, or in a transition to either
isOpen: false,
// Managed the toggle between the fade-in and fade-out classes
fadeIn: Ember.computed('isOpen', 'closing', function () {
return this.get('isOpen') && !this.get('closing');
}),
dropdown: Ember.inject.service(),
open: function () {
this.set('isOpen', true);
this.set('closing', false);
this.set('button.isOpen', true);
},
close: function () {
var self = this;
this.set('closing', true);
if (this.get('button')) {
this.set('button.isOpen', false);
}
this.$().on('animationend webkitAnimationEnd oanimationend MSAnimationEnd', function (event) {
if (event.originalEvent.animationName === 'fade-out') {
if (self.get('closing')) {
self.set('isOpen', false);
self.set('closing', false);
}
}
});
},
// Called by the dropdown service when any dropdown button is clicked.
toggle: function (options) {
var isClosing = this.get('closing'),
isOpen = this.get('isOpen'),
name = this.get('name'),
button = this.get('button'),
2014-09-28 19:39:25 +04:00
targetDropdownName = options.target;
2014-09-29 15:17:27 +04:00
2014-09-28 19:39:25 +04:00
if (name === targetDropdownName && (!isOpen || isClosing)) {
if (!button) {
button = options.button;
this.set('button', button);
}
this.open();
} else if (isOpen) {
this.close();
}
},
click: function (event) {
this._super(event);
if (this.get('closeOnClick')) {
return this.close();
}
},
didInsertElement: function () {
this._super();
2014-09-28 19:39:25 +04:00
var dropdownService = this.get('dropdown');
2014-09-28 19:39:25 +04:00
dropdownService.on('close', this, this.close);
dropdownService.on('toggle', this, this.toggle);
},
willDestroyElement: function () {
this._super();
2014-09-28 19:39:25 +04:00
var dropdownService = this.get('dropdown');
2014-09-28 19:39:25 +04:00
dropdownService.off('close', this, this.close);
dropdownService.off('toggle', this, this.toggle);
}
});