Component-Builder/media/uikit-v2/js/components/grid.js

312 lines
9.2 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(addon) {
var component;
2017-11-12 00:33:10 +00:00
if (window.UIkit2) {
component = addon(UIkit2);
2016-01-30 20:28:43 +00:00
}
2017-11-12 00:33:10 +00:00
if (typeof define == 'function' && define.amd) {
define('uikit-grid', ['uikit'], function(){
return component || addon(UIkit2);
2016-01-30 20:28:43 +00:00
});
}
})(function(UI){
"use strict";
UI.component('grid', {
defaults: {
colwidth : 'auto',
animation : true,
duration : 300,
gutter : 0,
controls : false,
2017-11-12 00:33:10 +00:00
filter : false,
origin : UI.langdirection
2016-01-30 20:28:43 +00:00
},
boot: function() {
// init code
UI.ready(function(context) {
UI.$('[data-uk-grid]', context).each(function(){
var ele = UI.$(this);
2017-11-12 00:33:10 +00:00
if(!ele.data('grid')) {
2016-03-19 01:51:35 +00:00
UI.grid(ele, UI.Utils.options(ele.attr('data-uk-grid')));
2016-01-30 20:28:43 +00:00
}
});
});
},
init: function() {
var $this = this, gutter = String(this.options.gutter).trim().split(' ');
this.gutterv = parseInt(gutter[0], 10);
this.gutterh = parseInt((gutter[1] || gutter[0]), 10);
// make sure parent element has the right position property
this.element.css({'position': 'relative'});
this.controls = null;
2017-11-12 00:33:10 +00:00
this.origin = this.options.origin;
2016-01-30 20:28:43 +00:00
if (this.options.controls) {
this.controls = UI.$(this.options.controls);
// filter
this.controls.on('click', '[data-uk-filter]', function(e){
e.preventDefault();
2017-11-12 00:33:10 +00:00
$this.filter(UI.$(this).attr('data-uk-filter'));
2016-01-30 20:28:43 +00:00
});
// sort
this.controls.on('click', '[data-uk-sort]', function(e){
e.preventDefault();
var cmd = UI.$(this).attr('data-uk-sort').split(':');
$this.sort(cmd[0], cmd[1]);
});
}
UI.$win.on('load resize orientationchange', UI.Utils.debounce(function(){
if ($this.currentfilter) {
$this.filter($this.currentfilter);
} else {
2017-11-12 00:33:10 +00:00
this.update();
2016-01-30 20:28:43 +00:00
}
}.bind(this), 100));
this.on('display.uk.check', function(){
2017-11-12 00:33:10 +00:00
if ($this.element.is(':visible')) $this.update();
2016-01-30 20:28:43 +00:00
});
2017-11-12 00:33:10 +00:00
UI.domObserve(this.element, function(e) {
$this.update();
2016-01-30 20:28:43 +00:00
});
if (this.options.filter !== false) {
this.filter(this.options.filter);
} else {
2017-11-12 00:33:10 +00:00
this.update();
2016-01-30 20:28:43 +00:00
}
},
_prepareElements: function() {
2019-04-22 13:31:39 +00:00
var children = this.element.children().not('[data-grid-prepared]'), css;
2016-01-30 20:28:43 +00:00
// exit if no already prepared elements found
if (!children.length) {
return;
}
css = {
2017-11-12 00:33:10 +00:00
position : 'absolute',
boxSizing : 'border-box',
width : this.options.colwidth == 'auto' ? '' : this.options.colwidth
2016-01-30 20:28:43 +00:00
};
if (this.options.gutter) {
2017-11-12 00:33:10 +00:00
css['padding-'+this.origin] = this.gutterh;
2016-01-30 20:28:43 +00:00
css['padding-bottom'] = this.gutterv;
2017-11-12 00:33:10 +00:00
this.element.css('margin-'+this.origin, this.gutterh * -1);
2016-01-30 20:28:43 +00:00
}
children.attr('data-grid-prepared', 'true').css(css);
},
2017-11-12 00:33:10 +00:00
update: function(elements) {
var $this = this;
2016-01-30 20:28:43 +00:00
this._prepareElements();
elements = elements || this.element.children(':visible');
2016-03-19 01:51:35 +00:00
var children = elements,
2016-01-30 20:28:43 +00:00
maxwidth = this.element.width() + (2*this.gutterh) + 2,
left = 0,
top = 0,
positions = [],
2017-11-12 00:33:10 +00:00
item, width, height, pos, posi, i, z, max, size;
2016-01-30 20:28:43 +00:00
this.trigger('beforeupdate.uk.grid', [children]);
children.each(function(index){
item = UI.$(this);
2019-04-22 13:31:39 +00:00
size = this.getBoundingClientRect();
width = size.width;
height = size.height;
2016-01-30 20:28:43 +00:00
left = 0;
top = 0;
for (i=0,max=positions.length;i<max;i++) {
pos = positions[i];
if (left <= pos.aX) { left = pos.aX; }
if (maxwidth < (left + width)) { left = 0; }
if (top <= pos.aY) { top = pos.aY; }
}
2017-11-12 00:33:10 +00:00
posi = {
ele : item,
top : top,
width : width,
height : height,
aY : (top + height),
aX : (left + width)
};
posi[$this.origin] = left;
positions.push(posi);
2016-01-30 20:28:43 +00:00
});
2017-11-12 00:33:10 +00:00
var posPrev, maxHeight = 0, positionto;
2016-01-30 20:28:43 +00:00
// fix top
for (i=0,max=positions.length;i<max;i++) {
pos = positions[i];
top = 0;
for (z=0;z<i;z++) {
posPrev = positions[z];
// (posPrev.left + 1) fixex 1px bug when using % based widths
2017-11-12 00:33:10 +00:00
if (pos[this.origin] < posPrev.aX && (posPrev[this.origin] +1) < pos.aX) {
2016-01-30 20:28:43 +00:00
top = posPrev.aY;
}
}
pos.top = top;
pos.aY = top + pos.height;
maxHeight = Math.max(maxHeight, pos.aY);
}
maxHeight = maxHeight - this.gutterv;
if (this.options.animation) {
this.element.stop().animate({'height': maxHeight}, 100);
positions.forEach(function(pos){
2017-11-12 00:33:10 +00:00
positionto = {"top": pos.top, opacity: 1};
positionto[$this.origin] = pos[$this.origin];
pos.ele.stop().animate(positionto, this.options.duration);
2016-01-30 20:28:43 +00:00
}.bind(this));
} else {
this.element.css('height', maxHeight);
positions.forEach(function(pos){
2017-11-12 00:33:10 +00:00
positionto = {"top": pos.top, opacity: 1};
positionto[$this.origin] = pos[$this.origin];
pos.ele.css(positionto);
2016-01-30 20:28:43 +00:00
}.bind(this));
}
// make sure to trigger possible scrollpies etc.
setTimeout(function() {
UI.$doc.trigger('scrolling.uk.document');
}, 2 * this.options.duration * (this.options.animation ? 1:0));
this.trigger('afterupdate.uk.grid', [children]);
},
filter: function(filter) {
this.currentfilter = filter;
filter = filter || [];
2016-03-19 01:51:35 +00:00
if (typeof(filter) === 'number') {
filter = filter.toString();
}
2016-01-30 20:28:43 +00:00
if (typeof(filter) === 'string') {
filter = filter.split(/,/).map(function(item){ return item.trim(); });
}
var $this = this, children = this.element.children(), elements = {"visible": [], "hidden": []}, visible, hidden;
children.each(function(index){
var ele = UI.$(this), f = ele.attr('data-uk-filter'), infilter = filter.length ? false : true;
if (f) {
f = f.split(/,/).map(function(item){ return item.trim(); });
filter.forEach(function(item){
if (f.indexOf(item) > -1) infilter = true;
});
}
elements[infilter ? "visible":"hidden"].push(ele);
});
// convert to jQuery collections
elements.hidden = UI.$(elements.hidden).map(function () {return this[0];});
elements.visible = UI.$(elements.visible).map(function () {return this[0];});
elements.hidden.attr('aria-hidden', 'true').filter(':visible').fadeOut(this.options.duration);
elements.visible.attr('aria-hidden', 'false').filter(':hidden').css('opacity', 0).show();
2017-11-12 00:33:10 +00:00
$this.update(elements.visible);
2016-01-30 20:28:43 +00:00
if (this.controls && this.controls.length) {
this.controls.find('[data-uk-filter]').removeClass('uk-active').filter('[data-uk-filter="'+filter+'"]').addClass('uk-active');
}
},
sort: function(by, order){
order = order || 1;
// covert from string (asc|desc) to number
if (typeof(order) === 'string') {
order = order.toLowerCase() == 'desc' ? -1 : 1;
}
var elements = this.element.children();
elements.sort(function(a, b){
a = UI.$(a);
b = UI.$(b);
return (b.data(by) || '') < (a.data(by) || '') ? order : (order*-1);
}).appendTo(this.element);
2017-11-12 00:33:10 +00:00
this.update(elements.filter(':visible'));
2016-01-30 20:28:43 +00:00
if (this.controls && this.controls.length) {
this.controls.find('[data-uk-sort]').removeClass('uk-active').filter('[data-uk-sort="'+by+':'+(order == -1 ? 'desc':'asc')+'"]').addClass('uk-active');
}
}
});
});