/*! UIkit 2.27.5 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */ (function(addon) { var component; if (window.UIkit2) { component = addon(UIkit2); } if (typeof define == 'function' && define.amd) { define('uikit-grid', ['uikit'], function(){ return component || addon(UIkit2); }); } })(function(UI){ "use strict"; UI.component('grid', { defaults: { colwidth : 'auto', animation : true, duration : 300, gutter : 0, controls : false, filter : false, origin : UI.langdirection }, boot: function() { // init code UI.ready(function(context) { UI.$('[data-uk-grid]', context).each(function(){ var ele = UI.$(this); if(!ele.data('grid')) { UI.grid(ele, UI.Utils.options(ele.attr('data-uk-grid'))); } }); }); }, 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; this.origin = this.options.origin; if (this.options.controls) { this.controls = UI.$(this.options.controls); // filter this.controls.on('click', '[data-uk-filter]', function(e){ e.preventDefault(); $this.filter(UI.$(this).attr('data-uk-filter')); }); // 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 { this.update(); } }.bind(this), 100)); this.on('display.uk.check', function(){ if ($this.element.is(':visible')) $this.update(); }); UI.domObserve(this.element, function(e) { $this.update(); }); if (this.options.filter !== false) { this.filter(this.options.filter); } else { this.update(); } }, _prepareElements: function() { var children = this.element.children().not('[data-grid-prepared]'), css; // exit if no already prepared elements found if (!children.length) { return; } css = { position : 'absolute', boxSizing : 'border-box', width : this.options.colwidth == 'auto' ? '' : this.options.colwidth }; if (this.options.gutter) { css['padding-'+this.origin] = this.gutterh; css['padding-bottom'] = this.gutterv; this.element.css('margin-'+this.origin, this.gutterh * -1); } children.attr('data-grid-prepared', 'true').css(css); }, update: function(elements) { var $this = this; this._prepareElements(); elements = elements || this.element.children(':visible'); var children = elements, maxwidth = this.element.width() + (2*this.gutterh) + 2, left = 0, top = 0, positions = [], item, width, height, pos, posi, i, z, max, size; this.trigger('beforeupdate.uk.grid', [children]); children.each(function(index){ item = UI.$(this); size = this.getBoundingClientRect(); width = size.width; height = size.height; left = 0; top = 0; for (i=0,max=positions.length;i -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(); $this.update(elements.visible); 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); this.update(elements.filter(':visible')); 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'); } } }); });