forked from s-yadav/angulargrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angulargrid.min.js
11 lines (9 loc) · 8.26 KB
/
angulargrid.min.js
1
2
3
4
5
6
7
8
9
10
11
/*
angularGrid.js v 0.6.5
Author: Sudhanshu Yadav
Copyright (c) 2015-2016 Sudhanshu Yadav - ignitersworld.com , released under the MIT license.
Demo on: http://ignitersworld.com/lab/angulargrid/
Documentation and download on https://github.com/s-yadav/angulargrid
*/
/* module to create pinterest like responsive masonry grid system for angular */
!function(o,n){"undefined"!=typeof module&&module.exports?module.exports=n(require("angular"),o):"function"==typeof define&&define.amd?define(["angular"],function(t){return n(t,o)}):n(o.angular,o)}(this,function(o,n,t){"use strict";function e(o){return o.complete&&(void 0===o.naturalWidth||0!==o.naturalWidth)}function i(o){return Array.prototype.slice.call(o)}var r={gridWidth:300,gutterSize:10,gridNo:"auto",direction:"ltor",refreshOnImgLoad:!0,cssGrid:!1,performantScroll:!1,pageSize:"auto",scrollContainer:"body",infiniteScrollDelay:3e3,infiniteScrollDistance:100},a=o.element,l=function(o){return o.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()},s={visibility:"hidden",opacity:0,top:0,left:0,width:""},d=function(){var o=a(n);return function(n){return o[0]=n,o}}();return a(document.head).append("<style>.ag-no-transition{-webkit-transition: none !important;transition: none !important;} .angular-grid{position : relative;} .angular-grid > *{opacity : 0} .angular-grid > .angular-grid-item{opacity : 1}</style>"),o.module("angularGrid",[]).directive("angularGrid",["$timeout","$window","$q","angularGridInstance",function(c,f,g,u){return{restrict:"A",scope:{model:"=angularGrid",dep_gridWidth:"=gridWidth",dep_gutterSize:"=gutterSize",dep_refreshOnImgLoad:"=refreshOnImgLoad",dep_direction:"=direction",dep_cssGrid:"=cssGrid",dep_options:"=angularGridOptions",dep_gridNo:"=gridNo",dep_agId:"@angularGridId",gridNo:"=agGridNo",gridWidth:"=agGridWidth",gutterSize:"=agGutterSize",refreshOnImgLoad:"=agRefreshOnImgLoad",direction:"=agDirection",cssGrid:"=agCssGrid",options:"=agOptions",agId:"@",pageSize:"=agPageSize",performantScroll:"=agPerformantScroll",scrollContainer:"@agScrollContainer",infiniteScroll:"&agInfiniteScroll",infiniteScrollDistance:"=agInfiniteScrollDistance",infiniteScrollDelay:"=agInfiniteScrollDelay",reflowedCB:"&agReflowed"},link:function(h,p,m){function S(){H={},Object.keys(r).forEach(function(o){h[o]!==t?H[o]=h[o]:h["dep_"+o]!==t&&(H[o]=h["dep_"+o])}),(H=o.extend({},r,H,h.options||h.dep_options)).cssGrid&&(H.gutterSize=0),"auto"==H.pageSize&&(H.pageSize=n.offsetWidth>=768?2:3)}function v(o,n){n=n||document.body;var t=0,e=0;if(o.offsetParent)do{t+=o.offsetLeft,e+=o.offsetTop,o=o.offsetParent}while(o&&o!=n);return{left:t,top:e}}function I(){var o=a(document.querySelector(H.scrollContainer)),n=o[0],t="body"===H.scrollContainer?k:o;return{height:t[0].innerHeight||t[0].offsetHeight,scrollHeight:n.scrollHeight,startFrom:v(P,n).top,$elm:t}}function C(o,n,t){A.pageInfo=[{from:0}];var e,i,r,a=H.pageSize,l=A.scrollContInfo.height*a,s=Math.ceil(n/l),d=0;for(d=0;d<s;d++)for(var c=0,f=o.length;c<f;c++)if(e=o[c],i=d?l*d:0,r=l*(d+1),e.bottom<i||e.top>r){if(e.top>r)break}else A.pageInfo[d]||(A.pageInfo[d]={from:c}),A.pageInfo[d].to=c;A.pageInfo=A.pageInfo.map(function(o,n){var t=Math.max(n-1,0),e=Math.min(n+1,A.pageInfo.length-1);return{from:A.pageInfo[t].from,to:A.pageInfo[e].to}})}function y(o){var n,t,e,i;for(e=0,i=N.length;e<i;e++)(n=(t=d(N[e])).data()).$scope&&(t.data("_agOldWatchers",n.$scope.$$watchers),n.$scope.$$watchers=[]);for(e=0,i=o.length;e<i;e++)(n=d(o[e]).data()).$scope&&(n.$scope.$$watchers=n._agOldWatchers||[],n.$scope.$digest())}function w(o){A.lastScrollPosition=o;var n;if(!A.isBusy){var t=0,e=H.pageSize;if(o>A.scrollContInfo.startFrom+A.scrollContInfo.height*e&&(t=Math.floor((o-A.scrollContInfo.startFrom)/(A.scrollContInfo.height*e))),t!=A.lastPage){A.lastPage=t;var i=A.pageInfo[t];i&&(p.children().detach(),y(n=Array.prototype.slice.call(N,i.from,i.to+1)),p.append(n))}}}function $(){clearTimeout(A.infiniteScrollTimeout),A.isLoading=!1}function z(o){!A.isLoading&&h.model.length&&o>=A.scrollContInfo.scrollHeight-A.scrollContInfo.height*(1+H.infiniteScrollDistance/100)&&(A.isLoading=!0,h.infiniteScroll(),A.infiniteScrollTimeout=setTimeout($,H.infiniteScrollDelay))}function L(){var o=this.scrollTop||this.scrollY;H.performantScroll&&w(o),h.infiniteScroll&&z(o)}function G(){var o,n=P.offsetWidth;if(H.cssGrid){(o=a(N[0]).clone()).css(s).addClass("ag-no-transition ag-clone"),p.append(o);var t=o[0].offsetWidth;return o.remove(),{no:t?Math.floor((n+12)/t):0,width:t}}var e="auto"==H.gridNo?H.gridWidth:Math.floor(n/H.gridNo)-H.gutterSize,i="auto"==H.gridNo?Math.floor((n+H.gutterSize)/(e+H.gutterSize)):H.gridNo,r=(n+H.gutterSize)%(e+H.gutterSize);return e+=Math.floor(r/i),{no:i,width:e}}function W(n,t){var r=t.beforeLoad||o.noop,a=t.onLoad||o.noop,l=t.isLoaded||o.noop,s=t.onFullLoad||o.noop,d=t.ignoreCheck||o.noop,c=[];i(n.find("img")).forEach(function(o){o.src&&(r(o),e(o)||d(o)?l(o):c.push(g(function(n,t){o.onload=function(){a(o),n()},o.onerror=t})))}),c.length?g.all(c).then(s,s):setTimeout(function(){s()},0)}function x(){if(N&&N.length){F++;var n,t=G(),e=t.width,r=t.no;if(r){var l=[];for(n=0;n<r;n++)l.push(0);i(N).forEach(function(o){var n=d(o);i(n.find("img")).forEach(function(o){var t=a(o);if(t.hasClass("img-loaded"))t.css("height","");else{n.addClass("ag-no-transition"),n.css("width",e+"px");var i=t.attr("actual-width")||t.attr("data-actual-width"),r=t.attr("actual-height")||t.attr("data-actual-height");i&&r&&t.css("height",r*o.width/i+"px")}}),n.removeClass("ag-no-transition")});var c=N.clone();c.addClass("ag-no-transition ag-clone");var f=o.extend({},s);f.width=e+"px",c.css(f),p.append(c),function(o){W(c,{ignoreCheck:function(o){return!d(o).hasClass("img-loaded")},onFullLoad:function(){if(o<F)c.remove();else{var n,t,i,a=[],s=[];for(t=0,i=c.length;t<i;t++)a.push(c[t].offsetHeight);for(t=0,i=N.length;t<i;t++){n=d(N[t]);var f=a[t],g=Math.min.apply(Math,l),u=l.indexOf(g);l[u]=g+f+H.gutterSize;var m=u*(e+H.gutterSize),S={position:"absolute",top:g+"px"};"rtol"==H.direction?S.right=m+"px":S.left=m+"px",S.width=e+"px",s.push({top:g,bottom:g+f}),n.css(S).addClass("angular-grid-item")}var v=Math.max.apply(Math,l);p.css("height",v+"px"),c.remove(),(H.performantScroll||h.infiniteScroll)&&(A.scrollContInfo=I()),H.performantScroll&&(A.lastPage=null,C(s,v,r),A.isBusy=!1,w(A.lastScrollPosition||0)),$(),h.reflowedCB({$event:{id:h.agId||null}})}}})}(F)}}}function _(){var o=!1;i(N).forEach(function(n){var t=a(n);t.find("img").length&&(t.addClass("img-loading"),W(t,{beforeLoad:function(o){d(o).addClass("img-loading")},isLoaded:function(o){d(o).removeClass("img-loading").addClass("img-loaded")},onLoad:function(n){!o&&H.refreshOnImgLoad&&(o=!0,c(function(){x(),o=!1},100)),d(n).removeClass("img-loading").addClass("img-loaded")},onFullLoad:function(){t.removeClass("img-loading").addClass("img-loaded")}}))})}function b(){return a(i(p.children()).filter(function(o){return!d(o).hasClass("ag-clone")}))}function O(){var o=i(N).filter(function(o){return d(o).hasClass("ng-leave")});return g(function(n){o.length?d(o[0]).one("webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd",function(){c(function(){N=b(),n()})}):n()})}function T(){A.isBusy=!0,c(function(){N=b(),O().then(function(){_(),c(function(){x()})})})}function M(){S(),N&&x()}function E(){A.isBusy=!0;var o=P.offsetWidth;q!=o&&(q=o,D&&c.cancel(D),D=c(function(){H.performantScroll&&(p.children().detach(),p.append(N)),x()},100))}var N,D,P=p[0],k=a(f),B=h.agId||h.dep_agId,F=0;p.addClass("angular-grid");var H;["gridWidth","gutterSize","refreshOnImgLoad","direction","options","cssGrid","gridNo","agId"].forEach(function(o){var n=l(o),e="ag-"+l(o);"options"==o&&(n="angular-grid-options"),"agId"==o&&(n="angular-grid-id",e="ag-id"),h["dep_"+o]!==t&&console&&console.warn&&console.warn(n+" is deprecated. Use "+e+" instead in template.")}),S();var A={};setTimeout(function(){A.scrollContInfo=I(),A.scrollContInfo.$elm.on("scroll",L)},0),h.$watch("model",T,!0),h.$watch("options",M,!0),Object.keys(r).forEach(function(o){h[o]!==t&&h.$watch(o,M)});var q=P.offsetWidth;k.on("resize",E),B&&(u[B]={refresh:function(){T()},handleScroll:function(o){H.performantScroll&&w(o),h.infiniteScroll&&z(o)}}),h.$on("$destroy",function(){B&&delete u[B],k.off("resize",E),clearTimeout(A.infiniteScrollTimeout),A.scrollContInfo&&A.scrollContInfo.$elm.off("scroll",L)})}}}]).factory("angularGridInstance",function(){return{}}).name});