Comparing version 0.2.5 to 0.2.6
{ | ||
"name": "ng-tasty", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"homepage": "https://github.com/Zizzamia/ng-tasty", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -5,3 +5,3 @@ /* | ||
* Version: 0.2.5 - 2014-08-21 | ||
* Version: 0.2.6 - 2014-08-26 | ||
* License: MIT | ||
@@ -234,3 +234,3 @@ */ | ||
if (angular.isDefined($attrs.query)) { | ||
$scope.query = $scope.$parent.$eval($attrs.query); | ||
$scope.query = $scope.$parent[$attrs.query]; | ||
} | ||
@@ -241,3 +241,3 @@ if (!angular.isDefined($attrs.resource) && !angular.isDefined($attrs.resourceCallback)) { | ||
if (angular.isDefined($attrs.resource)) { | ||
$scope.resource = $scope.$parent.$eval($attrs.resource); | ||
$scope.resource = $scope.$parent[$attrs.resource]; | ||
if (!angular.isObject($scope.resource)) { | ||
@@ -252,3 +252,3 @@ throw 'AngularJS tastyTable directive: the resource ('+ | ||
if (angular.isDefined($attrs.resourceCallback)) { | ||
$scope.resourceCallback = $scope.$parent.$eval($attrs.resourceCallback); | ||
$scope.resourceCallback = $scope.$parent[$attrs.resourceCallback]; | ||
if (!angular.isFunction($scope.resourceCallback)) { | ||
@@ -273,8 +273,11 @@ throw 'AngularJS tastyTable directive: the resource-callback ('+ | ||
$scope.setDirectivesValues = function (resource) { | ||
var sortBy; | ||
if (!resource) { | ||
return false; | ||
} | ||
sortBy = resource.sortBy || $scope.params.sortBy; | ||
sortBy = sortBy || resource.header[0].key; | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': resource.sortBy || $scope.params.sortBy, | ||
'sortBy': sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
@@ -287,6 +290,14 @@ }; | ||
$scope.buildClientResource = function() { | ||
var fromRow, toRow, rowToShow, reverse; | ||
var fromRow, toRow, rowToShow, reverse, listSortBy; | ||
if ($scope.theadDirective) { | ||
reverse = $scope.header.sortOrder === 'asc' ? false : true; | ||
$scope.rows = $filter('orderBy')($scope.rows, $scope.header.sortBy, reverse); | ||
listSortBy = [function(item) { | ||
return item[$scope.header.sortBy]; | ||
}]; | ||
if ($scope.header.columns[0].key !== $scope.header.sortBy) { | ||
listSortBy.push(function(item) { | ||
return item[$scope.header.columns[0].key]; | ||
}); | ||
} | ||
$scope.rows = $filter('orderBy')($scope.rows, listSortBy, reverse); | ||
} | ||
@@ -371,2 +382,10 @@ if ($scope.paginationDirective) { | ||
}, true); | ||
if ($scope.resource) { | ||
$scope.$parent.$watch($attrs.resource, function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
$scope.resource = newValue; | ||
$scope.updateClientSideResource(); | ||
} | ||
}, true); | ||
} | ||
@@ -492,6 +511,6 @@ // Init table | ||
if (angular.isDefined($attrs.itemsPerPage)) { | ||
$scope.itemsPerPage = $scope.$parent.$eval($attrs.itemsPerPage); | ||
$scope.itemsPerPage = $scope.$parent[$attrs.itemsPerPage]; | ||
} | ||
if (angular.isDefined($attrs.listItemsPerPage)) { | ||
$scope.listItemsPerPage = $scope.$parent.$eval($attrs.listItemsPerPage); | ||
$scope.listItemsPerPage = $scope.$parent[$attrs.listItemsPerPage]; | ||
} | ||
@@ -498,0 +517,0 @@ // Default configs |
@@ -5,5 +5,5 @@ /* | ||
* Version: 0.2.5 - 2014-08-21 | ||
* Version: 0.2.6 - 2014-08-26 | ||
* License: MIT | ||
*/ | ||
angular.module("ngTasty",["ngTasty.tpls","ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.tpls",["template/table/head.html","template/table/pagination.html"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.filterInt","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(input){return input.replace(/[^a-zA-Z0-9-]+/g,"-").toLowerCase()}}),angular.module("ngTasty.filter.filterInt",[]).filter("filterInt",function(){return function(input){return/^(\-|\+)?([0-9]+|Infinity)$/.test(input)?Number(input):0/0}}),angular.module("ngTasty.filter.range",[]).filter("range",["$filter",function($filter){return function(input,start,stop,step){if(start=$filter("filterInt")(start),stop=$filter("filterInt")(stop),step=$filter("filterInt")(step),isNaN(start)&&(start=0),isNaN(stop)&&(stop=start,start=0),isNaN(step)&&(step=1),step>0&&start>=stop||0>step&&stop>=start)return[];for(var i=start;step>0?stop>i:i>stop;i+=step)input.push(i);return input}}]),angular.module("ngTasty.service",["ngTasty.service.tastyUtil","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]),angular.module("ngTasty.service.tastyUtil",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).factory("tastyUtil",["debounce","setProperty","joinObjects",function(debounce,setProperty,joinObjects){return{debounce:debounce,setProperty:setProperty,joinObjects:joinObjects}}]),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function($timeout){return function(func,wait){var timeout;return function(){var context=this,args=arguments;$timeout.cancel(timeout),timeout=$timeout(function(){timeout=null,func.apply(context,args)},wait)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(objOne,objTwo,attrname){return"undefined"!=typeof objTwo[attrname]&&null!==objTwo[attrname]&&(objOne[attrname]=objTwo[attrname]),objOne}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(setProperty){return function(objOne,objTwo){for(var attrname in objTwo)setProperty(objOne,objTwo,attrname);return objOne}}]),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.tastyUtil"]).constant("tableConfig",{query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},resource:void 0,resourceCallback:void 0,listItemsPerPage:[5,25,50,100],itemsPerPage:5}).controller("TableController",["$scope","$attrs","$timeout","$filter","tableConfig","tastyUtil",function($scope,$attrs,$timeout,$filter,tableConfig,tastyUtil){"use strict";if(this.$scope=$scope,$scope.query=tableConfig.query,$scope.resource=tableConfig.resource,$scope.resourceCallback=tableConfig.resourceCallback,$scope.clientSide=!0,$scope.url="",$scope.header={columns:[]},$scope.rows=[],$scope.params={},$scope.pagination={count:5,page:1,pages:1,size:1},$scope.theadDirective=!1,$scope.paginationDirective=!1,angular.isDefined($attrs.query)&&($scope.query=$scope.$parent.$eval($attrs.query)),!angular.isDefined($attrs.resource)&&!angular.isDefined($attrs.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined($attrs.resource)){if($scope.resource=$scope.$parent.$eval($attrs.resource),!angular.isObject($scope.resource))throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") it's not an object";if(!$scope.resource.header&&!$scope.resource.rows)throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") has the property header or rows undefined"}if(angular.isDefined($attrs.resourceCallback)){if($scope.resourceCallback=$scope.$parent.$eval($attrs.resourceCallback),!angular.isFunction($scope.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+$attrs.resourceCallback+") it's not a function";$scope.clientSide=!1}this.activate=function(directiveName){$scope[directiveName+"Directive"]=!0,$scope.params[directiveName]=!0},this.setParams=function(key,value){$scope.params[key]=value},$scope.setDirectivesValues=function(resource){return resource?($scope.header={columns:resource.header,sortBy:resource.sortBy||$scope.params.sortBy,sortOrder:resource.sortOrder||$scope.params.sortOrder},$scope.rows=resource.rows,void($scope.pagination=resource.pagination||$scope.pagination)):!1},$scope.buildClientResource=function(){var fromRow,toRow,rowToShow,reverse;$scope.theadDirective&&(reverse="asc"===$scope.header.sortOrder?!1:!0,$scope.rows=$filter("orderBy")($scope.rows,$scope.header.sortBy,reverse)),$scope.paginationDirective&&($scope.pagination.page=$scope.params.page,$scope.pagination.count=$scope.params.count,$scope.pagination.size=$scope.rows.length,$scope.pagination.pages=Math.ceil($scope.rows.length/$scope.pagination.count),toRow=$scope.pagination.count*$scope.pagination.page,fromRow=toRow-$scope.pagination.count,fromRow>=0&&toRow>=0&&(rowToShow=$scope.rows.slice(fromRow,toRow),$scope.rows=rowToShow))},$scope.buildUrl=function(params,filters){var urlQuery,value;return urlQuery={},$scope.theadDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"sortBy"),urlQuery=tastyUtil.setProperty(urlQuery,params,"sortOrder")),$scope.paginationDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"page"),urlQuery=tastyUtil.setProperty(urlQuery,params,"count")),$attrs.filters&&(urlQuery=tastyUtil.joinObjects(urlQuery,filters)),Object.keys(urlQuery).map(function(key){return value=urlQuery[key],$scope.query[key]&&(key=$scope.query[key]),encodeURIComponent(key)+"="+encodeURIComponent(value)}).join("&")},$scope.updateClientSideResource=tastyUtil.debounce(function(){$scope.setDirectivesValues($scope.resource),$scope.buildClientResource()},100),$scope.updateServerSideResource=tastyUtil.debounce(function(){$scope.url=$scope.buildUrl($scope.params,$scope[$attrs.filters]),$scope.resourceCallback($scope.url).then(function(resource){$scope.setDirectivesValues(resource)})},100),$scope.initTable=function(){$scope.params.sortBy=void 0,$scope.params.sortOrder="asc",$scope.params.page=1,$scope.params.count=void 0,$scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource()},$attrs.filters&&$scope.$watch($attrs.filters,function(newValue,oldValue){newValue!==oldValue&&$scope.updateServerSideResource()},!0),$scope.$watch("params",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())},!0),$scope.initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(scope,element,attrs,tastyTable){"use strict";tastyTable.activate("thead"),scope.fields={},scope.setFields=function(){var lenHeader,width,active,sortable;lenHeader=scope.header.columns.length,scope.header.columns.forEach(function(column){width=parseFloat((100/lenHeader).toFixed(2)),sortable=!0,active=!1,scope.notSortBy&&(sortable=scope.notSortBy.indexOf(column.key)<0),(column.key===scope.header.sortBy||"-"+column.key===scope.header.sortBy)&&(active=!0),scope.fields[column.key]={active:active,sortable:sortable,width:{width:width+"%"},sort:$filter("cleanFieldName")(column.key)}}),"dsc"===scope.header.sortOrder&&"-"!==scope.header.sortBy[0]&&(scope.header.sortBy="-"+scope.header.sortBy)},scope.sortBy=function(field){if(scope.notSortBy&&scope.notSortBy.indexOf(field.key)>=0)return!1;var fieldName;fieldName=$filter("cleanFieldName")(field.key),scope.header.sortBy==fieldName?(scope.header.sortBy="-"+fieldName,tastyTable.setParams("sortOrder","dsc")):(scope.header.sortBy=fieldName,tastyTable.setParams("sortOrder","asc")),tastyTable.setParams("sortBy",field.key)},scope.isSortUp=function(field){return void 0===scope.fields[field.key]?!1:scope.header.sortBy=="-"+scope.fields[field.key].sort},scope.isSortDown=function(field){return void 0===scope.fields[field.key]?!1:scope.header.sortBy==scope.fields[field.key].sort},tastyTable.$scope.$watch("header",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.header=newValue,scope.setFields())},!0)}}}]).controller("TablePaginationController",["$scope","$attrs","tableConfig",function($scope,$attrs,tableConfig){angular.isDefined($attrs.itemsPerPage)&&($scope.itemsPerPage=$scope.$parent.$eval($attrs.itemsPerPage)),angular.isDefined($attrs.listItemsPerPage)&&($scope.listItemsPerPage=$scope.$parent.$eval($attrs.listItemsPerPage)),$scope.itemsPerPage=$scope.itemsPerPage||tableConfig.itemsPerPage,$scope.listItemsPerPage=$scope.listItemsPerPage||tableConfig.listItemsPerPage}]).directive("tastyPagination",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",controller:"TablePaginationController",link:function(scope,element,attrs,tastyTable){"use strict";var getPage,setCount,setPaginationRange,setPreviousRange,setRemainingRange,setPaginationRanges;tastyTable.activate("pagination"),scope.pagination={},scope.pagMinRange=1,scope.pagMaxRange=1,getPage=function(numPage){tastyTable.setParams("page",numPage)},setCount=function(count){var maxItems,page;maxItems=count*scope.pagination.page,maxItems>scope.pagination.size&&(page=Math.ceil(scope.pagination.size/count),tastyTable.setParams("page",page)),tastyTable.setParams("count",count)},setPaginationRange=function(){var currentPage;currentPage=scope.pagination.page,currentPage>scope.pagination.pages&&(currentPage=scope.pagination.pages),scope.pagMinRange=currentPage-2>0?currentPage-2:1,scope.pagMaxRange=currentPage+2,scope.pagination.page=currentPage,setPaginationRanges()},setPreviousRange=function(){return scope.pagHideMinRange===!0||scope.pagMinRange<1?!1:(scope.pagMaxRange=scope.pagMinRange,scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,void setPaginationRanges())},setRemainingRange=function(){return scope.pagHideMaxRange===!0||scope.pagMaxRange>scope.pagination.pages?!1:(scope.pagMinRange=scope.pagMaxRange,scope.pagMaxRange=scope.pagMinRange+scope.itemsPerPage,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages),scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,void setPaginationRanges())},setPaginationRanges=function(){scope.pagMinRange=scope.pagMinRange>0?scope.pagMinRange:1,scope.pagMaxRange=scope.pagMinRange+5,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages+1),scope.pagHideMinRange=scope.pagMinRange<=1,scope.pagHideMaxRange=scope.pagMaxRange>=scope.pagination.pages;for(var i=2;i<scope.listItemsPerPage.length;i++)if(scope.pagination.size<scope.listItemsPerPage[i]){scope.listItemsPerPageShow=scope.listItemsPerPage.slice(0,i);break}scope.rangePage=$filter("range")([],scope.pagMinRange,scope.pagMaxRange)},scope.page={get:getPage,setCount:setCount,previous:setPreviousRange,remaining:setRemainingRange},tastyTable.$scope.$watch("pagination",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.pagination=newValue,setPaginationRange())},!0),scope.page.setCount(scope.itemsPerPage)}}}]),angular.module("template/table/head.html",[]).run(["$templateCache",function($templateCache){$templateCache.put("template/table/head.html",'<tr>\n <th ng-repeat="column in header.columns" \n ng-class="{\'sortable\': fields[column.key].sortable, \n \'active\': fields[column.key].active}"\n ng-style="fields[column.key].width" \n ng-click="sortBy(column)">\n <span ng-bind="column.name"></span>\n <span ng-if="isSortUp(column)" class="fa fa-sort-up"></span>\n <span ng-if="isSortDown(column)" class="fa fa-sort-down"></span>\n </th> \n</tr>')}]),angular.module("template/table/pagination.html",[]).run(["$templateCache",function($templateCache){$templateCache.put("template/table/pagination.html",'<div class="row">\n <div class="col-xs-3 text-left">\n <div class="btn-group">\n <button type="button" class="btn btn-default" \n ng-repeat="count in listItemsPerPageShow" \n ng-class="{active: count == pagination.count}" \n ng-click="page.setCount(count)" ng-bind="count"></button>\n </div>\n </div>\n <div class="col-xs-6 text-center">\n <ul class="pagination">\n <li ng-class="{disabled: pagHideMinRange}">\n <span ng-click="page.previous()">«</span>\n </li>\n <li ng-repeat="numPage in rangePage" \n ng-class="{active: numPage == pagination.page}">\n <span ng-click="page.get(numPage)">\n <span ng-bind="numPage"></span>\n <span class="sr-only" ng-if="numPage == pagination.page">(current)</span>\n </span>\n </li>\n <li ng-class="{disabled: pagHideMaxRange}">\n <span ng-click="page.remaining()">»</span>\n </li>\n </ul>\n </div>\n <div class="col-xs-3 text-right">\n <p>Page <span ng-bind="pagination.page"></span> \n of <span ng-bind="pagination.pages"></span>,\n of <span ng-bind="pagination.size"></span> entries</p>\n </div>\n</div>')}]); | ||
angular.module("ngTasty",["ngTasty.tpls","ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.tpls",["template/table/head.html","template/table/pagination.html"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.filterInt","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(input){return input.replace(/[^a-zA-Z0-9-]+/g,"-").toLowerCase()}}),angular.module("ngTasty.filter.filterInt",[]).filter("filterInt",function(){return function(input){return/^(\-|\+)?([0-9]+|Infinity)$/.test(input)?Number(input):0/0}}),angular.module("ngTasty.filter.range",[]).filter("range",["$filter",function($filter){return function(input,start,stop,step){if(start=$filter("filterInt")(start),stop=$filter("filterInt")(stop),step=$filter("filterInt")(step),isNaN(start)&&(start=0),isNaN(stop)&&(stop=start,start=0),isNaN(step)&&(step=1),step>0&&start>=stop||0>step&&stop>=start)return[];for(var i=start;step>0?stop>i:i>stop;i+=step)input.push(i);return input}}]),angular.module("ngTasty.service",["ngTasty.service.tastyUtil","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]),angular.module("ngTasty.service.tastyUtil",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).factory("tastyUtil",["debounce","setProperty","joinObjects",function(debounce,setProperty,joinObjects){return{debounce:debounce,setProperty:setProperty,joinObjects:joinObjects}}]),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function($timeout){return function(func,wait){var timeout;return function(){var context=this,args=arguments;$timeout.cancel(timeout),timeout=$timeout(function(){timeout=null,func.apply(context,args)},wait)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(objOne,objTwo,attrname){return"undefined"!=typeof objTwo[attrname]&&null!==objTwo[attrname]&&(objOne[attrname]=objTwo[attrname]),objOne}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(setProperty){return function(objOne,objTwo){for(var attrname in objTwo)setProperty(objOne,objTwo,attrname);return objOne}}]),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.tastyUtil"]).constant("tableConfig",{query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},resource:void 0,resourceCallback:void 0,listItemsPerPage:[5,25,50,100],itemsPerPage:5}).controller("TableController",["$scope","$attrs","$timeout","$filter","tableConfig","tastyUtil",function($scope,$attrs,$timeout,$filter,tableConfig,tastyUtil){"use strict";if(this.$scope=$scope,$scope.query=tableConfig.query,$scope.resource=tableConfig.resource,$scope.resourceCallback=tableConfig.resourceCallback,$scope.clientSide=!0,$scope.url="",$scope.header={columns:[]},$scope.rows=[],$scope.params={},$scope.pagination={count:5,page:1,pages:1,size:1},$scope.theadDirective=!1,$scope.paginationDirective=!1,angular.isDefined($attrs.query)&&($scope.query=$scope.$parent[$attrs.query]),!angular.isDefined($attrs.resource)&&!angular.isDefined($attrs.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined($attrs.resource)){if($scope.resource=$scope.$parent[$attrs.resource],!angular.isObject($scope.resource))throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") it's not an object";if(!$scope.resource.header&&!$scope.resource.rows)throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") has the property header or rows undefined"}if(angular.isDefined($attrs.resourceCallback)){if($scope.resourceCallback=$scope.$parent[$attrs.resourceCallback],!angular.isFunction($scope.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+$attrs.resourceCallback+") it's not a function";$scope.clientSide=!1}this.activate=function(directiveName){$scope[directiveName+"Directive"]=!0,$scope.params[directiveName]=!0},this.setParams=function(key,value){$scope.params[key]=value},$scope.setDirectivesValues=function(resource){var sortBy;return resource?(sortBy=resource.sortBy||$scope.params.sortBy,sortBy=sortBy||resource.header[0].key,$scope.header={columns:resource.header,sortBy:sortBy,sortOrder:resource.sortOrder||$scope.params.sortOrder},$scope.rows=resource.rows,void($scope.pagination=resource.pagination||$scope.pagination)):!1},$scope.buildClientResource=function(){var fromRow,toRow,rowToShow,reverse,listSortBy;$scope.theadDirective&&(reverse="asc"===$scope.header.sortOrder?!1:!0,listSortBy=[function(item){return item[$scope.header.sortBy]}],$scope.header.columns[0].key!==$scope.header.sortBy&&listSortBy.push(function(item){return item[$scope.header.columns[0].key]}),$scope.rows=$filter("orderBy")($scope.rows,listSortBy,reverse)),$scope.paginationDirective&&($scope.pagination.page=$scope.params.page,$scope.pagination.count=$scope.params.count,$scope.pagination.size=$scope.rows.length,$scope.pagination.pages=Math.ceil($scope.rows.length/$scope.pagination.count),toRow=$scope.pagination.count*$scope.pagination.page,fromRow=toRow-$scope.pagination.count,fromRow>=0&&toRow>=0&&(rowToShow=$scope.rows.slice(fromRow,toRow),$scope.rows=rowToShow))},$scope.buildUrl=function(params,filters){var urlQuery,value;return urlQuery={},$scope.theadDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"sortBy"),urlQuery=tastyUtil.setProperty(urlQuery,params,"sortOrder")),$scope.paginationDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"page"),urlQuery=tastyUtil.setProperty(urlQuery,params,"count")),$attrs.filters&&(urlQuery=tastyUtil.joinObjects(urlQuery,filters)),Object.keys(urlQuery).map(function(key){return value=urlQuery[key],$scope.query[key]&&(key=$scope.query[key]),encodeURIComponent(key)+"="+encodeURIComponent(value)}).join("&")},$scope.updateClientSideResource=tastyUtil.debounce(function(){$scope.setDirectivesValues($scope.resource),$scope.buildClientResource()},100),$scope.updateServerSideResource=tastyUtil.debounce(function(){$scope.url=$scope.buildUrl($scope.params,$scope[$attrs.filters]),$scope.resourceCallback($scope.url).then(function(resource){$scope.setDirectivesValues(resource)})},100),$scope.initTable=function(){$scope.params.sortBy=void 0,$scope.params.sortOrder="asc",$scope.params.page=1,$scope.params.count=void 0,$scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource()},$attrs.filters&&$scope.$watch($attrs.filters,function(newValue,oldValue){newValue!==oldValue&&$scope.updateServerSideResource()},!0),$scope.$watch("params",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())},!0),$scope.resource&&$scope.$parent.$watch($attrs.resource,function(newValue,oldValue){newValue!==oldValue&&($scope.resource=newValue,$scope.updateClientSideResource())},!0),$scope.initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(scope,element,attrs,tastyTable){"use strict";tastyTable.activate("thead"),scope.fields={},scope.setFields=function(){var lenHeader,width,active,sortable;lenHeader=scope.header.columns.length,scope.header.columns.forEach(function(column){width=parseFloat((100/lenHeader).toFixed(2)),sortable=!0,active=!1,scope.notSortBy&&(sortable=scope.notSortBy.indexOf(column.key)<0),(column.key===scope.header.sortBy||"-"+column.key===scope.header.sortBy)&&(active=!0),scope.fields[column.key]={active:active,sortable:sortable,width:{width:width+"%"},sort:$filter("cleanFieldName")(column.key)}}),"dsc"===scope.header.sortOrder&&"-"!==scope.header.sortBy[0]&&(scope.header.sortBy="-"+scope.header.sortBy)},scope.sortBy=function(field){if(scope.notSortBy&&scope.notSortBy.indexOf(field.key)>=0)return!1;var fieldName;fieldName=$filter("cleanFieldName")(field.key),scope.header.sortBy==fieldName?(scope.header.sortBy="-"+fieldName,tastyTable.setParams("sortOrder","dsc")):(scope.header.sortBy=fieldName,tastyTable.setParams("sortOrder","asc")),tastyTable.setParams("sortBy",field.key)},scope.isSortUp=function(field){return void 0===scope.fields[field.key]?!1:scope.header.sortBy=="-"+scope.fields[field.key].sort},scope.isSortDown=function(field){return void 0===scope.fields[field.key]?!1:scope.header.sortBy==scope.fields[field.key].sort},tastyTable.$scope.$watch("header",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.header=newValue,scope.setFields())},!0)}}}]).controller("TablePaginationController",["$scope","$attrs","tableConfig",function($scope,$attrs,tableConfig){angular.isDefined($attrs.itemsPerPage)&&($scope.itemsPerPage=$scope.$parent[$attrs.itemsPerPage]),angular.isDefined($attrs.listItemsPerPage)&&($scope.listItemsPerPage=$scope.$parent[$attrs.listItemsPerPage]),$scope.itemsPerPage=$scope.itemsPerPage||tableConfig.itemsPerPage,$scope.listItemsPerPage=$scope.listItemsPerPage||tableConfig.listItemsPerPage}]).directive("tastyPagination",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",controller:"TablePaginationController",link:function(scope,element,attrs,tastyTable){"use strict";var getPage,setCount,setPaginationRange,setPreviousRange,setRemainingRange,setPaginationRanges;tastyTable.activate("pagination"),scope.pagination={},scope.pagMinRange=1,scope.pagMaxRange=1,getPage=function(numPage){tastyTable.setParams("page",numPage)},setCount=function(count){var maxItems,page;maxItems=count*scope.pagination.page,maxItems>scope.pagination.size&&(page=Math.ceil(scope.pagination.size/count),tastyTable.setParams("page",page)),tastyTable.setParams("count",count)},setPaginationRange=function(){var currentPage;currentPage=scope.pagination.page,currentPage>scope.pagination.pages&&(currentPage=scope.pagination.pages),scope.pagMinRange=currentPage-2>0?currentPage-2:1,scope.pagMaxRange=currentPage+2,scope.pagination.page=currentPage,setPaginationRanges()},setPreviousRange=function(){return scope.pagHideMinRange===!0||scope.pagMinRange<1?!1:(scope.pagMaxRange=scope.pagMinRange,scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,void setPaginationRanges())},setRemainingRange=function(){return scope.pagHideMaxRange===!0||scope.pagMaxRange>scope.pagination.pages?!1:(scope.pagMinRange=scope.pagMaxRange,scope.pagMaxRange=scope.pagMinRange+scope.itemsPerPage,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages),scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,void setPaginationRanges())},setPaginationRanges=function(){scope.pagMinRange=scope.pagMinRange>0?scope.pagMinRange:1,scope.pagMaxRange=scope.pagMinRange+5,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages+1),scope.pagHideMinRange=scope.pagMinRange<=1,scope.pagHideMaxRange=scope.pagMaxRange>=scope.pagination.pages;for(var i=2;i<scope.listItemsPerPage.length;i++)if(scope.pagination.size<scope.listItemsPerPage[i]){scope.listItemsPerPageShow=scope.listItemsPerPage.slice(0,i);break}scope.rangePage=$filter("range")([],scope.pagMinRange,scope.pagMaxRange)},scope.page={get:getPage,setCount:setCount,previous:setPreviousRange,remaining:setRemainingRange},tastyTable.$scope.$watch("pagination",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.pagination=newValue,setPaginationRange())},!0),scope.page.setCount(scope.itemsPerPage)}}}]),angular.module("template/table/head.html",[]).run(["$templateCache",function($templateCache){$templateCache.put("template/table/head.html",'<tr>\n <th ng-repeat="column in header.columns" \n ng-class="{\'sortable\': fields[column.key].sortable, \n \'active\': fields[column.key].active}"\n ng-style="fields[column.key].width" \n ng-click="sortBy(column)">\n <span ng-bind="column.name"></span>\n <span ng-if="isSortUp(column)" class="fa fa-sort-up"></span>\n <span ng-if="isSortDown(column)" class="fa fa-sort-down"></span>\n </th> \n</tr>')}]),angular.module("template/table/pagination.html",[]).run(["$templateCache",function($templateCache){$templateCache.put("template/table/pagination.html",'<div class="row">\n <div class="col-xs-3 text-left">\n <div class="btn-group">\n <button type="button" class="btn btn-default" \n ng-repeat="count in listItemsPerPageShow" \n ng-class="{active: count == pagination.count}" \n ng-click="page.setCount(count)" ng-bind="count"></button>\n </div>\n </div>\n <div class="col-xs-6 text-center">\n <ul class="pagination">\n <li ng-class="{disabled: pagHideMinRange}">\n <span ng-click="page.previous()">«</span>\n </li>\n <li ng-repeat="numPage in rangePage" \n ng-class="{active: numPage == pagination.page}">\n <span ng-click="page.get(numPage)">\n <span ng-bind="numPage"></span>\n <span class="sr-only" ng-if="numPage == pagination.page">(current)</span>\n </span>\n </li>\n <li ng-class="{disabled: pagHideMaxRange}">\n <span ng-click="page.remaining()">»</span>\n </li>\n </ul>\n </div>\n <div class="col-xs-3 text-right">\n <p>Page <span ng-bind="pagination.page"></span> \n of <span ng-bind="pagination.pages"></span>,\n of <span ng-bind="pagination.size"></span> entries</p>\n </div>\n</div>')}]); |
@@ -5,3 +5,3 @@ /* | ||
* Version: 0.2.5 - 2014-08-21 | ||
* Version: 0.2.6 - 2014-08-26 | ||
* License: MIT | ||
@@ -233,3 +233,3 @@ */ | ||
if (angular.isDefined($attrs.query)) { | ||
$scope.query = $scope.$parent.$eval($attrs.query); | ||
$scope.query = $scope.$parent[$attrs.query]; | ||
} | ||
@@ -240,3 +240,3 @@ if (!angular.isDefined($attrs.resource) && !angular.isDefined($attrs.resourceCallback)) { | ||
if (angular.isDefined($attrs.resource)) { | ||
$scope.resource = $scope.$parent.$eval($attrs.resource); | ||
$scope.resource = $scope.$parent[$attrs.resource]; | ||
if (!angular.isObject($scope.resource)) { | ||
@@ -251,3 +251,3 @@ throw 'AngularJS tastyTable directive: the resource ('+ | ||
if (angular.isDefined($attrs.resourceCallback)) { | ||
$scope.resourceCallback = $scope.$parent.$eval($attrs.resourceCallback); | ||
$scope.resourceCallback = $scope.$parent[$attrs.resourceCallback]; | ||
if (!angular.isFunction($scope.resourceCallback)) { | ||
@@ -272,8 +272,11 @@ throw 'AngularJS tastyTable directive: the resource-callback ('+ | ||
$scope.setDirectivesValues = function (resource) { | ||
var sortBy; | ||
if (!resource) { | ||
return false; | ||
} | ||
sortBy = resource.sortBy || $scope.params.sortBy; | ||
sortBy = sortBy || resource.header[0].key; | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': resource.sortBy || $scope.params.sortBy, | ||
'sortBy': sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
@@ -286,6 +289,14 @@ }; | ||
$scope.buildClientResource = function() { | ||
var fromRow, toRow, rowToShow, reverse; | ||
var fromRow, toRow, rowToShow, reverse, listSortBy; | ||
if ($scope.theadDirective) { | ||
reverse = $scope.header.sortOrder === 'asc' ? false : true; | ||
$scope.rows = $filter('orderBy')($scope.rows, $scope.header.sortBy, reverse); | ||
listSortBy = [function(item) { | ||
return item[$scope.header.sortBy]; | ||
}]; | ||
if ($scope.header.columns[0].key !== $scope.header.sortBy) { | ||
listSortBy.push(function(item) { | ||
return item[$scope.header.columns[0].key]; | ||
}); | ||
} | ||
$scope.rows = $filter('orderBy')($scope.rows, listSortBy, reverse); | ||
} | ||
@@ -370,2 +381,10 @@ if ($scope.paginationDirective) { | ||
}, true); | ||
if ($scope.resource) { | ||
$scope.$parent.$watch($attrs.resource, function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
$scope.resource = newValue; | ||
$scope.updateClientSideResource(); | ||
} | ||
}, true); | ||
} | ||
@@ -491,6 +510,6 @@ // Init table | ||
if (angular.isDefined($attrs.itemsPerPage)) { | ||
$scope.itemsPerPage = $scope.$parent.$eval($attrs.itemsPerPage); | ||
$scope.itemsPerPage = $scope.$parent[$attrs.itemsPerPage]; | ||
} | ||
if (angular.isDefined($attrs.listItemsPerPage)) { | ||
$scope.listItemsPerPage = $scope.$parent.$eval($attrs.listItemsPerPage); | ||
$scope.listItemsPerPage = $scope.$parent[$attrs.listItemsPerPage]; | ||
} | ||
@@ -497,0 +516,0 @@ // Default configs |
@@ -5,5 +5,5 @@ /* | ||
* Version: 0.2.5 - 2014-08-21 | ||
* Version: 0.2.6 - 2014-08-26 | ||
* License: MIT | ||
*/ | ||
angular.module("ngTasty",["ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.filterInt","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(input){return input.replace(/[^a-zA-Z0-9-]+/g,"-").toLowerCase()}}),angular.module("ngTasty.filter.filterInt",[]).filter("filterInt",function(){return function(input){return/^(\-|\+)?([0-9]+|Infinity)$/.test(input)?Number(input):0/0}}),angular.module("ngTasty.filter.range",[]).filter("range",["$filter",function($filter){return function(input,start,stop,step){if(start=$filter("filterInt")(start),stop=$filter("filterInt")(stop),step=$filter("filterInt")(step),isNaN(start)&&(start=0),isNaN(stop)&&(stop=start,start=0),isNaN(step)&&(step=1),step>0&&start>=stop||0>step&&stop>=start)return[];for(var i=start;step>0?stop>i:i>stop;i+=step)input.push(i);return input}}]),angular.module("ngTasty.service",["ngTasty.service.tastyUtil","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]),angular.module("ngTasty.service.tastyUtil",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).factory("tastyUtil",["debounce","setProperty","joinObjects",function(debounce,setProperty,joinObjects){return{debounce:debounce,setProperty:setProperty,joinObjects:joinObjects}}]),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function($timeout){return function(func,wait){var timeout;return function(){var context=this,args=arguments;$timeout.cancel(timeout),timeout=$timeout(function(){timeout=null,func.apply(context,args)},wait)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(objOne,objTwo,attrname){return"undefined"!=typeof objTwo[attrname]&&null!==objTwo[attrname]&&(objOne[attrname]=objTwo[attrname]),objOne}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(setProperty){return function(objOne,objTwo){for(var attrname in objTwo)setProperty(objOne,objTwo,attrname);return objOne}}]),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.tastyUtil"]).constant("tableConfig",{query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},resource:void 0,resourceCallback:void 0,listItemsPerPage:[5,25,50,100],itemsPerPage:5}).controller("TableController",["$scope","$attrs","$timeout","$filter","tableConfig","tastyUtil",function($scope,$attrs,$timeout,$filter,tableConfig,tastyUtil){"use strict";if(this.$scope=$scope,$scope.query=tableConfig.query,$scope.resource=tableConfig.resource,$scope.resourceCallback=tableConfig.resourceCallback,$scope.clientSide=!0,$scope.url="",$scope.header={columns:[]},$scope.rows=[],$scope.params={},$scope.pagination={count:5,page:1,pages:1,size:1},$scope.theadDirective=!1,$scope.paginationDirective=!1,angular.isDefined($attrs.query)&&($scope.query=$scope.$parent.$eval($attrs.query)),!angular.isDefined($attrs.resource)&&!angular.isDefined($attrs.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined($attrs.resource)){if($scope.resource=$scope.$parent.$eval($attrs.resource),!angular.isObject($scope.resource))throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") it's not an object";if(!$scope.resource.header&&!$scope.resource.rows)throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") has the property header or rows undefined"}if(angular.isDefined($attrs.resourceCallback)){if($scope.resourceCallback=$scope.$parent.$eval($attrs.resourceCallback),!angular.isFunction($scope.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+$attrs.resourceCallback+") it's not a function";$scope.clientSide=!1}this.activate=function(directiveName){$scope[directiveName+"Directive"]=!0,$scope.params[directiveName]=!0},this.setParams=function(key,value){$scope.params[key]=value},$scope.setDirectivesValues=function(resource){return resource?($scope.header={columns:resource.header,sortBy:resource.sortBy||$scope.params.sortBy,sortOrder:resource.sortOrder||$scope.params.sortOrder},$scope.rows=resource.rows,void($scope.pagination=resource.pagination||$scope.pagination)):!1},$scope.buildClientResource=function(){var fromRow,toRow,rowToShow,reverse;$scope.theadDirective&&(reverse="asc"===$scope.header.sortOrder?!1:!0,$scope.rows=$filter("orderBy")($scope.rows,$scope.header.sortBy,reverse)),$scope.paginationDirective&&($scope.pagination.page=$scope.params.page,$scope.pagination.count=$scope.params.count,$scope.pagination.size=$scope.rows.length,$scope.pagination.pages=Math.ceil($scope.rows.length/$scope.pagination.count),toRow=$scope.pagination.count*$scope.pagination.page,fromRow=toRow-$scope.pagination.count,fromRow>=0&&toRow>=0&&(rowToShow=$scope.rows.slice(fromRow,toRow),$scope.rows=rowToShow))},$scope.buildUrl=function(params,filters){var urlQuery,value;return urlQuery={},$scope.theadDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"sortBy"),urlQuery=tastyUtil.setProperty(urlQuery,params,"sortOrder")),$scope.paginationDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"page"),urlQuery=tastyUtil.setProperty(urlQuery,params,"count")),$attrs.filters&&(urlQuery=tastyUtil.joinObjects(urlQuery,filters)),Object.keys(urlQuery).map(function(key){return value=urlQuery[key],$scope.query[key]&&(key=$scope.query[key]),encodeURIComponent(key)+"="+encodeURIComponent(value)}).join("&")},$scope.updateClientSideResource=tastyUtil.debounce(function(){$scope.setDirectivesValues($scope.resource),$scope.buildClientResource()},100),$scope.updateServerSideResource=tastyUtil.debounce(function(){$scope.url=$scope.buildUrl($scope.params,$scope[$attrs.filters]),$scope.resourceCallback($scope.url).then(function(resource){$scope.setDirectivesValues(resource)})},100),$scope.initTable=function(){$scope.params.sortBy=void 0,$scope.params.sortOrder="asc",$scope.params.page=1,$scope.params.count=void 0,$scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource()},$attrs.filters&&$scope.$watch($attrs.filters,function(newValue,oldValue){newValue!==oldValue&&$scope.updateServerSideResource()},!0),$scope.$watch("params",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())},!0),$scope.initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(scope,element,attrs,tastyTable){"use strict";tastyTable.activate("thead"),scope.fields={},scope.setFields=function(){var lenHeader,width,active,sortable;lenHeader=scope.header.columns.length,scope.header.columns.forEach(function(column){width=parseFloat((100/lenHeader).toFixed(2)),sortable=!0,active=!1,scope.notSortBy&&(sortable=scope.notSortBy.indexOf(column.key)<0),(column.key===scope.header.sortBy||"-"+column.key===scope.header.sortBy)&&(active=!0),scope.fields[column.key]={active:active,sortable:sortable,width:{width:width+"%"},sort:$filter("cleanFieldName")(column.key)}}),"dsc"===scope.header.sortOrder&&"-"!==scope.header.sortBy[0]&&(scope.header.sortBy="-"+scope.header.sortBy)},scope.sortBy=function(field){if(scope.notSortBy&&scope.notSortBy.indexOf(field.key)>=0)return!1;var fieldName;fieldName=$filter("cleanFieldName")(field.key),scope.header.sortBy==fieldName?(scope.header.sortBy="-"+fieldName,tastyTable.setParams("sortOrder","dsc")):(scope.header.sortBy=fieldName,tastyTable.setParams("sortOrder","asc")),tastyTable.setParams("sortBy",field.key)},scope.isSortUp=function(field){return void 0===scope.fields[field.key]?!1:scope.header.sortBy=="-"+scope.fields[field.key].sort},scope.isSortDown=function(field){return void 0===scope.fields[field.key]?!1:scope.header.sortBy==scope.fields[field.key].sort},tastyTable.$scope.$watch("header",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.header=newValue,scope.setFields())},!0)}}}]).controller("TablePaginationController",["$scope","$attrs","tableConfig",function($scope,$attrs,tableConfig){angular.isDefined($attrs.itemsPerPage)&&($scope.itemsPerPage=$scope.$parent.$eval($attrs.itemsPerPage)),angular.isDefined($attrs.listItemsPerPage)&&($scope.listItemsPerPage=$scope.$parent.$eval($attrs.listItemsPerPage)),$scope.itemsPerPage=$scope.itemsPerPage||tableConfig.itemsPerPage,$scope.listItemsPerPage=$scope.listItemsPerPage||tableConfig.listItemsPerPage}]).directive("tastyPagination",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",controller:"TablePaginationController",link:function(scope,element,attrs,tastyTable){"use strict";var getPage,setCount,setPaginationRange,setPreviousRange,setRemainingRange,setPaginationRanges;tastyTable.activate("pagination"),scope.pagination={},scope.pagMinRange=1,scope.pagMaxRange=1,getPage=function(numPage){tastyTable.setParams("page",numPage)},setCount=function(count){var maxItems,page;maxItems=count*scope.pagination.page,maxItems>scope.pagination.size&&(page=Math.ceil(scope.pagination.size/count),tastyTable.setParams("page",page)),tastyTable.setParams("count",count)},setPaginationRange=function(){var currentPage;currentPage=scope.pagination.page,currentPage>scope.pagination.pages&&(currentPage=scope.pagination.pages),scope.pagMinRange=currentPage-2>0?currentPage-2:1,scope.pagMaxRange=currentPage+2,scope.pagination.page=currentPage,setPaginationRanges()},setPreviousRange=function(){return scope.pagHideMinRange===!0||scope.pagMinRange<1?!1:(scope.pagMaxRange=scope.pagMinRange,scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,void setPaginationRanges())},setRemainingRange=function(){return scope.pagHideMaxRange===!0||scope.pagMaxRange>scope.pagination.pages?!1:(scope.pagMinRange=scope.pagMaxRange,scope.pagMaxRange=scope.pagMinRange+scope.itemsPerPage,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages),scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,void setPaginationRanges())},setPaginationRanges=function(){scope.pagMinRange=scope.pagMinRange>0?scope.pagMinRange:1,scope.pagMaxRange=scope.pagMinRange+5,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages+1),scope.pagHideMinRange=scope.pagMinRange<=1,scope.pagHideMaxRange=scope.pagMaxRange>=scope.pagination.pages;for(var i=2;i<scope.listItemsPerPage.length;i++)if(scope.pagination.size<scope.listItemsPerPage[i]){scope.listItemsPerPageShow=scope.listItemsPerPage.slice(0,i);break}scope.rangePage=$filter("range")([],scope.pagMinRange,scope.pagMaxRange)},scope.page={get:getPage,setCount:setCount,previous:setPreviousRange,remaining:setRemainingRange},tastyTable.$scope.$watch("pagination",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.pagination=newValue,setPaginationRange())},!0),scope.page.setCount(scope.itemsPerPage)}}}]); | ||
angular.module("ngTasty",["ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.filterInt","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(input){return input.replace(/[^a-zA-Z0-9-]+/g,"-").toLowerCase()}}),angular.module("ngTasty.filter.filterInt",[]).filter("filterInt",function(){return function(input){return/^(\-|\+)?([0-9]+|Infinity)$/.test(input)?Number(input):0/0}}),angular.module("ngTasty.filter.range",[]).filter("range",["$filter",function($filter){return function(input,start,stop,step){if(start=$filter("filterInt")(start),stop=$filter("filterInt")(stop),step=$filter("filterInt")(step),isNaN(start)&&(start=0),isNaN(stop)&&(stop=start,start=0),isNaN(step)&&(step=1),step>0&&start>=stop||0>step&&stop>=start)return[];for(var i=start;step>0?stop>i:i>stop;i+=step)input.push(i);return input}}]),angular.module("ngTasty.service",["ngTasty.service.tastyUtil","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]),angular.module("ngTasty.service.tastyUtil",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).factory("tastyUtil",["debounce","setProperty","joinObjects",function(debounce,setProperty,joinObjects){return{debounce:debounce,setProperty:setProperty,joinObjects:joinObjects}}]),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function($timeout){return function(func,wait){var timeout;return function(){var context=this,args=arguments;$timeout.cancel(timeout),timeout=$timeout(function(){timeout=null,func.apply(context,args)},wait)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(objOne,objTwo,attrname){return"undefined"!=typeof objTwo[attrname]&&null!==objTwo[attrname]&&(objOne[attrname]=objTwo[attrname]),objOne}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(setProperty){return function(objOne,objTwo){for(var attrname in objTwo)setProperty(objOne,objTwo,attrname);return objOne}}]),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.tastyUtil"]).constant("tableConfig",{query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},resource:void 0,resourceCallback:void 0,listItemsPerPage:[5,25,50,100],itemsPerPage:5}).controller("TableController",["$scope","$attrs","$timeout","$filter","tableConfig","tastyUtil",function($scope,$attrs,$timeout,$filter,tableConfig,tastyUtil){"use strict";if(this.$scope=$scope,$scope.query=tableConfig.query,$scope.resource=tableConfig.resource,$scope.resourceCallback=tableConfig.resourceCallback,$scope.clientSide=!0,$scope.url="",$scope.header={columns:[]},$scope.rows=[],$scope.params={},$scope.pagination={count:5,page:1,pages:1,size:1},$scope.theadDirective=!1,$scope.paginationDirective=!1,angular.isDefined($attrs.query)&&($scope.query=$scope.$parent[$attrs.query]),!angular.isDefined($attrs.resource)&&!angular.isDefined($attrs.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined($attrs.resource)){if($scope.resource=$scope.$parent[$attrs.resource],!angular.isObject($scope.resource))throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") it's not an object";if(!$scope.resource.header&&!$scope.resource.rows)throw"AngularJS tastyTable directive: the resource ("+$attrs.resource+") has the property header or rows undefined"}if(angular.isDefined($attrs.resourceCallback)){if($scope.resourceCallback=$scope.$parent[$attrs.resourceCallback],!angular.isFunction($scope.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+$attrs.resourceCallback+") it's not a function";$scope.clientSide=!1}this.activate=function(directiveName){$scope[directiveName+"Directive"]=!0,$scope.params[directiveName]=!0},this.setParams=function(key,value){$scope.params[key]=value},$scope.setDirectivesValues=function(resource){var sortBy;return resource?(sortBy=resource.sortBy||$scope.params.sortBy,sortBy=sortBy||resource.header[0].key,$scope.header={columns:resource.header,sortBy:sortBy,sortOrder:resource.sortOrder||$scope.params.sortOrder},$scope.rows=resource.rows,void($scope.pagination=resource.pagination||$scope.pagination)):!1},$scope.buildClientResource=function(){var fromRow,toRow,rowToShow,reverse,listSortBy;$scope.theadDirective&&(reverse="asc"===$scope.header.sortOrder?!1:!0,listSortBy=[function(item){return item[$scope.header.sortBy]}],$scope.header.columns[0].key!==$scope.header.sortBy&&listSortBy.push(function(item){return item[$scope.header.columns[0].key]}),$scope.rows=$filter("orderBy")($scope.rows,listSortBy,reverse)),$scope.paginationDirective&&($scope.pagination.page=$scope.params.page,$scope.pagination.count=$scope.params.count,$scope.pagination.size=$scope.rows.length,$scope.pagination.pages=Math.ceil($scope.rows.length/$scope.pagination.count),toRow=$scope.pagination.count*$scope.pagination.page,fromRow=toRow-$scope.pagination.count,fromRow>=0&&toRow>=0&&(rowToShow=$scope.rows.slice(fromRow,toRow),$scope.rows=rowToShow))},$scope.buildUrl=function(params,filters){var urlQuery,value;return urlQuery={},$scope.theadDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"sortBy"),urlQuery=tastyUtil.setProperty(urlQuery,params,"sortOrder")),$scope.paginationDirective&&(urlQuery=tastyUtil.setProperty(urlQuery,params,"page"),urlQuery=tastyUtil.setProperty(urlQuery,params,"count")),$attrs.filters&&(urlQuery=tastyUtil.joinObjects(urlQuery,filters)),Object.keys(urlQuery).map(function(key){return value=urlQuery[key],$scope.query[key]&&(key=$scope.query[key]),encodeURIComponent(key)+"="+encodeURIComponent(value)}).join("&")},$scope.updateClientSideResource=tastyUtil.debounce(function(){$scope.setDirectivesValues($scope.resource),$scope.buildClientResource()},100),$scope.updateServerSideResource=tastyUtil.debounce(function(){$scope.url=$scope.buildUrl($scope.params,$scope[$attrs.filters]),$scope.resourceCallback($scope.url).then(function(resource){$scope.setDirectivesValues(resource)})},100),$scope.initTable=function(){$scope.params.sortBy=void 0,$scope.params.sortOrder="asc",$scope.params.page=1,$scope.params.count=void 0,$scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource()},$attrs.filters&&$scope.$watch($attrs.filters,function(newValue,oldValue){newValue!==oldValue&&$scope.updateServerSideResource()},!0),$scope.$watch("params",function(newValue,oldValue){newValue!==oldValue&&($scope.clientSide?$scope.updateClientSideResource():$scope.updateServerSideResource())},!0),$scope.resource&&$scope.$parent.$watch($attrs.resource,function(newValue,oldValue){newValue!==oldValue&&($scope.resource=newValue,$scope.updateClientSideResource())},!0),$scope.initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(scope,element,attrs,tastyTable){"use strict";tastyTable.activate("thead"),scope.fields={},scope.setFields=function(){var lenHeader,width,active,sortable;lenHeader=scope.header.columns.length,scope.header.columns.forEach(function(column){width=parseFloat((100/lenHeader).toFixed(2)),sortable=!0,active=!1,scope.notSortBy&&(sortable=scope.notSortBy.indexOf(column.key)<0),(column.key===scope.header.sortBy||"-"+column.key===scope.header.sortBy)&&(active=!0),scope.fields[column.key]={active:active,sortable:sortable,width:{width:width+"%"},sort:$filter("cleanFieldName")(column.key)}}),"dsc"===scope.header.sortOrder&&"-"!==scope.header.sortBy[0]&&(scope.header.sortBy="-"+scope.header.sortBy)},scope.sortBy=function(field){if(scope.notSortBy&&scope.notSortBy.indexOf(field.key)>=0)return!1;var fieldName;fieldName=$filter("cleanFieldName")(field.key),scope.header.sortBy==fieldName?(scope.header.sortBy="-"+fieldName,tastyTable.setParams("sortOrder","dsc")):(scope.header.sortBy=fieldName,tastyTable.setParams("sortOrder","asc")),tastyTable.setParams("sortBy",field.key)},scope.isSortUp=function(field){return void 0===scope.fields[field.key]?!1:scope.header.sortBy=="-"+scope.fields[field.key].sort},scope.isSortDown=function(field){return void 0===scope.fields[field.key]?!1:scope.header.sortBy==scope.fields[field.key].sort},tastyTable.$scope.$watch("header",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.header=newValue,scope.setFields())},!0)}}}]).controller("TablePaginationController",["$scope","$attrs","tableConfig",function($scope,$attrs,tableConfig){angular.isDefined($attrs.itemsPerPage)&&($scope.itemsPerPage=$scope.$parent[$attrs.itemsPerPage]),angular.isDefined($attrs.listItemsPerPage)&&($scope.listItemsPerPage=$scope.$parent[$attrs.listItemsPerPage]),$scope.itemsPerPage=$scope.itemsPerPage||tableConfig.itemsPerPage,$scope.listItemsPerPage=$scope.listItemsPerPage||tableConfig.listItemsPerPage}]).directive("tastyPagination",["$filter",function($filter){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",controller:"TablePaginationController",link:function(scope,element,attrs,tastyTable){"use strict";var getPage,setCount,setPaginationRange,setPreviousRange,setRemainingRange,setPaginationRanges;tastyTable.activate("pagination"),scope.pagination={},scope.pagMinRange=1,scope.pagMaxRange=1,getPage=function(numPage){tastyTable.setParams("page",numPage)},setCount=function(count){var maxItems,page;maxItems=count*scope.pagination.page,maxItems>scope.pagination.size&&(page=Math.ceil(scope.pagination.size/count),tastyTable.setParams("page",page)),tastyTable.setParams("count",count)},setPaginationRange=function(){var currentPage;currentPage=scope.pagination.page,currentPage>scope.pagination.pages&&(currentPage=scope.pagination.pages),scope.pagMinRange=currentPage-2>0?currentPage-2:1,scope.pagMaxRange=currentPage+2,scope.pagination.page=currentPage,setPaginationRanges()},setPreviousRange=function(){return scope.pagHideMinRange===!0||scope.pagMinRange<1?!1:(scope.pagMaxRange=scope.pagMinRange,scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,void setPaginationRanges())},setRemainingRange=function(){return scope.pagHideMaxRange===!0||scope.pagMaxRange>scope.pagination.pages?!1:(scope.pagMinRange=scope.pagMaxRange,scope.pagMaxRange=scope.pagMinRange+scope.itemsPerPage,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages),scope.pagMinRange=scope.pagMaxRange-scope.itemsPerPage,void setPaginationRanges())},setPaginationRanges=function(){scope.pagMinRange=scope.pagMinRange>0?scope.pagMinRange:1,scope.pagMaxRange=scope.pagMinRange+5,scope.pagMaxRange>scope.pagination.pages&&(scope.pagMaxRange=scope.pagination.pages+1),scope.pagHideMinRange=scope.pagMinRange<=1,scope.pagHideMaxRange=scope.pagMaxRange>=scope.pagination.pages;for(var i=2;i<scope.listItemsPerPage.length;i++)if(scope.pagination.size<scope.listItemsPerPage[i]){scope.listItemsPerPageShow=scope.listItemsPerPage.slice(0,i);break}scope.rangePage=$filter("range")([],scope.pagMinRange,scope.pagMaxRange)},scope.page={get:getPage,setCount:setCount,previous:setPreviousRange,remaining:setRemainingRange},tastyTable.$scope.$watch("pagination",function(newValue,oldValue){newValue&&newValue!==oldValue&&(scope.pagination=newValue,setPaginationRange())},!0),scope.page.setCount(scope.itemsPerPage)}}}]); |
{ | ||
"name": "ng-tasty", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "A lightweight, flexible, and tasty collection of reusable UI components for AngularJS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -28,16 +28,2 @@ # #ngTasty [![Bower version](https://badge.fury.io/bo/ng-tasty.svg)](https://github.com/Zizzamia/bower-ng-tasty) [![NPM version](https://badge.fury.io/js/ng-tasty.svg)](https://www.npmjs.org/package/ng-tasty) [![NPM Downloads](http://img.shields.io/npm/dm/ng-tasty.svg)](https://www.npmjs.org/package/ng-tasty) [![Build Status](https://secure.travis-ci.org/Zizzamia/ng-tasty.svg)](https://travis-ci.org/Zizzamia/ng-tasty) | ||
## Changelog | ||
### v0.2.5 (master, released on August 21th 2014) | ||
- Added items-per-page and list-items-per-page settings in table pagination [#15](https://github.com/Zizzamia/ng-tasty/issues/15) | ||
- Added ngTasty.service.tastyUtil [#24](https://github.com/Zizzamia/ng-tasty/issues/24) | ||
- Removed all the Grunt dependence [#23](https://github.com/Zizzamia/ng-tasty/issues/23) | ||
- Fixed table pagination responsive [#18](https://github.com/Zizzamia/ng-tasty/issues/18) | ||
### v0.2.4 (released on August 13th 2014) | ||
- Added a new table that has sorting and pagination client side | ||
- Improved `setDirectivesValues` in `ngTasty.table` | ||
- Fixed issues in `ngTasty.filter.range` | ||
- Fixed issue in `ngTasty.service.setProperty` | ||
## Creator | ||
@@ -44,0 +30,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
66650
1224
42