This repository was archived by the owner on Aug 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathdeeplink.js
More file actions
272 lines (215 loc) · 6.2 KB
/
deeplink.js
File metadata and controls
272 lines (215 loc) · 6.2 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
var argscheck = require('cordova/argscheck'),
utils = require('cordova/utils'),
exec = require('cordova/exec');
var PLUGIN_NAME = 'IonicDeeplinkPlugin';
var extend = function (out) {
out = out || {};
for (var i = 1; i < arguments.length; i++) {
if (!arguments[i]) {
continue;
}
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
out[key] = arguments[i][key];
}
}
}
return out;
};
var IonicDeeplink = {
/**
* How long to wait after a deeplink match before navigating.
* Default is 800ms which gives the app time to get back and then
* smoothly animate.
*/
NAVIGATION_DELAY: 800,
canOpenApp: function (app, cb) {
exec(cb, null, PLUGIN_NAME, 'canOpenApp', []);
},
route: function (paths, success, error) {
var self = this;
this.paths = paths;
this.onDeepLink(function (data) {
var realPath = self._getRealPath(data);
var args = self._queryToObject(data.url);
var matched = false;
var finalArgs;
var pathData;
for (var targetPath in paths) {
pathData = paths[targetPath];
var matchedParams = self.routeMatch(pathData, realPath);
if (matchedParams !== false) {
matched = true;
finalArgs = extend({}, matchedParams, args);
break;
}
}
if (matched === true) {
console.log('Match found', realPath);
if (typeof (success) === 'function') {
success({
$route: pathData,
$args: finalArgs,
$link: data,
});
}
return;
}
if (typeof (error) === 'function') {
console.log('No Match found');
error({ $link: data });
}
})
},
routeWithNavController: function (navController, paths, options, success, error) {
var self = this;
var defaultOptions = {
root: false,
};
if (typeof options !== 'function') {
options = extend(defaultOptions, options);
} else {
success = options;
error = success;
options = defaultOptions;
}
this.route(paths, function (match) {
// Defer this to ensure animations run
setTimeout(function () {
if (options.root === true) {
navController.setRoot(match.$route, match.$args);
} else {
navController.push(match.$route, match.$args);
}
}, self.NAVIGATION_DELAY);
if (typeof (success) === 'function') {
success(match);
}
}, function (nomatch) {
if (typeof (error) === 'function') {
error(nomatch);
}
});
},
/**
* Check if the path matches the route.
*/
routeMatch: function (route, path) {
if (route === path) {
return {};
}
var parts = path.split('/');
var routeParts = route.split('/');
// Our aggregated route params that matched our route path.
// This is used for things like /post/:id
var routeParams = {};
if (parts.length !== routeParts.length) {
// Can't possibly match if the lengths are different
return false;
}
// Otherwise, we need to check each part
var rp,
pp;
for (var i = 0; i < parts.length; i++) {
pp = parts[i];
rp = routeParts[i];
if (rp[0] == ':') {
// We have a route param, store it in our
// route params without the colon
routeParams[rp.slice(1)] = pp;
} else if (pp !== rp) {
return false;
}
}
return routeParams;
},
_queryToObject: function (q) {
if (!q) return {};
var qIndex = q.indexOf('?');
if (qIndex > -1) {
q = q.slice(qIndex + 1);
}
var i = 0,
retObj = {},
pair = null,
qArr = q.split('&');
for (; i < qArr.length; i++) {
if (!qArr[i]) {
continue;
}
pair = qArr[i].split('=');
retObj[pair[0]] = pair[1];
}
return retObj;
},
_stripFragmentLeadingHash: function (fragment) {
var hs = fragment.indexOf('#');
if (hs > -1) {
fragment.slice(0, hs);
}
return fragment;
},
/**
* We're fairly flexible when it comes to matching a URL. We support
* host-less custom URL scheme matches like ionic://camera?blah but also support
* and match against fragments.
*
* This method tries to infer what the proper "path" is from the URL
*/
_getRealPath: function (data) {
// 1. Let's just do the obvious and return the parsed 'path' first, if available.
if (!!data.path && data.path !== "") {
return data.path;
}
// 2. Now, are we using a non-standard scheme?
var isCustomScheme = data.scheme.indexOf('http') === -1;
// 3. Nope so we'll go fragment first if available as that should be what comes after
if (!isCustomScheme) {
if (!!data.fragment) {
return self._stripFragmentLeadingHash(data.fragment);
}
}
// 4. Now fall back to host / authority
if (!!data.host) {
if (data.host.charAt(0) != '/') {
data.host = '/' + data.host;
}
return data.host;
}
// 5. We'll use fragment next if we're in a custom scheme, though this might need a little more thought
if (isCustomScheme && !!data.fragment) {
return self._stripFragmentLeadingHash(data.fragment);
}
// 6. Last resort - no obvious path, fragment or host, so we
// slice out the scheme and any query string or fragment from the full url.
var restOfUrl = data.url;
var separator = data.url.indexOf('://');
if (separator !== -1) {
restOfUrl = data.url.slice(separator + 3);
} else {
separator = data.url.indexOf(':/');
if (separator !== -1) {
restOfUrl = data.url.slice(separator + 2);
}
}
var qs = restOfUrl.indexOf('?');
if (qs > -1) {
restOfUrl = restOfUrl.slice(0, qs);
}
var hs = restOfUrl.indexOf('#');
if (hs > -1) {
restOfUrl = restOfUrl.slice(0, hs);
}
return restOfUrl;
},
onDeepLink: function (callback) {
var innerCB = function (data) {
callback(data);
};
exec(innerCB, null, PLUGIN_NAME, 'onDeepLink', []);
},
getHardwareInfo: function (callback) {
exec(callback, null, PLUGIN_NAME, 'getHardwareInfo', []);
},
};
module.exports = IonicDeeplink;