-
Notifications
You must be signed in to change notification settings - Fork 1
/
rect-ng-bootstrap.min.js
1 lines (1 loc) · 10.5 KB
/
rect-ng-bootstrap.min.js
1
angular.module("rectNG",[]).directive("rectng",function(){return{restrict:"E",scope:{},controller:["$scope","$element","$attrs",function(e,t,a){e.lastSelectIndex=-1,e.sortAscending=!0,e.lastSortIndex=-1,e.visibleData=[],e.visibleModel=[],e.multiselect=!0,e.showPager=!0,e.currentPage=1,e.availableItemsPerPage=[50,100,500],e.itemsPerPage=50,e.showPageInputBox=!1,e.useSelection=!!a.selectedRows,e.hrefField=a.hrefField,e.cellClick=function(t,l){if(e.useSelection){var i,n,s=[];if(void 0==e.multiselect||0==e.multiselect){for(n=0;n<e.visibleData.length;n++)e.visibleData[n].selected=!1;return e.visibleData[t].selected=!0,e.lastSelectIndex=t,i=e.cloneObj(e.visibleData[t]),delete i.selected,s.push(i),a.selectedRows&&(e.$parent[a.selectedRows]=[e.visibleData[t]]),void 0}if(l.shiftKey)if(-1==e.lastSelectIndex){for(n=0;n<e.visibleData.length;n++)e.visibleData[n].selected=!1;e.visibleData[t].selected=!0,i=e.cloneObj(e.visibleData[n]),delete i.selected,s.push(i)}else if(t>e.lastSelectIndex)for(n=e.lastSelectIndex;t>=n;n++)e.visibleData[n].selected=!0,i=e.cloneObj(e.visibleData[n]),delete i.selected,s.push(i);else for(n=t;n<=e.lastSelectIndex;n++)e.visibleData[n].selected=!0,i=e.cloneObj(e.visibleData[n]),delete i.selected,s.push(i);else if(l.ctrlKey||l.metaKey)for(e.visibleData[t].selected=!e.visibleData[t].selected,n=0;n<e.visibleData.length;n++)e.visibleData[n].selected&&(i=e.cloneObj(e.visibleData[n]),delete i.selected,s.push(i));else{for(n=0;n<e.visibleData.length;n++)e.visibleData[n].selected=!1;e.visibleData[t].selected=!0,i=e.cloneObj(e.visibleData[t]),delete i.selected,s.push(i)}e.lastSelectIndex=t,a.selectedRows&&(e.$parent[a.selectedRows]=s)}},e.sortBy=function(t,a){t>=e.visibleModel.length||(t!=e.lastSortIndex||a||(e.sortAscending=!e.sortAscending),e.data.sort(e.sortFunction(e.visibleModel[t].id,e.sortAscending)),e.lastSortIndex=t,e.updateVisibleData())},e.updateVisibleModel=function(){for(var t=[],a=0;a<e.columns.length;a++)(void 0===e.columns[a].visible||e.columns[a].visible===!0)&&t.push(e.columns[a]);e.visibleModel=t},e.updateVisibleData=function(){e.visibleData=[];var t,a=(e.currentPage-1)*e.itemsPerPage;if(void 0==e.filter||""==e.filter){var l=e.itemsPerPage<e.data.length-a?e.itemsPerPage:e.data.length-a;for(t=0;l>t;t++)e.visibleData.push(e.data[a+t])}else{var i=[],n=new RegExp(e.filter,"i");for(t=0;t<e.data.length;t++){for(var s="",r=0;r<e.columns.length;r++)s+=e.data[t][e.columns[r].id]+" ";if(n.test(s)&&(i.push(e.data[t]),i.length>=a+e.itemsPerPage))break}var l=e.itemsPerPage<i.length-a?e.itemsPerPage:i.length-a;for(t=0;l>t;t++)e.visibleData.push(i[a+t])}},e.countVisibleEntries=function(){if(e.filter){for(var t,a=0,l=new RegExp(e.filter,"i"),i=0;i<e.data.length;i++){t="";for(var n=0;n<e.columns.length;n++)t+=e.data[i][e.columns[n].id]+" ";l.test(t)&&a++}return a}return e.data.length},e.countPages=function(){return Math.ceil(e.countVisibleEntries()/e.itemsPerPage)},e.nextPage=function(){var t=e.countPages();e.currentPage+1>t?e.currentPage=t:e.currentPage++,e.updateVisibleData()},e.prevPage=function(){e.countPages();e.currentPage-1<1?e.currentPage=1:e.currentPage--,e.updateVisibleData()},e.toggleItemsPerPage=function(){var t=e.availableItemsPerPage.indexOf(e.itemsPerPage);t=(t+1)%e.availableItemsPerPage.length,e.itemsPerPage=e.availableItemsPerPage[t];var a=e.countPages();parseInt(e.currentPage)>a&&(e.currentPage=a),e.updateVisibleData()},e.gotoPage=function(t){if(t&&"keydown"==t.type&&13==t.keyCode||t&&"blur"==t.type){t.preventDefault(),e.showPageInputBox=!1;var a=e.countPages();e.currentPage=parseInt(e.currentPage)>a?a:parseInt(e.currentPage)<1?1:parseInt(e.currentPage)||1,e.updateVisibleData()}},e.selectAll=function(){if(a.useSelection&&e.multiselect){var t,l,i=[];if(a.selectedRows){for(t=0;t<e.visibleData.length;t++)e.visibleData[t].selected=!0,l=e.cloneObj(e.visibleData[t]),delete l.selected,i.push(l);e.$parent[a.selectedRows]=i}else for(t=0;t<e.visibleData.length;t++)e.visibleData[t].selected=!0;e.lastSelectIndex=0}},e.selectNone=function(){if(e.useSelection&&e.multiselect){for(var t=0;t<e.visibleData.length;t++)e.visibleData[t].selected=!1;a.selectedRows&&(e.$parent[a.selectedRows]=[]),e.lastSelectIndex=0}},e.keyUp=function(t){var l;for(e.lastSelectIndex>0?e.lastSelectIndex--:e.lastSelectIndex=0,l=0;l<e.visibleData.length;l++)e.visibleData[l].selected=!1;e.visibleData[e.lastSelectIndex].selected=!0;var i=0,n=t.target.querySelector(".rectNG-body"),s=n.querySelectorAll(".rectNG-row");for(l=0;l<e.lastSelectIndex&&l<s.length;l++)i+=s[l].offsetHeight;if(i<n.scrollTop&&(n.scrollTop=i),a.selectedRows){var r=e.cloneObj(e.visibleData[e.lastSelectIndex]);delete r.selected,e.$parent[a.selectedRows]=[r]}},e.keyDown=function(t){var l;for(e.lastSelectIndex<e.visibleData.length-1?e.lastSelectIndex++:e.lastSelectIndex=e.visibleData.length-1,l=0;l<e.visibleData.length;l++)e.visibleData[l].selected=!1;e.visibleData[e.lastSelectIndex].selected=!0;var i=0,n=0,s=t.target.querySelector(".rectNG-body"),r=s.querySelectorAll(".rectNG-row");for(l=0;l<=e.lastSelectIndex&&l<r.length;l++)i+=r[l].offsetHeight,n=r[l].offsetHeight;if(i>=s.scrollTop+s.offsetHeight&&(s.scrollTop=i-s.offsetHeight),a.selectedRows){var c=e.cloneObj(e.visibleData[e.lastSelectIndex]);delete c.selected,e.$parent[a.selectedRows]=[c]}},e.onKey=function(t){"keydown"==t.type&&(t.metaKey&&65==t.keyCode||t.ctrlKey&&t.keyCode65?(t.preventDefault(),e.selectAll()):38==t.keyCode?(t.preventDefault(),e.keyUp(t)):40==t.keyCode&&(t.preventDefault(),e.keyDown(t)))},e.isRowSelected=function(t){return e.visibleData[t].selected?"rectNG-selected":""},e.getColumnWidth=function(){if(!e.width||e.width.indexOf("%")>0)return 100/e.visibleModel.length+"%";var t=parseFloat(e.width);return t/e.visibleModel.length+"px"},e.columnWidth=e.getColumnWidth(),e.$on("rectngSelectRow",function(t,l){if(!(0>l||l>=e.visibleData.length)){e.lastSelectIndex=l;for(var i=0;i<e.visibleData.length;i++)e.visibleData[i].selected=!1;if(e.visibleData[l].selected=!0,a.selectedRows){var n=e.cloneObj(e.visibleData[e.lastSelectIndex]);delete n.selected,e.$parent[a.selectedRows]=[n],e.$parent.$$phase||e.$parent.$digest()}e.$parent.$$phase||e.$$phase||e.$digest()}}),e.$on("rectngSelectAll",function(){e.selectAll(),e.$parent.$$phase||e.$$phase||e.$digest()}),e.$on("rectngSelectNone",function(){e.selectNone(),e.$parent.$$phase||e.$$phase||e.$digest()}),e.cloneObj=function(e){var t={};for(var a in e)e.hasOwnProperty(a)&&(t[a]=e[a]);return t},e.sortFunction=function(e,t,a){var l=a?function(t){return a(t[e])}:function(t){return t[e]};return t=[-1,1][+!!t],function(e,a){return e=l(e),a=l(a),e||a?e?a?t*((e>a)-(a>e)):t:-t:0}},e.$watch(a.data,function(){e.$parent.$watch(a.data,function(){e.data=e.$parent.$eval(a.data)||[],-1==e.lastSortIndex?e.updateVisibleData():e.sortBy(e.lastSortIndex,!0)}),e.data=e.$parent.$eval(a.data)||[],-1==e.lastSortIndex?e.updateVisibleData():e.sortBy(e.lastSortIndex,!0)}),e.$watch(a.columns,function(){e.$parent.$watch(a.columns,function(){e.columns=e.$parent.$eval(a.columns)||[],e.updateVisibleModel()}),e.columns=e.$parent.$eval(a.columns)||[],e.updateVisibleModel()}),e.$watch(a.filter,function(){e.$parent.$watch(a.filter,function(){e.filter=e.$parent.$eval(a.filter)||"",e.updateVisibleData()}),e.filter=e.$parent.$eval(a.filter)||"",e.updateVisibleData()}),e.$watch(a.multiselect,function(){e.multiselect=void 0===a.multiselect||"true"==a.multiselect}),e.$watch(a.pager,function(){e.showPager=void 0===a.pager||"true"==a.pager,e.showPager||(e.itemsPerPage=1e13)}),e.$watch(a.height,function(){e.$parent.$watch(a.height,function(){e.height=e.$parent.$eval(a.height)||e.height}),e.height=e.$parent.$eval(a.height)||e.height}),e.$watch(a.width,function(){e.$parent.$watch(a.width,function(){e.width=e.$parent.$eval(a.width)||e.width}),e.width=e.$parent.$eval(a.width)||e.width,e.columnWidth=e.getColumnWidth()})}],template:['<div class="rectNG">',"<style>",".rectNG, .rectNG div {outline: none; user-select: none; -ms-user-select: none; -moz-user-select: none; -webkit-user-select: none;}",".rectNG table {margin: 0;}",".rectNG .rectNG-header thead tr {height: 38px; cursor: pointer;}",".rectNG .rectNG-body {overflow: auto;}",".rectNG .rectNG-footer {width: 100%; height: 65px; border-top: 2px solid #ccc;}",".rectNG .rectNG-footer > .rectNG-pager {padding-top: 15px; }",".rectNG .rectNG-footer > .rectNG-pager input { text-align: center; }",".rectNG .rectNG-footer > .rectNG-pager > .rectNG-entries {}",".rectNG .rectNG-footer > .rectNG-pager > .rectNG-page {max-width: 180px; float: right;}",".rectNG .rectNG-row.rectNG-selected {background-color: #feffee !important;}","</style>",'<div ng-init="init()" class="rectNG-header" tabindex="10000" style="width: {{width}} !important;" >','<table class="table table-responsive table-hover table-striped">',"<thead>","<tr>",'<th class="rectNG-title" ng-repeat="c in visibleModel" style="width: {{columnWidth}} !important;" ng-click="sortBy($index)">{{c.title}} <span ng-show="lastSortIndex==$index && sortAscending">↓</span><span ng-show="lastSortIndex==$index && !sortAscending">↑</span></div>',"</tr>","</thead>","</table>","</div>",'<div class="rectNG-body" tabindex="10000" style="width: {{width}} !important; height: {{height}} !important;" ng-keydown="onKey($event)">','<table class="table table-hover">',"<tbody>",'<tr class="rectNG-row" ng-repeat="row in visibleData track by $index" ng-click="cellClick($index, $event)" ng-class="isRowSelected($index)">','<td class="rectNG-cell" ng-repeat="c in visibleModel" style="width: {{columnWidth}} !important; {{useSelection ? \'cursor: pointer;\' : \'\'}}">','<a ng-if="hrefField" ng-href="{{visibleData[$parent.$parent.$index][hrefField]}}">{{visibleData[$parent.$parent.$index][c.id]}}</a>','<span ng-if="!hrefField">{{visibleData[$parent.$parent.$index][c.id]}}</span>',"</div>","</tr>","</tbody>","</table>","</div>",'<div class="rectNG-footer" ng-show="showPager" tabindex="10000" style="width: {{width}} !important;">','<div class="row rectNG-pager">','<div class="rectNG-entries col-xs-5">','<button type="button" class="btn btn-default" tabindex="-1" ng-click="toggleItemsPerPage()">{{itemsPerPage}}</button>',"</div> ",'<div class="rectNG-page col-xs-7">','<div class="input-group">','<div class="input-group-btn">','<button type="button" class="btn btn-default" tabindex="-1" ng-click="prevPage()">←</button>',"</div>",'<input type="text" ng-keydown="gotoPage($event)" ng-blur="gotoPage($event)" ng-model="currentPage" class="form-control">','<div class="input-group-btn">','<button type="button" class="btn btn-default" tabindex="-1" ng-click="nextPage()">→</button>',"</div>","</div>","</div>","</div>","</div>","</div>"].join(""),replace:!0}});