-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pagination): Introduce pagination-href directive
Pagination href-directive allows us to execute an arbitrary function for generating pagination hyperlinks.
- Loading branch information
Showing
3 changed files
with
54 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 31 additions & 3 deletions
34
src/components/directives/angular-ui/bootstrap/pagination/pagination.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,48 @@ | ||
angular.module( | ||
"bawApp.directives.ui.bootstrap.pagination", | ||
[]) | ||
["ui.bootstrap.pagination"]) | ||
.run([ | ||
"$templateCache", | ||
function($templateCache) { | ||
// add ng-href and remove ng-click | ||
const | ||
targetTemplate = "uib/template/pagination/pagination.html", | ||
pageRegex = /(href).*(?:ng-click="selectPage\(([^,]+), \$event\)")/gm, | ||
replaceString = `ng-href="{{ $parent.$parent.getPaginationLink($2) }}" href`; | ||
replaceString = `ng-href="{{ pagination.href($2) }}" href`; | ||
|
||
var oldTemplate = $templateCache.get(targetTemplate); | ||
|
||
var newTemplate = oldTemplate.replace(pageRegex, replaceString); | ||
|
||
$templateCache.put(targetTemplate, newTemplate); | ||
}]); | ||
}]) | ||
.directive("paginationHref", ["$parse", function($parse) { | ||
return { | ||
require: ["uibPagination"], | ||
controller: "UibPaginationController", | ||
controllerAs: "pagination", | ||
replace: true, | ||
link: function(scope, element, attrs, ctrls) { | ||
var paginationCtrl = ctrls[0]; | ||
let parentScope = scope; | ||
|
||
// this is dodgy AF but its the only way i can think of to get | ||
// the instance for which the actual expression is attached! | ||
let parts = attrs.paginationHref.split("."); | ||
let parent = parentScope; | ||
if (parts.length > 1) { | ||
parts.splice(-1, 1); | ||
let parentExpression = parts.join("."); | ||
parent = $parse(parentExpression)(parentScope); | ||
} | ||
|
||
let f = $parse(attrs.paginationHref)(parentScope); | ||
paginationCtrl.href = function (...args) { | ||
return f.apply(parent, args); | ||
}; | ||
} | ||
}; | ||
}]); | ||
|
||
|
||
|