Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for ignoring paths #389

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .npm/package/npm-shrinkwrap.json

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

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,23 @@ FlowRouter.notFound = {
};
~~~

## Ignoring Links

If you want that some links are not handled by FlowRouter but that a normal HTTP request is made to the server
(for example, to link to a static file/asset), you can register path to be ignored:

~~~js
FlowRouter.ignore('/storage');
~~~~

This will ignore all same-origin links which start with `/storage`. You can also pass an regexp instead.

Additionally, links are ignored by FlowRouter if:
* Links are not of the same origin.
* Links have the `download` attribute.
* Links have the `target` attribute.
* Links have the `rel="external"` attribute.

## API

FlowRouter has a rich API to help you to navigate the router and reactively get information from the router.
Expand Down
57 changes: 57 additions & 0 deletions client/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Router = function () {
this._triggersExit = [];
this._routes = [];
this._routesMap = {};
this._ignoredPaths = [];
this._updateCallbacks();
this.notFound = this.notfound = null;
// indicate it's okay (or not okay) to run the tracker
Expand Down Expand Up @@ -184,6 +185,52 @@ Router.prototype.path = function(pathDef, fields, queryParams) {
return path;
};

Router.prototype.ignore = function(path) {
this._ignoredPaths.push(path);
};

Router.prototype._isIgnored = function(path) {
return _.some(this._ignoredPaths, function(ignoredPath) {
if (_.isRegExp(ignoredPath)) {
if (ignoredPath.test(path)) return true;
}
else {
// does path starts with ignoredPath
if (path.lastIndexOf(ignoredPath, 0) === 0) return true;
}
return false;
});
};

Router.prototype._getPath = function(event) {
function which(e) {
e = e || window.event;
return null === e.which ? e.button : e.which;
}

if (1 !== which(event)) return;

if (event.metaKey || event.ctrlKey || event.shiftKey) return;
if (event.defaultPrevented) return;

// ensure link
var el = event.target;
while (el && 'A' !== el.nodeName) el = el.parentNode;
if (!el || 'A' !== el.nodeName) return;

// check target
if (el.target) return;

// x-origin
if (!page.sameOrigin(el.href)) return;

// fix for IE9 for not having leading slash (issue #259)
var pathname = el.pathname;
pathname = pathname[0] !== "/" ? "/" + pathname : pathname;

return pathname;
};

Router.prototype.go = function(pathDef, fields, queryParams) {
var path = this.path(pathDef, fields, queryParams);

Expand Down Expand Up @@ -383,6 +430,16 @@ Router.prototype.initialize = function(options) {
};
});

var originalOnClick = self._page.onclick;
self._page.onclick = function(event) {
var path = self._getPath(event);
if (path && self._isIgnored(path)) {
return;
}

originalOnClick.call(this, event);
};

// this is very ugly part of pagejs and it does decoding few times
// in unpredicatable manner. See #168
// this is the default behaviour and we need keep it like that
Expand Down
3 changes: 2 additions & 1 deletion package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Package.describe({
Npm.depends({
// In order to support IE9, we had to fork pagejs and apply
// this PR: https://github.com/visionmedia/page.js/pull/288
'page':'https://github.com/kadirahq/page.js/archive/f29d4d4491178b285b9058c32d74975a4f945dea.tar.gz',
// And this PR: https://github.com/visionmedia/page.js/pull/331
'page':'https://github.com/peerlibrary/page.js/archive/b90fb4b32f55aba71bb374c946130d3ed82a9d1f.tar.gz',
'qs':'3.1.0'
});

Expand Down