platform/lib/browser.dart

205 lines
5.6 KiB
Dart
Raw Normal View History

2016-11-27 22:24:30 +00:00
import 'dart:async' show Stream, StreamController;
2017-03-20 19:46:54 +00:00
import 'dart:html';
2016-11-27 22:24:30 +00:00
import 'angel_route.dart';
2017-03-20 19:46:54 +00:00
import 'package:path/path.dart' as p;
2016-11-27 22:24:30 +00:00
final RegExp _hash = new RegExp(r'^#/');
2017-03-20 19:46:54 +00:00
final RegExp _straySlashes = new RegExp(r'(^/+)|(/+$)');
2016-11-27 22:24:30 +00:00
/// A variation of the [Router] support both hash routing and push state.
abstract class BrowserRouter extends Router {
/// Fires whenever the active route changes. Fires `null` if none is selected (404).
Stream<RoutingResult> get onResolve;
/// Fires whenever the active route changes. Fires `null` if none is selected (404).
Stream<Route> get onRoute;
/// Set `hash` to true to use hash routing instead of push state.
/// `listen` as `true` will call `listen` after initialization.
2017-03-20 19:46:54 +00:00
factory BrowserRouter({bool hash: false, bool listen: false}) {
2016-11-27 22:24:30 +00:00
return hash
? new _HashRouter(listen: listen)
: new _PushStateRouter(listen: listen);
}
BrowserRouter._() : super();
void _goTo(String path);
/// Navigates to the path generated by calling
/// [navigate] with the given [linkParams].
///
/// This always navigates to an absolute path.
void go(List linkParams);
2017-03-20 19:46:54 +00:00
// Handles a route path, manually.
// void handle(String path);
2016-11-27 22:24:30 +00:00
/// Begins listen for location changes.
void listen();
/// Identical to [all].
2018-06-23 02:54:20 +00:00
Route on(String path, handler, {List middleware});
2016-11-27 22:24:30 +00:00
}
2017-08-03 16:14:21 +00:00
abstract class _BrowserRouterImpl extends Router implements BrowserRouter {
2017-11-18 05:29:08 +00:00
bool _listening = false;
2016-11-27 22:24:30 +00:00
Route _current;
StreamController<RoutingResult> _onResolve =
new StreamController<RoutingResult>();
StreamController<Route> _onRoute = new StreamController<Route>();
Route get currentRoute => _current;
@override
Stream<RoutingResult> get onResolve => _onResolve.stream;
@override
Stream<Route> get onRoute => _onRoute.stream;
_BrowserRouterImpl({bool listen}) : super() {
2016-12-19 04:43:55 +00:00
if (listen != false) this.listen();
2016-11-27 22:24:30 +00:00
prepareAnchors();
}
@override
2016-12-19 04:43:55 +00:00
void go(Iterable linkParams) => _goTo(navigate(linkParams));
2016-11-27 22:24:30 +00:00
2018-06-23 02:54:20 +00:00
Route on(String path, handler, {List middleware}) =>
2017-08-03 16:14:21 +00:00
all(path, handler, middleware: middleware);
2016-11-27 22:24:30 +00:00
void prepareAnchors() {
final anchors = window.document.querySelectorAll('a:not([dynamic])');
for (final AnchorElement $a in anchors) {
if ($a.attributes.containsKey('href') &&
!$a.attributes.containsKey('download') &&
!$a.attributes.containsKey('target') &&
$a.attributes['rel'] != 'external') {
$a.onClick.listen((e) {
e.preventDefault();
go($a.attributes['href'].split('/').where((str) => str.isNotEmpty));
});
}
$a.attributes['dynamic'] = 'true';
}
}
2017-11-18 05:29:08 +00:00
void _listen();
@override
void listen() {
if (_listening)
throw new StateError('The router is already listening for page changes.');
_listening = true;
_listen();
}
2016-11-27 22:24:30 +00:00
}
class _HashRouter extends _BrowserRouterImpl {
_HashRouter({bool listen}) : super(listen: listen) {
if (listen) this.listen();
}
@override
void _goTo(String uri) {
window.location.hash = '#$uri';
}
2017-03-20 19:46:54 +00:00
void handleHash([_]) {
final path = window.location.hash.replaceAll(_hash, '');
2017-11-25 00:26:06 +00:00
final resolved = resolveAbsolute(path).first;
2016-11-27 22:24:30 +00:00
2017-03-20 19:46:54 +00:00
if (resolved == null) {
_onResolve.add(null);
_onRoute.add(_current = null);
} else if (resolved != null && resolved.route != _current) {
_onResolve.add(resolved);
_onRoute.add(_current = resolved.route);
}
}
void handlePath(String path) {
2017-11-25 00:26:06 +00:00
final resolved = resolveAbsolute(path).first;
2017-03-20 19:46:54 +00:00
if (resolved == null) {
_onResolve.add(null);
_onRoute.add(_current = null);
} else if (resolved != null && resolved.route != _current) {
_onResolve.add(resolved);
_onRoute.add(_current = resolved.route);
2017-03-20 19:18:58 +00:00
}
2017-03-20 19:46:54 +00:00
}
2017-03-20 19:18:58 +00:00
2017-03-20 19:46:54 +00:00
@override
2017-11-18 05:29:08 +00:00
void _listen() {
2017-03-20 19:18:58 +00:00
window.onHashChange.listen(handleHash);
handleHash();
2016-11-27 22:24:30 +00:00
}
}
class _PushStateRouter extends _BrowserRouterImpl {
2017-03-20 19:46:54 +00:00
String _basePath;
2016-11-27 22:24:30 +00:00
_PushStateRouter({bool listen, Route root}) : super(listen: listen) {
2017-03-20 19:46:54 +00:00
var $base = window.document.querySelector('base[href]') as BaseElement;
if ($base?.href?.isNotEmpty != true)
throw new StateError(
'You must have a <base href="<base-url-here>"> element present in your document to run the push state router.');
_basePath = $base.href.replaceAll(_straySlashes, '');
2016-11-27 22:24:30 +00:00
if (listen) this.listen();
}
@override
void _goTo(String uri) {
2017-11-25 00:26:06 +00:00
final resolved = resolveAbsolute(uri).first;
2017-03-20 19:46:54 +00:00
var relativeUri = uri;
if (_basePath?.isNotEmpty == true) {
relativeUri = p.join(_basePath, uri.replaceAll(_straySlashes, ''));
}
2016-11-27 22:24:30 +00:00
if (resolved == null) {
_onResolve.add(null);
_onRoute.add(_current = null);
} else {
final route = resolved.route;
window.history.pushState(
2017-08-03 16:14:21 +00:00
{'path': route.path, 'params': {}},
2016-11-27 22:24:30 +00:00
route.name ?? route.path,
2017-03-20 19:46:54 +00:00
relativeUri);
2016-11-27 22:24:30 +00:00
_onResolve.add(resolved);
_onRoute.add(_current = route);
}
}
2017-03-20 19:46:54 +00:00
void handleState(state) {
if (state is Map && state.containsKey('path')) {
2018-06-23 02:54:20 +00:00
var path = state['path'].toString();
2017-11-25 00:26:06 +00:00
final resolved = resolveAbsolute(path).first;
2017-03-20 19:46:54 +00:00
if (resolved != null && resolved.route != _current) {
2017-08-03 16:14:21 +00:00
//properties.addAll(state['properties'] ?? {});
2017-03-20 19:46:54 +00:00
_onResolve.add(resolved);
2017-11-25 00:26:06 +00:00
_onRoute.add(_current = resolved.route);
2016-11-27 22:24:30 +00:00
} else {
_onResolve.add(null);
_onRoute.add(_current = null);
}
2017-03-20 19:46:54 +00:00
} else {
_onResolve.add(null);
_onRoute.add(_current = null);
2017-03-20 19:18:58 +00:00
}
2017-03-20 19:46:54 +00:00
}
2017-03-20 19:18:58 +00:00
2017-03-20 19:46:54 +00:00
@override
2017-11-18 05:29:08 +00:00
void _listen() {
2017-03-20 19:18:58 +00:00
window.onPopState.listen((e) {
handleState(e.state);
2016-11-27 22:24:30 +00:00
});
2017-03-20 19:18:58 +00:00
handleState(window.history.state);
2016-11-27 22:24:30 +00:00
}
}