cycle-router
Version:
Router for Cycle.js
145 lines (114 loc) • 4.59 kB
JavaScript
;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createHref = exports.createLocation = exports.makeRouterDriver = undefined;
var _rx = require('rx');
var _rx2 = _interopRequireDefault(_rx);
var _switchPath2 = require('switch-path');
var _switchPath3 = _interopRequireDefault(_switchPath2);
var _history = require('history');
var _history2 = require('./history');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function filterPush(location) {
return location.action === 'PUSH' || location.action === 'REPLACE';
}
function isStrictlyInScope(namespace, path) {
var pathParts = path.split('/').filter(function (x) {
return x.length > 0;
});
return namespace.every(function (v, i) {
return pathParts[i] === v;
});
}
function makeDefinitionResolver(source$) {
return function definitionResolver(routeDefinitions) {
var props$ = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var namespace = this.namespace;
var props$_ = props$ === null ? this.props$ : props$;
var history$ = this.history$;
return source$.map(function (_ref) {
var pathname = _ref.pathname;
var pathParts = pathname.split('/').filter(function (x) {
return x.length > 0;
});
var path_ = pathParts.filter(function (i) {
return namespace.indexOf(i) < 0;
}).join('/');
var _switchPath = (0, _switchPath3.default)('/' + path_, routeDefinitions);
var path = _switchPath.path;
var value = _switchPath.value;
return {
path: path,
value: value,
routeDefinitions: routeDefinitions,
fullPath: pathname,
props$: props$_.shareReplay(1),
history$: history$
};
}).filter(function (_ref2) {
var path = _ref2.path;
return path !== null;
}).share();
};
}
function makePathFilter(source$) {
return function pathFilter(path) {
var props$ = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var namespace = this.namespace;
var props$_ = props$ === null ? this.props$ : props$;
var history$ = this.history$;
var scopedNamespace = namespace.concat(path.split('/').filter(function (s) {
return s.length > 0;
}));
var scopedSource$ = source$.filter(function (_ref3) {
var pathname = _ref3.pathname;
return isStrictlyInScope(scopedNamespace, pathname);
}).share();
return {
namespace: scopedNamespace,
observable: scopedSource$,
path: makePathFilter(scopedSource$),
define: makeDefinitionResolver(scopedSource$, props$),
props$: props$_.shareReplay(1),
history$: history$
};
};
}
var defaultOptions = {
hash: true,
basename: '/'
};
function makeRouterDriver() {
var config = arguments.length <= 0 || arguments[0] === undefined ? defaultOptions : arguments[0];
var hash = config.hash;
var options = _objectWithoutProperties(config, ['hash']);
var history = (0, _history2.makeHistory)(hash, options);
return function routerDriver(sink$) {
var source$ = new _rx2.default.ReplaySubject(1);
var history$ = new _rx2.default.ReplaySubject(1);
sink$.subscribe((0, _history2.makePushState)(history));
history.listen(function (_ref4) {
var pathname = _ref4.pathname;
var location = _objectWithoutProperties(_ref4, ['pathname']);
var path = pathname === '/' ? pathname : '/' + pathname;
source$.onNext(_extends({ pathname: path }, location));
if (location.action !== 'POP') {
history$.onNext(location);
}
});
return {
namespace: [],
observable: source$.filter(filterPush).share(),
path: makePathFilter(source$.filter(filterPush)),
define: makeDefinitionResolver(source$.filter(filterPush)),
props$: _rx2.default.Observable.just({}).replay(1),
history$: history$
};
};
}
exports.makeRouterDriver = makeRouterDriver;
exports.createLocation = _history.createLocation;
exports.createHref = _history.createHref;