Component-Builder/media/uikit-v2/js/core/nav.js

154 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-04-22 13:31:39 +00:00
/*! UIkit 2.27.5 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2016-01-30 20:28:43 +00:00
(function(UI) {
"use strict";
UI.component('nav', {
defaults: {
2017-11-12 00:33:10 +00:00
toggle: '>li.uk-parent > a[href="#"]',
lists: '>li.uk-parent > ul',
multiple: false
2016-01-30 20:28:43 +00:00
},
boot: function() {
// init code
UI.ready(function(context) {
2017-11-12 00:33:10 +00:00
UI.$('[data-uk-nav]', context).each(function() {
2016-01-30 20:28:43 +00:00
var nav = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!nav.data('nav')) {
var obj = UI.nav(nav, UI.Utils.options(nav.attr('data-uk-nav')));
2016-01-30 20:28:43 +00:00
}
});
});
},
init: function() {
var $this = this;
2017-11-12 00:33:10 +00:00
this.on('click.uk.nav', this.options.toggle, function(e) {
2016-01-30 20:28:43 +00:00
e.preventDefault();
var ele = UI.$(this);
$this.open(ele.parent()[0] == $this.element[0] ? ele : ele.parent("li"));
});
2017-11-12 00:33:10 +00:00
this.update();
UI.domObserve(this.element, function(e) {
if ($this.element.find($this.options.lists).not('[role]').length) {
$this.update();
}
});
},
update: function() {
var $this = this;
2016-01-30 20:28:43 +00:00
this.find(this.options.lists).each(function() {
2017-11-12 00:33:10 +00:00
var $ele = UI.$(this).attr('role', 'menu'),
parent = $ele.closest('li'),
2016-01-30 20:28:43 +00:00
active = parent.hasClass("uk-active");
2017-11-12 00:33:10 +00:00
if (!parent.data('list-container')) {
$ele.wrap('<div style="overflow:hidden;height:0;position:relative;"></div>');
parent.data('list-container', $ele.parent()[active ? 'removeClass':'addClass']('uk-hidden'));
}
2016-01-30 20:28:43 +00:00
// Init ARIA
parent.attr('aria-expanded', parent.hasClass("uk-open"));
if (active) $this.open(parent, true);
});
},
open: function(li, noanimation) {
var $this = this, element = this.element, $li = UI.$(li), $container = $li.data('list-container');
if (!this.options.multiple) {
element.children('.uk-open').not(li).each(function() {
var ele = UI.$(this);
if (ele.data('list-container')) {
ele.data('list-container').stop().animate({height: 0}, function() {
UI.$(this).parent().removeClass('uk-open').end().addClass('uk-hidden');
});
}
});
}
$li.toggleClass('uk-open');
// Update ARIA
$li.attr('aria-expanded', $li.hasClass('uk-open'));
if ($container) {
if ($li.hasClass('uk-open')) {
$container.removeClass('uk-hidden');
}
if (noanimation) {
$container.stop().height($li.hasClass('uk-open') ? 'auto' : 0);
if (!$li.hasClass('uk-open')) {
$container.addClass('uk-hidden');
}
this.trigger('display.uk.check');
} else {
$container.stop().animate({
height: ($li.hasClass('uk-open') ? getHeight($container.find('ul:first')) : 0)
}, function() {
if (!$li.hasClass('uk-open')) {
$container.addClass('uk-hidden');
} else {
$container.css('height', '');
}
$this.trigger('display.uk.check');
});
}
}
}
});
// helper
function getHeight(ele) {
2017-11-12 00:33:10 +00:00
var $ele = UI.$(ele), height = 'auto';
if ($ele.is(':visible')) {
2016-01-30 20:28:43 +00:00
height = $ele.outerHeight();
} else {
2017-11-12 00:33:10 +00:00
2016-01-30 20:28:43 +00:00
var tmp = {
2017-11-12 00:33:10 +00:00
position: $ele.css('position'),
visibility: $ele.css('visibility'),
display: $ele.css('display')
2016-01-30 20:28:43 +00:00
};
height = $ele.css({position: 'absolute', visibility: 'hidden', display: 'block'}).outerHeight();
$ele.css(tmp); // reset element
}
return height;
}
2017-11-12 00:33:10 +00:00
})(UIkit2);