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';
|
2014-04-07 02:11:22 +04:00
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
export default Ember.Component.extend(DropdownMixin, {
|
2015-08-13 10:09:07 +03:00
|
|
|
classNames: 'dropdown',
|
2015-05-26 05:10:50 +03:00
|
|
|
classNameBindings: ['fadeIn:fade-in-scale:fade-out', 'isOpen:open:closed'],
|
|
|
|
|
2014-05-31 04:07:15 +04:00
|
|
|
name: null,
|
2014-08-06 18:32:10 +04:00
|
|
|
closeOnClick: false,
|
2014-10-25 01:09:50 +04:00
|
|
|
|
|
|
|
// Helps track the user re-opening the menu while it's fading out.
|
2014-08-06 18:32:10 +04:00
|
|
|
closing: false,
|
2014-10-25 01:09:50 +04:00
|
|
|
|
|
|
|
// Helps track whether the dropdown is open or closes, or in a transition to either
|
2014-09-24 17:36:20 +04:00
|
|
|
isOpen: false,
|
2014-10-25 01:09:50 +04:00
|
|
|
|
|
|
|
// Managed the toggle between the fade-in and fade-out classes
|
2014-09-24 17:36:20 +04:00
|
|
|
fadeIn: Ember.computed('isOpen', 'closing', function () {
|
|
|
|
return this.get('isOpen') && !this.get('closing');
|
|
|
|
}),
|
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
dropdown: Ember.inject.service(),
|
2014-07-01 22:56:02 +04:00
|
|
|
|
|
|
|
open: function () {
|
|
|
|
this.set('isOpen', true);
|
2014-09-24 17:36:20 +04:00
|
|
|
this.set('closing', false);
|
2014-08-06 18:32:10 +04:00
|
|
|
this.set('button.isOpen', true);
|
2014-07-01 22:56:02 +04:00
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-07-01 22:56:02 +04:00
|
|
|
close: function () {
|
|
|
|
var self = this;
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-07-01 22:56:02 +04:00
|
|
|
this.set('closing', true);
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-08-06 18:32:10 +04:00
|
|
|
if (this.get('button')) {
|
|
|
|
this.set('button.isOpen', false);
|
|
|
|
}
|
2014-09-24 17:36:20 +04:00
|
|
|
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);
|
|
|
|
}
|
2014-07-01 22:56:02 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
|
|
|
// Called by the dropdown service when any dropdown button is clicked.
|
2014-07-01 22:56:02 +04:00
|
|
|
toggle: function (options) {
|
|
|
|
var isClosing = this.get('closing'),
|
|
|
|
isOpen = this.get('isOpen'),
|
|
|
|
name = this.get('name'),
|
2014-08-06 18:32:10 +04:00
|
|
|
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)) {
|
2014-08-06 18:32:10 +04:00
|
|
|
if (!button) {
|
|
|
|
button = options.button;
|
|
|
|
this.set('button', button);
|
|
|
|
}
|
2014-07-01 22:56:02 +04:00
|
|
|
this.open();
|
|
|
|
} else if (isOpen) {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
click: function (event) {
|
|
|
|
this._super(event);
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-07-01 22:56:02 +04:00
|
|
|
if (this.get('closeOnClick')) {
|
|
|
|
return this.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-05-31 04:07:15 +04:00
|
|
|
didInsertElement: function () {
|
|
|
|
this._super();
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-09-28 19:39:25 +04:00
|
|
|
var dropdownService = this.get('dropdown');
|
2014-05-31 04:07:15 +04:00
|
|
|
|
2014-09-28 19:39:25 +04:00
|
|
|
dropdownService.on('close', this, this.close);
|
|
|
|
dropdownService.on('toggle', this, this.toggle);
|
2014-05-31 04:07:15 +04:00
|
|
|
},
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-05-31 04:07:15 +04:00
|
|
|
willDestroyElement: function () {
|
|
|
|
this._super();
|
2014-10-25 01:09:50 +04:00
|
|
|
|
2014-09-28 19:39:25 +04:00
|
|
|
var dropdownService = this.get('dropdown');
|
2014-05-31 04:07:15 +04:00
|
|
|
|
2014-09-28 19:39:25 +04:00
|
|
|
dropdownService.off('close', this, this.close);
|
|
|
|
dropdownService.off('toggle', this, this.toggle);
|
2014-05-31 04:07:15 +04:00
|
|
|
}
|
2014-04-07 02:11:22 +04:00
|
|
|
});
|