forked from alalonde/angular-scrollable-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular-scrollable-table.min.js
9 lines (9 loc) · 4.65 KB
/
angular-scrollable-table.min.js
1
2
3
4
5
6
7
8
9
(function(f){function m(a){return parseInt(a.replace(/px|%/,""),10)}f.module("scrollable-table",[]).directive("scrollableTable",["$timeout","$q","$parse",function(a,n,h){return{transclude:!0,restrict:"E",scope:{rows:"=watch",sortFn:"="},template:'<div class="scrollableContainer"><div class="headerSpacer"></div><div class="scrollArea" ng-transclude></div></div>',controller:["$scope","$element","$attrs",function(c,b,t){function k(){function c(){0===b.find("table:visible").length?a(c,100):l.resolve()}
var l=n.defer();a(c);return l.promise}function s(){b.find("thead th .th-inner").length||b.find("thead th").wrapInner('<div class="th-inner"></div>');var a=1;b.find("table th .th-inner").each(function(c,d){d=f.element(d);var p=d.parent().width(),g=b.find("table th:visible:last"),e=p;if("center"!==g.css("text-align")){var r=b.find(".scrollArea").height()<b.find("table").height();g[0]==d.parent()[0]&&r&&(e+=b.find(".scrollArea").width()-b.find("tbody tr").width(),e=Math.max(e,p))}var r=m(d.parent().css("min-width")),
k=d.parent().attr("title"),p=Math.max(r,p),e=Math.max(r,e);d.css("width",e);k||(k=d.children().length?d.find(".title .ng-scope").html():d.html());d.attr("title",k.trim());g[0]!=d.parent()[0]&&d.parent().css("width",p);d.css("left",a);a+=p});v.resolve()}function q(a){var c=b.find("table th:last"),d=b.find("table th").length;b.find("table th").each(function(p,b){b=f.element(b);if(c.get(0)!=b.get(0)){var e=b.data("width"),e=/\d+%$/.test(e)?Math.ceil(a*m(e)/100):a/d;b.css("width",e+"px")}});k().then(s)}
this.getSortExpr=function(){return c.sortExpr};this.isAsc=function(){return c.asc};this.setSortExpr=function(a){c.asc=!0;c.sortExpr=a};this.toggleSort=function(){c.asc=!c.asc};this.doSort=function(a){a?c.rows.sort(function(b,d){var p=a(b,d);return c.asc?p:-1*p}):c.rows.sort(function(a,d){var b;var g=c.sortExpr.match(/(.+)\s+as\s+(.+)/),e={};e[g[1]]=a;b=h(g[2])(e);e[g[1]]=d;g=h(g[2])(e);b=b===g?0:b>g?1:-1;return c.asc?b:-1*b})};this.resizeColumn=function(){s()};c.$on("rowSelected",function(a,c){var d=
b.find(".scrollArea table tr[row-id='"+c+"']");1===d.length&&n.all([k(),v.promise]).then(function(){var a=b.find(".headerSpacer").height(),c=b.find(".scrollArea").scrollTop();b.find(".scrollArea").scrollTop(c+d.position().top-a)})});var v=n.defer();f.element(window).on("resize",function(){c.$apply()});c.$watch(function(){return b.find(".scrollArea").width()},function(a,b){0!==a*b&&q(a)});c.$watch("rows",function(a,l){a&&(k().then(s),c.sortExpr=null,b.find(".scrollArea").scrollTop(0),q(b.find(".scrollArea").width()))});
c.asc=!t.hasOwnProperty("desc");c.sortAttr=t.sortAttr;b.find(".scrollArea").scroll(function(a){b.find("thead th .th-inner").css("margin-left",0-a.target.scrollLeft)})}]}}]).directive("sortableHeader",[function(){return{transclude:!0,scope:!0,require:"^scrollableTable",template:'<div class="box"><div ng-mouseenter="enter()" ng-mouseleave="leave()"><div class="title" ng-transclude></div><span class="orderWrapper"><span class="order" ng-show="focused || isActive()" ng-click="toggleSort($event)" ng-class="{active:isActive()}"><i ng-show="isAscending()" class="glyphicon glyphicon-chevron-up"></i><i ng-show="!isAscending()" class="glyphicon glyphicon-chevron-down"></i></span></span></div><div class="resize-rod" ng-mousedown="resizing($event)"></div></div>',
link:function(a,n,h,c){var b=h.on||"a as a."+h.col;a.element=f.element(n);a.isActive=function(){return c.getSortExpr()===b};a.toggleSort=function(f){a.isActive()?c.toggleSort():c.setSortExpr(b);c.doSort(a[h.comparatorFn]);f.preventDefault()};a.isAscending=function(){return a.focused&&!a.isActive()?!0:c.isAsc()};a.enter=function(){a.focused=!0};a.leave=function(){a.focused=!1};a.resizing=function(b){var k=m(a.element.children().css("left"))+a.element.children().width(),h=b.pageX,q=f.element(document),
n=f.element("body"),u=f.element(".scrollableContainer .resizing-cover"),l=f.element('<div class="scaler">');n.addClass("scrollable-resizing");u.addClass("active");f.element(".scrollableContainer").append(l);l.css("left",k);q.bind("mousemove",function(b){var c=b.pageX-h,g=m(l.css("left"))-k,e=a.element.width(),f=m(a.element.css("min-width")),n=a.element.next().width(),q=m(a.element.next().css("min-width"));h=b.pageX;b.preventDefault();0<c&&n-g<=q||0>c&&e+g<=f||l.css("left",m(l.css("left"))+c)});q.bind("mouseup",
function(b){b.preventDefault();l.remove();n.removeClass("scrollable-resizing");u.removeClass("active");q.unbind("mousemove");q.unbind("mouseup");b=m(l.css("left"))-k;var f=a.element.width(),g=m(a.element.css("min-width")),e=a.element.next().width(),h=m(a.element.next().css("min-width"));0<b&&e-b<=h&&(b=e-h);a.element.next().removeAttr("style");a.element.css("width",Math.max(g,f+b));c.resizeColumn()})}}}}])})(angular);