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

336 lines
8.7 KiB
JavaScript
Raw Permalink 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 stacks = [];
UI.component('stackMargin', {
defaults: {
2016-03-19 01:51:35 +00:00
cls: 'uk-margin-small-top',
2017-11-12 00:33:10 +00:00
rowfirst: false,
observe: 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-margin]', context).each(function() {
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
var ele = UI.$(this);
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (!ele.data('stackMargin')) {
UI.stackMargin(ele, UI.Utils.options(ele.attr('data-uk-margin')));
2016-01-30 20:28:43 +00:00
}
});
});
},
init: function() {
var $this = this;
UI.$win.on('resize orientationchange', (function() {
var fn = function() {
$this.process();
};
UI.$(function() {
fn();
2017-11-12 00:33:10 +00:00
UI.$win.on('load', fn);
2016-01-30 20:28:43 +00:00
});
return UI.Utils.debounce(fn, 20);
})());
2017-11-12 00:33:10 +00:00
this.on('display.uk.check', function(e) {
if (this.element.is(':visible')) this.process();
2016-01-30 20:28:43 +00:00
}.bind(this));
2017-11-12 00:33:10 +00:00
if (this.options.observe) {
UI.domObserve(this.element, function(e) {
if ($this.element.is(':visible')) $this.process();
});
}
2016-01-30 20:28:43 +00:00
stacks.push(this);
},
process: function() {
2016-03-19 01:51:35 +00:00
var $this = this, columns = this.element.children();
2016-01-30 20:28:43 +00:00
2016-03-19 01:51:35 +00:00
UI.Utils.stackMargin(columns, this.options);
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (!this.options.rowfirst || !columns.length) {
2016-03-19 01:51:35 +00:00
return this;
}
// Mark first column elements
2017-11-12 00:33:10 +00:00
var group = {}, minleft = false;
2016-03-19 01:51:35 +00:00
2017-11-12 00:33:10 +00:00
columns.removeClass(this.options.rowfirst).each(function(offset, $ele){
$ele = UI.$(this);
if (this.style.display != 'none') {
offset = $ele.offset().left;
((group[offset] = group[offset] || []) && group[offset]).push(this);
minleft = minleft === false ? offset : Math.min(minleft, offset);
}
});
UI.$(group[minleft]).addClass(this.options.rowfirst);
2016-01-30 20:28:43 +00:00
return this;
}
2016-03-19 01:51:35 +00:00
2016-01-30 20:28:43 +00:00
});
// responsive element e.g. iframes
(function(){
var elements = [], check = function(ele) {
if (!ele.is(':visible')) return;
var width = ele.parent().width(),
iwidth = ele.data('width'),
ratio = (width / iwidth),
height = Math.floor(ratio * ele.data('height'));
2017-11-12 00:33:10 +00:00
ele.css({height: (width < iwidth) ? height : ele.data('height')});
2016-01-30 20:28:43 +00:00
};
UI.component('responsiveElement', {
defaults: {},
boot: function() {
// init code
UI.ready(function(context) {
2017-11-12 00:33:10 +00:00
UI.$('iframe.uk-responsive-width, [data-uk-responsive]', context).each(function() {
2016-01-30 20:28:43 +00:00
var ele = UI.$(this), obj;
2017-11-12 00:33:10 +00:00
if (!ele.data('responsiveElement')) {
2016-01-30 20:28:43 +00:00
obj = UI.responsiveElement(ele, {});
}
});
});
},
init: function() {
var ele = this.element;
if (ele.attr('width') && ele.attr('height')) {
ele.data({
2017-11-12 00:33:10 +00:00
width : ele.attr('width'),
height: ele.attr('height')
2016-01-30 20:28:43 +00:00
}).on('display.uk.check', function(){
check(ele);
});
check(ele);
elements.push(ele);
}
}
});
UI.$win.on('resize load', UI.Utils.debounce(function(){
elements.forEach(function(ele){
check(ele);
});
}, 15));
})();
// helper
UI.Utils.stackMargin = function(elements, options) {
options = UI.$.extend({
2017-11-12 00:33:10 +00:00
cls: 'uk-margin-small-top'
2016-01-30 20:28:43 +00:00
}, options);
elements = UI.$(elements).removeClass(options.cls);
2017-11-12 00:33:10 +00:00
var min = false;
elements.each(function(offset, height, pos, $ele){
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
$ele = UI.$(this);
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if ($ele.css('display') != 'none') {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
offset = $ele.offset();
height = $ele.outerHeight();
pos = offset.top + height;
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
$ele.data({
ukMarginPos: pos,
ukMarginTop: offset.top
});
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
if (min === false || (offset.top < min.top) ) {
2016-01-30 20:28:43 +00:00
2017-11-12 00:33:10 +00:00
min = {
top : offset.top,
left : offset.left,
pos : pos
};
2016-01-30 20:28:43 +00:00
}
}
2017-11-12 00:33:10 +00:00
}).each(function($ele) {
$ele = UI.$(this);
if ($ele.css('display') != 'none' && $ele.data('ukMarginTop') > min.top && $ele.data('ukMarginPos') > min.pos) {
$ele.addClass(options.cls);
}
2016-01-30 20:28:43 +00:00
});
};
UI.Utils.matchHeights = function(elements, options) {
elements = UI.$(elements).css('min-height', '');
options = UI.$.extend({ row : true }, options);
var matchHeights = function(group){
if (group.length < 2) return;
var max = 0;
group.each(function() {
max = Math.max(max, UI.$(this).outerHeight());
}).each(function() {
var element = UI.$(this),
height = max - (element.css('box-sizing') == 'border-box' ? 0 : (element.outerHeight() - element.height()));
element.css('min-height', height + 'px');
});
};
if (options.row) {
elements.first().width(); // force redraw
setTimeout(function(){
var lastoffset = false, group = [];
elements.each(function() {
var ele = UI.$(this), offset = ele.offset().top;
if (offset != lastoffset && group.length) {
matchHeights(UI.$(group));
group = [];
offset = ele.offset().top;
}
group.push(ele);
lastoffset = offset;
});
if (group.length) {
matchHeights(UI.$(group));
}
}, 0);
} else {
matchHeights(elements);
}
};
2016-03-19 01:51:35 +00:00
(function(cacheSvgs){
UI.Utils.inlineSvg = function(selector, root) {
var images = UI.$(selector || 'img[src$=".svg"]', root || document).each(function(){
var img = UI.$(this),
src = img.attr('src');
if (!cacheSvgs[src]) {
var d = UI.$.Deferred();
UI.$.get(src, {nc: Math.random()}, function(data){
d.resolve(UI.$(data).find('svg'));
});
cacheSvgs[src] = d.promise();
}
cacheSvgs[src].then(function(svg) {
var $svg = UI.$(svg).clone();
if (img.attr('id')) $svg.attr('id', img.attr('id'));
if (img.attr('class')) $svg.attr('class', img.attr('class'));
if (img.attr('style')) $svg.attr('style', img.attr('style'));
if (img.attr('width')) {
$svg.attr('width', img.attr('width'));
if (!img.attr('height')) $svg.removeAttr('height');
}
if (img.attr('height')){
$svg.attr('height', img.attr('height'));
if (!img.attr('width')) $svg.removeAttr('width');
}
img.replaceWith($svg);
});
});
};
// init code
UI.ready(function(context) {
UI.Utils.inlineSvg('[data-uk-svg]', context);
});
})({});
2017-11-12 00:33:10 +00:00
UI.Utils.getCssVar = function(name) {
/* usage in css: .var-name:before { content:"xyz" } */
var val, doc = document.documentElement, element = doc.appendChild(document.createElement('div'));
element.classList.add('var-'+name);
try {
val = JSON.parse(val = getComputedStyle(element, ':before').content.replace(/^["'](.*)["']$/, '$1'));
} catch (e) {
val = undefined;
}
doc.removeChild(element);
return val;
}
})(UIkit2);