parse_route.dart
7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import 'package:flutter/widgets.dart';
import '../../get_navigation.dart';
import '../routes/get_route.dart';
class ParseRouteTree {
final List<_ParseRouteTreeNode> _nodes = <_ParseRouteTreeNode>[];
// bool _hasDefaultRoute = false;
void addRoute(GetPage route) {
var path = route.name;
if (path == Navigator.defaultRouteName) {
// if (_hasDefaultRoute) {
// throw ("Default route was already defined");
// }
var node = _ParseRouteTreeNode(path, _ParseRouteTreeNodeType.component);
node.routes = [route];
_nodes.add(node);
// _hasDefaultRoute = true;
return;
}
if (path.startsWith("/")) {
path = path.substring(1);
}
var pathComponents = path.split('/');
_ParseRouteTreeNode parent;
for (var i = 0; i < pathComponents.length; i++) {
var component = pathComponents[i];
var node = _nodeForComponent(component, parent);
if (node == null) {
var type = _typeForComponent(component);
node = _ParseRouteTreeNode(component, type);
node.parent = parent;
if (parent == null) {
_nodes.add(node);
} else {
parent.nodes.add(node);
}
}
if (i == pathComponents.length - 1) {
if (node.routes == null) {
node.routes = [route];
} else {
node.routes.add(route);
}
}
parent = node;
}
// Add Page children.
for (var page in _flattenPage(route)) {
addRoute(page);
}
}
List<GetPage> _flattenPage(GetPage route) {
final result = <GetPage>[];
if (route.children == null || route.children.isEmpty) {
return result;
}
final parentPath = route.name;
for (var page in route.children) {
// Add Parent middlewares to children
final pageMiddlewares = page.middlewares ?? <GetMiddleware>[];
pageMiddlewares.addAll(route.middlewares ?? <GetMiddleware>[]);
result.add(_addChild(page, parentPath, pageMiddlewares));
final children = _flattenPage(page);
for (var child in children) {
pageMiddlewares.addAll(child.middlewares ?? <GetMiddleware>[]);
result.add(_addChild(child, parentPath, pageMiddlewares));
}
}
return result;
}
/// Change the Path for a [GetPage]
GetPage _addChild(
GetPage origin, String parentPath, List<GetMiddleware> middlewares) =>
GetPage(
name: parentPath + origin.name,
page: origin.page,
title: origin.title,
alignment: origin.alignment,
transition: origin.transition,
binding: origin.binding,
bindings: origin.bindings,
curve: origin.curve,
customTransition: origin.customTransition,
fullscreenDialog: origin.fullscreenDialog,
maintainState: origin.maintainState,
opaque: origin.opaque,
parameter: origin.parameter,
popGesture: origin.popGesture,
settings: origin.settings,
transitionDuration: origin.transitionDuration,
middlewares: middlewares,
);
_GetPageMatch matchRoute(String path) {
var usePath = path;
if (usePath.startsWith("/")) {
usePath = path.substring(1);
}
// should take off url parameters first..
final uri = Uri.tryParse(usePath);
// List<String> components = usePath.split("/");
var components = uri.pathSegments;
if (path == Navigator.defaultRouteName) {
components = ["/"];
}
var nodeMatches = <_ParseRouteTreeNode, _ParseRouteTreeNodeMatch>{};
var nodesToCheck = _nodes;
for (final checkComponent in components) {
final currentMatches = <_ParseRouteTreeNode, _ParseRouteTreeNodeMatch>{};
final nextNodes = <_ParseRouteTreeNode>[];
for (final node in nodesToCheck) {
var pathPart = checkComponent;
var queryMap = <String, String>{};
if (checkComponent.contains("?") && !checkComponent.contains("=")) {
var splitParam = checkComponent.split("?");
pathPart = splitParam[0];
queryMap = {pathPart: splitParam[1]};
} else if (checkComponent.contains("?")) {
var splitParam = checkComponent.split("?");
var splitParam2 = splitParam[1].split("=");
if (!splitParam2[1].contains("&")) {
pathPart = splitParam[0];
queryMap = {splitParam2[0]: splitParam2[1]};
} else {
pathPart = splitParam[0];
final second = splitParam[1];
var other = second.split(RegExp(r"[&,=]"));
for (var i = 0; i < (other.length - 1); i++) {
var isOdd = (i % 2 == 0);
if (isOdd) {
queryMap.addAll({other[0 + i]: other[1 + i]});
}
}
}
}
final isMatch = (node.part == pathPart || node.isParameter());
if (isMatch) {
final parentMatch = nodeMatches[node.parent];
final match = _ParseRouteTreeNodeMatch.fromMatch(parentMatch, node);
// TODO: find a way to clean this implementation.
match.parameters.addAll(uri.queryParameters);
if (node.isParameter()) {
final paramKey = node.part.substring(1);
match.parameters[paramKey] = pathPart;
}
if (queryMap != null) {
match.parameters.addAll(queryMap);
}
currentMatches[node] = match;
if (node.nodes != null) {
nextNodes.addAll(node.nodes);
}
}
}
nodeMatches = currentMatches;
nodesToCheck = nextNodes;
if (currentMatches.values.length == 0) {
return null;
}
}
var matches = nodeMatches.values.toList();
if (matches.length > 0) {
var match = matches.first;
var nodeToUse = match.node;
if (nodeToUse != null &&
nodeToUse.routes != null &&
nodeToUse.routes.length > 0) {
var routes = nodeToUse.routes;
var routeMatch = _GetPageMatch(routes[0]);
routeMatch.parameters = match.parameters;
return routeMatch;
}
}
return null;
}
_ParseRouteTreeNode _nodeForComponent(
String component,
_ParseRouteTreeNode parent,
) {
var nodes = _nodes;
if (parent != null) {
nodes = parent.nodes;
}
for (var node in nodes) {
if (node.part == component) {
return node;
}
}
return null;
}
_ParseRouteTreeNodeType _typeForComponent(String component) {
var type = _ParseRouteTreeNodeType.component;
if (_isParameterComponent(component)) {
type = _ParseRouteTreeNodeType.parameter;
}
return type;
}
bool _isParameterComponent(String component) {
return component.startsWith(":");
}
Map<String, String> parseQueryString(String query) {
var search = RegExp('([^&=]+)=?([^&]*)');
var params = <String, String>{};
if (query.startsWith('?')) query = query.substring(1);
decode(String s) => Uri.decodeComponent(s.replaceAll('+', ' '));
for (Match match in search.allMatches(query)) {
var key = decode(match.group(1));
final value = decode(match.group(2));
params[key] = value;
}
return params;
}
}
class _ParseRouteTreeNodeMatch {
_ParseRouteTreeNodeMatch(this.node);
_ParseRouteTreeNodeMatch.fromMatch(
_ParseRouteTreeNodeMatch match, this.node) {
parameters = <String, String>{};
if (match != null) {
parameters.addAll(match.parameters);
}
}
_ParseRouteTreeNode node;
Map<String, String> parameters = <String, String>{};
}
class _ParseRouteTreeNode {
_ParseRouteTreeNode(this.part, this.type);
String part;
_ParseRouteTreeNodeType type;
List<GetPage> routes = <GetPage>[];
List<_ParseRouteTreeNode> nodes = <_ParseRouteTreeNode>[];
_ParseRouteTreeNode parent;
bool isParameter() {
return type == _ParseRouteTreeNodeType.parameter;
}
}
class _GetPageMatch {
_GetPageMatch(this.route);
GetPage route;
Map<String, String> parameters = <String, String>{};
}
enum _ParseRouteTreeNodeType {
component,
parameter,
}