Ui Dropdown
Ui Dropdown
Ui Dropdown
var $ = require('jquery');
var UI = require('./core');
var animation = UI.support.animation;
/**
* @via https://github.com/Minwe/bootstrap/blob/master/js/dropdown.js
* @copyright (c) 2011-2014 Twitter, Inc
* @license The MIT License
*/
options = this.options;
this.$element = $(element);
this.$toggle = this.$element.find(options.selector.toggle);
this.$dropdown = this.$element.find(options.selector.dropdown);
this.$boundary = (options.boundary === window) ? $(window) :
this.$element.closest(options.boundary);
this.$justify = (options.justify && $(options.justify).length &&
$(options.justify)) || undefined;
this.events();
};
Dropdown.DEFAULTS = {
animation: 'am-animation-slide-top-fixed',
boundary: window,
justify: undefined,
selector: {
dropdown: '.am-dropdown-content',
toggle: '.am-dropdown-toggle'
},
trigger: 'click'
};
Dropdown.prototype.toggle = function() {
this.clear();
if (this.animating) {
return;
}
Dropdown.prototype.open = function(e) {
var $toggle = this.$toggle;
var $element = this.$element;
var $dropdown = this.$dropdown;
if ($toggle.is('.am-disabled, :disabled')) {
return;
}
if (this.active) {
return;
}
$element.trigger('open.dropdown.amui').addClass('am-active');
$toggle.trigger('focus');
this.checkDimensions(e);
if (animation) {
this.animating = 1;
$dropdown.addClass(this.options.animation).
one(animation.end + '.open.dropdown.amui', $.proxy(function() {
complete();
$dropdown.removeClass(this.options.animation);
}, this));
} else {
complete();
}
};
Dropdown.prototype.close = function() {
if (!this.active) {
return;
}
// fix #165
// var animationName = this.options.animation + ' am-animation-reverse';
var animationName = 'am-dropdown-animation';
var $element = this.$element;
var $dropdown = this.$dropdown;
$element.trigger('close.dropdown.amui');
if (animation) {
$dropdown.removeClass(this.options.animation);
$dropdown.addClass(animationName);
this.animating = 1;
// animation
$dropdown.one(animation.end + '.close.dropdown.amui', function() {
$dropdown.removeClass(animationName);
complete();
});
} else {
complete();
}
};
Dropdown.prototype.enable = function() {
this.$toggle.prop('disabled', false);
},
Dropdown.prototype.disable = function() {
this.$toggle.prop('disabled', true);
},
Dropdown.prototype.checkDimensions = function(e) {
if (!this.$dropdown.length) {
return;
}
// @see #873
if (e && e.offset) {
$dropdown.offset(e.offset);
}
if (this.$justify) {
// jQuery.fn.width() is really...
$dropdown.css({'min-width': this.$justify.css('width')});
}
Dropdown.prototype.clear = function() {
$('[data-am-dropdown]').not(this.$element).each(function() {
var data = $(this).data('amui.dropdown');
data && data.close();
});
};
Dropdown.prototype.events = function() {
var eventNS = 'dropdown.amui';
// triggers = this.options.trigger.split(' '),
var $toggle = this.$toggle;
$toggle.on('click.' + eventNS, $.proxy(function(e) {
e.preventDefault();
this.toggle();
}, this));
$(document).on('keydown.dropdown.amui', $.proxy(function(e) {
e.keyCode === 27 && this.active && this.close();
}, this)).on('click.outer.dropdown.amui', $.proxy(function(e) {
// var $target = $(e.target);
if (this.active &&
(this.$element[0] === e.target || !this.$element.find(e.target).length)) {
this.close();
}
}, this));
};
// Dropdown Plugin
UI.plugin('dropdown', Dropdown);
// Init code
UI.ready(function(context) {
$('[data-am-dropdown]', context).dropdown();
});