From ff7771b15801d398bde8baf1dfd16f7fb534b95e Mon Sep 17 00:00:00 2001 From: Anterio Vieira Date: Tue, 9 Aug 2016 08:58:18 -0300 Subject: [PATCH 1/6] Checks if the page can be resolved --- src/paging.js | 104 +++++++++++++++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/src/paging.js b/src/paging.js index 249d7c2..73daeb3 100644 --- a/src/paging.js +++ b/src/paging.js @@ -18,7 +18,7 @@ angular.module('bw.paging', []).directive('paging', function () { * Feel free to tweak / fork values for your application */ var regex = /\{page\}/g; - + /** * The angular return value required for the directive @@ -31,7 +31,7 @@ angular.module('bw.paging', []).directive('paging', function () { // Assign the angular link function link: fieldLink, - + // Assign the angular directive template HTML template: fieldTemplate, @@ -62,7 +62,7 @@ angular.module('bw.paging', []).directive('paging', function () { textTitleNext: '@', textTitlePrev: '@' } - + }; @@ -80,12 +80,12 @@ 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 */ @@ -95,14 +95,14 @@ angular.module('bw.paging', []).directive('paging', function () { 'title="{{Item.title}}" ' + 'data-ng-class="Item.liClass" ' + 'data-ng-repeat="Item in List"> ' + - ''+ + 'data-ng-bind="Item.value">'+ ' ' + '' + - '' + '' } @@ -117,14 +117,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 +133,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); @@ -156,7 +156,7 @@ angular.module('bw.paging', []).directive('paging', function () { /** * 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 */ @@ -198,6 +198,24 @@ angular.module('bw.paging', []).directive('paging', function () { } } + /** + * Checks if the page can be resolved + * + * @param {mixed} paging + */ + function isResolve(paging) { + return angular.isObject(paging) && paging.then; + } + + /** + * Scroll up to the top of the page + */ + function scrollTop() { + // If allowed scroll up to the top of the page + if (scope.scrollTop) { + scrollTo(0, 0); + } + } /** * Assign the method action to take when a page is clicked @@ -212,7 +230,7 @@ angular.module('bw.paging', []).directive('paging', function () { return; } - // Block if we are forcing disabled + // Block if we are forcing disabled if(scope.isDisabled) { return; @@ -222,16 +240,24 @@ angular.module('bw.paging', []).directive('paging', function () { scope.page = page; // Pass our parameters to the paging action - scope.pagingAction({ + var paging scope.pagingAction({ page: scope.page, pageSize: scope.pageSize, total: scope.total }); - // If allowed scroll up to the top of the page - if (scope.scrollTop) { - scrollTo(0, 0); + // Update the page in scope when resolved promise + if (isResolve(paging)) { + return paging.then(function () { + scope.page = page; + scrollTop(); + }); + } else { + scope.page = page; + scrollTop(); } + + } @@ -266,19 +292,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 +315,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 +345,7 @@ angular.module('bw.paging', []).directive('paging', function () { internalAction(scope, item.page); } } - }; + }; }; // Force disabled if specified @@ -330,13 +356,13 @@ 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); } } @@ -350,13 +376,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){ From 0a5b2a4bcfceda1a72cedce2174fdb4b64fb8901 Mon Sep 17 00:00:00 2001 From: Anterio Vieira Date: Tue, 9 Aug 2016 09:02:55 -0300 Subject: [PATCH 2/6] Fix method name --- src/paging.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/paging.js b/src/paging.js index 73daeb3..fd0286a 100644 --- a/src/paging.js +++ b/src/paging.js @@ -210,7 +210,7 @@ angular.module('bw.paging', []).directive('paging', function () { /** * Scroll up to the top of the page */ - function scrollTop() { + function scrollUpTop() { // If allowed scroll up to the top of the page if (scope.scrollTop) { scrollTo(0, 0); @@ -231,8 +231,7 @@ angular.module('bw.paging', []).directive('paging', function () { } // Block if we are forcing disabled - if(scope.isDisabled) - { + if(scope.isDisabled) { return; } @@ -250,11 +249,14 @@ angular.module('bw.paging', []).directive('paging', function () { if (isResolve(paging)) { return paging.then(function () { scope.page = page; - scrollTop(); + scrollUpTop(); }); - } else { + } + + // Update the page in scope + else { scope.page = page; - scrollTop(); + scrollUpTop(); } From 4debd2b81e343b9f2e83ae85dc0f9043be9536c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=A9rio=20vieira=20da=20silva?= Date: Sat, 13 Aug 2016 13:42:13 -0300 Subject: [PATCH 3/6] Fix on the declaration of variable --- src/paging.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/paging.js b/src/paging.js index fd0286a..cfe6516 100644 --- a/src/paging.js +++ b/src/paging.js @@ -239,7 +239,7 @@ angular.module('bw.paging', []).directive('paging', function () { scope.page = page; // Pass our parameters to the paging action - var paging scope.pagingAction({ + var paging = scope.pagingAction({ page: scope.page, pageSize: scope.pageSize, total: scope.total From 137a941ea8352313fc855e2d97cdc7fdba0c67ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=A9rio=20vieira=20da=20silva?= Date: Sat, 13 Aug 2016 14:03:05 -0300 Subject: [PATCH 4/6] Fix method scroll page --- src/paging.js | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/paging.js b/src/paging.js index cfe6516..5aa1348 100644 --- a/src/paging.js +++ b/src/paging.js @@ -198,24 +198,7 @@ angular.module('bw.paging', []).directive('paging', function () { } } - /** - * Checks if the page can be resolved - * - * @param {mixed} paging - */ - function isResolve(paging) { - return angular.isObject(paging) && paging.then; - } - /** - * Scroll up to the top of the page - */ - function scrollUpTop() { - // If allowed scroll up to the top of the page - if (scope.scrollTop) { - scrollTo(0, 0); - } - } /** * Assign the method action to take when a page is clicked @@ -249,19 +232,39 @@ angular.module('bw.paging', []).directive('paging', function () { if (isResolve(paging)) { return paging.then(function () { scope.page = page; - scrollUpTop(); + scrollUpTop(scope); }); } // Update the page in scope else { scope.page = page; - scrollUpTop(); + scrollUpTop(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 scrollUpTop(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 From 421f964eed164203d5f462433316aed8dc522704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=A9rio=20vieira=20da=20silva?= Date: Sat, 13 Aug 2016 14:05:02 -0300 Subject: [PATCH 5/6] Fix method scroll page --- src/paging.js | 60 +++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/src/paging.js b/src/paging.js index 5aa1348..01b29e9 100644 --- a/src/paging.js +++ b/src/paging.js @@ -8,18 +8,15 @@ * 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 * Feel free to tweak / fork values for your application @@ -65,7 +62,6 @@ angular.module('bw.paging', []).directive('paging', function () { }; - /** * Link the directive to enable our scope watch values * @@ -81,7 +77,6 @@ angular.module('bw.paging', []).directive('paging', function () { }); } - /** * Create our template html * We use a function to figure out how to handle href correctly @@ -90,22 +85,21 @@ angular.module('bw.paging', []).directive('paging', function () { * @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 @@ -152,7 +146,6 @@ 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 @@ -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,8 +190,6 @@ angular.module('bw.paging', []).directive('paging', function () { } } - - /** * Assign the method action to take when a page is clicked * @@ -232,14 +222,14 @@ angular.module('bw.paging', []).directive('paging', function () { if (isResolve(paging)) { return paging.then(function () { scope.page = page; - scrollUpTop(scope); + scrollPage(scope); }); } // Update the page in scope else { scope.page = page; - scrollUpTop(scope); + scrollPage(scope); } } @@ -250,7 +240,9 @@ angular.module('bw.paging', []).directive('paging', function () { * @param {mixed} paging */ function isResolve(paging) { + return angular.isObject(paging) && paging.then; + } /** @@ -259,11 +251,13 @@ angular.module('bw.paging', []).directive('paging', function () { * * @param {Object} scope - The local directive scope object */ - function scrollUpTop(scope) { + function scrollPage(scope) { + // If allowed scroll up to the top of the page if (scope.scrollTop) { scrollTo(0, 0); } + } /** @@ -369,8 +363,8 @@ angular.module('bw.paging', []).directive('paging', function () { var betaItem = buildItem(beta, disabled); scope.List.push(betaItem); } - } + } /** * Adds a range of numbers to our list @@ -395,7 +389,6 @@ angular.module('bw.paging', []).directive('paging', function () { liClass = scope.disabledClass; } - scope.List.push({ value: i, title: scope.textTitlePage.replace(regex, i), @@ -406,8 +399,8 @@ angular.module('bw.paging', []).directive('paging', function () { } }); } - } + } /** * Add Dots ie: 1 2 [...] 10 11 12 [...] 56 57 @@ -416,12 +409,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 @@ -439,8 +433,8 @@ angular.module('bw.paging', []).directive('paging', function () { if (next != 3) { addDots(scope); } - } + } /** * Add the last or end items in our paging list @@ -460,9 +454,8 @@ angular.module('bw.paging', []).directive('paging', function () { } addRange(pageCount - 1, pageCount, scope); - } - + } /** * The main build function used to determine the paging logic @@ -545,6 +538,7 @@ angular.module('bw.paging', []).directive('paging', function () { // Add the next and last buttons to our paging list addPrevNext(scope, pageCount, 'next'); + } }); From d714e3de39528d093d0e02f7f424f02cc298d663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ant=C3=A9rio=20vieira=20da=20silva?= Date: Sat, 13 Aug 2016 16:36:06 -0300 Subject: [PATCH 6/6] Some minor adjustments --- src/paging.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/paging.js b/src/paging.js index 01b29e9..d6deed1 100644 --- a/src/paging.js +++ b/src/paging.js @@ -208,20 +208,18 @@ angular.module('bw.paging', []).directive('paging', function () { return; } - // Update the page in scope - scope.page = page; - // Pass our parameters to the paging action var paging = scope.pagingAction({ - page: scope.page, + page: page, pageSize: scope.pageSize, total: scope.total }); // Update the page in scope when resolved promise if (isResolve(paging)) { - return paging.then(function () { + paging.then(function () { scope.page = page; + scrollPage(scope); }); } @@ -229,6 +227,7 @@ angular.module('bw.paging', []).directive('paging', function () { // Update the page in scope else { scope.page = page; + scrollPage(scope); }