-
Notifications
You must be signed in to change notification settings - Fork 1
/
rect-ng.min.js
1 lines (1 loc) · 11.6 KB
/
rect-ng.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,r,n=[];if(void 0==e.multiselect||0==e.multiselect){for(r=0;r<e.visibleData.length;r++)e.visibleData[r].selected=!1;return e.visibleData[t].selected=!0,e.lastSelectIndex=t,i=e.cloneObj(e.visibleData[t]),delete i.selected,n.push(i),a.selectedRows&&(e.$parent[a.selectedRows]=[e.visibleData[t]]),void 0}if(l.shiftKey)if(-1==e.lastSelectIndex){for(r=0;r<e.visibleData.length;r++)e.visibleData[r].selected=!1;e.visibleData[t].selected=!0,i=e.cloneObj(e.visibleData[r]),delete i.selected,n.push(i)}else if(t>e.lastSelectIndex)for(r=e.lastSelectIndex;t>=r;r++)e.visibleData[r].selected=!0,i=e.cloneObj(e.visibleData[r]),delete i.selected,n.push(i);else for(r=t;r<=e.lastSelectIndex;r++)e.visibleData[r].selected=!0,i=e.cloneObj(e.visibleData[r]),delete i.selected,n.push(i);else if(l.ctrlKey||l.metaKey)for(e.visibleData[t].selected=!e.visibleData[t].selected,r=0;r<e.visibleData.length;r++)e.visibleData[r].selected&&(i=e.cloneObj(e.visibleData[r]),delete i.selected,n.push(i));else{for(r=0;r<e.visibleData.length;r++)e.visibleData[r].selected=!1;e.visibleData[t].selected=!0,i=e.cloneObj(e.visibleData[t]),delete i.selected,n.push(i)}e.lastSelectIndex=t,a.selectedRows&&(e.$parent[a.selectedRows]=n)}},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=[],r=new RegExp(e.filter,"i");for(t=0;t<e.data.length;t++){for(var n="",s=0;s<e.columns.length;s++)n+=e.data[t][e.columns[s].id]+" ";if(r.test(n)&&(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 r=0;r<e.columns.length;r++)t+=e.data[i][e.columns[r].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(e.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,r=t.target.querySelector(".rectNG-body"),n=r.querySelectorAll(".rectNG-row");for(l=0;l<e.lastSelectIndex&&l<n.length;l++)i+=n[l].offsetHeight;if(i<r.scrollTop&&(r.scrollTop=i),a.selectedRows){var s=e.cloneObj(e.visibleData[e.lastSelectIndex]);delete s.selected,e.$parent[a.selectedRows]=[s]}},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,r=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,r=s[l].offsetHeight;if(i>=n.scrollTop+n.offsetHeight&&(n.scrollTop=i-n.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 r=e.cloneObj(e.visibleData[e.lastSelectIndex]);delete r.selected,e.$parent[a.selectedRows]=[r],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>","<style>",".rectNG {margin: 0; padding: 0 0 79px; display: block; outline: none; user-select: none; -ms-user-select: none; -moz-user-select: none; -webkit-user-select: none;}",".rectNG > div.rectNG-head {display: table;width: 100%;border-bottom: 2px solid #ccc;}",".rectNG > div.rectNG-head > div:first-child {display: table-row;height: 38px;cursor: pointer;}",".rectNG-title {display: table-cell;color:#555;font-size: 16px;font-weight: bold;vertical-align: middle;padding: 3px 6px;}",".rectNG > .rectNG-body {width: 100%;height: 100%;overflow: auto;display: block;}",".rectNG > .rectNG-body > .rectNG-inner{display: table;width: 100%;}",".rectNG-inner > .rectNG-row {display: table-row;width: 100%;height: 33px;cursor: pointer;}",".rectNG-inner > .rectNG-row:nth-child(2n) {background-color: #fefefe;}",".rectNG-inner > .rectNG-row:nth-child(2n+1) {background-color: #f9f9f9;}",".rectNG-inner > .rectNG-row:hover {background-color: #eee;}",".rectNG-row > .rectNG-cell {display: table-cell;vertical-align: middle;padding: 3px 6px;}",".rectNG > .rectNG-footer {width: 100%; height: 35px; border-top: 2px solid #ccc;}",".rectNG > .rectNG-footer > .rectNG-pager {float: right; padding-top: 12px;}",".rectNG > .rectNG-footer > .rectNG-pager > .rectNG-page {display:table-cell;}",".rectNG > .rectNG-footer > .rectNG-pager > .rectNG-page > span {padding: 4px 8px; background-color: #f9f9f9;cursor: pointer;}",".rectNG > .rectNG-footer > .rectNG-pager > .rectNG-page > span:hover {background-color: #ddd;}",".rectNG > .rectNG-footer > .rectNG-pager > .rectNG-page > .rectNG-pageinput {width: 50px; height: 20px; margin: 0; border: 0; font-size: 14px; text-align: center; outline: none; user-select: none; -ms-user-select: none; -moz-user-select: none; -webkit-user-select: none;}",".rectNG > .rectNG-footer > .rectNG-pager > .rectNG-entries {display:table-cell;}",".rectNG > .rectNG-footer > .rectNG-pager > .rectNG-entries > span {margin-right: 20px; padding: 4px 20px; background-color: #f9f9f9;cursor: pointer;}",".rectNG > .rectNG-footer > .rectNG-pager > .rectNG-entries > span:hover {background-color: #ddd;}","div.rectNG-row.rectNG-selected {background-color: #ddd !important;}","</style>",'<div class="rectNG" ng-init="init()" ng-keydown="onKey($event)" tabindex="10000" style="width: {{width}}; height: {{height}}; padding: {{showPager ? \'0 0 79px\' : \'0 0 44px\'}}; ">','<div class="rectNG-head">',"<div>",'<div class="rectNG-title" ng-repeat="c in visibleModel" style="width: {{columnWidth}};" ng-click="sortBy($index)">{{c.title}} <span ng-show="lastSortIndex==$index && sortAscending">↓</span><span ng-show="lastSortIndex==$index && !sortAscending">↑</span></div>',"</div>","</div>",'<div class="rectNG-body">','<div class="rectNG-inner">','<div class="rectNG-row" ng-repeat="row in visibleData track by $index" ng-click="cellClick($index, $event)" ng-class="isRowSelected($index)">','<div class="rectNG-cell" ng-repeat="c in visibleModel" style="width: {{columnWidth}};">','<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>","</div>","</div>","</div>",'<div class="rectNG-footer" ng-show="showPager">','<div class="rectNG-pager">','<div class="rectNG-entries"><span class="arrow" ng-click="toggleItemsPerPage()">{{itemsPerPage}}</span></div>','<div class="rectNG-page"><span class="arrow" ng-click="prevPage()">←</span></div>','<div class="rectNG-page">','<span ng-show="!showPageInputBox" ng-click="gotoPage($event)">{{currentPage + \' / \' + countPages()}}</span>','<input class="rectNG-pageinput" ng-show="showPageInputBox" ng-keydown="gotoPage($event)" ng-blur="gotoPage($event)" ng-model="currentPage" type="text"></input>',"</div>",'<div class="rectNG-page"><span class="arrow" ng-click="nextPage()">→</span></div>',"</div>","</div>","</div>","</div>"].join(""),replace:!0}});