Component-Builder/admin/custom/uikit-v2/js/core/dropdown.js

535 lines
18 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";
2016-03-19 01:51:35 +00:00
var active = false, hoverIdle, flips = {
2017-11-12 00:33:10 +00:00
x: {
'bottom-left' : 'bottom-right',
'bottom-right' : 'bottom-left',
'bottom-center' : 'bottom-center',
'top-left' : 'top-right',
'top-right' : 'top-left',
'top-center' : 'top-center',
'left-top' : 'right-top',
'left-bottom' : 'right-bottom',
'left-center' : 'right-center',
'right-top' : 'left-top',
'right-bottom' : 'left-bottom',
'right-center' : 'left-center'
2016-03-19 01:51:35 +00:00
},
2017-11-12 00:33:10 +00:00
y: {
'bottom-left' : 'top-left',
'bottom-right' : 'top-right',
'bottom-center' : 'top-center',
'top-left' : 'bottom-left',
'top-right' : 'bottom-right',
'top-center' : 'bottom-center',
'left-top' : 'left-bottom',
'left-bottom' : 'left-top',
'left-center' : 'left-center',
'right-top' : 'right-bottom',
'right-bottom' : 'right-top',
'right-center' : 'right-center'
2016-03-19 01:51:35 +00:00
},
2017-11-12 00:33:10 +00:00
xy: {
'bottom-left' : 'top-right',
'bottom-right' : 'top-left',
'bottom-center' : 'top-center',
'top-left' : 'bottom-right',
'top-right' : 'bottom-left',
'top-center' : 'bottom-center',
'left-top' : 'right-bottom',
'left-bottom' : 'right-top',
'left-center' : 'right-center',
'right-top' : 'left-bottom',
'right-bottom' : 'left-top',
'right-center' : 'left-center'
2016-03-19 01:51:35 +00:00
}
};
2016-01-30 20:28:43 +00:00
UI.component('dropdown', {
defaults: {
2017-11-12 00:33:10 +00:00
mode : 'hover',
pos : 'bottom-left',
offset : 0,
remaintime : 800,
justify : false,
boundary : UI.$win,
delay : 0,
dropdownSelector: '.uk-dropdown,.uk-dropdown-blank',
hoverDelayIdle : 250,
preventflip : false
2016-01-30 20:28:43 +00:00
},
remainIdle: false,
boot: function() {
2017-11-12 00:33:10 +00:00
var triggerevent = UI.support.touch ? 'click' : 'mouseenter';
2016-01-30 20:28:43 +00:00
// init code
2017-11-12 00:33:10 +00:00
UI.$html.on(triggerevent+'.dropdown.uikit focus pointerdown', '[data-uk-dropdown]', function(e) {
2016-01-30 20:28:43 +00:00
var ele = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!ele.data('dropdown')) {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
var dropdown = UI.dropdown(ele, UI.Utils.options(ele.attr('data-uk-dropdown')));
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (e.type=='click' || (e.type=='mouseenter' && dropdown.options.mode=='hover')) {
2016-01-30 20:28:43 +00:00
dropdown.element.trigger(triggerevent);
}
2017-11-12 00:33:10 +00:00
if (dropdown.dropdown.length) {
2016-01-30 20:28:43 +00:00
e.preventDefault();
}
}
});
},
init: function() {
var $this = this;
2016-03-19 01:51:35 +00:00
this.dropdown = this.find(this.options.dropdownSelector);
this.offsetParent = this.dropdown.parents().filter(function() {
return UI.$.inArray(UI.$(this).css('position'), ['relative', 'fixed', 'absolute']) !== -1;
}).slice(0,1);
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (!this.offsetParent.length) {
this.offsetParent = this.element;
}
2016-01-30 20:28:43 +00:00
this.centered = this.dropdown.hasClass('uk-dropdown-center');
this.justified = this.options.justify ? UI.$(this.options.justify) : false;
this.boundary = UI.$(this.options.boundary);
if (!this.boundary.length) {
this.boundary = UI.$win;
}
2016-03-19 01:51:35 +00:00
// legacy DEPRECATED!
if (this.dropdown.hasClass('uk-dropdown-up')) {
this.options.pos = 'top-left';
}
if (this.dropdown.hasClass('uk-dropdown-flip')) {
this.options.pos = this.options.pos.replace('left','right');
}
if (this.dropdown.hasClass('uk-dropdown-center')) {
this.options.pos = this.options.pos.replace(/(left|right)/,'center');
}
//-- end legacy
2016-01-30 20:28:43 +00:00
// Init ARIA
this.element.attr('aria-haspopup', 'true');
2017-11-12 00:33:10 +00:00
this.element.attr('aria-expanded', this.element.hasClass('uk-open'));
this.dropdown.attr('aria-hidden', 'true');
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (this.options.mode == 'click' || UI.support.touch) {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
this.on('click.uk.dropdown', function(e) {
2016-01-30 20:28:43 +00:00
var $target = UI.$(e.target);
2016-03-19 01:51:35 +00:00
if (!$target.parents($this.options.dropdownSelector).length) {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if ($target.is("a[href='#']") || $target.parent().is("a[href='#']") || ($this.dropdown.length && !$this.dropdown.is(':visible')) ){
2016-01-30 20:28:43 +00:00
e.preventDefault();
}
$target.blur();
}
if (!$this.element.hasClass('uk-open')) {
$this.show();
} else {
2017-11-12 00:33:10 +00:00
if (!$this.dropdown.find(e.target).length || $target.is('.uk-dropdown-close') || $target.parents('.uk-dropdown-close').length) {
2016-01-30 20:28:43 +00:00
$this.hide();
}
}
});
} else {
2017-11-12 00:33:10 +00:00
this.on('mouseenter', function(e) {
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
$this.trigger('pointerenter.uk.dropdown', [$this]);
2016-01-30 20:28:43 +00:00
if ($this.remainIdle) {
clearTimeout($this.remainIdle);
}
if (hoverIdle) {
clearTimeout(hoverIdle);
}
if (active && active == $this) {
return;
}
// pseudo manuAim
if (active && active != $this) {
hoverIdle = setTimeout(function() {
hoverIdle = setTimeout($this.show.bind($this), $this.options.delay);
}, $this.options.hoverDelayIdle);
} else {
hoverIdle = setTimeout($this.show.bind($this), $this.options.delay);
}
2017-11-12 00:33:10 +00:00
}).on('mouseleave', function() {
2016-01-30 20:28:43 +00:00
if (hoverIdle) {
clearTimeout(hoverIdle);
}
$this.remainIdle = setTimeout(function() {
if (active && active == $this) $this.hide();
}, $this.options.remaintime);
2016-03-19 01:51:35 +00:00
$this.trigger('pointerleave.uk.dropdown', [$this]);
2017-11-12 00:33:10 +00:00
}).on('click', function(e){
2016-01-30 20:28:43 +00:00
var $target = UI.$(e.target);
if ($this.remainIdle) {
clearTimeout($this.remainIdle);
}
2016-03-19 01:51:35 +00:00
if (active && active == $this) {
2017-11-12 00:33:10 +00:00
if (!$this.dropdown.find(e.target).length || $target.is('.uk-dropdown-close') || $target.parents('.uk-dropdown-close').length) {
2016-03-19 01:51:35 +00:00
$this.hide();
}
return;
}
2016-01-30 20:28:43 +00:00
if ($target.is("a[href='#']") || $target.parent().is("a[href='#']")){
e.preventDefault();
}
$this.show();
});
}
},
show: function(){
2017-11-12 00:33:10 +00:00
UI.$html.off('click.outer.dropdown');
2016-01-30 20:28:43 +00:00
if (active && active != this) {
2016-03-19 01:51:35 +00:00
active.hide(true);
2016-01-30 20:28:43 +00:00
}
if (hoverIdle) {
clearTimeout(hoverIdle);
}
2016-03-19 01:51:35 +00:00
this.trigger('beforeshow.uk.dropdown', [this]);
2016-01-30 20:28:43 +00:00
this.checkDimensions();
this.element.addClass('uk-open');
// Update ARIA
this.element.attr('aria-expanded', 'true');
2017-11-12 00:33:10 +00:00
this.dropdown.attr('aria-hidden', 'false');
2016-01-30 20:28:43 +00:00
this.trigger('show.uk.dropdown', [this]);
UI.Utils.checkDisplay(this.dropdown, true);
2017-11-12 00:33:10 +00:00
UI.Utils.focus(this.dropdown);
2016-01-30 20:28:43 +00:00
active = this;
this.registerOuterClick();
},
2016-03-19 01:51:35 +00:00
hide: function(force) {
this.trigger('beforehide.uk.dropdown', [this, force]);
2016-01-30 20:28:43 +00:00
this.element.removeClass('uk-open');
if (this.remainIdle) {
clearTimeout(this.remainIdle);
}
this.remainIdle = false;
// Update ARIA
this.element.attr('aria-expanded', 'false');
2017-11-12 00:33:10 +00:00
this.dropdown.attr('aria-hidden', 'true');
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
this.trigger('hide.uk.dropdown', [this, force]);
2016-01-30 20:28:43 +00:00
if (active == this) active = false;
},
registerOuterClick: function(){
var $this = this;
2017-11-12 00:33:10 +00:00
UI.$html.off('click.outer.dropdown');
2016-01-30 20:28:43 +00:00
setTimeout(function() {
2017-11-12 00:33:10 +00:00
UI.$html.on('click.outer.dropdown', function(e) {
2016-01-30 20:28:43 +00:00
if (hoverIdle) {
clearTimeout(hoverIdle);
}
var $target = UI.$(e.target);
2016-03-19 01:51:35 +00:00
if (active == $this && !$this.element.find(e.target).length) {
$this.hide(true);
2017-11-12 00:33:10 +00:00
UI.$html.off('click.outer.dropdown');
2016-01-30 20:28:43 +00:00
}
});
}, 10);
},
checkDimensions: function() {
if (!this.dropdown.length) return;
2016-03-19 01:51:35 +00:00
// reset
2017-11-12 00:33:10 +00:00
this.dropdown.removeClass('uk-dropdown-top uk-dropdown-bottom uk-dropdown-left uk-dropdown-right uk-dropdown-stack uk-dropdown-autoflip').css({
topLeft :'',
left :'',
marginLeft :'',
marginRight :''
2016-03-19 01:51:35 +00:00
});
2016-01-30 20:28:43 +00:00
if (this.justified && this.justified.length) {
2017-11-12 00:33:10 +00:00
this.dropdown.css('min-width', '');
2016-01-30 20:28:43 +00:00
}
2016-03-19 01:51:35 +00:00
var $this = this,
pos = UI.$.extend({}, this.offsetParent.offset(), {width: this.offsetParent[0].offsetWidth, height: this.offsetParent[0].offsetHeight}),
posoffset = this.options.offset,
dropdown = this.dropdown,
offset = dropdown.show().offset() || {left: 0, top: 0},
width = dropdown.outerWidth(),
height = dropdown.outerHeight(),
2016-01-30 20:28:43 +00:00
boundarywidth = this.boundary.width(),
2016-03-19 01:51:35 +00:00
boundaryoffset = this.boundary[0] !== window && this.boundary.offset() ? this.boundary.offset(): {top:0, left:0},
dpos = this.options.pos;
var variants = {
2017-11-12 00:33:10 +00:00
'bottom-left' : {top: 0 + pos.height + posoffset, left: 0},
'bottom-right' : {top: 0 + pos.height + posoffset, left: 0 + pos.width - width},
'bottom-center' : {top: 0 + pos.height + posoffset, left: 0 + pos.width / 2 - width / 2},
'top-left' : {top: 0 - height - posoffset, left: 0},
'top-right' : {top: 0 - height - posoffset, left: 0 + pos.width - width},
'top-center' : {top: 0 - height - posoffset, left: 0 + pos.width / 2 - width / 2},
'left-top' : {top: 0, left: 0 - width - posoffset},
'left-bottom' : {top: 0 + pos.height - height, left: 0 - width - posoffset},
'left-center' : {top: 0 + pos.height / 2 - height / 2, left: 0 - width - posoffset},
'right-top' : {top: 0, left: 0 + pos.width + posoffset},
'right-bottom' : {top: 0 + pos.height - height, left: 0 + pos.width + posoffset},
'right-center' : {top: 0 + pos.height / 2 - height / 2, left: 0 + pos.width + posoffset}
2016-03-19 01:51:35 +00:00
},
css = {},
pp;
pp = dpos.split('-');
css = variants[dpos] ? variants[dpos] : variants['bottom-left'];
2016-01-30 20:28:43 +00:00
// justify dropdown
if (this.justified && this.justified.length) {
2016-03-19 01:51:35 +00:00
justify(dropdown.css({left:0}), this.justified, boundarywidth);
} else {
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
if (this.options.preventflip !== true) {
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
var fdpos;
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
switch(this.checkBoundary(pos.left + css.left, pos.top + css.top, width, height, boundarywidth)) {
case "x":
if(this.options.preventflip !=='x') fdpos = flips['x'][dpos] || 'right-top';
break;
case "y":
if(this.options.preventflip !=='y') fdpos = flips['y'][dpos] || 'top-left';
break;
case "xy":
if(!this.options.preventflip) fdpos = flips['xy'][dpos] || 'right-bottom';
break;
}
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
if (fdpos) {
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
pp = fdpos.split('-');
css = variants[fdpos] ? variants[fdpos] : variants['bottom-left'];
2017-11-12 00:33:10 +00:00
dropdown.addClass('uk-dropdown-autoflip');
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
// check flipped
if (this.checkBoundary(pos.left + css.left, pos.top + css.top, width, height, boundarywidth)) {
pp = dpos.split('-');
css = variants[dpos] ? variants[dpos] : variants['bottom-left'];
}
}
2016-01-30 20:28:43 +00:00
}
2016-03-19 01:51:35 +00:00
}
if (width > boundarywidth) {
2017-11-12 00:33:10 +00:00
dropdown.addClass('uk-dropdown-stack');
2016-03-19 01:51:35 +00:00
this.trigger('stack.uk.dropdown', [this]);
}
2017-11-12 00:33:10 +00:00
dropdown.css(css).css('display', '').addClass('uk-dropdown-'+pp[0]);
2016-03-19 01:51:35 +00:00
},
checkBoundary: function(left, top, width, height, boundarywidth) {
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
var axis = "";
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
if (left < 0 || ((left - UI.$win.scrollLeft())+width) > boundarywidth) {
axis += "x";
2016-01-30 20:28:43 +00:00
}
2016-03-19 01:51:35 +00:00
if ((top - UI.$win.scrollTop()) < 0 || ((top - UI.$win.scrollTop())+height) > window.innerHeight) {
axis += "y";
2016-01-30 20:28:43 +00:00
}
2016-03-19 01:51:35 +00:00
return axis;
}
});
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
UI.component('dropdownOverlay', {
defaults: {
2017-11-12 00:33:10 +00:00
justify : false,
cls : '',
duration: 200
2016-03-19 01:51:35 +00:00
},
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
boot: function() {
// init code
UI.ready(function(context) {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
UI.$('[data-uk-dropdown-overlay]', context).each(function() {
2016-03-19 01:51:35 +00:00
var ele = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!ele.data('dropdownOverlay')) {
UI.dropdownOverlay(ele, UI.Utils.options(ele.attr('data-uk-dropdown-overlay')));
2016-01-30 20:28:43 +00:00
}
2016-03-19 01:51:35 +00:00
});
});
},
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
init: function() {
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
var $this = this;
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
this.justified = this.options.justify ? UI.$(this.options.justify) : false;
this.overlay = this.element.find('uk-dropdown-overlay');
if (!this.overlay.length) {
this.overlay = UI.$('<div class="uk-dropdown-overlay"></div>').appendTo(this.element);
2016-01-30 20:28:43 +00:00
}
2016-03-19 01:51:35 +00:00
this.overlay.addClass(this.options.cls);
this.on({
'beforeshow.uk.dropdown': function(e, dropdown) {
$this.dropdown = dropdown;
if ($this.justified && $this.justified.length) {
2017-11-12 00:33:10 +00:00
justify($this.overlay.css({display:'block', marginLeft:'', marginRight:''}), $this.justified, $this.justified.outerWidth());
2016-03-19 01:51:35 +00:00
}
},
'show.uk.dropdown': function(e, dropdown) {
var h = $this.dropdown.dropdown.outerHeight(true);
$this.dropdown.element.removeClass('uk-open');
$this.overlay.stop().css('display', 'block').animate({height: h}, $this.options.duration, function() {
$this.dropdown.dropdown.css('visibility', '');
$this.dropdown.element.addClass('uk-open');
UI.Utils.checkDisplay($this.dropdown.dropdown, true);
});
$this.pointerleave = false;
},
'hide.uk.dropdown': function() {
$this.overlay.stop().animate({height: 0}, $this.options.duration);
},
'pointerenter.uk.dropdown': function(e, dropdown) {
clearTimeout($this.remainIdle);
},
'pointerleave.uk.dropdown': function(e, dropdown) {
$this.pointerleave = true;
}
});
this.overlay.on({
'mouseenter': function() {
if ($this.remainIdle) {
clearTimeout($this.dropdown.remainIdle);
clearTimeout($this.remainIdle);
}
},
'mouseleave': function(){
if ($this.pointerleave && active) {
$this.remainIdle = setTimeout(function() {
if(active) active.hide();
}, active.options.remaintime);
}
}
})
2016-01-30 20:28:43 +00:00
}
});
2016-03-19 01:51:35 +00:00
function justify(ele, justifyTo, boundarywidth, offset) {
ele = UI.$(ele);
justifyTo = UI.$(justifyTo);
boundarywidth = boundarywidth || window.innerWidth;
offset = offset || ele.offset();
if (justifyTo.length) {
var jwidth = justifyTo.outerWidth();
2017-11-12 00:33:10 +00:00
ele.css('min-width', jwidth);
2016-03-19 01:51:35 +00:00
if (UI.langdirection == 'right') {
var right1 = boundarywidth - (justifyTo.offset().left + jwidth),
right2 = boundarywidth - (ele.offset().left + ele.outerWidth());
2017-11-12 00:33:10 +00:00
ele.css('margin-right', right1 - right2);
2016-03-19 01:51:35 +00:00
} else {
2017-11-12 00:33:10 +00:00
ele.css('margin-left', justifyTo.offset().left - offset.left);
2016-03-19 01:51:35 +00:00
}
}
}
2017-11-12 00:33:10 +00:00
})(UIkit2);