text: angular.module("tm.pagination",[]).directive("tmPagination",[function(){return{restrict:"EA",template:'<div class="page-list">'+'<ul class="pagination" ng-show="conf.totalItems > 0">'+'<li ng-class="{disabled: conf.currentPage == 1}" ng-click="prevIndex()"><span>首页</span></li>'+'<li ng-class="{disabled: conf.currentPage == 1}" ng-click="prevPage()"><span>上一页</span></li>'+'<li ng-repeat="item in pageList track by $index" ng-class="{active: item == conf.currentPage, separate: item == \'...\'}" '+'ng-click="changeCurrentPage(item)">'+"<span>{{ item }}</span>"+"</li>"+'<li ng-class="{disabled: conf.currentPage == conf.numberOfPages}" ng-click="nextPage()"><span>下一页</span></li>'+'<li ng-class="{disabled: conf.currentPage == conf.numberOfPages}" ng-click="nextBack()"><span>尾页</span></li>'+'<li><div>跳转至<input type="text" ng-model="jumpPageNum" ng-keyup="jumpPageKeyUp($event)"</div></li>'+"</ul>"+'<div class="no-items" ng-show="conf.totalItems <= 0">暂无数据</div>'+"</div>",replace:true,scope:{conf:"="},link:function(scope,element,attrs){var conf=scope.conf;var defaultPagesLength=9;var defaultPerPageOptions=[10,15,20,30,50];var defaultPerPage=11;if(conf.pagesLength){conf.pagesLength=parseInt(conf.pagesLength,10);if(!conf.pagesLength){conf.pagesLength=defaultPagesLength}if(conf.pagesLength%2===0){conf.pagesLength+=1}}else{conf.pagesLength=defaultPagesLength}if(!conf.perPageOptions){conf.perPageOptions=defaultPagesLength}if(!conf.totalItems){conf.totalItems=0}function getPagination(newValue,oldValue){if(conf.currentPage){conf.currentPage=parseInt(scope.conf.currentPage,10)}if(!conf.currentPage){conf.currentPage=1}if(conf.totalItems){conf.totalItems=parseInt(conf.totalItems,10)}if(!conf.totalItems){conf.totalItems=0;return}if(conf.itemsPerPage){conf.itemsPerPage=parseInt(conf.itemsPerPage,10)}if(!conf.itemsPerPage){conf.itemsPerPage=defaultPerPage}conf.numberOfPages=Math.ceil(conf.totalItems/conf.itemsPerPage);if(scope.conf.numberOfPages>0&&scope.conf.currentPage>scope.conf.numberOfPages){scope.conf.currentPage=scope.conf.numberOfPages}var perPageOptionsLength=scope.conf.perPageOptions.length;scope.pageList=[];if(conf.numberOfPages<=conf.pagesLength){for(i=1;i<=conf.numberOfPages;i++){scope.pageList.push(i)}}else{var offset=(conf.pagesLength-1)/2;if(conf.currentPage<=offset){for(i=1;i<=offset+1;i++){scope.pageList.push(i)}}else{if(conf.currentPage>conf.numberOfPages-offset){for(i=offset+1;i>=1;i--){scope.pageList.push(conf.numberOfPages-i)}scope.pageList.push(conf.numberOfPages)}else{for(i=Math.ceil(offset/2);i>=1;i--){scope.pageList.push(conf.currentPage-i)}scope.pageList.push(conf.currentPage);for(i=1;i<=offset/2;i++){scope.pageList.push(conf.currentPage+i)}}}}scope.$parent.conf=conf}scope.prevPage=function(){if(conf.currentPage>1){conf.currentPage-=1;getPagination();if(conf.onChange){conf.onChange()}}};scope.prevIndex=function(){if(scope.conf.currentPage>1){scope.conf.currentPage=1;getPagination();if(conf.onChange){conf.onChange()}}};scope.nextPage=function(){if(conf.currentPage<conf.numberOfPages){conf.currentPage+=1;getPagination();if(conf.onChange){conf.onChange()}}};scope.nextBack=function(){if(scope.conf.currentPage<scope.conf.numberOfPages){scope.conf.currentPage=scope.conf.numberOfPages;getPagination();if(conf.onChange){conf.onChange()}}};scope.changeCurrentPage=function(item){if(item=="..."){return}else{conf.currentPage=item;getPagination();if(conf.onChange){conf.onChange()}}};scope.changeItemsPerPage=function(){conf.currentPage=1;getPagination();if(conf.onChange){conf.onChange()}};scope.jumpToPage=function(){num=scope.jumpPageNum;if(num.match(/\d+/)){num=parseInt(num,10);if(num&&num!=conf.currentPage){if(num>conf.numberOfPages){num=conf.numberOfPages}conf.currentPage=num;getPagination();if(conf.onChange){conf.onChange()}scope.jumpPageNum=""}}};scope.jumpPageKeyUp=function(e){var keycode=window.event?e.keyCode:e.which;if(keycode==13){scope.jumpToPage()}};scope.$watch("conf.totalItems",function(value,oldValue){if(!value||value==oldValue){if(conf.onChange){conf.onChange()}}getPagination()})}}}]);