Skip to content

Commit

Permalink
Allow user to customize path being compared
Browse files Browse the repository at this point in the history
- window.location[this.props.location] allows user to specify, hash,
  pathname, search etc. Defaults to hash still
  • Loading branch information
jdlehman committed Apr 25, 2015
1 parent 1003c14 commit a91e604
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 13 deletions.
2 changes: 1 addition & 1 deletion dist/switcheroo.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 docs/components/Switcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ When true, `Switcher` listens to the `load` event and looks for path changes on

### location (default: 'hash')

By default `window.location.hash` is used to match paths. If `location` is set to 'pathname', then `window.location.pathname` is used instead.
By default `window.location.hash` is used to match paths. If `location` is set to 'pathname', then `window.location.pathname` is used instead. Under the hood, it is using `window.location[this.props.location]`, so you can use `search` or any other valid property on `window.location`.

### defaultHandler

Expand Down
12 changes: 1 addition & 11 deletions modules/components/Switcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export default class Switcher extends Component {
super(props);
// bind methods
this.getLocation = this.getLocation.bind(this);
this.getHashLocation = this.getHashLocation.bind(this);
this.getHistoryLocation = this.getHistoryLocation.bind(this);
this.handleRouteChange = this.handleRouteChange.bind(this);
this.getSwitch = this.getSwitch.bind(this);

Expand Down Expand Up @@ -43,7 +41,7 @@ export default class Switcher extends Component {
}

getLocation() {
var location = this.props.location === 'pathname' ? this.getHistoryLocation() : this.getHashLocation();
var location = decodeURI(window.location[this.props.location].slice(1).split('?')[0]);
if(location.charAt(0) !== '/') {
return `/${location}`;
}
Expand All @@ -52,14 +50,6 @@ export default class Switcher extends Component {
}
}

getHashLocation() {
return decodeURI(window.location.hash.slice(1).split('?')[0]);
}

getHistoryLocation() {
return decodeURI(window.location.pathname);
}

getSwitch(path) {
var children = [].concat(this.props.children);
return children.filter((child) => {
Expand Down

0 comments on commit a91e604

Please sign in to comment.