diff --git a/src/paging.js b/src/paging.js index 249d7c2..d6deed1 100644 --- a/src/paging.js +++ b/src/paging.js @@ -8,17 +8,14 @@ * information. * * @element EA - * */ angular.module('bw.paging', []).directive('paging', function () { - /** * The regex expression to use for any replace methods * Feel free to tweak / fork values for your application */ var regex = /\{page\}/g; - /** * The angular return value required for the directive @@ -31,7 +28,7 @@ angular.module('bw.paging', []).directive('paging', function () { // Assign the angular link function link: fieldLink, - + // Assign the angular directive template HTML template: fieldTemplate, @@ -62,9 +59,8 @@ angular.module('bw.paging', []).directive('paging', function () { textTitleNext: '@', textTitlePrev: '@' } - - }; + }; /** * Link the directive to enable our scope watch values @@ -80,32 +76,30 @@ angular.module('bw.paging', []).directive('paging', function () { build(scope, attrs); }); } - - + /** - * Create our template html + * Create our template html * We use a function to figure out how to handle href correctly - * + * * @param {object} el - Angular link element * @param {object} attrs - Angular link attribute */ function fieldTemplate(el, attrs){ - return '' + return '' } - /** * Assign default scope values from settings * Feel free to tweak / fork these for your application @@ -117,14 +111,14 @@ angular.module('bw.paging', []).directive('paging', function () { scope.List = []; scope.Hide = false; - + scope.page = parseInt(scope.page) || 1; scope.total = parseInt(scope.total) || 0; scope.adjacent = parseInt(scope.adjacent) || 2; scope.pgHref = scope.pgHref || ''; scope.dots = scope.dots || '...'; - + scope.ulClass = scope.ulClass || 'pagination'; scope.activeClass = scope.activeClass || 'active'; scope.disabledClass = scope.disabledClass || 'disabled'; @@ -133,17 +127,17 @@ angular.module('bw.paging', []).directive('paging', function () { scope.textLast = scope.textLast || '>>'; scope.textNext = scope.textNext || '>'; scope.textPrev = scope.textPrev || '<'; - + scope.textFirstClass = scope.textFirstClass || ''; scope.textLastClass= scope.textLastClass || ''; scope.textNextClass = scope.textNextClass || ''; scope.textPrevClass = scope.textPrevClass || ''; scope.textTitlePage = scope.textTitlePage || 'Page {page}'; - scope.textTitleFirst = scope.textTitleFirst || 'First Page'; - scope.textTitleLast = scope.textTitleLast || 'Last Page'; - scope.textTitleNext = scope.textTitleNext || 'Next Page'; - scope.textTitlePrev = scope.textTitlePrev || 'Previous Page'; + scope.textTitleFirst = scope.textTitleFirst || 'First Page'; + scope.textTitleLast = scope.textTitleLast || 'Last Page'; + scope.textTitleNext = scope.textTitleNext || 'Next Page'; + scope.textTitlePrev = scope.textTitlePrev || 'Previous Page'; scope.hideIfEmpty = evalBoolAttribute(scope, attrs.hideIfEmpty); scope.showPrevNext = evalBoolAttribute(scope, attrs.showPrevNext); @@ -152,11 +146,10 @@ angular.module('bw.paging', []).directive('paging', function () { scope.isDisabled = evalBoolAttribute(scope, attrs.disabled); } - /** * A helper to perform our boolean eval on attributes * This allows flexibility in the attribute for strings and variables in scope - * + * * @param {Object} scope - The local directive scope object * @param {Object} value - The attribute value of interest */ @@ -166,7 +159,6 @@ angular.module('bw.paging', []).directive('paging', function () { : false; } - /** * Validate and clean up any scope values * This happens after we have set the scope values @@ -198,7 +190,6 @@ angular.module('bw.paging', []).directive('paging', function () { } } - /** * Assign the method action to take when a page is clicked * @@ -212,28 +203,61 @@ angular.module('bw.paging', []).directive('paging', function () { return; } - // Block if we are forcing disabled - if(scope.isDisabled) - { + // Block if we are forcing disabled + if(scope.isDisabled) { return; } - // Update the page in scope - scope.page = page; - // Pass our parameters to the paging action - scope.pagingAction({ - page: scope.page, + var paging = scope.pagingAction({ + page: page, pageSize: scope.pageSize, total: scope.total }); + // Update the page in scope when resolved promise + if (isResolve(paging)) { + paging.then(function () { + scope.page = page; + + scrollPage(scope); + }); + } + + // Update the page in scope + else { + scope.page = page; + + scrollPage(scope); + } + + } + + /** + * Checks if the page can be resolved + * + * @param {mixed} paging + */ + function isResolve(paging) { + + return angular.isObject(paging) && paging.then; + + } + + /** + * An auxiliary to scroll the page to the top, + * this allows us reuses it in other places + * + * @param {Object} scope - The local directive scope object + */ + function scrollPage(scope) { + // If allowed scroll up to the top of the page if (scope.scrollTop) { scrollTo(0, 0); } - } + } /** * Add the first, previous, next, and last buttons if desired @@ -266,19 +290,19 @@ angular.module('bw.paging', []).directive('paging', function () { if(scope.showFirstLast){ alpha = { value: scope.textFirst, - title: scope.textTitleFirst, + title: scope.textTitleFirst, aClass: scope.textFirstClass, page: 1 - }; + }; } if(scope.showPrevNext){ beta = { value: scope.textPrev, - title: scope.textTitlePrev, + title: scope.textTitlePrev, aClass: scope.textPrevClass, page: prevPage - }; + }; } } else { @@ -289,21 +313,21 @@ angular.module('bw.paging', []).directive('paging', function () { if(scope.showPrevNext){ alpha = { value: scope.textNext, - title: scope.textTitleNext, + title: scope.textTitleNext, aClass: scope.textNextClass, page: nextPage - }; + }; } - + if(scope.showFirstLast){ beta = { value: scope.textLast, - title: scope.textTitleLast, + title: scope.textTitleLast, aClass: scope.textLastClass, page: pageCount - }; + }; } - + } // Create the Add Item Function @@ -319,7 +343,7 @@ angular.module('bw.paging', []).directive('paging', function () { internalAction(scope, item.page); } } - }; + }; }; // Force disabled if specified @@ -330,16 +354,16 @@ angular.module('bw.paging', []).directive('paging', function () { // Add alpha items if(alpha){ var alphaItem = buildItem(alpha, disabled); - scope.List.push(alphaItem); + scope.List.push(alphaItem); } - + // Add beta items if(beta){ var betaItem = buildItem(beta, disabled); - scope.List.push(betaItem); + scope.List.push(betaItem); } - } + } /** * Adds a range of numbers to our list @@ -350,13 +374,13 @@ angular.module('bw.paging', []).directive('paging', function () { * @param {Object} scope - The local directive scope object */ function addRange(start, finish, scope) { - + // Add our items where i is the page number var i = 0; for (i = start; i <= finish; i++) { var pgHref = scope.pgHref.replace(regex, i); - var liClass = scope.page == i ? scope.activeClass : ''; + var liClass = scope.page == i ? scope.activeClass : ''; // Handle items that are affected by disabled if(scope.isDisabled){ @@ -364,7 +388,6 @@ angular.module('bw.paging', []).directive('paging', function () { liClass = scope.disabledClass; } - scope.List.push({ value: i, title: scope.textTitlePage.replace(regex, i), @@ -375,8 +398,8 @@ angular.module('bw.paging', []).directive('paging', function () { } }); } - } + } /** * Add Dots ie: 1 2 [...] 10 11 12 [...] 56 57 @@ -385,12 +408,13 @@ angular.module('bw.paging', []).directive('paging', function () { * @param {Object} scope - The local directive scope object */ function addDots(scope) { + scope.List.push({ value: scope.dots, liClass: scope.disabledClass }); - } + } /** * Add the first or beginning items in our paging list @@ -408,8 +432,8 @@ angular.module('bw.paging', []).directive('paging', function () { if (next != 3) { addDots(scope); } - } + } /** * Add the last or end items in our paging list @@ -429,9 +453,8 @@ angular.module('bw.paging', []).directive('paging', function () { } addRange(pageCount - 1, pageCount, scope); - } - + } /** * The main build function used to determine the paging logic @@ -514,6 +537,7 @@ angular.module('bw.paging', []).directive('paging', function () { // Add the next and last buttons to our paging list addPrevNext(scope, pageCount, 'next'); + } });