ionic-plugin-deeplinks
Advanced tools
Comparing version
{ | ||
"name": "ionic-plugin-deeplinks", | ||
"version": "1.0.17", | ||
"version": "1.0.18", | ||
"cordova": { | ||
@@ -5,0 +5,0 @@ "id": "ionic-plugin-deeplinks", |
@@ -7,2 +7,3 @@ # Community Maintained | ||
If you used to handle URI schemes with the help of this plugin and have migrated to Branch Metrics, you can make use of a plugin such as [https://github.com/EddyVerbruggen/Custom-URL-scheme](https://github.com/EddyVerbruggen/Custom-URL-scheme) to facilitate custom URL schemes. | ||
@@ -9,0 +10,0 @@ Ionic Deeplinks Plugin |
@@ -0,1 +1,20 @@ | ||
function parseSchemeFromUrl (url) { | ||
var _sep = url.indexOf(':'); | ||
if (_sep > -1) { | ||
return url.slice(0, _sep + 1); | ||
} | ||
return undefined; | ||
} | ||
function parseQueryStringFromUrl(url) { | ||
var qs = url.indexOf('?'); | ||
if (qs > -1) { | ||
return url.slice(qs + 1); | ||
} | ||
return undefined; | ||
} | ||
function locationToData(l) { | ||
@@ -6,3 +25,5 @@ return { | ||
host: l.hostname, | ||
fragment: l.hash | ||
fragment: l.hash, | ||
scheme: parseSchemeFromUrl(l.href), | ||
queryString: parseQueryStringFromUrl(l.href) | ||
} | ||
@@ -9,0 +30,0 @@ } |
@@ -1,17 +0,18 @@ | ||
var argscheck = require('cordova/argscheck'), | ||
utils = require('cordova/utils'), | ||
exec = require('cordova/exec'); | ||
utils = require('cordova/utils'), | ||
exec = require('cordova/exec'); | ||
var PLUGIN_NAME = 'IonicDeeplinkPlugin'; | ||
var extend = function(out) { | ||
var extend = function (out) { | ||
out = out || {}; | ||
for (var i = 1; i < arguments.length; i++) { | ||
if (!arguments[i]) | ||
if (!arguments[i]) { | ||
continue; | ||
} | ||
for (var key in arguments[i]) { | ||
if (arguments[i].hasOwnProperty(key)) | ||
if (arguments[i].hasOwnProperty(key)) { | ||
out[key] = arguments[i][key]; | ||
} | ||
} | ||
@@ -24,2 +25,3 @@ } | ||
var IonicDeeplink = { | ||
/** | ||
@@ -32,55 +34,61 @@ * How long to wait after a deeplink match before navigating. | ||
canOpenApp: function(app, cb) { | ||
canOpenApp: function (app, cb) { | ||
exec(cb, null, PLUGIN_NAME, 'canOpenApp', []); | ||
}, | ||
route: function(paths, success, error) { | ||
route: function (paths, success, error) { | ||
var self = this; | ||
this.paths = paths; | ||
this.onDeepLink(function(data) { | ||
console.log('On deep link', data); | ||
var realPath, pathData, matchedParams, args, finalArgs, didRoute; | ||
this.onDeepLink(function (data) { | ||
var realPath = self._getRealPath(data); | ||
realPath = self._getRealPath(data); | ||
args = self._queryToObject(data.url) | ||
var args = self._queryToObject(data.queryString); | ||
for(var targetPath in paths) { | ||
var matched = false; | ||
var finalArgs; | ||
var pathData; | ||
for (var targetPath in paths) { | ||
pathData = paths[targetPath]; | ||
matchedParams = self.routeMatch(targetPath, realPath); | ||
var matchedParams = self.routeMatch(targetPath, realPath); | ||
if(matchedParams !== false) { | ||
if (matchedParams !== false) { | ||
matched = true; | ||
finalArgs = extend({}, matchedParams, args); | ||
if(typeof(success) === 'function') { | ||
success({ | ||
$route: pathData, | ||
$args: finalArgs, | ||
$link: data | ||
}); | ||
} | ||
didRoute = true; | ||
break; | ||
} | ||
} | ||
if(!didRoute) { | ||
if(typeof(error) === 'function') { | ||
error({ | ||
$link: data | ||
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) { | ||
routeWithNavController: function (navController, paths, options, success, error) { | ||
var self = this; | ||
var defaultOptions = { | ||
root: false | ||
root: false, | ||
}; | ||
if(typeof options !== 'function') { | ||
if (typeof options !== 'function') { | ||
options = extend(defaultOptions, options); | ||
@@ -93,7 +101,6 @@ } else { | ||
this.route(paths, function(match) { | ||
this.route(paths, function (match) { | ||
// Defer this to ensure animations run | ||
setTimeout(function() { | ||
if(options.root === true) { | ||
setTimeout(function () { | ||
if (options.root === true) { | ||
navController.setRoot(match.$route, match.$args); | ||
@@ -105,7 +112,7 @@ } else { | ||
if(typeof(success) === 'function') { | ||
if (typeof (success) === 'function') { | ||
success(match); | ||
} | ||
}, function(nomatch) { | ||
if(typeof(error) === 'function') { | ||
}, function (nomatch) { | ||
if (typeof (error) === 'function') { | ||
error(nomatch); | ||
@@ -119,3 +126,3 @@ } | ||
*/ | ||
routeMatch: function(route, path) { | ||
routeMatch: function (route, path) { | ||
if (route === path) { | ||
@@ -132,3 +139,3 @@ return {}; | ||
if(parts.length !== routeParts.length) { | ||
if (parts.length !== routeParts.length) { | ||
// Can't possibly match if the lengths are different | ||
@@ -140,15 +147,16 @@ return false; | ||
var rp, pp; | ||
for(var i = 0; i < parts.length; i++) { | ||
var rp, | ||
pp; | ||
for (var i = 0; i < parts.length; i++) { | ||
pp = parts[i]; | ||
rp = routeParts[i]; | ||
if(rp[0] == ':') { | ||
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) { | ||
} else if (pp !== rp) { | ||
return false; | ||
} | ||
} | ||
@@ -158,19 +166,24 @@ return routeParams; | ||
_queryToObject: function(q) { | ||
if(!q) return {}; | ||
_queryToObject: function (q) { | ||
if (!q) return {}; | ||
var qIndex = q.lastIndexOf('?'); | ||
if(qIndex < 0) return {}; | ||
var qIndex = q.indexOf('?'); | ||
// Get everything after the ? | ||
q = q.slice(q.lastIndexOf('?') + 1); | ||
if (qIndex > -1) { | ||
q = q.slice(qIndex + 1); | ||
} | ||
var i = 0, retObj = {}, pair = null, | ||
qArr = q.split('&'); | ||
var i = 0, | ||
retObj = {}, | ||
pair = null, | ||
qArr = q.split('&'); | ||
for (; i < qArr.length; i++) { | ||
if(!qArr[i]) { continue; } | ||
if (!qArr[i]) { | ||
continue; | ||
} | ||
pair = qArr[i].split('='); | ||
retObj[pair[0]] = pair[1]; | ||
}; | ||
} | ||
@@ -180,2 +193,12 @@ return retObj; | ||
_stripFragmentLeadingHash: function (fragment) { | ||
var hs = fragment.indexOf('#'); | ||
if (hs > -1) { | ||
fragment.slice(0, hs); | ||
} | ||
return fragment; | ||
}, | ||
/** | ||
@@ -188,33 +211,62 @@ * We're fairly flexible when it comes to matching a URL. We support | ||
*/ | ||
_getRealPath: function(data) { | ||
// If we have a fragment, we use that as the path | ||
if(data.fragment) { | ||
var fi = data.fragment.indexOf('?'); | ||
if(fi > -1) { | ||
return data.fragment.slice(0, fi).slice(1); | ||
_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); | ||
} | ||
return data.fragment.slice(1); | ||
} | ||
if(!data.path) { | ||
if(data.host.charAt(0) != '/') data.host = '/' + data.host; | ||
// 4. Now fall back to host / authority | ||
if (!!data.host) { | ||
if (data.host.charAt(0) != '/') { | ||
data.host = '/' + data.host; | ||
} | ||
return data.host; | ||
} | ||
var hostOrScheme = data.host || data.scheme + '://'; | ||
var restOfUrl = data.url.slice(data.url.indexOf(hostOrScheme) + hostOrScheme.length); | ||
// 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); | ||
} | ||
if(restOfUrl.indexOf('?') > -1) { | ||
restOfUrl = restOfUrl.slice(0, restOfUrl.indexOf('?')); | ||
// 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); | ||
} | ||
} | ||
if(restOfUrl.indexOf('#') > -1) { | ||
restOfUrl = restOfUrl.slice(0); | ||
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) { | ||
onDeepLink: function (callback) { | ||
var innerCB = function (data) { | ||
callback(data); | ||
@@ -225,7 +277,7 @@ }; | ||
getHardwareInfo: function(callback) { | ||
getHardwareInfo: function (callback) { | ||
exec(callback, null, PLUGIN_NAME, 'getHardwareInfo', []); | ||
} | ||
}, | ||
}; | ||
module.exports = IonicDeeplink; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
46276
30.04%11
10%495
12.76%198
0.51%