Created
November 13, 2014 22:11
-
-
Save jamesknelson/9fb4c00502f50f85ec42 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| equal = require('deep-equal') | |
| React = require('react') | |
| Reflux = require('reflux') | |
| routes = require('../routes') | |
| RouteStore = require('../stores/RouteStore') | |
| isLeftClickEvent = (event) -> | |
| event.button == 0 | |
| isModifiedEvent = (event) -> | |
| !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey) | |
| module.exports = React.createClass | |
| mixins: [ | |
| Reflux.listenTo(RouteStore, "onRouteChange") | |
| ] | |
| propTypes: | |
| activeClassName: React.PropTypes.string.isRequired | |
| to: React.PropTypes.string.isRequired | |
| params: React.PropTypes.object | |
| query: React.PropTypes.object | |
| getDefaultProps: -> | |
| activeClassName: 'active' | |
| getInitialState: -> | |
| currentRoute: RouteStore.getCurrentRoute() | |
| onRouteChange: -> | |
| @setState(@getInitialState()) | |
| isActive: -> | |
| stateParts = @state.currentRoute.name.split('.') | |
| propsParts = @props.to.split('.') | |
| equal(stateParts.slice(0, propsParts.length), propsParts) and | |
| ([email protected] or equal(@state.currentRoute.params, @props.params)) | |
| render: -> | |
| activeClassName = if @isActive() then @props.activeClassName+' ' else '' | |
| linkOptions = Object.assign {}, @props, | |
| href: '#'+routes.makePath(@props.to, @props.params) | |
| className: 'Link ' + activeClassName + (@props.className ? '') | |
| React.DOM.a(linkOptions, @props.children); |
This file contains hidden or 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
| Reflux = require('reflux') | |
| module.exports = | |
| navigate: Reflux.createAction() |
This file contains hidden or 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
| Router = require('routr') | |
| paths = | |
| 'login': "/login" | |
| 'resetPassword': "/reset-password" | |
| 'contacts': "/contacts" | |
| 'contacts.add': "/contacts/new" | |
| 'contacts.importFromFacebook': "/contacts/import/facebook" | |
| 'contacts.importFromLinkedin': "/contacts/import/linkedin" | |
| 'contacts.details': "/contacts/:contactSlug" | |
| 'settings': "/settings" | |
| 'settings.accountDetails': "/settings/account-details" | |
| 'settings.linkedAccounts': "/settings/linked-accounts" | |
| 'settings.invoices': "/settings/invoices" | |
| 'settings.invoices.details': "/settings/invoices/:invoiceId" | |
| routes = {} | |
| for name, path of paths | |
| routes[name] = | |
| path: path | |
| method: "get" | |
| module.exports = new Router routes |
This file contains hidden or 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
| Reflux = require('reflux') | |
| routes = require('../routes') | |
| RouteActions = require('../actions/RouteActions') | |
| getLocation = -> | |
| window.location.hash.substr(1) | |
| getCurrentRoute = -> | |
| routes.getRoute(getLocation()) ? {name: '404'} | |
| ensureSlash = -> | |
| path = getLocation() | |
| if path.charAt(0) == '/' | |
| true | |
| else | |
| windowPath = window.location.pathname + window.location.search | |
| window.location.replace(windowPath + '#/' + path) | |
| false | |
| onHashChange = -> | |
| if ensureSlash() | |
| RouteActions.navigate.trigger(getCurrentRoute()) | |
| # | |
| # Router state | |
| # | |
| # Make sure the path is correct before setting the intial route | |
| ensureSlash() | |
| # Set initial route | |
| _currentRoute = getCurrentRoute() | |
| RouteStore = Reflux.createStore | |
| listenables: RouteActions | |
| onNavigate: (route) -> | |
| _currentRoute = route | |
| @trigger() | |
| getCurrentRoute: -> | |
| _currentRoute | |
| # | |
| # Listen for changes | |
| # | |
| if window.addEventListener | |
| window.addEventListener('hashchange', onHashChange, false) | |
| else | |
| window.attachEvent('onhashchange', onHashChange) | |
| module.exports = RouteStore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment