Component-Builder/admin/custom/uikit/js/core/tab.js

170 lines
5.3 KiB
JavaScript
Raw Normal View History

2017-11-12 00:33:10 +00:00
/*! UIkit 2.27.4 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2016-01-30 20:28:43 +00:00
(function(UI) {
"use strict";
UI.component('tab', {
defaults: {
2017-11-12 00:33:10 +00:00
target : '>li:not(.uk-tab-responsive, .uk-disabled)',
connect : false,
active : 0,
animation : false,
duration : 200,
swiping : true
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-tab]', context).each(function() {
2016-01-30 20:28:43 +00:00
var tab = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!tab.data('tab')) {
var obj = UI.tab(tab, UI.Utils.options(tab.attr('data-uk-tab')));
2016-01-30 20:28:43 +00:00
}
});
});
},
init: function() {
var $this = this;
this.current = false;
2017-11-12 00:33:10 +00:00
this.on('click.uk.tab', this.options.target, function(e) {
2016-01-30 20:28:43 +00:00
e.preventDefault();
if ($this.switcher && $this.switcher.animating) {
return;
}
var current = $this.find($this.options.target).not(this);
2017-11-12 00:33:10 +00:00
current.removeClass('uk-active').blur();
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
$this.trigger('change.uk.tab', [UI.$(this).addClass('uk-active'), $this.current]);
2016-01-30 20:28:43 +00:00
$this.current = UI.$(this);
// Update ARIA
if (!$this.options.connect) {
current.attr('aria-expanded', 'false');
UI.$(this).attr('aria-expanded', 'true');
}
});
if (this.options.connect) {
this.connect = UI.$(this.options.connect);
}
// init responsive tab
this.responsivetab = UI.$('<li class="uk-tab-responsive uk-active"><a></a></li>').append('<div class="uk-dropdown uk-dropdown-small"><ul class="uk-nav uk-nav-dropdown"></ul><div>');
this.responsivetab.dropdown = this.responsivetab.find('.uk-dropdown');
this.responsivetab.lst = this.responsivetab.dropdown.find('ul');
this.responsivetab.caption = this.responsivetab.find('a:first');
2017-11-12 00:33:10 +00:00
if (this.element.hasClass('uk-tab-bottom')) this.responsivetab.dropdown.addClass('uk-dropdown-up');
2016-01-30 20:28:43 +00:00
// handle click
2016-03-19 01:51:35 +00:00
this.responsivetab.lst.on('click.uk.tab', 'a', function(e) {
2016-01-30 20:28:43 +00:00
e.preventDefault();
e.stopPropagation();
var link = UI.$(this);
$this.element.children('li:not(.uk-tab-responsive)').eq(link.data('index')).trigger('click');
});
this.on('show.uk.switcher change.uk.tab', function(e, tab) {
$this.responsivetab.caption.html(tab.text());
});
this.element.append(this.responsivetab);
// init UIkit components
if (this.options.connect) {
2017-11-12 00:33:10 +00:00
2016-01-30 20:28:43 +00:00
this.switcher = UI.switcher(this.element, {
2017-11-12 00:33:10 +00:00
toggle : '>li:not(.uk-tab-responsive)',
connect : this.options.connect,
active : this.options.active,
animation : this.options.animation,
duration : this.options.duration,
swiping : this.options.swiping
2016-01-30 20:28:43 +00:00
});
}
2017-11-12 00:33:10 +00:00
UI.dropdown(this.responsivetab, {mode: 'click', preventflip: 'y'});
2016-01-30 20:28:43 +00:00
// init
2017-11-12 00:33:10 +00:00
$this.trigger('change.uk.tab', [this.element.find(this.options.target).not('.uk-tab-responsive').filter('.uk-active')]);
2016-01-30 20:28:43 +00:00
this.check();
UI.$win.on('resize orientationchange', UI.Utils.debounce(function(){
2017-11-12 00:33:10 +00:00
if ($this.element.is(':visible')) $this.check();
2016-01-30 20:28:43 +00:00
}, 100));
this.on('display.uk.check', function(){
2017-11-12 00:33:10 +00:00
if ($this.element.is(':visible')) $this.check();
2016-01-30 20:28:43 +00:00
});
},
check: function() {
var children = this.element.children('li:not(.uk-tab-responsive)').removeClass('uk-hidden');
if (!children.length) {
this.responsivetab.addClass('uk-hidden');
return;
}
var top = (children.eq(0).offset().top + Math.ceil(children.eq(0).height()/2)),
doresponsive = false,
item, link, clone;
this.responsivetab.lst.empty();
children.each(function(){
if (UI.$(this).offset().top > top) {
doresponsive = true;
}
});
if (doresponsive) {
for (var i = 0; i < children.length; i++) {
item = UI.$(children.eq(i));
link = item.find('a');
if (item.css('float') != 'none' && !item.attr('uk-dropdown')) {
if (!item.hasClass('uk-disabled')) {
2017-11-12 00:33:10 +00:00
clone = UI.$(item[0].outerHTML);
clone.find('a').data('index', i);
2016-01-30 20:28:43 +00:00
this.responsivetab.lst.append(clone);
}
item.addClass('uk-hidden');
}
}
}
this.responsivetab[this.responsivetab.lst.children('li').length ? 'removeClass':'addClass']('uk-hidden');
}
});
2017-11-12 00:33:10 +00:00
})(UIkit2);