Comparing version 0.2.4 to 0.2.5
{ | ||
"name": "ng-tasty", | ||
"version": "0.2.2", | ||
"version": "0.2.5", | ||
"homepage": "https://github.com/Zizzamia/ng-tasty", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -5,3 +5,3 @@ /* | ||
* Version: 0.2.4 - 2014-08-13 | ||
* Version: 0.2.5 - 2014-08-21 | ||
* License: MIT | ||
@@ -18,2 +18,3 @@ */ | ||
'ngTasty.filter.cleanFieldName', | ||
'ngTasty.filter.filterInt', | ||
'ngTasty.filter.range' | ||
@@ -42,2 +43,18 @@ ]); | ||
* @ngdoc filter | ||
* @name filterInt | ||
* @kind function | ||
* | ||
*/ | ||
angular.module('ngTasty.filter.filterInt', []) | ||
.filter('filterInt', function() { | ||
return function (input) { | ||
if(/^(\-|\+)?([0-9]+|Infinity)$/.test(input)) { | ||
return Number(input); | ||
} | ||
return NaN; | ||
}; | ||
}); | ||
/** | ||
* @ngdoc filter | ||
* @name range | ||
@@ -54,30 +71,27 @@ * @kind function | ||
*/ | ||
angular.module('ngTasty.filter.range', ['ngTasty.service.filterInt']) | ||
.filter('range', [ | ||
'filterInt', | ||
function(filterInt) { | ||
return function(input, start, stop, step) { | ||
start = filterInt(start); | ||
stop = filterInt(stop); | ||
step = filterInt(step); | ||
if (isNaN(start)) { | ||
start = 0; | ||
} | ||
if (isNaN(stop)) { | ||
stop = start; | ||
start = 0; | ||
} | ||
if (isNaN(step)) { | ||
step = 1; | ||
} | ||
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)){ | ||
return []; | ||
} | ||
for (var i = start; step > 0 ? i < stop : i > stop; i += step){ | ||
input.push(i); | ||
} | ||
return input; | ||
}; | ||
} | ||
]); | ||
angular.module('ngTasty.filter.range', []) | ||
.filter('range', ["$filter", function($filter) { | ||
return function(input, start, stop, step) { | ||
start = $filter('filterInt')(start); | ||
stop = $filter('filterInt')(stop); | ||
step = $filter('filterInt')(step); | ||
if (isNaN(start)) { | ||
start = 0; | ||
} | ||
if (isNaN(stop)) { | ||
stop = start; | ||
start = 0; | ||
} | ||
if (isNaN(step)) { | ||
step = 1; | ||
} | ||
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)){ | ||
return []; | ||
} | ||
for (var i = start; step > 0 ? i < stop : i > stop; i += step){ | ||
input.push(i); | ||
} | ||
return input; | ||
}; | ||
}]); | ||
@@ -90,2 +104,3 @@ /** | ||
angular.module('ngTasty.service', [ | ||
'ngTasty.service.tastyUtil', | ||
'ngTasty.service.debounce', | ||
@@ -101,10 +116,14 @@ 'ngTasty.service.setProperty', | ||
*/ | ||
angular.module('ngTasty.service.filterInt', []) | ||
.factory('filterInt', function() { | ||
return function (value) { | ||
if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value)) | ||
return Number(value); | ||
return NaN; | ||
} | ||
}); | ||
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 | ||
}; | ||
}]); | ||
@@ -117,18 +136,15 @@ /** | ||
angular.module('ngTasty.service.debounce', []) | ||
.factory('debounce', [ | ||
'$timeout', | ||
function($timeout) { | ||
return function(func, wait, immediate) { | ||
var timeout; | ||
return function() { | ||
var context = this, args = arguments; | ||
$timeout.cancel(timeout); | ||
timeout = $timeout(function() { | ||
timeout = null; | ||
func.apply(context, args); | ||
}, wait); | ||
}; | ||
.factory('debounce', ["$timeout", function($timeout) { | ||
return function(func, wait, immediate) { | ||
var timeout; | ||
return function() { | ||
var context = this, args = arguments; | ||
$timeout.cancel(timeout); | ||
timeout = $timeout(function() { | ||
timeout = null; | ||
func.apply(context, args); | ||
}, wait); | ||
}; | ||
} | ||
]); | ||
}; | ||
}]); | ||
@@ -157,13 +173,10 @@ /** | ||
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; | ||
}; | ||
} | ||
]); | ||
.factory('joinObjects', ["setProperty", function(setProperty) { | ||
return function(objOne, objTwo) { | ||
for (var attrname in objTwo) { | ||
setProperty(objOne, objTwo, attrname); | ||
} | ||
return objOne; | ||
}; | ||
}]); | ||
@@ -183,5 +196,3 @@ /** | ||
'ngTasty.filter.range', | ||
'ngTasty.service.debounce', | ||
'ngTasty.service.setProperty', | ||
'ngTasty.service.joinObjects' | ||
'ngTasty.service.tastyUtil' | ||
]) | ||
@@ -196,149 +207,162 @@ .constant('tableConfig', { | ||
resource: undefined, | ||
resourceCallback: undefined | ||
resourceCallback: undefined, | ||
listItemsPerPage: [5, 25, 50, 100], | ||
itemsPerPage: 5 | ||
}) | ||
.controller('TableController', [ | ||
'$scope', | ||
'$attrs', | ||
'$timeout', | ||
'$filter', | ||
'tableConfig', | ||
'debounce', | ||
'setProperty', | ||
'joinObjects', | ||
function($scope, $attrs, $timeout, $filter, tableConfig, debounce, setProperty, joinObjects) { | ||
'use strict'; | ||
this.$scope = $scope; | ||
.controller('TableController', ["$scope", "$attrs", "$timeout", "$filter", "tableConfig", "tastyUtil", function($scope, $attrs, $timeout, $filter, tableConfig, tastyUtil) { | ||
'use strict'; | ||
this.$scope = $scope; | ||
// Default configs | ||
$scope.query = tableConfig.query; | ||
$scope.resource = tableConfig.resource; | ||
$scope.resourceCallback = tableConfig.resourceCallback; | ||
// Default configs | ||
$scope.query = tableConfig.query; | ||
$scope.resource = tableConfig.resource; | ||
$scope.resourceCallback = tableConfig.resourceCallback; | ||
// Defualt variables | ||
$scope.clientSide = true; | ||
$scope.url = ''; | ||
$scope.header = { | ||
'columns': [] | ||
}; | ||
$scope.rows = []; | ||
$scope.params = {}; | ||
$scope.pagination = { | ||
'count': 5, | ||
'page': 1, | ||
'pages': 1, | ||
'size': 1 | ||
}; | ||
$scope.theadDirective = false; | ||
$scope.paginationDirective = false; | ||
// Defualt variables | ||
$scope.clientSide = true; | ||
$scope.url = ''; | ||
$scope.header = { | ||
'columns': [] | ||
}; | ||
$scope.rows = []; | ||
$scope.params = {}; | ||
$scope.pagination = { | ||
'count': 5, | ||
'page': 1, | ||
'pages': 1, | ||
'size': 1 | ||
}; | ||
$scope.theadDirective = false; | ||
$scope.paginationDirective = false; | ||
// Set custom configs | ||
if (angular.isDefined($attrs.query)) { | ||
$scope.query = $scope.$parent.$eval($attrs.query); | ||
/* Set custom configs | ||
* In the future you will have a way to change | ||
* these values by an isolate optional scope variable, | ||
* more info here https://github.com/angular/angular.js/issues/6404 */ | ||
if (angular.isDefined($attrs.query)) { | ||
$scope.query = $scope.$parent.$eval($attrs.query); | ||
} | ||
if (!angular.isDefined($attrs.resource) && !angular.isDefined($attrs.resourceCallback)) { | ||
throw 'AngularJS tastyTable directive: need the resource or resource-callback attribute'; | ||
} | ||
if (angular.isDefined($attrs.resource)) { | ||
$scope.resource = $scope.$parent.$eval($attrs.resource); | ||
if (!angular.isObject($scope.resource)) { | ||
throw 'AngularJS tastyTable directive: the resource ('+ | ||
$attrs.resource + ') it\'s not an object'; | ||
} else 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.resource) && !angular.isDefined($attrs.resourceCallback)) { | ||
throw 'AngularJS tastyTable directive: need the resource or resource-callback attribute'; | ||
} | ||
if (angular.isDefined($attrs.resourceCallback)) { | ||
$scope.resourceCallback = $scope.$parent.$eval($attrs.resourceCallback); | ||
if (!angular.isFunction($scope.resourceCallback)) { | ||
throw 'AngularJS tastyTable directive: the resource-callback ('+ | ||
$attrs.resourceCallback + ') it\'s not a function'; | ||
} | ||
if (angular.isDefined($attrs.resource)) { | ||
$scope.resource = $scope.$parent.$eval($attrs.resource); | ||
if (!angular.isObject($scope.resource)) { | ||
throw 'AngularJS tastyTable directive: the resource ('+ | ||
$attrs.resource + ') it\'s not an object'; | ||
} else 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)) { | ||
$scope.resourceCallback = $scope.$parent.$eval($attrs.resourceCallback); | ||
if (!angular.isFunction($scope.resourceCallback)) { | ||
throw 'AngularJS tastyTable directive: the resource-callback ('+ | ||
$attrs.resourceCallback + ') it\'s not a function'; | ||
} | ||
$scope.clientSide = false; | ||
} | ||
$scope.clientSide = false; | ||
} | ||
// In TableController, by using `this` we build an API | ||
// for other directives to talk to this one. | ||
this.activate = function(directiveName) { | ||
$scope[directiveName + 'Directive'] = true; | ||
$scope.params[directiveName] = true; | ||
}; | ||
// In TableController, by using `this` we build an API | ||
// for other directives to talk to this one. | ||
this.activate = function(directiveName) { | ||
$scope[directiveName + 'Directive'] = true; | ||
$scope.params[directiveName] = true; | ||
}; | ||
this.setParams = function(key, value) { | ||
$scope.params[key] = value; | ||
}; | ||
this.setParams = function(key, value) { | ||
$scope.params[key] = value; | ||
}; | ||
$scope.setDirectivesValues = function (resource) { | ||
if (!resource) { | ||
return false; | ||
} | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': resource.sortBy || $scope.params.sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
}; | ||
$scope.rows = resource.rows; | ||
$scope.pagination = resource.pagination || $scope.pagination; | ||
$scope.setDirectivesValues = function (resource) { | ||
if (!resource) { | ||
return false; | ||
} | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': resource.sortBy || $scope.params.sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
}; | ||
$scope.rows = resource.rows; | ||
$scope.pagination = resource.pagination || $scope.pagination; | ||
}; | ||
$scope.buildClientResource = function() { | ||
var fromRow, toRow, rowToShow, reverse; | ||
if ($scope.theadDirective) { | ||
reverse = $scope.header.sortOrder === 'asc' ? false : true; | ||
$scope.rows = $filter('orderBy')($scope.rows, $scope.header.sortBy, reverse); | ||
$scope.buildClientResource = function() { | ||
var fromRow, toRow, rowToShow, reverse; | ||
if ($scope.theadDirective) { | ||
reverse = $scope.header.sortOrder === 'asc' ? false : true; | ||
$scope.rows = $filter('orderBy')($scope.rows, $scope.header.sortBy, reverse); | ||
} | ||
if ($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; | ||
if (fromRow >= 0 && toRow >= 0) { | ||
rowToShow = $scope.rows.slice(fromRow, toRow); | ||
$scope.rows = rowToShow; | ||
} | ||
if ($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; | ||
if (fromRow >= 0 && toRow >= 0) { | ||
rowToShow = $scope.rows.slice(fromRow, toRow); | ||
$scope.rows = rowToShow; | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
$scope.buildUrl = function(params, filters) { | ||
var urlQuery, value, url; | ||
urlQuery = {}; | ||
if ($scope.theadDirective) { | ||
urlQuery = setProperty(urlQuery, params, 'sortBy'); | ||
urlQuery = setProperty(urlQuery, params, 'sortOrder'); | ||
$scope.buildUrl = function(params, filters) { | ||
var urlQuery, value, url; | ||
urlQuery = {}; | ||
if ($scope.theadDirective) { | ||
urlQuery = tastyUtil.setProperty(urlQuery, params, 'sortBy'); | ||
urlQuery = tastyUtil.setProperty(urlQuery, params, 'sortOrder'); | ||
} | ||
if ($scope.paginationDirective) { | ||
urlQuery = tastyUtil.setProperty(urlQuery, params, 'page'); | ||
urlQuery = tastyUtil.setProperty(urlQuery, params, 'count'); | ||
} | ||
if ($attrs.filters) { | ||
urlQuery = tastyUtil.joinObjects(urlQuery, filters); | ||
} | ||
return Object.keys(urlQuery).map(function(key) { | ||
value = urlQuery[key]; | ||
if ($scope.query[key]) { | ||
key = $scope.query[key]; | ||
} | ||
if ($scope.paginationDirective) { | ||
urlQuery = setProperty(urlQuery, params, 'page'); | ||
urlQuery = setProperty(urlQuery, params, 'count'); | ||
} | ||
if ($attrs.filters) { | ||
urlQuery = joinObjects(urlQuery, filters); | ||
} | ||
return Object.keys(urlQuery).map(function(key) { | ||
value = urlQuery[key]; | ||
if ($scope.query[key]) { | ||
key = $scope.query[key]; | ||
} | ||
return encodeURIComponent(key) + '=' + encodeURIComponent(value); | ||
}).join('&'); | ||
}; | ||
return encodeURIComponent(key) + '=' + encodeURIComponent(value); | ||
}).join('&'); | ||
}; | ||
$scope.updateClientSideResource = debounce(function() { | ||
$scope.setDirectivesValues($scope.resource); | ||
$scope.buildClientResource(); | ||
}, 100); | ||
$scope.updateClientSideResource = tastyUtil.debounce(function() { | ||
$scope.setDirectivesValues($scope.resource); | ||
$scope.buildClientResource(); | ||
}, 100); | ||
$scope.updateServerSideResource = debounce(function() { | ||
$scope.url = $scope.buildUrl($scope.params, $scope[$attrs.filters]); | ||
$scope.resourceCallback($scope.url).then(function (resource) { | ||
$scope.setDirectivesValues(resource); | ||
}); | ||
}, 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'] = undefined; | ||
$scope.params['sortOrder'] = 'asc'; | ||
$scope.params['page'] = 1; | ||
$scope.params['count'] = 5; | ||
$scope.initTable = function () { | ||
$scope.params['sortBy'] = undefined; | ||
$scope.params['sortOrder'] = 'asc'; | ||
$scope.params['page'] = 1; | ||
$scope.params['count'] = undefined; | ||
if ($scope.clientSide) { | ||
$scope.updateClientSideResource(); | ||
} else { | ||
$scope.updateServerSideResource(); | ||
} | ||
}; | ||
// AngularJs $watch callbacks | ||
if ($attrs.filters) { | ||
$scope.$watch($attrs.filters, function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
$scope.updateServerSideResource(); | ||
} | ||
}, true); | ||
} | ||
$scope.$watch('params', function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
if ($scope.clientSide) { | ||
@@ -349,26 +373,8 @@ $scope.updateClientSideResource(); | ||
} | ||
}; | ||
// AngularJs $watch callbacks | ||
if ($attrs.filters) { | ||
$scope.$watch($attrs.filters, function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
$scope.updateServerSideResource(); | ||
} | ||
}, true); | ||
} | ||
$scope.$watch('params', function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
if ($scope.clientSide) { | ||
$scope.updateClientSideResource(); | ||
} else { | ||
$scope.updateServerSideResource(); | ||
} | ||
} | ||
}, true); | ||
}, true); | ||
// Init table | ||
$scope.initTable(); | ||
} | ||
]) | ||
// Init table | ||
$scope.initTable(); | ||
}]) | ||
.directive('tastyTable', function(){ | ||
@@ -393,87 +399,84 @@ return { | ||
*/ | ||
.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'; | ||
// Thead it's called | ||
tastyTable.activate('thead'); | ||
.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'; | ||
// Thead it's called | ||
tastyTable.activate('thead'); | ||
scope.fields = {}; | ||
scope.fields = {}; | ||
scope.setFields = function () { | ||
var lenHeader, width, i, active, sortable; | ||
lenHeader = scope.header.columns.length; | ||
scope.header.columns.forEach(function (column) { | ||
width = parseFloat((100 / lenHeader).toFixed(2)); | ||
sortable = true; | ||
active = false; | ||
if (scope.notSortBy) { | ||
sortable = scope.notSortBy.indexOf(column.key) < 0; | ||
} | ||
if (column.key === scope.header.sortBy || | ||
'-' + column.key === scope.header.sortBy) { | ||
active = true; | ||
} | ||
scope.fields[column.key] = { | ||
'active': active, | ||
'sortable': sortable, | ||
'width': { 'width': width + '%' }, | ||
'sort': $filter('cleanFieldName')(column.key) | ||
}; | ||
}); | ||
if (scope.header.sortOrder === 'dsc' && | ||
scope.header.sortBy[0] !== '-') { | ||
scope.header.sortBy = '-' + scope.header.sortBy; | ||
scope.setFields = function () { | ||
var lenHeader, width, i, active, sortable; | ||
lenHeader = scope.header.columns.length; | ||
scope.header.columns.forEach(function (column) { | ||
width = parseFloat((100 / lenHeader).toFixed(2)); | ||
sortable = true; | ||
active = false; | ||
if (scope.notSortBy) { | ||
sortable = scope.notSortBy.indexOf(column.key) < 0; | ||
} | ||
}; | ||
scope.sortBy = function (field) { | ||
if (scope.notSortBy && scope.notSortBy.indexOf(field.key) >= 0) { | ||
return false; | ||
if (column.key === scope.header.sortBy || | ||
'-' + column.key === scope.header.sortBy) { | ||
active = true; | ||
} | ||
var fieldName; | ||
fieldName = $filter('cleanFieldName')(field.key); | ||
if (scope.header.sortBy == fieldName) { | ||
scope.header.sortBy = '-' + fieldName; | ||
tastyTable.setParams('sortOrder', 'dsc'); | ||
} else { | ||
scope.header.sortBy = fieldName; | ||
tastyTable.setParams('sortOrder', 'asc'); | ||
} | ||
tastyTable.setParams('sortBy', field.key); | ||
}; | ||
scope.fields[column.key] = { | ||
'active': active, | ||
'sortable': sortable, | ||
'width': { 'width': width + '%' }, | ||
'sort': $filter('cleanFieldName')(column.key) | ||
}; | ||
}); | ||
if (scope.header.sortOrder === 'dsc' && | ||
scope.header.sortBy[0] !== '-') { | ||
scope.header.sortBy = '-' + scope.header.sortBy; | ||
} | ||
}; | ||
scope.isSortUp = function(field) { | ||
if (scope.fields[field.key] === undefined) { | ||
return false; | ||
} | ||
return scope.header.sortBy == '-' + scope.fields[field.key].sort; | ||
}; | ||
scope.sortBy = function (field) { | ||
if (scope.notSortBy && scope.notSortBy.indexOf(field.key) >= 0) { | ||
return false; | ||
} | ||
var fieldName; | ||
fieldName = $filter('cleanFieldName')(field.key); | ||
if (scope.header.sortBy == fieldName) { | ||
scope.header.sortBy = '-' + fieldName; | ||
tastyTable.setParams('sortOrder', 'dsc'); | ||
} else { | ||
scope.header.sortBy = fieldName; | ||
tastyTable.setParams('sortOrder', 'asc'); | ||
} | ||
tastyTable.setParams('sortBy', field.key); | ||
}; | ||
scope.isSortDown = function(field) { | ||
if (scope.fields[field.key] === undefined) { | ||
return false; | ||
} | ||
return scope.header.sortBy == scope.fields[field.key].sort; | ||
}; | ||
scope.isSortUp = function(field) { | ||
if (scope.fields[field.key] === undefined) { | ||
return false; | ||
} | ||
return scope.header.sortBy == '-' + scope.fields[field.key].sort; | ||
}; | ||
tastyTable.$scope.$watch('header', function (newValue, oldValue){ | ||
if (newValue && (newValue !== oldValue)) { | ||
scope.header = newValue; | ||
scope.setFields(); | ||
} | ||
}, true); | ||
} | ||
}; | ||
} | ||
]) | ||
scope.isSortDown = function(field) { | ||
if (scope.fields[field.key] === undefined) { | ||
return false; | ||
} | ||
return scope.header.sortBy == scope.fields[field.key].sort; | ||
}; | ||
tastyTable.$scope.$watch('header', function (newValue, oldValue){ | ||
if (newValue && (newValue !== oldValue)) { | ||
scope.header = newValue; | ||
scope.setFields(); | ||
} | ||
}, true); | ||
} | ||
}; | ||
}]) | ||
/** | ||
@@ -492,113 +495,121 @@ * @ngdoc directive | ||
*/ | ||
.directive('tastyPagination', [ | ||
'$filter', | ||
function($filter) { | ||
return { | ||
restrict: 'AE', | ||
require: '^tastyTable', | ||
scope: {}, | ||
templateUrl: 'template/table/pagination.html', | ||
link: function (scope, element, attrs, tastyTable) { | ||
'use strict'; | ||
var getPage, setCount, setPaginationRange, | ||
setPreviousRange, setRemainingRange, | ||
setPaginationRanges; | ||
.controller('TablePaginationController', ["$scope", "$attrs", "tableConfig", function($scope, $attrs, tableConfig) { | ||
if (angular.isDefined($attrs.itemsPerPage)) { | ||
$scope.itemsPerPage = $scope.$parent.$eval($attrs.itemsPerPage); | ||
} | ||
if (angular.isDefined($attrs.listItemsPerPage)) { | ||
$scope.listItemsPerPage = $scope.$parent.$eval($attrs.listItemsPerPage); | ||
} | ||
// Default configs | ||
$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; | ||
// Pagination it's called | ||
tastyTable.activate('pagination'); | ||
// Pagination it's called | ||
tastyTable.activate('pagination'); | ||
/* In the future you will have a way to change | ||
* these values by an isolate optional scope variable, | ||
* more info here https://github.com/angular/angular.js/issues/6404 */ | ||
scope.numPaginations = 5; | ||
scope.pagListCount = [5, 25, 50, 100]; | ||
// Internal variable | ||
scope.pagination = {}; | ||
scope.pagMinRange = 1; | ||
scope.pagMaxRange = 1; | ||
// Internal variable | ||
scope.pagination = {}; | ||
scope.pagMinRange = 1; | ||
scope.pagMaxRange = 1; | ||
getPage = function (numPage) { | ||
tastyTable.setParams('page', numPage); | ||
}; | ||
getPage = function (numPage) { | ||
tastyTable.setParams('page', numPage); | ||
}; | ||
setCount = function(count) { | ||
var maxItems, page; | ||
//scope.pagination.count = count; | ||
maxItems = count * scope.pagination.page; | ||
if (maxItems > scope.pagination.size) { | ||
page = Math.ceil(scope.pagination.size / count); | ||
tastyTable.setParams('page', page); | ||
} | ||
tastyTable.setParams('count', count); | ||
}; | ||
setCount = function(count) { | ||
var maxItems, page; | ||
maxItems = count * scope.pagination.page; | ||
if (maxItems > scope.pagination.size) { | ||
page = Math.ceil(scope.pagination.size / count); | ||
tastyTable.setParams('page', page); | ||
} | ||
tastyTable.setParams('count', count); | ||
}; | ||
setPaginationRange = function () { | ||
var currentPage, totalPages; | ||
currentPage = scope.pagination.page; | ||
//scope.pagination.count = scope.itemsPerPage; | ||
if (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(); | ||
}; | ||
setPaginationRange = function () { | ||
var currentPage, totalPages; | ||
currentPage = scope.pagination.page; | ||
if (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 () { | ||
if (scope.pagHideMinRange === true || scope.pagMinRange < 1) { | ||
return false; | ||
} | ||
scope.pagMaxRange = scope.pagMinRange; | ||
scope.pagMinRange = scope.pagMaxRange - scope.itemsPerPage; | ||
setPaginationRanges(); | ||
}; | ||
setPreviousRange = function () { | ||
if (scope.pagHideMinRange === true || scope.pagMinRange < 1) { | ||
return false; | ||
} | ||
scope.pagMaxRange = scope.pagMinRange; | ||
scope.pagMinRange = scope.pagMaxRange - scope.numPaginations; | ||
setPaginationRanges(); | ||
}; | ||
setRemainingRange = function () { | ||
if (scope.pagHideMaxRange === true || scope.pagMaxRange > scope.pagination.pages) { | ||
return false; | ||
} | ||
scope.pagMinRange = scope.pagMaxRange; | ||
scope.pagMaxRange = scope.pagMinRange + scope.itemsPerPage; | ||
if (scope.pagMaxRange > scope.pagination.pages) { | ||
scope.pagMaxRange = scope.pagination.pages; | ||
} | ||
scope.pagMinRange = scope.pagMaxRange - scope.itemsPerPage; | ||
setPaginationRanges(); | ||
}; | ||
setRemainingRange = function () { | ||
if (scope.pagHideMaxRange === true || scope.pagMaxRange > scope.pagination.pages) { | ||
return false; | ||
setPaginationRanges = function () { | ||
scope.pagMinRange = scope.pagMinRange > 0 ? scope.pagMinRange : 1; | ||
scope.pagMaxRange = scope.pagMinRange + 5; | ||
if (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.pagMinRange = scope.pagMaxRange; | ||
scope.pagMaxRange = scope.pagMinRange + scope.numPaginations; | ||
if (scope.pagMaxRange > scope.pagination.pages) { | ||
scope.pagMaxRange = scope.pagination.pages; | ||
} | ||
scope.pagMinRange = scope.pagMaxRange - scope.numPaginations; | ||
setPaginationRanges(); | ||
}; | ||
} | ||
scope.rangePage = $filter('range')([], scope.pagMinRange, scope.pagMaxRange); | ||
}; | ||
setPaginationRanges = function () { | ||
scope.pagMinRange = scope.pagMinRange > 0 ? scope.pagMinRange : 1; | ||
scope.pagMaxRange = scope.pagMinRange + scope.numPaginations; | ||
if (scope.pagMaxRange > scope.pagination.pages) { | ||
scope.pagMaxRange = scope.pagination.pages + 1; | ||
} | ||
scope.pagHideMinRange = scope.pagMinRange <= 1; | ||
scope.pagHideMaxRange = scope.pagMaxRange >= scope.pagination.pages; | ||
if (scope.pagination.size < 50) { | ||
scope.pagListCount = [5, 25]; | ||
} else if (scope.pagination.size < 100) { | ||
scope.pagListCount = [5, 25, 50]; | ||
} else { | ||
scope.pagListCount = [5, 25, 50, 100]; | ||
} | ||
scope.rangePage = $filter('range')([], scope.pagMinRange, scope.pagMaxRange); | ||
}; | ||
scope.page = { | ||
'get': getPage, | ||
'setCount': setCount, | ||
'previous': setPreviousRange, | ||
'remaining': setRemainingRange | ||
}; | ||
scope.page = { | ||
'get': getPage, | ||
'setCount': setCount, | ||
'previous': setPreviousRange, | ||
'remaining': setRemainingRange | ||
}; | ||
tastyTable.$scope.$watch('pagination', function (newValue, oldValue){ | ||
if (newValue && (newValue !== oldValue)) { | ||
scope.pagination = newValue; | ||
setPaginationRange(); | ||
} | ||
}, true); | ||
tastyTable.$scope.$watch('pagination', function (newValue, oldValue){ | ||
if (newValue && (newValue !== oldValue)) { | ||
scope.pagination = newValue; | ||
setPaginationRange(); | ||
} | ||
}, true); | ||
} | ||
}; | ||
} | ||
]); | ||
// Init Pagination | ||
scope.page.setCount(scope.itemsPerPage); | ||
} | ||
}; | ||
}]); | ||
angular.module('template/table/head.html', []).run(['$templateCache', function($templateCache) { | ||
@@ -622,6 +633,6 @@ $templateCache.put('template/table/head.html', | ||
'<div class="row">\n' + | ||
' <div class="col-md-3 text-left">\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 pagListCount" \n' + | ||
' ng-repeat="count in listItemsPerPageShow" \n' + | ||
' ng-class="{active: count == pagination.count}" \n' + | ||
@@ -631,3 +642,3 @@ ' ng-click="page.setCount(count)" ng-bind="count"></button>\n' + | ||
' </div>\n' + | ||
' <div class="col-md-6 text-center">\n' + | ||
' <div class="col-xs-6 text-center">\n' + | ||
' <ul class="pagination">\n' + | ||
@@ -649,3 +660,3 @@ ' <li ng-class="{disabled: pagHideMinRange}">\n' + | ||
' </div>\n' + | ||
' <div class="col-md-3 text-right">\n' + | ||
' <div class="col-xs-3 text-right">\n' + | ||
' <p>Page <span ng-bind="pagination.page"></span> \n' + | ||
@@ -652,0 +663,0 @@ ' of <span ng-bind="pagination.pages"></span>,\n' + |
@@ -1,1 +0,8 @@ | ||
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.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(e){return e.replace(/[^a-zA-Z0-9-]+/g,"-").toLowerCase()}}),angular.module("ngTasty.filter.range",["ngTasty.service.filterInt"]).filter("range",["filterInt",function(e){return function(a,n,t,r){if(n=e(n),t=e(t),r=e(r),isNaN(n)&&(n=0),isNaN(t)&&(t=n,n=0),isNaN(r)&&(r=1),r>0&&n>=t||0>r&&t>=n)return[];for(var i=n;r>0?t>i:i>t;i+=r)a.push(i);return a}}]),angular.module("ngTasty.service",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]),angular.module("ngTasty.service.filterInt",[]).factory("filterInt",function(){return function(e){return/^(\-|\+)?([0-9]+|Infinity)$/.test(e)?Number(e):0/0}}),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function(e){return function(a,n){var t;return function(){var r=this,i=arguments;e.cancel(t),t=e(function(){t=null,a.apply(r,i)},n)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(e,a,n){return"undefined"!=typeof a[n]&&null!==a[n]&&(e[n]=a[n]),e}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(e){return function(a,n){for(var t in n)e(a,n,t);return a}}]),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).constant("tableConfig",{query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},resource:void 0,resourceCallback:void 0}).controller("TableController",["$scope","$attrs","$timeout","$filter","tableConfig","debounce","setProperty","joinObjects",function(e,a,n,t,r,i,s,o){"use strict";if(this.$scope=e,e.query=r.query,e.resource=r.resource,e.resourceCallback=r.resourceCallback,e.clientSide=!0,e.url="",e.header={columns:[]},e.rows=[],e.params={},e.pagination={count:5,page:1,pages:1,size:1},e.theadDirective=!1,e.paginationDirective=!1,angular.isDefined(a.query)&&(e.query=e.$parent.$eval(a.query)),!angular.isDefined(a.resource)&&!angular.isDefined(a.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined(a.resource)){if(e.resource=e.$parent.$eval(a.resource),!angular.isObject(e.resource))throw"AngularJS tastyTable directive: the resource ("+a.resource+") it's not an object";if(!e.resource.header&&!e.resource.rows)throw"AngularJS tastyTable directive: the resource ("+a.resource+") has the property header or rows undefined"}if(angular.isDefined(a.resourceCallback)){if(e.resourceCallback=e.$parent.$eval(a.resourceCallback),!angular.isFunction(e.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+a.resourceCallback+") it's not a function";e.clientSide=!1}this.activate=function(a){e[a+"Directive"]=!0,e.params[a]=!0},this.setParams=function(a,n){e.params[a]=n},e.setDirectivesValues=function(a){return a?(e.header={columns:a.header,sortBy:a.sortBy||e.params.sortBy,sortOrder:a.sortOrder||e.params.sortOrder},e.rows=a.rows,void(e.pagination=a.pagination||e.pagination)):!1},e.buildClientResource=function(){var a,n,r,i;e.theadDirective&&(i="asc"===e.header.sortOrder?!1:!0,e.rows=t("orderBy")(e.rows,e.header.sortBy,i)),e.paginationDirective&&(e.pagination.page=e.params.page,e.pagination.count=e.params.count,e.pagination.size=e.rows.length,e.pagination.pages=Math.ceil(e.rows.length/e.pagination.count),n=e.pagination.count*e.pagination.page,a=n-e.pagination.count,a>=0&&n>=0&&(r=e.rows.slice(a,n),e.rows=r))},e.buildUrl=function(n,t){var r,i;return r={},e.theadDirective&&(r=s(r,n,"sortBy"),r=s(r,n,"sortOrder")),e.paginationDirective&&(r=s(r,n,"page"),r=s(r,n,"count")),a.filters&&(r=o(r,t)),Object.keys(r).map(function(a){return i=r[a],e.query[a]&&(a=e.query[a]),encodeURIComponent(a)+"="+encodeURIComponent(i)}).join("&")},e.updateClientSideResource=i(function(){e.setDirectivesValues(e.resource),e.buildClientResource()},100),e.updateServerSideResource=i(function(){e.url=e.buildUrl(e.params,e[a.filters]),e.resourceCallback(e.url).then(function(a){e.setDirectivesValues(a)})},100),e.initTable=function(){e.params.sortBy=void 0,e.params.sortOrder="asc",e.params.page=1,e.params.count=5,e.clientSide?e.updateClientSideResource():e.updateServerSideResource()},a.filters&&e.$watch(a.filters,function(a,n){a!==n&&e.updateServerSideResource()},!0),e.$watch("params",function(a,n){a!==n&&(e.clientSide?e.updateClientSideResource():e.updateServerSideResource())},!0),e.initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function(e){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(a,n,t,r){"use strict";r.activate("thead"),a.fields={},a.setFields=function(){var n,t,r,i;n=a.header.columns.length,a.header.columns.forEach(function(s){t=parseFloat((100/n).toFixed(2)),i=!0,r=!1,a.notSortBy&&(i=a.notSortBy.indexOf(s.key)<0),(s.key===a.header.sortBy||"-"+s.key===a.header.sortBy)&&(r=!0),a.fields[s.key]={active:r,sortable:i,width:{width:t+"%"},sort:e("cleanFieldName")(s.key)}}),"dsc"===a.header.sortOrder&&"-"!==a.header.sortBy[0]&&(a.header.sortBy="-"+a.header.sortBy)},a.sortBy=function(n){if(a.notSortBy&&a.notSortBy.indexOf(n.key)>=0)return!1;var t;t=e("cleanFieldName")(n.key),a.header.sortBy==t?(a.header.sortBy="-"+t,r.setParams("sortOrder","dsc")):(a.header.sortBy=t,r.setParams("sortOrder","asc")),r.setParams("sortBy",n.key)},a.isSortUp=function(e){return void 0===a.fields[e.key]?!1:a.header.sortBy=="-"+a.fields[e.key].sort},a.isSortDown=function(e){return void 0===a.fields[e.key]?!1:a.header.sortBy==a.fields[e.key].sort},r.$scope.$watch("header",function(e,n){e&&e!==n&&(a.header=e,a.setFields())},!0)}}}]).directive("tastyPagination",["$filter",function(e){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",link:function(a,n,t,r){"use strict";var i,s,o,c,g,l;r.activate("pagination"),a.numPaginations=5,a.pagListCount=[5,25,50,100],a.pagination={},a.pagMinRange=1,a.pagMaxRange=1,i=function(e){r.setParams("page",e)},s=function(e){var n,t;n=e*a.pagination.page,n>a.pagination.size&&(t=Math.ceil(a.pagination.size/e),r.setParams("page",t)),r.setParams("count",e)},o=function(){var e;e=a.pagination.page,e>a.pagination.pages&&(e=a.pagination.pages),a.pagMinRange=e-2>0?e-2:1,a.pagMaxRange=e+2,a.pagination.page=e,l()},c=function(){return a.pagHideMinRange===!0||a.pagMinRange<1?!1:(a.pagMaxRange=a.pagMinRange,a.pagMinRange=a.pagMaxRange-a.numPaginations,void l())},g=function(){return a.pagHideMaxRange===!0||a.pagMaxRange>a.pagination.pages?!1:(a.pagMinRange=a.pagMaxRange,a.pagMaxRange=a.pagMinRange+a.numPaginations,a.pagMaxRange>a.pagination.pages&&(a.pagMaxRange=a.pagination.pages),a.pagMinRange=a.pagMaxRange-a.numPaginations,void l())},l=function(){a.pagMinRange=a.pagMinRange>0?a.pagMinRange:1,a.pagMaxRange=a.pagMinRange+a.numPaginations,a.pagMaxRange>a.pagination.pages&&(a.pagMaxRange=a.pagination.pages+1),a.pagHideMinRange=a.pagMinRange<=1,a.pagHideMaxRange=a.pagMaxRange>=a.pagination.pages,a.pagListCount=a.pagination.size<50?[5,25]:a.pagination.size<100?[5,25,50]:[5,25,50,100],a.rangePage=e("range")([],a.pagMinRange,a.pagMaxRange)},a.page={get:i,setCount:s,previous:c,remaining:g},r.$scope.$watch("pagination",function(e,n){e&&e!==n&&(a.pagination=e,o())},!0)}}}]),angular.module("template/table/head.html",[]).run(["$templateCache",function(e){e.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(e){e.put("template/table/pagination.html",'<div class="row">\n <div class="col-md-3 text-left">\n <div class="btn-group">\n <button type="button" class="btn btn-default" \n ng-repeat="count in pagListCount" \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-md-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-md-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>')}]); | ||
/* | ||
* ng-tasty | ||
* https://github.com/Zizzamia/ng-tasty | ||
* Version: 0.2.5 - 2014-08-21 | ||
* 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>')}]); |
798
ng-tasty.js
@@ -5,3 +5,3 @@ /* | ||
* Version: 0.2.4 - 2014-08-13 | ||
* Version: 0.2.5 - 2014-08-21 | ||
* License: MIT | ||
@@ -17,2 +17,3 @@ */ | ||
'ngTasty.filter.cleanFieldName', | ||
'ngTasty.filter.filterInt', | ||
'ngTasty.filter.range' | ||
@@ -41,2 +42,18 @@ ]); | ||
* @ngdoc filter | ||
* @name filterInt | ||
* @kind function | ||
* | ||
*/ | ||
angular.module('ngTasty.filter.filterInt', []) | ||
.filter('filterInt', function() { | ||
return function (input) { | ||
if(/^(\-|\+)?([0-9]+|Infinity)$/.test(input)) { | ||
return Number(input); | ||
} | ||
return NaN; | ||
}; | ||
}); | ||
/** | ||
* @ngdoc filter | ||
* @name range | ||
@@ -53,30 +70,27 @@ * @kind function | ||
*/ | ||
angular.module('ngTasty.filter.range', ['ngTasty.service.filterInt']) | ||
.filter('range', [ | ||
'filterInt', | ||
function(filterInt) { | ||
return function(input, start, stop, step) { | ||
start = filterInt(start); | ||
stop = filterInt(stop); | ||
step = filterInt(step); | ||
if (isNaN(start)) { | ||
start = 0; | ||
} | ||
if (isNaN(stop)) { | ||
stop = start; | ||
start = 0; | ||
} | ||
if (isNaN(step)) { | ||
step = 1; | ||
} | ||
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)){ | ||
return []; | ||
} | ||
for (var i = start; step > 0 ? i < stop : i > stop; i += step){ | ||
input.push(i); | ||
} | ||
return input; | ||
}; | ||
} | ||
]); | ||
angular.module('ngTasty.filter.range', []) | ||
.filter('range', ["$filter", function($filter) { | ||
return function(input, start, stop, step) { | ||
start = $filter('filterInt')(start); | ||
stop = $filter('filterInt')(stop); | ||
step = $filter('filterInt')(step); | ||
if (isNaN(start)) { | ||
start = 0; | ||
} | ||
if (isNaN(stop)) { | ||
stop = start; | ||
start = 0; | ||
} | ||
if (isNaN(step)) { | ||
step = 1; | ||
} | ||
if ((step > 0 && start >= stop) || (step < 0 && start <= stop)){ | ||
return []; | ||
} | ||
for (var i = start; step > 0 ? i < stop : i > stop; i += step){ | ||
input.push(i); | ||
} | ||
return input; | ||
}; | ||
}]); | ||
@@ -89,2 +103,3 @@ /** | ||
angular.module('ngTasty.service', [ | ||
'ngTasty.service.tastyUtil', | ||
'ngTasty.service.debounce', | ||
@@ -100,10 +115,14 @@ 'ngTasty.service.setProperty', | ||
*/ | ||
angular.module('ngTasty.service.filterInt', []) | ||
.factory('filterInt', function() { | ||
return function (value) { | ||
if(/^(\-|\+)?([0-9]+|Infinity)$/.test(value)) | ||
return Number(value); | ||
return NaN; | ||
} | ||
}); | ||
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 | ||
}; | ||
}]); | ||
@@ -116,18 +135,15 @@ /** | ||
angular.module('ngTasty.service.debounce', []) | ||
.factory('debounce', [ | ||
'$timeout', | ||
function($timeout) { | ||
return function(func, wait, immediate) { | ||
var timeout; | ||
return function() { | ||
var context = this, args = arguments; | ||
$timeout.cancel(timeout); | ||
timeout = $timeout(function() { | ||
timeout = null; | ||
func.apply(context, args); | ||
}, wait); | ||
}; | ||
.factory('debounce', ["$timeout", function($timeout) { | ||
return function(func, wait, immediate) { | ||
var timeout; | ||
return function() { | ||
var context = this, args = arguments; | ||
$timeout.cancel(timeout); | ||
timeout = $timeout(function() { | ||
timeout = null; | ||
func.apply(context, args); | ||
}, wait); | ||
}; | ||
} | ||
]); | ||
}; | ||
}]); | ||
@@ -156,13 +172,10 @@ /** | ||
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; | ||
}; | ||
} | ||
]); | ||
.factory('joinObjects', ["setProperty", function(setProperty) { | ||
return function(objOne, objTwo) { | ||
for (var attrname in objTwo) { | ||
setProperty(objOne, objTwo, attrname); | ||
} | ||
return objOne; | ||
}; | ||
}]); | ||
@@ -182,5 +195,3 @@ /** | ||
'ngTasty.filter.range', | ||
'ngTasty.service.debounce', | ||
'ngTasty.service.setProperty', | ||
'ngTasty.service.joinObjects' | ||
'ngTasty.service.tastyUtil' | ||
]) | ||
@@ -195,149 +206,162 @@ .constant('tableConfig', { | ||
resource: undefined, | ||
resourceCallback: undefined | ||
resourceCallback: undefined, | ||
listItemsPerPage: [5, 25, 50, 100], | ||
itemsPerPage: 5 | ||
}) | ||
.controller('TableController', [ | ||
'$scope', | ||
'$attrs', | ||
'$timeout', | ||
'$filter', | ||
'tableConfig', | ||
'debounce', | ||
'setProperty', | ||
'joinObjects', | ||
function($scope, $attrs, $timeout, $filter, tableConfig, debounce, setProperty, joinObjects) { | ||
'use strict'; | ||
this.$scope = $scope; | ||
.controller('TableController', ["$scope", "$attrs", "$timeout", "$filter", "tableConfig", "tastyUtil", function($scope, $attrs, $timeout, $filter, tableConfig, tastyUtil) { | ||
'use strict'; | ||
this.$scope = $scope; | ||
// Default configs | ||
$scope.query = tableConfig.query; | ||
$scope.resource = tableConfig.resource; | ||
$scope.resourceCallback = tableConfig.resourceCallback; | ||
// Default configs | ||
$scope.query = tableConfig.query; | ||
$scope.resource = tableConfig.resource; | ||
$scope.resourceCallback = tableConfig.resourceCallback; | ||
// Defualt variables | ||
$scope.clientSide = true; | ||
$scope.url = ''; | ||
$scope.header = { | ||
'columns': [] | ||
}; | ||
$scope.rows = []; | ||
$scope.params = {}; | ||
$scope.pagination = { | ||
'count': 5, | ||
'page': 1, | ||
'pages': 1, | ||
'size': 1 | ||
}; | ||
$scope.theadDirective = false; | ||
$scope.paginationDirective = false; | ||
// Defualt variables | ||
$scope.clientSide = true; | ||
$scope.url = ''; | ||
$scope.header = { | ||
'columns': [] | ||
}; | ||
$scope.rows = []; | ||
$scope.params = {}; | ||
$scope.pagination = { | ||
'count': 5, | ||
'page': 1, | ||
'pages': 1, | ||
'size': 1 | ||
}; | ||
$scope.theadDirective = false; | ||
$scope.paginationDirective = false; | ||
// Set custom configs | ||
if (angular.isDefined($attrs.query)) { | ||
$scope.query = $scope.$parent.$eval($attrs.query); | ||
/* Set custom configs | ||
* In the future you will have a way to change | ||
* these values by an isolate optional scope variable, | ||
* more info here https://github.com/angular/angular.js/issues/6404 */ | ||
if (angular.isDefined($attrs.query)) { | ||
$scope.query = $scope.$parent.$eval($attrs.query); | ||
} | ||
if (!angular.isDefined($attrs.resource) && !angular.isDefined($attrs.resourceCallback)) { | ||
throw 'AngularJS tastyTable directive: need the resource or resource-callback attribute'; | ||
} | ||
if (angular.isDefined($attrs.resource)) { | ||
$scope.resource = $scope.$parent.$eval($attrs.resource); | ||
if (!angular.isObject($scope.resource)) { | ||
throw 'AngularJS tastyTable directive: the resource ('+ | ||
$attrs.resource + ') it\'s not an object'; | ||
} else 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.resource) && !angular.isDefined($attrs.resourceCallback)) { | ||
throw 'AngularJS tastyTable directive: need the resource or resource-callback attribute'; | ||
} | ||
if (angular.isDefined($attrs.resourceCallback)) { | ||
$scope.resourceCallback = $scope.$parent.$eval($attrs.resourceCallback); | ||
if (!angular.isFunction($scope.resourceCallback)) { | ||
throw 'AngularJS tastyTable directive: the resource-callback ('+ | ||
$attrs.resourceCallback + ') it\'s not a function'; | ||
} | ||
if (angular.isDefined($attrs.resource)) { | ||
$scope.resource = $scope.$parent.$eval($attrs.resource); | ||
if (!angular.isObject($scope.resource)) { | ||
throw 'AngularJS tastyTable directive: the resource ('+ | ||
$attrs.resource + ') it\'s not an object'; | ||
} else 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)) { | ||
$scope.resourceCallback = $scope.$parent.$eval($attrs.resourceCallback); | ||
if (!angular.isFunction($scope.resourceCallback)) { | ||
throw 'AngularJS tastyTable directive: the resource-callback ('+ | ||
$attrs.resourceCallback + ') it\'s not a function'; | ||
} | ||
$scope.clientSide = false; | ||
} | ||
$scope.clientSide = false; | ||
} | ||
// In TableController, by using `this` we build an API | ||
// for other directives to talk to this one. | ||
this.activate = function(directiveName) { | ||
$scope[directiveName + 'Directive'] = true; | ||
$scope.params[directiveName] = true; | ||
}; | ||
// In TableController, by using `this` we build an API | ||
// for other directives to talk to this one. | ||
this.activate = function(directiveName) { | ||
$scope[directiveName + 'Directive'] = true; | ||
$scope.params[directiveName] = true; | ||
}; | ||
this.setParams = function(key, value) { | ||
$scope.params[key] = value; | ||
}; | ||
this.setParams = function(key, value) { | ||
$scope.params[key] = value; | ||
}; | ||
$scope.setDirectivesValues = function (resource) { | ||
if (!resource) { | ||
return false; | ||
} | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': resource.sortBy || $scope.params.sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
}; | ||
$scope.rows = resource.rows; | ||
$scope.pagination = resource.pagination || $scope.pagination; | ||
$scope.setDirectivesValues = function (resource) { | ||
if (!resource) { | ||
return false; | ||
} | ||
$scope.header = { | ||
'columns': resource.header, | ||
'sortBy': resource.sortBy || $scope.params.sortBy, | ||
'sortOrder': resource.sortOrder || $scope.params.sortOrder | ||
}; | ||
$scope.rows = resource.rows; | ||
$scope.pagination = resource.pagination || $scope.pagination; | ||
}; | ||
$scope.buildClientResource = function() { | ||
var fromRow, toRow, rowToShow, reverse; | ||
if ($scope.theadDirective) { | ||
reverse = $scope.header.sortOrder === 'asc' ? false : true; | ||
$scope.rows = $filter('orderBy')($scope.rows, $scope.header.sortBy, reverse); | ||
$scope.buildClientResource = function() { | ||
var fromRow, toRow, rowToShow, reverse; | ||
if ($scope.theadDirective) { | ||
reverse = $scope.header.sortOrder === 'asc' ? false : true; | ||
$scope.rows = $filter('orderBy')($scope.rows, $scope.header.sortBy, reverse); | ||
} | ||
if ($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; | ||
if (fromRow >= 0 && toRow >= 0) { | ||
rowToShow = $scope.rows.slice(fromRow, toRow); | ||
$scope.rows = rowToShow; | ||
} | ||
if ($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; | ||
if (fromRow >= 0 && toRow >= 0) { | ||
rowToShow = $scope.rows.slice(fromRow, toRow); | ||
$scope.rows = rowToShow; | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
$scope.buildUrl = function(params, filters) { | ||
var urlQuery, value, url; | ||
urlQuery = {}; | ||
if ($scope.theadDirective) { | ||
urlQuery = setProperty(urlQuery, params, 'sortBy'); | ||
urlQuery = setProperty(urlQuery, params, 'sortOrder'); | ||
$scope.buildUrl = function(params, filters) { | ||
var urlQuery, value, url; | ||
urlQuery = {}; | ||
if ($scope.theadDirective) { | ||
urlQuery = tastyUtil.setProperty(urlQuery, params, 'sortBy'); | ||
urlQuery = tastyUtil.setProperty(urlQuery, params, 'sortOrder'); | ||
} | ||
if ($scope.paginationDirective) { | ||
urlQuery = tastyUtil.setProperty(urlQuery, params, 'page'); | ||
urlQuery = tastyUtil.setProperty(urlQuery, params, 'count'); | ||
} | ||
if ($attrs.filters) { | ||
urlQuery = tastyUtil.joinObjects(urlQuery, filters); | ||
} | ||
return Object.keys(urlQuery).map(function(key) { | ||
value = urlQuery[key]; | ||
if ($scope.query[key]) { | ||
key = $scope.query[key]; | ||
} | ||
if ($scope.paginationDirective) { | ||
urlQuery = setProperty(urlQuery, params, 'page'); | ||
urlQuery = setProperty(urlQuery, params, 'count'); | ||
} | ||
if ($attrs.filters) { | ||
urlQuery = joinObjects(urlQuery, filters); | ||
} | ||
return Object.keys(urlQuery).map(function(key) { | ||
value = urlQuery[key]; | ||
if ($scope.query[key]) { | ||
key = $scope.query[key]; | ||
} | ||
return encodeURIComponent(key) + '=' + encodeURIComponent(value); | ||
}).join('&'); | ||
}; | ||
return encodeURIComponent(key) + '=' + encodeURIComponent(value); | ||
}).join('&'); | ||
}; | ||
$scope.updateClientSideResource = debounce(function() { | ||
$scope.setDirectivesValues($scope.resource); | ||
$scope.buildClientResource(); | ||
}, 100); | ||
$scope.updateClientSideResource = tastyUtil.debounce(function() { | ||
$scope.setDirectivesValues($scope.resource); | ||
$scope.buildClientResource(); | ||
}, 100); | ||
$scope.updateServerSideResource = debounce(function() { | ||
$scope.url = $scope.buildUrl($scope.params, $scope[$attrs.filters]); | ||
$scope.resourceCallback($scope.url).then(function (resource) { | ||
$scope.setDirectivesValues(resource); | ||
}); | ||
}, 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'] = undefined; | ||
$scope.params['sortOrder'] = 'asc'; | ||
$scope.params['page'] = 1; | ||
$scope.params['count'] = 5; | ||
$scope.initTable = function () { | ||
$scope.params['sortBy'] = undefined; | ||
$scope.params['sortOrder'] = 'asc'; | ||
$scope.params['page'] = 1; | ||
$scope.params['count'] = undefined; | ||
if ($scope.clientSide) { | ||
$scope.updateClientSideResource(); | ||
} else { | ||
$scope.updateServerSideResource(); | ||
} | ||
}; | ||
// AngularJs $watch callbacks | ||
if ($attrs.filters) { | ||
$scope.$watch($attrs.filters, function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
$scope.updateServerSideResource(); | ||
} | ||
}, true); | ||
} | ||
$scope.$watch('params', function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
if ($scope.clientSide) { | ||
@@ -348,26 +372,8 @@ $scope.updateClientSideResource(); | ||
} | ||
}; | ||
// AngularJs $watch callbacks | ||
if ($attrs.filters) { | ||
$scope.$watch($attrs.filters, function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
$scope.updateServerSideResource(); | ||
} | ||
}, true); | ||
} | ||
$scope.$watch('params', function (newValue, oldValue){ | ||
if (newValue !== oldValue) { | ||
if ($scope.clientSide) { | ||
$scope.updateClientSideResource(); | ||
} else { | ||
$scope.updateServerSideResource(); | ||
} | ||
} | ||
}, true); | ||
}, true); | ||
// Init table | ||
$scope.initTable(); | ||
} | ||
]) | ||
// Init table | ||
$scope.initTable(); | ||
}]) | ||
.directive('tastyTable', function(){ | ||
@@ -392,87 +398,84 @@ return { | ||
*/ | ||
.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'; | ||
// Thead it's called | ||
tastyTable.activate('thead'); | ||
.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'; | ||
// Thead it's called | ||
tastyTable.activate('thead'); | ||
scope.fields = {}; | ||
scope.fields = {}; | ||
scope.setFields = function () { | ||
var lenHeader, width, i, active, sortable; | ||
lenHeader = scope.header.columns.length; | ||
scope.header.columns.forEach(function (column) { | ||
width = parseFloat((100 / lenHeader).toFixed(2)); | ||
sortable = true; | ||
active = false; | ||
if (scope.notSortBy) { | ||
sortable = scope.notSortBy.indexOf(column.key) < 0; | ||
} | ||
if (column.key === scope.header.sortBy || | ||
'-' + column.key === scope.header.sortBy) { | ||
active = true; | ||
} | ||
scope.fields[column.key] = { | ||
'active': active, | ||
'sortable': sortable, | ||
'width': { 'width': width + '%' }, | ||
'sort': $filter('cleanFieldName')(column.key) | ||
}; | ||
}); | ||
if (scope.header.sortOrder === 'dsc' && | ||
scope.header.sortBy[0] !== '-') { | ||
scope.header.sortBy = '-' + scope.header.sortBy; | ||
scope.setFields = function () { | ||
var lenHeader, width, i, active, sortable; | ||
lenHeader = scope.header.columns.length; | ||
scope.header.columns.forEach(function (column) { | ||
width = parseFloat((100 / lenHeader).toFixed(2)); | ||
sortable = true; | ||
active = false; | ||
if (scope.notSortBy) { | ||
sortable = scope.notSortBy.indexOf(column.key) < 0; | ||
} | ||
}; | ||
scope.sortBy = function (field) { | ||
if (scope.notSortBy && scope.notSortBy.indexOf(field.key) >= 0) { | ||
return false; | ||
if (column.key === scope.header.sortBy || | ||
'-' + column.key === scope.header.sortBy) { | ||
active = true; | ||
} | ||
var fieldName; | ||
fieldName = $filter('cleanFieldName')(field.key); | ||
if (scope.header.sortBy == fieldName) { | ||
scope.header.sortBy = '-' + fieldName; | ||
tastyTable.setParams('sortOrder', 'dsc'); | ||
} else { | ||
scope.header.sortBy = fieldName; | ||
tastyTable.setParams('sortOrder', 'asc'); | ||
} | ||
tastyTable.setParams('sortBy', field.key); | ||
}; | ||
scope.fields[column.key] = { | ||
'active': active, | ||
'sortable': sortable, | ||
'width': { 'width': width + '%' }, | ||
'sort': $filter('cleanFieldName')(column.key) | ||
}; | ||
}); | ||
if (scope.header.sortOrder === 'dsc' && | ||
scope.header.sortBy[0] !== '-') { | ||
scope.header.sortBy = '-' + scope.header.sortBy; | ||
} | ||
}; | ||
scope.isSortUp = function(field) { | ||
if (scope.fields[field.key] === undefined) { | ||
return false; | ||
} | ||
return scope.header.sortBy == '-' + scope.fields[field.key].sort; | ||
}; | ||
scope.sortBy = function (field) { | ||
if (scope.notSortBy && scope.notSortBy.indexOf(field.key) >= 0) { | ||
return false; | ||
} | ||
var fieldName; | ||
fieldName = $filter('cleanFieldName')(field.key); | ||
if (scope.header.sortBy == fieldName) { | ||
scope.header.sortBy = '-' + fieldName; | ||
tastyTable.setParams('sortOrder', 'dsc'); | ||
} else { | ||
scope.header.sortBy = fieldName; | ||
tastyTable.setParams('sortOrder', 'asc'); | ||
} | ||
tastyTable.setParams('sortBy', field.key); | ||
}; | ||
scope.isSortDown = function(field) { | ||
if (scope.fields[field.key] === undefined) { | ||
return false; | ||
} | ||
return scope.header.sortBy == scope.fields[field.key].sort; | ||
}; | ||
scope.isSortUp = function(field) { | ||
if (scope.fields[field.key] === undefined) { | ||
return false; | ||
} | ||
return scope.header.sortBy == '-' + scope.fields[field.key].sort; | ||
}; | ||
tastyTable.$scope.$watch('header', function (newValue, oldValue){ | ||
if (newValue && (newValue !== oldValue)) { | ||
scope.header = newValue; | ||
scope.setFields(); | ||
} | ||
}, true); | ||
} | ||
}; | ||
} | ||
]) | ||
scope.isSortDown = function(field) { | ||
if (scope.fields[field.key] === undefined) { | ||
return false; | ||
} | ||
return scope.header.sortBy == scope.fields[field.key].sort; | ||
}; | ||
tastyTable.$scope.$watch('header', function (newValue, oldValue){ | ||
if (newValue && (newValue !== oldValue)) { | ||
scope.header = newValue; | ||
scope.setFields(); | ||
} | ||
}, true); | ||
} | ||
}; | ||
}]) | ||
/** | ||
@@ -491,112 +494,119 @@ * @ngdoc directive | ||
*/ | ||
.directive('tastyPagination', [ | ||
'$filter', | ||
function($filter) { | ||
return { | ||
restrict: 'AE', | ||
require: '^tastyTable', | ||
scope: {}, | ||
templateUrl: 'template/table/pagination.html', | ||
link: function (scope, element, attrs, tastyTable) { | ||
'use strict'; | ||
var getPage, setCount, setPaginationRange, | ||
setPreviousRange, setRemainingRange, | ||
setPaginationRanges; | ||
.controller('TablePaginationController', ["$scope", "$attrs", "tableConfig", function($scope, $attrs, tableConfig) { | ||
if (angular.isDefined($attrs.itemsPerPage)) { | ||
$scope.itemsPerPage = $scope.$parent.$eval($attrs.itemsPerPage); | ||
} | ||
if (angular.isDefined($attrs.listItemsPerPage)) { | ||
$scope.listItemsPerPage = $scope.$parent.$eval($attrs.listItemsPerPage); | ||
} | ||
// Default configs | ||
$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; | ||
// Pagination it's called | ||
tastyTable.activate('pagination'); | ||
// Pagination it's called | ||
tastyTable.activate('pagination'); | ||
/* In the future you will have a way to change | ||
* these values by an isolate optional scope variable, | ||
* more info here https://github.com/angular/angular.js/issues/6404 */ | ||
scope.numPaginations = 5; | ||
scope.pagListCount = [5, 25, 50, 100]; | ||
// Internal variable | ||
scope.pagination = {}; | ||
scope.pagMinRange = 1; | ||
scope.pagMaxRange = 1; | ||
// Internal variable | ||
scope.pagination = {}; | ||
scope.pagMinRange = 1; | ||
scope.pagMaxRange = 1; | ||
getPage = function (numPage) { | ||
tastyTable.setParams('page', numPage); | ||
}; | ||
getPage = function (numPage) { | ||
tastyTable.setParams('page', numPage); | ||
}; | ||
setCount = function(count) { | ||
var maxItems, page; | ||
//scope.pagination.count = count; | ||
maxItems = count * scope.pagination.page; | ||
if (maxItems > scope.pagination.size) { | ||
page = Math.ceil(scope.pagination.size / count); | ||
tastyTable.setParams('page', page); | ||
} | ||
tastyTable.setParams('count', count); | ||
}; | ||
setCount = function(count) { | ||
var maxItems, page; | ||
maxItems = count * scope.pagination.page; | ||
if (maxItems > scope.pagination.size) { | ||
page = Math.ceil(scope.pagination.size / count); | ||
tastyTable.setParams('page', page); | ||
} | ||
tastyTable.setParams('count', count); | ||
}; | ||
setPaginationRange = function () { | ||
var currentPage, totalPages; | ||
currentPage = scope.pagination.page; | ||
//scope.pagination.count = scope.itemsPerPage; | ||
if (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(); | ||
}; | ||
setPaginationRange = function () { | ||
var currentPage, totalPages; | ||
currentPage = scope.pagination.page; | ||
if (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 () { | ||
if (scope.pagHideMinRange === true || scope.pagMinRange < 1) { | ||
return false; | ||
} | ||
scope.pagMaxRange = scope.pagMinRange; | ||
scope.pagMinRange = scope.pagMaxRange - scope.itemsPerPage; | ||
setPaginationRanges(); | ||
}; | ||
setPreviousRange = function () { | ||
if (scope.pagHideMinRange === true || scope.pagMinRange < 1) { | ||
return false; | ||
} | ||
scope.pagMaxRange = scope.pagMinRange; | ||
scope.pagMinRange = scope.pagMaxRange - scope.numPaginations; | ||
setPaginationRanges(); | ||
}; | ||
setRemainingRange = function () { | ||
if (scope.pagHideMaxRange === true || scope.pagMaxRange > scope.pagination.pages) { | ||
return false; | ||
} | ||
scope.pagMinRange = scope.pagMaxRange; | ||
scope.pagMaxRange = scope.pagMinRange + scope.itemsPerPage; | ||
if (scope.pagMaxRange > scope.pagination.pages) { | ||
scope.pagMaxRange = scope.pagination.pages; | ||
} | ||
scope.pagMinRange = scope.pagMaxRange - scope.itemsPerPage; | ||
setPaginationRanges(); | ||
}; | ||
setRemainingRange = function () { | ||
if (scope.pagHideMaxRange === true || scope.pagMaxRange > scope.pagination.pages) { | ||
return false; | ||
setPaginationRanges = function () { | ||
scope.pagMinRange = scope.pagMinRange > 0 ? scope.pagMinRange : 1; | ||
scope.pagMaxRange = scope.pagMinRange + 5; | ||
if (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.pagMinRange = scope.pagMaxRange; | ||
scope.pagMaxRange = scope.pagMinRange + scope.numPaginations; | ||
if (scope.pagMaxRange > scope.pagination.pages) { | ||
scope.pagMaxRange = scope.pagination.pages; | ||
} | ||
scope.pagMinRange = scope.pagMaxRange - scope.numPaginations; | ||
setPaginationRanges(); | ||
}; | ||
} | ||
scope.rangePage = $filter('range')([], scope.pagMinRange, scope.pagMaxRange); | ||
}; | ||
setPaginationRanges = function () { | ||
scope.pagMinRange = scope.pagMinRange > 0 ? scope.pagMinRange : 1; | ||
scope.pagMaxRange = scope.pagMinRange + scope.numPaginations; | ||
if (scope.pagMaxRange > scope.pagination.pages) { | ||
scope.pagMaxRange = scope.pagination.pages + 1; | ||
} | ||
scope.pagHideMinRange = scope.pagMinRange <= 1; | ||
scope.pagHideMaxRange = scope.pagMaxRange >= scope.pagination.pages; | ||
if (scope.pagination.size < 50) { | ||
scope.pagListCount = [5, 25]; | ||
} else if (scope.pagination.size < 100) { | ||
scope.pagListCount = [5, 25, 50]; | ||
} else { | ||
scope.pagListCount = [5, 25, 50, 100]; | ||
} | ||
scope.rangePage = $filter('range')([], scope.pagMinRange, scope.pagMaxRange); | ||
}; | ||
scope.page = { | ||
'get': getPage, | ||
'setCount': setCount, | ||
'previous': setPreviousRange, | ||
'remaining': setRemainingRange | ||
}; | ||
scope.page = { | ||
'get': getPage, | ||
'setCount': setCount, | ||
'previous': setPreviousRange, | ||
'remaining': setRemainingRange | ||
}; | ||
tastyTable.$scope.$watch('pagination', function (newValue, oldValue){ | ||
if (newValue && (newValue !== oldValue)) { | ||
scope.pagination = newValue; | ||
setPaginationRange(); | ||
} | ||
}, true); | ||
tastyTable.$scope.$watch('pagination', function (newValue, oldValue){ | ||
if (newValue && (newValue !== oldValue)) { | ||
scope.pagination = newValue; | ||
setPaginationRange(); | ||
} | ||
}, true); | ||
} | ||
}; | ||
} | ||
]); | ||
// Init Pagination | ||
scope.page.setCount(scope.itemsPerPage); | ||
} | ||
}; | ||
}]); |
@@ -1,1 +0,8 @@ | ||
angular.module("ngTasty",["ngTasty.filter","ngTasty.service","ngTasty.table"]),angular.module("ngTasty.filter",["ngTasty.filter.cleanFieldName","ngTasty.filter.range"]),angular.module("ngTasty.filter.cleanFieldName",[]).filter("cleanFieldName",function(){return function(e){return e.replace(/[^a-zA-Z0-9-]+/g,"-").toLowerCase()}}),angular.module("ngTasty.filter.range",["ngTasty.service.filterInt"]).filter("range",["filterInt",function(e){return function(a,t,n,r){if(t=e(t),n=e(n),r=e(r),isNaN(t)&&(t=0),isNaN(n)&&(n=t,t=0),isNaN(r)&&(r=1),r>0&&t>=n||0>r&&n>=t)return[];for(var i=t;r>0?n>i:i>n;i+=r)a.push(i);return a}}]),angular.module("ngTasty.service",["ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]),angular.module("ngTasty.service.filterInt",[]).factory("filterInt",function(){return function(e){return/^(\-|\+)?([0-9]+|Infinity)$/.test(e)?Number(e):0/0}}),angular.module("ngTasty.service.debounce",[]).factory("debounce",["$timeout",function(e){return function(a,t){var n;return function(){var r=this,i=arguments;e.cancel(n),n=e(function(){n=null,a.apply(r,i)},t)}}}]),angular.module("ngTasty.service.setProperty",[]).factory("setProperty",function(){return function(e,a,t){return"undefined"!=typeof a[t]&&null!==a[t]&&(e[t]=a[t]),e}}),angular.module("ngTasty.service.joinObjects",[]).factory("joinObjects",["setProperty",function(e){return function(a,t){for(var n in t)e(a,t,n);return a}}]),angular.module("ngTasty.table",["ngTasty.filter.cleanFieldName","ngTasty.filter.range","ngTasty.service.debounce","ngTasty.service.setProperty","ngTasty.service.joinObjects"]).constant("tableConfig",{query:{page:"page",count:"count",sortBy:"sort-by",sortOrder:"sort-order"},resource:void 0,resourceCallback:void 0}).controller("TableController",["$scope","$attrs","$timeout","$filter","tableConfig","debounce","setProperty","joinObjects",function(e,a,t,n,r,i,o,s){"use strict";if(this.$scope=e,e.query=r.query,e.resource=r.resource,e.resourceCallback=r.resourceCallback,e.clientSide=!0,e.url="",e.header={columns:[]},e.rows=[],e.params={},e.pagination={count:5,page:1,pages:1,size:1},e.theadDirective=!1,e.paginationDirective=!1,angular.isDefined(a.query)&&(e.query=e.$parent.$eval(a.query)),!angular.isDefined(a.resource)&&!angular.isDefined(a.resourceCallback))throw"AngularJS tastyTable directive: need the resource or resource-callback attribute";if(angular.isDefined(a.resource)){if(e.resource=e.$parent.$eval(a.resource),!angular.isObject(e.resource))throw"AngularJS tastyTable directive: the resource ("+a.resource+") it's not an object";if(!e.resource.header&&!e.resource.rows)throw"AngularJS tastyTable directive: the resource ("+a.resource+") has the property header or rows undefined"}if(angular.isDefined(a.resourceCallback)){if(e.resourceCallback=e.$parent.$eval(a.resourceCallback),!angular.isFunction(e.resourceCallback))throw"AngularJS tastyTable directive: the resource-callback ("+a.resourceCallback+") it's not a function";e.clientSide=!1}this.activate=function(a){e[a+"Directive"]=!0,e.params[a]=!0},this.setParams=function(a,t){e.params[a]=t},e.setDirectivesValues=function(a){return a?(e.header={columns:a.header,sortBy:a.sortBy||e.params.sortBy,sortOrder:a.sortOrder||e.params.sortOrder},e.rows=a.rows,void(e.pagination=a.pagination||e.pagination)):!1},e.buildClientResource=function(){var a,t,r,i;e.theadDirective&&(i="asc"===e.header.sortOrder?!1:!0,e.rows=n("orderBy")(e.rows,e.header.sortBy,i)),e.paginationDirective&&(e.pagination.page=e.params.page,e.pagination.count=e.params.count,e.pagination.size=e.rows.length,e.pagination.pages=Math.ceil(e.rows.length/e.pagination.count),t=e.pagination.count*e.pagination.page,a=t-e.pagination.count,a>=0&&t>=0&&(r=e.rows.slice(a,t),e.rows=r))},e.buildUrl=function(t,n){var r,i;return r={},e.theadDirective&&(r=o(r,t,"sortBy"),r=o(r,t,"sortOrder")),e.paginationDirective&&(r=o(r,t,"page"),r=o(r,t,"count")),a.filters&&(r=s(r,n)),Object.keys(r).map(function(a){return i=r[a],e.query[a]&&(a=e.query[a]),encodeURIComponent(a)+"="+encodeURIComponent(i)}).join("&")},e.updateClientSideResource=i(function(){e.setDirectivesValues(e.resource),e.buildClientResource()},100),e.updateServerSideResource=i(function(){e.url=e.buildUrl(e.params,e[a.filters]),e.resourceCallback(e.url).then(function(a){e.setDirectivesValues(a)})},100),e.initTable=function(){e.params.sortBy=void 0,e.params.sortOrder="asc",e.params.page=1,e.params.count=5,e.clientSide?e.updateClientSideResource():e.updateServerSideResource()},a.filters&&e.$watch(a.filters,function(a,t){a!==t&&e.updateServerSideResource()},!0),e.$watch("params",function(a,t){a!==t&&(e.clientSide?e.updateClientSideResource():e.updateServerSideResource())},!0),e.initTable()}]).directive("tastyTable",function(){return{restrict:"A",scope:!0,controller:"TableController"}}).directive("tastyThead",["$filter",function(e){return{restrict:"AE",require:"^tastyTable",scope:{notSortBy:"="},templateUrl:"template/table/head.html",link:function(a,t,n,r){"use strict";r.activate("thead"),a.fields={},a.setFields=function(){var t,n,r,i;t=a.header.columns.length,a.header.columns.forEach(function(o){n=parseFloat((100/t).toFixed(2)),i=!0,r=!1,a.notSortBy&&(i=a.notSortBy.indexOf(o.key)<0),(o.key===a.header.sortBy||"-"+o.key===a.header.sortBy)&&(r=!0),a.fields[o.key]={active:r,sortable:i,width:{width:n+"%"},sort:e("cleanFieldName")(o.key)}}),"dsc"===a.header.sortOrder&&"-"!==a.header.sortBy[0]&&(a.header.sortBy="-"+a.header.sortBy)},a.sortBy=function(t){if(a.notSortBy&&a.notSortBy.indexOf(t.key)>=0)return!1;var n;n=e("cleanFieldName")(t.key),a.header.sortBy==n?(a.header.sortBy="-"+n,r.setParams("sortOrder","dsc")):(a.header.sortBy=n,r.setParams("sortOrder","asc")),r.setParams("sortBy",t.key)},a.isSortUp=function(e){return void 0===a.fields[e.key]?!1:a.header.sortBy=="-"+a.fields[e.key].sort},a.isSortDown=function(e){return void 0===a.fields[e.key]?!1:a.header.sortBy==a.fields[e.key].sort},r.$scope.$watch("header",function(e,t){e&&e!==t&&(a.header=e,a.setFields())},!0)}}}]).directive("tastyPagination",["$filter",function(e){return{restrict:"AE",require:"^tastyTable",scope:{},templateUrl:"template/table/pagination.html",link:function(a,t,n,r){"use strict";var i,o,s,c,u,g;r.activate("pagination"),a.numPaginations=5,a.pagListCount=[5,25,50,100],a.pagination={},a.pagMinRange=1,a.pagMaxRange=1,i=function(e){r.setParams("page",e)},o=function(e){var t,n;t=e*a.pagination.page,t>a.pagination.size&&(n=Math.ceil(a.pagination.size/e),r.setParams("page",n)),r.setParams("count",e)},s=function(){var e;e=a.pagination.page,e>a.pagination.pages&&(e=a.pagination.pages),a.pagMinRange=e-2>0?e-2:1,a.pagMaxRange=e+2,a.pagination.page=e,g()},c=function(){return a.pagHideMinRange===!0||a.pagMinRange<1?!1:(a.pagMaxRange=a.pagMinRange,a.pagMinRange=a.pagMaxRange-a.numPaginations,void g())},u=function(){return a.pagHideMaxRange===!0||a.pagMaxRange>a.pagination.pages?!1:(a.pagMinRange=a.pagMaxRange,a.pagMaxRange=a.pagMinRange+a.numPaginations,a.pagMaxRange>a.pagination.pages&&(a.pagMaxRange=a.pagination.pages),a.pagMinRange=a.pagMaxRange-a.numPaginations,void g())},g=function(){a.pagMinRange=a.pagMinRange>0?a.pagMinRange:1,a.pagMaxRange=a.pagMinRange+a.numPaginations,a.pagMaxRange>a.pagination.pages&&(a.pagMaxRange=a.pagination.pages+1),a.pagHideMinRange=a.pagMinRange<=1,a.pagHideMaxRange=a.pagMaxRange>=a.pagination.pages,a.pagListCount=a.pagination.size<50?[5,25]:a.pagination.size<100?[5,25,50]:[5,25,50,100],a.rangePage=e("range")([],a.pagMinRange,a.pagMaxRange)},a.page={get:i,setCount:o,previous:c,remaining:u},r.$scope.$watch("pagination",function(e,t){e&&e!==t&&(a.pagination=e,s())},!0)}}}]); | ||
/* | ||
* ng-tasty | ||
* https://github.com/Zizzamia/ng-tasty | ||
* Version: 0.2.5 - 2014-08-21 | ||
* 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)}}}]); |
{ | ||
"name": "ng-tasty", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "A lightweight, flexible, and tasty collection of reusable UI components for AngularJS.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
# #ngTasty [![Bower version](https://badge.fury.io/bo/ng-tasty.svg)](http://badge.fury.io/bo/ng-tasty) [![Build Status](https://secure.travis-ci.org/Zizzamia/ng-tasty.svg)](http://travis-ci.org/Zizzamia/ng-tasty) | ||
# #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) | ||
> A lightweight, flexible, and tasty collection of reusable UI components for [AngularJS](https://angularjs.org/). | ||
@@ -13,3 +13,8 @@ | ||
Include the required library: | ||
or installing Via NPM | ||
``` | ||
npm install ng-tasty | ||
``` | ||
Include the required bower component: | ||
``` html | ||
@@ -26,3 +31,9 @@ <script src="bower_components/ng-tasty/ng-tasty-tpls.min.js"></script> | ||
### v0.2.4 (master, released on August 13th 2014) | ||
### 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 | ||
@@ -35,5 +46,14 @@ - Improved `setDirectivesValues` in `ngTasty.table` | ||
**Leonardo Zizzamia** | ||
Designed and built by Leonardo Zizzamia, like grandma used to make. | ||
- <http://twitter.com/zizzamia> | ||
- <http://github.com/zizzamia> | ||
[bower]: https://github.com/Zizzamia/bower-ng-tasty | ||
[bower-badge]: https://badge.fury.io/bo/ng-tasty.svg | ||
[npm-site]: https://www.npmjs.org/ | ||
[npm]: https://www.npmjs.org/package/ng-tasty | ||
[npm-badge]: https://badge.fury.io/js/ng-tasty.svg | ||
[npm-downloads]: http://img.shields.io/npm/dm/ng-tasty.svg | ||
[travis]: https://travis-ci.org/Zizzamia/ng-tasty | ||
[travis-badge]: https://secure.travis-ci.org/Zizzamia/ng-tasty.svg |
Sorry, the diff of this file is not supported yet
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
65437
1186
56