Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 91 additions & 67 deletions src/paging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,

Expand Down Expand Up @@ -62,9 +59,8 @@ angular.module('bw.paging', []).directive('paging', function () {
textTitleNext: '@',
textTitlePrev: '@'
}

};

};

/**
* Link the directive to enable our scope watch values
Expand All @@ -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 '<ul data-ng-hide="Hide" data-ng-class="ulClass"> ' +
'<li ' +
'title="{{Item.title}}" ' +
'data-ng-class="Item.liClass" ' +
'data-ng-repeat="Item in List"> ' +
'<a ' +
(attrs.pgHref ? 'data-ng-href="{{Item.pgHref}}" ' : 'href ') +
'data-ng-class="Item.aClass" ' +
'data-ng-click="Item.action()" ' +
'data-ng-bind="Item.value">'+
'</a> ' +
'</li>' +
'</ul>'
return '<ul data-ng-hide="Hide" data-ng-class="ulClass"> ' +
'<li ' +
'title="{{Item.title}}" ' +
'data-ng-class="Item.liClass" ' +
'data-ng-repeat="Item in List"> ' +
'<a ' +
(attrs.pgHref ? 'data-ng-href="{{Item.pgHref}}" ' : 'href ') +
'data-ng-class="Item.aClass" ' +
'data-ng-click="Item.action()" ' +
'data-ng-bind="Item.value">'+
'</a> ' +
'</li>' +
'</ul>'
}


/**
* Assign default scope values from settings
* Feel free to tweak / fork these for your application
Expand All @@ -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';
Expand All @@ -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);
Expand All @@ -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
*/
Expand All @@ -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
Expand Down Expand Up @@ -198,7 +190,6 @@ angular.module('bw.paging', []).directive('paging', function () {
}
}


/**
* Assign the method action to take when a page is clicked
*
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -319,7 +343,7 @@ angular.module('bw.paging', []).directive('paging', function () {
internalAction(scope, item.page);
}
}
};
};
};

// Force disabled if specified
Expand All @@ -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
Expand All @@ -350,21 +374,20 @@ 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){
pgHref = '';
liClass = scope.disabledClass;
}


scope.List.push({
value: i,
title: scope.textTitlePage.replace(regex, i),
Expand All @@ -375,8 +398,8 @@ angular.module('bw.paging', []).directive('paging', function () {
}
});
}
}

}

/**
* Add Dots ie: 1 2 [...] 10 11 12 [...] 56 57
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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');

}

});