|
| 1 | +var ActiveStore = require('../stores/ActiveStore'); |
| 2 | + |
| 3 | +function routeIsActive(activeRoutes, routeName) { |
| 4 | + return activeRoutes.some(function (route) { |
| 5 | + return route.props.name === routeName; |
| 6 | + }); |
| 7 | +} |
| 8 | + |
| 9 | +function paramsAreActive(activeParams, params) { |
| 10 | + for (var property in params) { |
| 11 | + if (activeParams[property] !== String(params[property])) |
| 12 | + return false; |
| 13 | + } |
| 14 | + |
| 15 | + return true; |
| 16 | +} |
| 17 | + |
| 18 | +function queryIsActive(activeQuery, query) { |
| 19 | + for (var property in query) { |
| 20 | + if (activeQuery[property] !== String(query[property])) |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + return true; |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * A mixin for components that need to know about the routes, params, |
| 29 | + * and query that are currently active. Components that use it get two |
| 30 | + * things: |
| 31 | + * |
| 32 | + * 1. An `isActive` static method they can use to check if a route, |
| 33 | + * params, and query are active. |
| 34 | + * 2. An `updateActiveState` instance method that is called when the |
| 35 | + * active state changes. |
| 36 | + * |
| 37 | + * Example: |
| 38 | + * |
| 39 | + * var Tab = React.createClass({ |
| 40 | + * |
| 41 | + * mixins: [ Router.ActiveState ], |
| 42 | + * |
| 43 | + * getInitialState: function () { |
| 44 | + * return { |
| 45 | + * isActive: false |
| 46 | + * }; |
| 47 | + * }, |
| 48 | + * |
| 49 | + * updateActiveState: function () { |
| 50 | + * this.setState({ |
| 51 | + * isActive: Tab.isActive(routeName, params, query) |
| 52 | + * }) |
| 53 | + * } |
| 54 | + * |
| 55 | + * }); |
| 56 | + */ |
| 57 | +var ActiveState = { |
| 58 | + |
| 59 | + statics: { |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns true if the route with the given name, URL parameters, and query |
| 63 | + * are all currently active. |
| 64 | + */ |
| 65 | + isActive: function (routeName, params, query) { |
| 66 | + var state = ActiveStore.getState(); |
| 67 | + var isActive = routeIsActive(state.routes, routeName) && paramsAreActive(state.params, params); |
| 68 | + |
| 69 | + if (query) |
| 70 | + return isActive && queryIsActive(state.query, query); |
| 71 | + |
| 72 | + return isActive; |
| 73 | + } |
| 74 | + |
| 75 | + }, |
| 76 | + |
| 77 | + componentWillMount: function () { |
| 78 | + ActiveStore.addChangeListener(this.handleActiveStateChange); |
| 79 | + }, |
| 80 | + |
| 81 | + componentDidMount: function () { |
| 82 | + if (this.updateActiveState) |
| 83 | + this.updateActiveState(); |
| 84 | + }, |
| 85 | + |
| 86 | + componentWillUnmount: function () { |
| 87 | + ActiveStore.removeChangeListener(this.handleActiveStateChange); |
| 88 | + }, |
| 89 | + |
| 90 | + handleActiveStateChange: function () { |
| 91 | + if (this.isMounted() && this.updateActiveState) |
| 92 | + this.updateActiveState(); |
| 93 | + } |
| 94 | + |
| 95 | +}; |
| 96 | + |
| 97 | +module.exports = ActiveState; |
0 commit comments