Skip to content

Commit

Permalink
Adding getLinkPath function #120
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir Tsonev committed Apr 25, 2017
1 parent a8831c7 commit c54d0eb
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.7.0

* Adding `getLinkPath` function to cover [#120](https://github.com/krasimir/navigo/issues/120)

## 4.6.2

* Fixing a bug in IE9 [#110](https://github.com/krasimir/navigo/pull/110)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ You may provide hooks in two other cases:
* `router.updatePageLinks()` - it triggers the `data-navigo` links binding process
* `router.notFound(function)` - adding a handler for not-found URL (404 page)
* `router.lastRouteResolved()` - returns an object with the format of `{ url: <string>, query: <string> }` matching the latest resolved route
* `router.getLinkPath` - you may overwrite that function to provide a different mechanism for fetching paths from links which are currently on the page (with `data-navigo` attribute)

There are couple of static properties. You'll probably never need to touch them but here're they:

Expand Down
5 changes: 4 additions & 1 deletion lib/navigo.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/navigo.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/navigo.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "navigo",
"version": "4.6.2",
"version": "4.7.0",
"description": "A simple vanilla JavaScript router with a fallback for older browsers",
"main": "lib/navigo.js",
"jsnext:main": "src/index.js",
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ Navigo.prototype = {
this._findLinks().forEach(link => {
if (!link.hasListenerAttached) {
link.addEventListener('click', function (e) {
var location = link.pathname;
var location = self.getLinkPath(link);

if (!self._destroyed) {
e.preventDefault();
Expand Down Expand Up @@ -344,6 +344,9 @@ Navigo.prototype = {
lastRouteResolved() {
return this._lastRouteResolved;
},
getLinkPath(link) {
return link.pathname || link.getAttribute('href');
},
_add: function (route, handler = null, hooks = null) {
if (typeof route === 'string') {
route = encodeURI(route);
Expand Down
26 changes: 26 additions & 0 deletions test/spec/InBrowser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,30 @@ describe('Given the Navigo library on the page', function () {
.and.to.be.calledWith('answer=42');
});
});
describe('and the problem described in #120', function () {
it('should use a custom function for fetching link\'s url', function () {
var event = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true
});
var link = document.createElement('LINK');
var body = document.querySelector('body');

link.setAttribute('id', 'myLink');
link.setAttribute('href', 'overview/getting-started');
link.setAttribute('data-custom-url', 'foo/bar');
link.setAttribute('data-navigo', 'yes');
body.appendChild(link);

router = new Navigo(null, true);
router.getLinkPath = function (link) {
return link.getAttribute('data-custom-url');
};
link.dispatchEvent(event);

expect(window.location.href.split('#')[1]).to.be.equal('foo/bar');
body.removeChild(link);
});
});
});

0 comments on commit c54d0eb

Please sign in to comment.