platform/lib/src/routing_result.dart

92 lines
2.2 KiB
Dart
Raw Normal View History

2016-11-25 23:22:33 +00:00
part of angel_route.src.router;
2016-12-19 04:43:55 +00:00
/// Represents a complex result of navigating to a path.
2016-11-25 23:22:33 +00:00
class RoutingResult {
2016-12-19 04:43:55 +00:00
/// The Regex match that matched the given sub-path.
2016-11-25 23:22:33 +00:00
final Match match;
2016-12-19 04:43:55 +00:00
/// A nested instance, if a sub-path was matched.
2017-11-25 00:26:06 +00:00
final Iterable<RoutingResult> nested;
2016-12-19 04:43:55 +00:00
/// All route params matching this route on the current sub-path.
2016-11-25 23:22:33 +00:00
final Map<String, dynamic> params = {};
2016-12-19 04:43:55 +00:00
/// The [Route] that answered this sub-path.
///
/// This is mostly for internal use, and useless in production.
2016-11-27 22:24:30 +00:00
final Route shallowRoute;
2016-12-19 04:43:55 +00:00
/// The [Router] that answered this sub-path.
///
/// Only really for internal use.
2016-11-27 22:24:30 +00:00
final Router shallowRouter;
2016-12-19 04:43:55 +00:00
/// The remainder of the full path that was not matched, and was passed to [nested] routes.
2016-11-25 23:22:33 +00:00
final String tail;
2016-12-19 04:43:55 +00:00
/// The [RoutingResult] that matched the most specific sub-path.
2016-11-25 23:22:33 +00:00
RoutingResult get deepest {
var search = this;
2017-11-25 00:26:06 +00:00
while (search?.nested?.isNotEmpty == true) search = search.nested.first;
2016-11-25 23:22:33 +00:00
return search;
}
2016-12-19 04:43:55 +00:00
/// The most specific route.
2016-11-27 22:24:30 +00:00
Route get route => deepest.shallowRoute;
2016-12-19 04:43:55 +00:00
/// The most specific router.
2016-11-27 22:24:30 +00:00
Router get router => deepest.shallowRouter;
2016-11-25 23:22:33 +00:00
2016-12-19 04:43:55 +00:00
/// The handlers at this sub-path.
2016-11-25 23:22:33 +00:00
List get handlers {
2016-11-27 22:24:30 +00:00
return []..addAll(shallowRouter.middleware)..addAll(shallowRoute.handlers);
2016-11-25 23:22:33 +00:00
}
2016-12-19 04:43:55 +00:00
/// All handlers on this sub-path and its children.
2016-11-25 23:22:33 +00:00
List get allHandlers {
final handlers = [];
2017-11-25 00:26:06 +00:00
void crawl(RoutingResult result) {
handlers.addAll(result.handlers);
if (result.nested?.isNotEmpty == true) {
for (var r in result.nested)
crawl(r);
}
2016-11-25 23:22:33 +00:00
}
2017-11-25 00:26:06 +00:00
crawl(this);
2016-11-25 23:22:33 +00:00
return handlers;
}
2016-12-19 04:43:55 +00:00
/// All parameters on this sub-path and its children.
2016-11-27 23:39:03 +00:00
Map<String, dynamic> get allParams {
2016-12-21 23:28:00 +00:00
final Map<String, dynamic> params = {};
2016-11-27 23:39:03 +00:00
2017-11-25 00:26:06 +00:00
void crawl(RoutingResult result) {
params.addAll(result.params);
if (result.nested?.isNotEmpty == true) {
for (var r in result.nested)
crawl(r);
}
2016-11-27 23:39:03 +00:00
}
2017-11-25 00:26:06 +00:00
crawl(this);
2016-11-27 23:39:03 +00:00
return params;
}
2016-11-25 23:22:33 +00:00
RoutingResult(
{this.match,
Map<String, dynamic> params: const {},
this.nested,
2016-11-27 22:24:30 +00:00
this.shallowRoute,
this.shallowRouter,
2016-11-25 23:22:33 +00:00
this.tail}) {
this.params.addAll(params ?? {});
}
}