-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
wookmark.min.js
12 lines (12 loc) · 9.32 KB
/
wookmark.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*!
wookmark plugin
@name wookmark.js
@author Christoph Ono ([email protected] or @gbks)
@author Sebastian Helzle ([email protected] or @sebobo)
@version 2.2.0
@date 11/24/2019
@category jQuery plugin
@copyright (c) 2009-2019 Christoph Ono (www.wookmark.com)
@license Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
*/
!function(a){!function(a){"function"==typeof define&&define.amd?define(a):a()}(function(){function b(a,b){return function(){return a.apply(b,arguments)}}function c(a,b){var c;for(c in b)b.hasOwnProperty(c)&&(a.style[c]=b[c])}function d(a,b){u(function(){var d,e;for(d=0;d<a.length;d++)e=a[d],c(e.el,e.css);"function"==typeof b&&u(b)})}function e(a){return a.replace(/^\s+|\s+$/g,"").toLowerCase()}function f(b,c,d){window.jQuery?a(b).off(c,d):b.removeEventListener?b.removeEventListener(c,d):b.detachEvent("on"+c,d)}function g(b,c,d){f(b,c,d),window.jQuery?a(b).on(c,d):b.addEventListener?b.addEventListener(c,d):b.attachEvent("on"+c,function(){d.call(b)})}function h(a){return null===a.offsetParent}function i(a){return a.offsetHeight}function j(a){return a.offsetWidth}function k(a,b){return a.classList?a.classList.contains(b):new RegExp("(^| )"+b+"( |$)","gi").test(a.className)}function l(a,b){a.classList?a.classList.add(b):a.className+=" "+b}function m(a,b){a.classList?a.classList.remove(b):a.className=a.className.replace(new RegExp("(^|\\b)"+b.split(" ").join("|")+"(\\b|$)","gi")," ")}function n(a,b,c,d){void 0===d&&(d="wookmark-");var e=a.getAttribute("data-"+d+b);return!0===c?parseInt(e,10):e}function o(a,b,c,d){void 0===d&&(d="wookmark-"),a.setAttribute("data-"+d+b,c)}function p(a){for(var b,c={},d=[],e=a.length;e--;)b=n(a[e],"id",!0),c.hasOwnProperty(b)||(c[b]=1,d.push(a[e]));return d}function q(a,b){return void 0!==window.getComputedStyle?window.getComputedStyle(a,null).getPropertyValue(b):a.currentStyle[b]}function r(a,b){var c,d=a.length;for(c=0;c<d;c++)if(a[c]===b)return c;return-1}function s(a,c){c=c||{},"string"==typeof a&&(a=document.querySelector(a)),this.container=a,this.columns=this.resizeTimer=null,this.activeItemCount=0,this.placeholders=[],this.itemHeightsInitialized=!1,this.itemHeightsDirty=!1,this.elementTag="div",this.initItems=b(this.initItems,this),this.updateOptions=b(this.updateOptions,this),this.onResize=b(this.onResize,this),this.onRefresh=b(this.onRefresh,this),this.getItemWidth=b(this.getItemWidth,this),this.layout=b(this.layout,this),this.layoutFull=b(this.layoutFull,this),this.layoutColumns=b(this.layoutColumns,this),this.filter=b(this.filter,this),this.clear=b(this.clear,this),this.getActiveItems=b(this.getActiveItems,this),this.refreshPlaceholders=b(this.refreshPlaceholders,this),this.sortElements=b(this.sortElements,this),this.updateFilterClasses=b(this.updateFilterClasses,this),this.initItems(),this.container.style.display="block",this.updateOptions(c),this.updateFilterClasses(),this.autoResize&&g(window,"resize",this.onResize),g(this.container,"refreshWookmark",this.onRefresh)}var t={align:"center",autoResize:!0,comparator:null,direction:void 0,ignoreInactiveItems:!0,inactiveClass:"wookmark-inactive",itemSelector:void 0,itemWidth:0,fillEmptySpace:!1,flexibleWidth:0,offset:5,outerOffset:0,onLayoutChanged:void 0,placeholderClass:"wookmark-placeholder",possibleFilters:[],resizeDelay:50,verticalOffset:void 0},u=window.requestAnimationFrame||function(a){a()};return s.prototype.initItems=function(){if(void 0===this.itemSelector){for(var a,b=[],c=this.container.children,d=c.length;d--;)a=c[d],8!==a.nodeType&&(a.style.display="",o(a,"id",d),b.unshift(a));this.items=b}else this.items=this.container.querySelectorAll(this.itemSelector);this.items.length&&(this.elementTag=this.items[0].tagName),this.itemHeightsDirty=!0},s.prototype.updateFilterClasses=function(){for(var a,b,c,d,f,g=this.items.length,h={},i=this.possibleFilters,j=i.length;g--;)if(c=this.items[g],(b=JSON.parse(n(c,"filter-class",!1,"")))&&"object"==typeof b)for(a=b.length;a--;)d=e(b[a]),h.hasOwnProperty(d)||(h[d]=[]),h[d].push(c);for(;j--;)f=e(i[j]),h.hasOwnProperty(f)||(h[f]=[]);this.filterClasses=h},s.prototype.updateOptions=function(a){var b;this.itemHeightsDirty=!0,a=a||{};for(b in t)t.hasOwnProperty(b)&&(a.hasOwnProperty(b)?this[b]=a[b]:this.hasOwnProperty(b)||(this[b]=t[b]));this.verticalOffset=this.verticalOffset||this.offset,this.layout(!0)},s.prototype.onResize=function(){clearTimeout(this.resizeTimer),this.itemHeightsDirty=0!==this.flexibleWidth,this.resizeTimer=setTimeout(this.layout,this.resizeDelay)},s.prototype.onRefresh=function(){this.itemHeightsDirty=!0,this.layout()},s.prototype.filter=function(a,b,c){var d,f,g,h,i,j=[],k=[];if(a=a||[],b=b||"or",c=c||!1,a.length){for(f=0;f<a.length;f++)i=e(a[f]),this.filterClasses.hasOwnProperty(i)&&j.push(this.filterClasses[i]);if(f=d=j.length,"or"===b||1===d)for(;f--;)k=k.concat(j[f]);else if("and"===b){for(var n,o,q,s=j[0],t=!0;f--;)j[f].length<s.length&&(s=j[f]);for(s=s||[],f=s.length;f--;){for(o=s[f],g=d,t=!0;g--&&t;)if(q=j[g],s!==q){for(n=!1,h=q.length;h--&&!n;)n=q[h]===o;t&=n}t&&(k=k.concat(s[f]))}}if(d>1&&(k=p(k)),!c)for(f=this.items.length;f--;)-1===r(k,this.items[f])&&l(this.items[f],this.inactiveClass)}else k=this.items;if(!c){for(f=k.length;f--;)m(k[f],this.inactiveClass);this.columns=null,this.layout()}return k},s.prototype.refreshPlaceholders=function(a,b){var d,e,f,g,h,j,k,l=i(this.container),m=this.columns.length,o="";if(this.placeholders.length<m){for(d=0;d<m-this.placeholders.length;d++)o+="<"+this.elementTag+' class="'+this.placeholderClass+'"/>';this.container.insertAdjacentHTML("beforeend",o),this.placeholders=this.container.querySelectorAll("."+this.placeholderClass)}for(g=this.offset+2*parseInt(q(this.placeholders[0],"border-left-width"),10)||0,g+=2*parseInt(q(this.placeholders[0],"padding-left"),10)||0,d=0;d<this.placeholders.length;d++)j=this.placeholders[d],e=this.columns[d],d>=m||0===e.length?j.style.display="none":(h=e[e.length-1],k=n(h,"top",!0)+n(h,"height",!0)+this.verticalOffset,f=Math.max(0,l-k-g),c(j,{position:"absolute",display:f>0?"block":"none",left:d*a+b+"px",top:k+"px",width:a-g+"px",height:f+"px"}))},s.prototype.getActiveItems=function(){var a,b,c=this.inactiveClass,d=[],e=this.items;if(!this.ignoreInactiveItems)return e;for(a=0;a<e.length;a++)b=e[a],k(b,c)||d.push(b);return d},s.prototype.getItemWidth=function(){var a=this.itemWidth,b=j(this.container)-2*this.outerOffset,c=this.flexibleWidth;if("function"==typeof a&&(a=this.itemWidth()),this.items.length>0&&(void 0===a||0===a&&!this.flexibleWidth)?a=j(this.items[0]):"string"==typeof a&&a.indexOf("%")>=0&&(a=parseFloat(a)/100*b),c){"function"==typeof c&&(c=c()),"string"==typeof c&&c.indexOf("%")>=0&&(c=parseFloat(c)/100*b);var d=b+this.offset,e=Math.floor(.5+d/(c+this.offset)),f=Math.floor(d/(a+this.offset)),g=Math.max(e,f),h=Math.min(c,Math.floor((b-(g-1)*this.offset)/g));a=Math.max(a,h)}return a},s.prototype.layout=function(a,b){if(a||!h(this.container)){var c,d,e=this.getItemWidth(),f=e+this.offset,g=j(this.container),i=g-2*this.outerOffset,k=Math.floor((i+this.offset)/f),l=0,m=this.getActiveItems(),n=m.length;if(a||this.itemHeightsDirty||!this.itemHeightsInitialized){for(var p=0;p<n;p++)d=m[p],this.flexibleWidth&&(d.style.width=e+"px"),o(d,"height",d.offsetHeight);this.itemHeightsDirty=!1,this.itemHeightsInitialized=!0}k=Math.max(1,Math.min(k,n)),c=this.outerOffset,"center"===this.align&&(c+=Math.floor(i-(k*f-this.offset)+.5>>1)),this.direction=this.direction||("right"===this.align?"right":"left"),l=a||null===this.columns||this.columns.length!==k||this.activeItemCount!==n?this.layoutFull(f,k,c):this.layoutColumns(f,c),this.activeItemCount=n,this.container.style.height=l+"px",this.fillEmptySpace&&this.refreshPlaceholders(f,c),void 0!==this.onLayoutChanged&&"function"==typeof this.onLayoutChanged&&this.onLayoutChanged(),"function"==typeof b&&b()}},s.prototype.sortElements=function(a){return"function"==typeof this.comparator?a.sort(this.comparator):a},s.prototype.layoutFull=function(a,b,c){var e,f,g,h,i=0,j=0,m=null,p=null,q=[],r=[],s="left"===this.align,t=this;for(this.columns=[],f=this.sortElements(this.getActiveItems()),g=f.length;q.length<b;)q.push(this.outerOffset),this.columns.push([]);for(;j<g;){for(e=f[j],m=q[0],p=0,i=0;i<b;i++)q[i]<m&&(m=q[i],p=i);o(e,"top",m),h=c,(p>0||!s)&&(h+=p*a),r[j]={el:e,css:{position:"absolute",top:m+"px"}},r[j].css[this.direction]=h+"px",q[p]+=n(e,"height",!0)+this.verticalOffset,this.columns[p].push(e),j++}return d(r,function(){k(t.container,"wookmark-initialised")||l(t.container,"wookmark-initialised")}),Math.max.apply(Math,q)},s.prototype.layoutColumns=function(a,b){for(var c,e,f,g,h=[],i=[],j=0,k=0,l=this.columns.length;l--;){for(c=this.outerOffset,h.push(c),e=this.columns[l],g=l*a+b,j=0;j<e.length;j++,k++)f=e[j],o(f,"top",c),i[k]={el:f,css:{top:c+"px"}},i[k].css[this.direction]=g+"px",c+=n(f,"height",!0)+this.verticalOffset;h[l]=c}return d(i),Math.max.apply(Math,h)},s.prototype.clear=function(){clearTimeout(this.resizeTimer);for(var a=this.placeholders.length;a--;)this.container.removeChild(this.placeholders[a]);f(window,"resize",this.onResize),f(this.container,"refreshWookmark",this.onRefresh)},void 0!==window.jQuery&&(jQuery.fn.wookmark=function(b){var c=this.length;if(void 0!==b&&b.container instanceof jQuery&&(b.container=b.container[0]),c>1)for(;c--;)a(this).eq(c).wookmark(b);else 1===c&&(this.wookmarkInstance?this.wookmarkInstance.updateOptions(b||{}):this.wookmarkInstance=new s(this[0],b||{}));return this}),window.Wookmark=s,s})}(window.jQuery);