Component-Builder-fork/media/uikit-v2/js/core/offcanvas.js

200 lines
6.1 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 scrollpos = {x: window.scrollX, y: window.scrollY},
$win = UI.$win,
$doc = UI.$doc,
$html = UI.$html,
Offcanvas = {
2017-11-12 00:33:10 +00:00
show: function(element, options) {
2016-01-30 20:28:43 +00:00
element = UI.$(element);
if (!element.length) return;
2017-11-12 00:33:10 +00:00
options = UI.$.extend({mode: 'push'}, options);
2016-01-30 20:28:43 +00:00
var $body = UI.$('body'),
2017-11-12 00:33:10 +00:00
bar = element.find('.uk-offcanvas-bar:first'),
rtl = (UI.langdirection == 'right'),
flip = bar.hasClass('uk-offcanvas-bar-flip') ? -1:1,
2016-03-19 01:51:35 +00:00
dir = flip * (rtl ? -1 : 1),
scrollbarwidth = window.innerWidth - $body.width();
2016-01-30 20:28:43 +00:00
scrollpos = {x: window.pageXOffset, y: window.pageYOffset};
2017-11-12 00:33:10 +00:00
bar.attr('mode', options.mode);
element.addClass('uk-active');
$body.css({width: window.innerWidth - scrollbarwidth, height: window.innerHeight}).addClass('uk-offcanvas-page');
if (options.mode == 'push' || options.mode == 'reveal') {
$body.css((rtl ? 'margin-right' : 'margin-left'), (rtl ? -1 : 1) * (bar.outerWidth() * dir));
}
if (options.mode == 'reveal') {
bar.css('clip', 'rect(0, '+bar.outerWidth()+'px, 100vh, 0)');
}
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
$html.css('margin-top', scrollpos.y * -1).width(); // .width() - force redraw
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
bar.addClass('uk-offcanvas-bar-show');
2016-01-30 20:28:43 +00:00
this._initElement(element);
bar.trigger('show.uk.offcanvas', [element, bar]);
// Update ARIA
element.attr('aria-hidden', 'false');
},
hide: function(force) {
var $body = UI.$('body'),
2017-11-12 00:33:10 +00:00
panel = UI.$('.uk-offcanvas.uk-active'),
rtl = (UI.langdirection == 'right'),
bar = panel.find('.uk-offcanvas-bar:first'),
2016-01-30 20:28:43 +00:00
finalize = function() {
2017-11-12 00:33:10 +00:00
$body.removeClass('uk-offcanvas-page').css({width: '', height: '', marginLeft: '', marginRight: ''});
panel.removeClass('uk-active');
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
bar.removeClass('uk-offcanvas-bar-show');
2016-01-30 20:28:43 +00:00
$html.css('margin-top', '');
window.scrollTo(scrollpos.x, scrollpos.y);
bar.trigger('hide.uk.offcanvas', [panel, bar]);
// Update ARIA
panel.attr('aria-hidden', 'true');
};
if (!panel.length) return;
2017-11-12 00:33:10 +00:00
if (bar.attr('mode') == 'none') force = true;
2016-01-30 20:28:43 +00:00
if (UI.support.transition && !force) {
$body.one(UI.support.transition.end, function() {
finalize();
2017-11-12 00:33:10 +00:00
}).css((rtl ? 'margin-right' : 'margin-left'), '');
if (bar.attr('mode') == 'reveal') {
bar.css('clip', '');
}
2016-01-30 20:28:43 +00:00
setTimeout(function(){
2017-11-12 00:33:10 +00:00
bar.removeClass('uk-offcanvas-bar-show');
2016-01-30 20:28:43 +00:00
}, 0);
} else {
finalize();
}
},
_initElement: function(element) {
2017-11-12 00:33:10 +00:00
if (element.data('OffcanvasInit')) return;
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
element.on('click.uk.offcanvas swipeRight.uk.offcanvas swipeLeft.uk.offcanvas', function(e) {
2016-01-30 20:28:43 +00:00
var target = UI.$(e.target);
2019-04-22 13:31:39 +00:00
if (e.type.match(/swipe/)) {
if (target.parents('.uk-offcanvas-bar:first').length) return;
} else {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (!target.hasClass('uk-offcanvas-close')) {
if (target.hasClass('uk-offcanvas-bar')) return;
if (target.parents('.uk-offcanvas-bar:first').length) return;
2016-01-30 20:28:43 +00:00
}
}
e.stopImmediatePropagation();
Offcanvas.hide();
});
2017-11-12 00:33:10 +00:00
element.on('click', 'a[href*="#"]', function(e){
2016-01-30 20:28:43 +00:00
var link = UI.$(this),
2017-11-12 00:33:10 +00:00
href = link.attr('href');
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (href == '#') {
2016-01-30 20:28:43 +00:00
return;
}
UI.$doc.one('hide.uk.offcanvas', function() {
var target;
try {
2016-03-19 01:51:35 +00:00
target = UI.$(link[0].hash);
2016-01-30 20:28:43 +00:00
} catch (e){
2016-03-19 01:51:35 +00:00
target = '';
2016-01-30 20:28:43 +00:00
}
if (!target.length) {
2016-03-19 01:51:35 +00:00
target = UI.$('[name="'+link[0].hash.replace('#','')+'"]');
2016-01-30 20:28:43 +00:00
}
2016-03-19 01:51:35 +00:00
if (target.length && UI.Utils.scrollToElement) {
2016-01-30 20:28:43 +00:00
UI.Utils.scrollToElement(target, UI.Utils.options(link.attr('data-uk-smooth-scroll') || '{}'));
} else {
window.location.href = href;
}
});
Offcanvas.hide();
});
2017-11-12 00:33:10 +00:00
element.data('OffcanvasInit', true);
2016-01-30 20:28:43 +00:00
}
};
UI.component('offcanvasTrigger', {
boot: function() {
// init code
2017-11-12 00:33:10 +00:00
$html.on('click.offcanvas.uikit', '[data-uk-offcanvas]', function(e) {
2016-01-30 20:28:43 +00:00
e.preventDefault();
var ele = UI.$(this);
2017-11-12 00:33:10 +00:00
if (!ele.data('offcanvasTrigger')) {
var obj = UI.offcanvasTrigger(ele, UI.Utils.options(ele.attr('data-uk-offcanvas')));
2016-01-30 20:28:43 +00:00
ele.trigger("click");
}
});
$html.on('keydown.uk.offcanvas', function(e) {
if (e.keyCode === 27) { // ESC
Offcanvas.hide();
}
});
},
init: function() {
var $this = this;
this.options = UI.$.extend({
2017-11-12 00:33:10 +00:00
target: $this.element.is('a') ? $this.element.attr('href') : false,
mode: 'push'
2016-01-30 20:28:43 +00:00
}, this.options);
2017-11-12 00:33:10 +00:00
this.on('click', function(e) {
2016-01-30 20:28:43 +00:00
e.preventDefault();
2017-11-12 00:33:10 +00:00
Offcanvas.show($this.options.target, $this.options);
2016-01-30 20:28:43 +00:00
});
}
});
UI.offcanvas = Offcanvas;
2017-11-12 00:33:10 +00:00
})(UIkit2);