UNPKG

react-crossroads

Version:

Client side router for web applications built with React and utilizing the Flux architecture. The backing routing engine is CrossroadsJs.

91 lines (80 loc) 2.38 kB
var ExecutionEnvironment, HistoryLocation, getWindowPath, invariant, join, _initialPath, _initialPathRgx, _isSupported, _onChange; invariant = require('react/lib/invariant'); getWindowPath = require('./getWindowPath'); ExecutionEnvironment = require('react/lib/ExecutionEnvironment'); join = require('path').join; _onChange = null; _initialPath = null; _initialPathRgx = null; _isSupported = function() { var userAgent; userAgent = navigator.userAgent; if ((userAgent.indexOf('Android 2.') !== -1 || userAgent.indexOf('Android 4.0') !== -1) && userAgent.indexOf('Mobile Safari') !== -1 && userAgent.indexOf('Chrome') === -1) { return false; } return window.history && (window.history.pushState != null); }; HistoryLocation = { setup: function(onChange, initialPath) { invariant(ExecutionEnvironment.canUseDOM, 'You cannot use HistoryLocation in an environment with no DOM'); _initialPath = initialPath || ''; _initialPathRgx = new RegExp("^" + (initialPath || '')); _onChange = onChange; if (window.addEventListener) { return window.addEventListener('popstate', _onChange, false); } else { return window.attachEvent('popstate', _onChange); } }, teardown: function() { if (window.removeEventListener) { return window.removeEventListener('popstate', _onChange, false); } else { return window.detachEvent('popstate', _onChange); } }, push: function(path) { path = join(_initialPath, path); window.history.pushState({ path: path }, '', path); return _onChange(); }, replace: function(path) { path = join(_initialPath, path); window.history.replaceState({ path: path }, '', path); return _onChange(); }, pop: function() { return window.history.back(); }, getCurrentPath: function() { var path; path = getWindowPath(); path = path.replace(_initialPathRgx, ''); if (path === '') { return '/'; } else { return path; } }, isSupportedOrFallback: function() { if (!ExecutionEnvironment.canUseDOM) { return 'memory'; } if (_isSupported()) { return true; } else { return 'refresh'; } }, pathToHref: function(path) { return join(_initialPath, path); }, toString: function() { return '<HistoryLocation>'; } }; module.exports = HistoryLocation;