-
Notifications
You must be signed in to change notification settings - Fork 105
/
angulargrid.min.js
11 lines (9 loc) · 8.25 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(a,b){"undefined"!=typeof module&&module.exports?module.exports=b(require("angular"),a):"function"==typeof define&&define.amd?define(["angular"],function(c){return b(c,a)}):b(a.angular,a)}(this,function(a,b,c){"use strict";function i(a){return a.complete&&("undefined"==typeof a.naturalWidth||0!==a.naturalWidth)}function j(a){return Array.prototype.slice.call(a)}var d={gridWidth:300,gutterSize:10,gridNo:"auto",direction:"ltor",refreshOnImgLoad:!0,cssGrid:!1,performantScroll:!1,pageSize:"auto",scrollContainer:"body",infiniteScrollDelay:3e3,infiniteScrollDistance:100},e=a.element,f=function(a){return a.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase()},g={visibility:"hidden",opacity:0,top:0,left:0,width:""},h=function(){var a=e(b);return function(b){return a[0]=b,a}}();return e(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>"),a.module("angularGrid",[]).directive("angularGrid",["$timeout","$window","$q","angularGridInstance",function(k,l,m,n){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"},link:function(o,p,q){function y(){x={},Object.keys(d).forEach(function(a){o[a]!==c?x[a]=o[a]:o["dep_"+a]!==c&&(x[a]=o["dep_"+a])}),x=a.extend({},d,x,o.options||o.dep_options),x.cssGrid&&(x.gutterSize=0),"auto"==x.pageSize&&(x.pageSize=b.offsetWidth>=768?2:3)}function A(a,b){b=b||document.body;var c=0,d=0;if(a.offsetParent)do c+=a.offsetLeft,d+=a.offsetTop,a=a.offsetParent;while(a&&a!=b);return{left:c,top:d}}function B(){var a=e(document.querySelector(x.scrollContainer)),b=a[0],c="body"===x.scrollContainer?s:a;return{height:c[0].innerHeight||c[0].offsetHeight,scrollHeight:b.scrollHeight,startFrom:A(r,b).top,$elm:c}}function C(a,b,c){z.pageInfo=[{from:0}];var d,e,f,g=x.pageSize,h=z.scrollContInfo.height,i=h*g,j=Math.ceil(b/i),k=0;for(k=0;k<j;k++)for(var l=0,m=a.length;l<m;l++)if(d=a[l],e=k?i*k:0,f=i*(k+1),d.bottom<e||d.top>f){if(d.top>f)break}else z.pageInfo[k]||(z.pageInfo[k]={from:l}),z.pageInfo[k].to=l;z.pageInfo=z.pageInfo.map(function(a,b){var c=Math.max(b-1,0),d=Math.min(b+1,z.pageInfo.length-1);return{from:z.pageInfo[c].from,to:z.pageInfo[d].to}})}function D(a){var b,c,d,e;for(d=0,e=u.length;d<e;d++)c=h(u[d]),b=c.data(),b.$scope&&(c.data("_agOldWatchers",b.$scope.$$watchers),b.$scope.$$watchers=[]);for(d=0,e=a.length;d<e;d++)b=h(a[d]).data(),b.$scope&&(b.$scope.$$watchers=b._agOldWatchers||[],b.$scope.$digest())}function E(a){z.lastScrollPosition=a;var b;if(!z.isBusy){var c=0,d=x.pageSize;if(a>z.scrollContInfo.startFrom+z.scrollContInfo.height*d&&(c=Math.floor((a-z.scrollContInfo.startFrom)/(z.scrollContInfo.height*d))),c!=z.lastPage){z.lastPage=c;var e=z.pageInfo[c];e&&(p.children().detach(),b=Array.prototype.slice.call(u,e.from,e.to+1),D(b),p.append(b))}}}function F(){clearTimeout(z.infiniteScrollTimeout),z.isLoading=!1}function G(a){if(!z.isLoading&&o.model.length){var b=z.scrollContInfo.scrollHeight,c=z.scrollContInfo.height;a>=b-c*(1+x.infiniteScrollDistance/100)&&(z.isLoading=!0,o.infiniteScroll(),z.infiniteScrollTimeout=setTimeout(F,x.infiniteScrollDelay))}}function H(){var a=this.scrollTop||this.scrollY;x.performantScroll&&E(a),o.infiniteScroll&&G(a)}function I(){var b,a=r.offsetWidth;if(x.cssGrid){b=e(u[0]).clone(),b.css(g).addClass("ag-no-transition ag-clone"),p.append(b);var c=b[0].offsetWidth;return b.remove(),{no:c?Math.floor((a+12)/c):0,width:c}}var d="auto"==x.gridNo?x.gridWidth:Math.floor(a/x.gridNo)-x.gutterSize,f="auto"==x.gridNo?Math.floor((a+x.gutterSize)/(d+x.gutterSize)):x.gridNo,h=(a+x.gutterSize)%(d+x.gutterSize);return d+=Math.floor(h/f),{no:f,width:d}}function J(b,c){var d=c.beforeLoad||a.noop,e=c.onLoad||a.noop,f=c.isLoaded||a.noop,g=c.onFullLoad||a.noop,h=c.ignoreCheck||a.noop,k=b.find("img"),l=[];j(k).forEach(function(a){a.src&&(d(a),i(a)||h(a)?f(a):l.push(m(function(b,c){a.onload=function(){e(a),b()},a.onerror=c})))}),l.length?m.all(l).then(g,g):setTimeout(function(){g()},0)}function K(){if(u&&u.length){v++;var f,b=I(),c=b.width,d=b.no;if(d){var i=[];for(f=0;f<d;f++)i.push(0);j(u).forEach(function(a){var b=h(a);j(b.find("img")).forEach(function(a){var d=e(a);if(d.hasClass("img-loaded"))return void d.css("height","");b.addClass("ag-no-transition"),b.css("width",c+"px");var f=d.attr("actual-width")||d.attr("data-actual-width"),g=d.attr("actual-height")||d.attr("data-actual-height");f&&g&&d.css("height",g*a.width/f+"px")}),b.removeClass("ag-no-transition")});var k=u.clone();k.addClass("ag-no-transition ag-clone");var l=a.extend({},g);l.width=c+"px",k.css(l),p.append(k),function(a){J(k,{ignoreCheck:function(a){return!h(a).hasClass("img-loaded")},onFullLoad:function(){if(a<v)return void k.remove();var f,g,j,b=[],e=[];for(g=0,j=k.length;g<j;g++)b.push(k[g].offsetHeight);for(g=0,j=u.length;g<j;g++){f=h(u[g]);var l=b[g],m=Math.min.apply(Math,i),n=i.indexOf(m);i[n]=m+l+x.gutterSize;var q=n*(c+x.gutterSize),r={position:"absolute",top:m+"px"};"rtol"==x.direction?r.right=q+"px":r.left=q+"px",r.width=c+"px",e.push({top:m,bottom:m+l}),f.css(r).addClass("angular-grid-item")}var s=Math.max.apply(Math,i);p.css("height",s+"px"),k.remove(),(x.performantScroll||o.infiniteScroll)&&(z.scrollContInfo=B()),x.performantScroll&&(z.lastPage=null,C(e,s,d),z.isBusy=!1,E(z.lastScrollPosition||0)),F()}})}(v)}}}function L(){var a=!1;j(u).forEach(function(b){var c=e(b),d=c.find("img");d.length&&(c.addClass("img-loading"),J(c,{beforeLoad:function(a){h(a).addClass("img-loading")},isLoaded:function(a){h(a).removeClass("img-loading").addClass("img-loaded")},onLoad:function(b){!a&&x.refreshOnImgLoad&&(a=!0,k(function(){K(),a=!1},100)),h(b).removeClass("img-loading").addClass("img-loaded")},onFullLoad:function(){c.removeClass("img-loading").addClass("img-loaded")}}))})}function M(){return e(j(p.children()).filter(function(a){return!h(a).hasClass("ag-clone")}))}function N(){var a=j(u).filter(function(a){return h(a).hasClass("ng-leave")});return m(function(b){a.length?h(a[0]).one("webkitTransitionEnd transitionend msTransitionEnd oTransitionEnd",function(){k(function(){u=M(),b()})}):b()})}function O(){z.isBusy=!0,k(function(){u=M(),N().then(function(){L(),k(function(){K()})})})}function P(){y(),u&&K()}function R(){z.isBusy=!0;var a=r.offsetWidth;Q!=a&&(Q=a,w&&k.cancel(w),w=k(function(){x.performantScroll&&(p.children().detach(),p.append(u)),K()},100))}var u,w,r=p[0],s=e(l),t=o.agId||o.dep_agId,v=0;p.addClass("angular-grid");var x;["gridWidth","gutterSize","refreshOnImgLoad","direction","options","cssGrid","gridNo","agId"].forEach(function(a){var b=f(a),d="ag-"+f(a);"options"==a&&(b="angular-grid-options"),"agId"==a&&(b="angular-grid-id",d="ag-id"),o["dep_"+a]!==c&&console&&console.warn&&console.warn(b+" is deprecated. Use "+d+" instead in template.")}),y();var z={};setTimeout(function(){z.scrollContInfo=B(),z.scrollContInfo.$elm.on("scroll",H)},0),o.$watch("model",O,!0),o.$watch("options",P,!0),Object.keys(d).forEach(function(a){o[a]!==c&&o.$watch(a,P)});var Q=r.offsetWidth;s.on("resize",R),t&&(n[t]={refresh:function(){O()},handleScroll:function(a){x.performantScroll&&E(a),o.infiniteScroll&&G(a)}}),o.$on("$destroy",function(){t&&delete n[t],s.off("resize",R),clearTimeout(z.infiniteScrollTimeout),z.scrollContInfo&&z.scrollContInfo.$elm.off("scroll",H)})}}}]).factory("angularGridInstance",function(){var a={};return a}).name});