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

308 lines
8.7 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";
var Animations;
UI.component('switcher', {
defaults: {
connect : false,
2017-11-12 00:33:10 +00:00
toggle : '>*',
2016-01-30 20:28:43 +00:00
active : 0,
animation : false,
2016-03-19 01:51:35 +00:00
duration : 200,
swiping : true
2016-01-30 20:28:43 +00:00
},
animating: false,
boot: function() {
// init code
UI.ready(function(context) {
2017-11-12 00:33:10 +00:00
UI.$('[data-uk-switcher]', context).each(function() {
2016-01-30 20:28:43 +00:00
var switcher = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!switcher.data('switcher')) {
var obj = UI.switcher(switcher, UI.Utils.options(switcher.attr('data-uk-switcher')));
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.switcher', this.options.toggle, function(e) {
2016-01-30 20:28:43 +00:00
e.preventDefault();
$this.show(this);
});
2017-11-12 00:33:10 +00:00
if (!this.options.connect) {
return;
}
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
this.connect = UI.$(this.options.connect);
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (!this.connect.length) {
return;
}
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
this.connect.on('click.uk.switcher', '[data-uk-switcher-item]', function(e) {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
e.preventDefault();
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
var item = UI.$(this).attr('data-uk-switcher-item');
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if ($this.index == item) return;
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
switch(item) {
case 'next':
case 'previous':
$this.show($this.index + (item=='next' ? 1:-1));
break;
default:
$this.show(parseInt(item, 10));
}
});
2016-03-19 01:51:35 +00:00
2017-11-12 00:33:10 +00:00
if (this.options.swiping) {
2016-03-19 01:51:35 +00:00
2017-11-12 00:33:10 +00:00
this.connect.on('swipeRight swipeLeft', function(e) {
e.preventDefault();
if (!window.getSelection().toString()) {
$this.show($this.index + (e.type == 'swipeLeft' ? 1 : -1));
2016-03-19 01:51:35 +00:00
}
2017-11-12 00:33:10 +00:00
});
}
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
this.update();
},
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
update: function() {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
this.connect.children().removeClass('uk-active').attr('aria-hidden', 'true');
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
var toggles = this.find(this.options.toggle),
active = toggles.filter('.uk-active');
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (active.length) {
this.show(active, false);
} else {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (this.options.active===false) return;
active = toggles.eq(this.options.active);
this.show(active.length ? active : toggles.eq(0), false);
2016-01-30 20:28:43 +00:00
}
2017-11-12 00:33:10 +00:00
// Init ARIA for toggles
toggles.not(active).attr('aria-expanded', 'false');
active.attr('aria-expanded', 'true');
2016-01-30 20:28:43 +00:00
},
show: function(tab, animate) {
if (this.animating) {
return;
}
2017-11-12 00:33:10 +00:00
var toggles = this.find(this.options.toggle);
2016-01-30 20:28:43 +00:00
if (isNaN(tab)) {
tab = UI.$(tab);
} else {
tab = tab < 0 ? toggles.length-1 : tab;
tab = toggles.eq(toggles[tab] ? tab : 0);
}
var $this = this,
active = UI.$(tab),
animation = Animations[this.options.animation] || function(current, next) {
if (!$this.options.animation) {
return Animations.none.apply($this);
}
var anim = $this.options.animation.split(',');
if (anim.length == 1) {
anim[1] = anim[0];
}
anim[0] = anim[0].trim();
anim[1] = anim[1].trim();
return coreAnimation.apply($this, [anim, current, next]);
};
if (animate===false || !UI.support.animation) {
animation = Animations.none;
}
if (active.hasClass("uk-disabled")) return;
// Update ARIA for Toggles
toggles.attr('aria-expanded', 'false');
active.attr('aria-expanded', 'true');
toggles.filter(".uk-active").removeClass("uk-active");
active.addClass("uk-active");
if (this.options.connect && this.connect.length) {
this.index = this.find(this.options.toggle).index(active);
if (this.index == -1 ) {
this.index = 0;
}
this.connect.each(function() {
var container = UI.$(this),
children = UI.$(container.children()),
current = UI.$(children.filter('.uk-active')),
next = UI.$(children.eq($this.index));
$this.animating = true;
animation.apply($this, [current, next]).then(function(){
current.removeClass("uk-active");
next.addClass("uk-active");
// Update ARIA for connect
current.attr('aria-hidden', 'true');
next.attr('aria-hidden', 'false');
UI.Utils.checkDisplay(next, true);
$this.animating = false;
2016-03-19 01:51:35 +00:00
2016-01-30 20:28:43 +00:00
});
});
}
this.trigger("show.uk.switcher", [active]);
}
});
Animations = {
'none': function() {
var d = UI.$.Deferred();
d.resolve();
return d.promise();
},
'fade': function(current, next) {
return coreAnimation.apply(this, ['uk-animation-fade', current, next]);
},
'slide-bottom': function(current, next) {
return coreAnimation.apply(this, ['uk-animation-slide-bottom', current, next]);
},
'slide-top': function(current, next) {
return coreAnimation.apply(this, ['uk-animation-slide-top', current, next]);
},
'slide-vertical': function(current, next, dir) {
var anim = ['uk-animation-slide-top', 'uk-animation-slide-bottom'];
if (current && current.index() > next.index()) {
anim.reverse();
}
return coreAnimation.apply(this, [anim, current, next]);
},
'slide-left': function(current, next) {
return coreAnimation.apply(this, ['uk-animation-slide-left', current, next]);
},
'slide-right': function(current, next) {
return coreAnimation.apply(this, ['uk-animation-slide-right', current, next]);
},
'slide-horizontal': function(current, next, dir) {
var anim = ['uk-animation-slide-right', 'uk-animation-slide-left'];
if (current && current.index() > next.index()) {
anim.reverse();
}
return coreAnimation.apply(this, [anim, current, next]);
},
'scale': function(current, next) {
return coreAnimation.apply(this, ['uk-animation-scale-up', current, next]);
}
};
UI.switcher.animations = Animations;
// helpers
function coreAnimation(cls, current, next) {
var d = UI.$.Deferred(), clsIn = cls, clsOut = cls, release;
if (next[0]===current[0]) {
d.resolve();
return d.promise();
}
if (typeof(cls) == 'object') {
clsIn = cls[0];
clsOut = cls[1] || cls[0];
}
2016-03-19 01:51:35 +00:00
UI.$body.css('overflow-x', 'hidden'); // fix scroll jumping in iOS
2016-01-30 20:28:43 +00:00
release = function() {
if (current) current.hide().removeClass('uk-active '+clsOut+' uk-animation-reverse');
next.addClass(clsIn).one(UI.support.animation.end, function() {
2017-11-12 00:33:10 +00:00
setTimeout(function () {
next.removeClass(''+clsIn+'').css({opacity:'', display:''});
}, 0);
2016-01-30 20:28:43 +00:00
d.resolve();
2016-03-19 01:51:35 +00:00
UI.$body.css('overflow-x', '');
2016-01-30 20:28:43 +00:00
if (current) current.css({opacity:'', display:''});
}.bind(this)).show();
};
next.css('animation-duration', this.options.duration+'ms');
if (current && current.length) {
current.css('animation-duration', this.options.duration+'ms');
current.css('display', 'none').addClass(clsOut+' uk-animation-reverse').one(UI.support.animation.end, function() {
release();
}.bind(this)).css('display', '');
} else {
next.addClass('uk-active');
release();
}
return d.promise();
}
2017-11-12 00:33:10 +00:00
})(UIkit2);