-
Notifications
You must be signed in to change notification settings - Fork 48
/
angular-scrollable-table.min.js
1 lines (1 loc) · 6.77 KB
/
angular-scrollable-table.min.js
1
!function(e){"use strict";function t(e){return parseInt(e.replace(/px|%/,""),10)}var n=navigator.userAgent.toLowerCase().indexOf("firefox")>-1;e.module("scrollable-table",[]).directive("scrollableTable",["$timeout","$q","$parse",function(i,r,s){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(a,l,o){function c(e,t){var n=a.sortExpr.match(/(.+)\s+as\s+(.+)/),i={};i[n[1]]=e;var r=s(n[2])(i);i[n[1]]=t;var l=s(n[2])(i);return r===l?0:r>l?1:-1}function d(e){var t=l.find(".headerSpacer").height(),n=l.find(".scrollArea").scrollTop();l.find(".scrollArea").scrollTop(n+e.position().top-t)}function h(){function e(){0===l.find("table:visible").length?i(e,100):t.resolve()}var t=r.defer();return i(e),t.promise}function f(){l.find("thead th .th-inner").length||l.find("thead th").wrapInner('<div class="th-inner"></div>'),l.find("thead th .th-inner:not(:has(.box))").length&&l.find("thead th .th-inner:not(:has(.box))").wrapInner('<div class="box"></div>'),l.find("table th .th-inner:visible").each(function(n,i){var r=(i=e.element(i)).parent().width(),s=l.find("table th:visible:last"),a=r;if("center"!==s.css("text-align")){var o=l.find(".scrollArea").height()<l.find("table").height();s[0]==i.parent()[0]&&o&&(a+=l.find(".scrollArea").width()-l.find("tbody tr").width(),a=Math.max(a,r))}var c=t(i.parent().css("min-width")),d=i.parent().attr("title");a=Math.max(c,a),i.css("width",a),d||(d=i.find(".title .ng-scope").text()||i.find(".box").text()),i.attr("title",d.trim())}),v.resolve()}function u(){for(var e=h().then(f),t=a.headerResizeHanlers||[],n=0;n<t.length;n++)e=e.then(t[n]);return e}this.getSortExpr=function(){return a.sortExpr},this.isAsc=function(){return a.asc},this.setSortExpr=function(e){a.asc=!0,a.sortExpr=e},this.toggleSort=function(){a.asc=!a.asc},this.doSort=function(e){e?a.rows.sort(function(t,n){var i=e(t,n);return a.asc?i:-1*i}):a.rows.sort(function(e,t){var n=c(e,t);return a.asc?n:-1*n})},this.renderTalble=function(){return h().then(f)},this.getTableElement=function(){return l},this.appendTableResizingHandler=function(e){for(var t=a.headerResizeHanlers||[],n=0;n<t.length;n++)if(t[n].name===e.name)return;t.push(e),a.headerResizeHanlers=t},a.$on("rowSelected",function(e,t){var n=l.find(".scrollArea table tr[row-id='"+t+"']");1===n.length&&r.all([h(),v.promise]).then(function(){d(n)})});var v=r.defer();a.$watch("rows",function(e,t){e&&(u(l.find(".scrollArea").width()),a.sortExpr=null,l.find(".scrollArea").scrollTop(0))}),a.asc=!o.hasOwnProperty("desc"),a.sortAttr=o.sortAttr;var p=n?"thead":"thead th .th-inner";l.find(".scrollArea").on("scroll",function(e){l.find(p).css("margin-left",0-e.target.scrollLeft)}),a.$on("renderScrollableTable",function(){u(l.find(".scrollArea").width())});var b=function(){i(function(){a.$apply()})};e.element(window).on("resize",b),a.$on("$destroy",function(){e.element(window).off("resize",b)}),a.$watch(function(){return l.find(".scrollArea").width()},function(e,t){e*t<=0||u()})}]}}]).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>',link:function(t,n,i,r){var s=i.on||"a as a."+i.col;t.element=e.element(n),t.isActive=function(){return r.getSortExpr()===s},t.toggleSort=function(e){t.isActive()?r.toggleSort():r.setSortExpr(s),r.doSort(t[i.comparatorFn]),e.preventDefault()},t.isAscending=function(){return!(!t.focused||t.isActive())||r.isAsc()},t.enter=function(){t.focused=!0},t.leave=function(){t.focused=!1},t.isLastCol=function(){return n.parent().find("th:last-child").get(0)===n.get(0)}}}}]).directive("resizable",["$compile",function(n){return{restrict:"A",priority:0,scope:!1,require:"scrollableTable",link:function(i,r,s,a){function l(){var t=r.find("table th:not(:last-child) .th-inner");if(0==t.find(".resize-rod").length){a.getTableElement().find(".scrollArea table").css("table-layout","auto");var s=e.element('<div class="resize-rod" ng-mousedown="resizing($event)"></div>');t.append(n(s)(i))}}function o(){var n=1;a.getTableElement().find("table th .th-inner:visible").each(function(i,r){var s=(r=e.element(r)).parent().width(),a=t(r.parent().css("min-width"));s=Math.max(a,s),r.css("left",n),n+=s})}function c(){var n=1,i=a.getTableElement();a.getTableElement().find("table th .th-inner:visible").each(function(r,s){var a=(s=e.element(s)).parent().width(),l=i.find("table th:visible:last"),o=t(s.parent().css("min-width"));a=Math.max(o,a),l[0]!=s.parent()[0]&&s.parent().css("width",a),s.css("left",n),n+=a})}function d(n){var i=a.getTableElement(),r=i.find("table th:visible").length,s=i.find("table th:visible:last");i.find("table th:visible").each(function(i,a){if(a=e.element(a),s.get(0)!=a.get(0)){var l=a.data("width");l=/\d+%$/.test(l)?Math.ceil(n*t(l)/100):n/r,a.css("width",l+"px")}else a.css("width","auto")}),a.renderTalble().then(c())}a.appendTableResizingHandler(function(){l()}),a.appendTableResizingHandler(function(){var e=a.getTableElement().find(".scrollArea table");"auto"===e.css("table-layout")?o():d(e.parent().width())}),i.resizing=function(n){var i=a.getTableElement().find(".scrollArea").scrollLeft(),r=e.element(n.target).parent(),s=r.parent(),l=t(r.css("left"))+r.width()-i,o=n.pageX,d=e.element(document),h=e.element("body"),f=e.element(".scrollableContainer .resizing-cover"),u=e.element('<div class="scaler">');h.addClass("scrollable-resizing"),f.addClass("active"),e.element(".scrollableContainer").append(u),u.css("left",l),d.bind("mousemove",function(e){var n=e.pageX-o,i=t(u.css("left"))-l,r=s.width(),a=s.nextAll("th:visible").first(),c=t(s.css("min-width")),d=a.width(),h=t(a.css("min-width"));o=e.pageX,e.preventDefault(),n>0&&d-i<=h||n<0&&r+i<=c||u.css("left",t(u.css("left"))+n)}),d.bind("mouseup",function(n){n.preventDefault(),u.remove(),h.removeClass("scrollable-resizing"),f.removeClass("active"),d.unbind("mousemove"),d.unbind("mouseup");var i=t(u.css("left"))-l,r=s.width(),o=t(s.css("min-width")),v=s.nextAll("th:visible").first(),p=v.width(),b=t(v.css("min-width")),g=a.getTableElement().find(".scrollArea table");"auto"===g.css("table-layout")&&g.find("th .th-inner").each(function(t,n){var i=(n=e.element(n)).parent().width();n.parent().css("width",i)}),g.css("table-layout","fixed"),i>0&&p-i<=b&&(i=p-b),v.removeAttr("style"),r+=i,s.css("width",Math.max(o,r)),v.css("width",p-i),a.renderTalble().then(c())})}}}}])}(angular);