-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.js
254 lines (219 loc) · 8.62 KB
/
index.js
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
/**
* @providesModule react-native-launch-navigator
* @flow
*/
'use strict';
import {Platform, NativeModules} from 'react-native';
import RNLogger from './RNLogger';
let ln;
if(Platform.OS === "ios"){
ln = require('./ios');
}else if(Platform.OS === "android"){
ln = require('./android');
}else{
throw "Unsupported platform: " + Platform.OS;
}
/*********************
* Internal properties
*********************/
let RNLaunchNavigator = NativeModules.RNLaunchNavigator;
let Logger = new RNLogger(RNLaunchNavigator);
/********************
* Internal functions
********************/
/******************
* Public API
******************/
/**
* Sets debug mode status in the native launch navigator module
* @param {boolean} enabled - if true, debug mode is enabled; if false, debug mode is disabled.
*/
ln.enableDebug = RNLaunchNavigator.enableDebug;
/**
* Returns a list indicating which apps are installed and available on the current device.
* @return {Promise}
* resolve - Will be passed a key/value object where the key is the app name and the value is a boolean indicating whether the app is available.
* reject - Will be passed a single string argument containing the error message.
*/
ln.getAvailableApps = function(){
return RNLaunchNavigator.getAvailableApps();
};
/**
* Determines if the given app is installed and available on the current device.
* @param {string} appName - name of the app to check availability for. Define as a constant using ln.APP
* @return {Promise}
* resolve - Will be passed a single boolean argument indicating the availability of the app.
* reject - Will be passed a single string argument containing the error message.
*/
ln.isAppAvailable = function(appName){
return RNLaunchNavigator.isAppAvailable(appName);
};
/**
* Returns the display name of the specified app.
* @param {string} app - specified as a constant in `launchnavigator.APP`. e.g. `launchnavigator.APP.GOOGLE_MAPS`.
* @return {string} - app display name. e.g. "Google Maps".
*/
ln.getAppDisplayName = function(app){
ln.util.validateApp(app);
return ln.APP_NAMES[app];
};
/**
* Returns list of supported apps on a given platform.
* @param {string} platform - specified as a constant in `launchnavigator.PLATFORM`. e.g. `launchnavigator.PLATFORM.IOS`.
* @return {string[]} - apps supported on specified platform as a list of `launchnavigator.APP` constants.
*/
ln.getAppsForPlatform = function(platform){
ln.util.validatePlatform(platform);
return ln.APPS_BY_PLATFORM[platform];
};
/**
* Indicates if an app on a given platform supports specification of launch mode.
* @param {string} app - specified as a constant in `launchnavigator.APP`. e.g. `launchnavigator.APP.GOOGLE_MAPS`.
* @param {string} platform - specified as a constant in `launchnavigator.PLATFORM`. e.g. `launchnavigator.PLATFORM.ANDROID`.
* @return {boolean} - true if app/platform combination supports specification of transport mode.
*/
ln.supportsLaunchMode = function(app, platform) {
ln.util.validateApp(app);
ln.util.validatePlatform(platform);
return !!ln.SUPPORTS_LAUNCH_MODE[platform] && ln.util.arrayContainsValue(ln.SUPPORTS_LAUNCH_MODE[platform], app);
};
/**
* Indicates if an app on a given platform supports specification of a custom nickname for start location.
* @param {string} app - specified as a constant in `launchnavigator.APP`. e.g. `launchnavigator.APP.GOOGLE_MAPS`.
* @param {string} platform - specified as a constant in `launchnavigator.PLATFORM`. e.g. `launchnavigator.PLATFORM.IOS`.
* @return {boolean} - true if app/platform combination supports specification of start location.
*/
ln._supportsStartName = function(app, platform){
ln.util.validateApp(app);
ln.util.validatePlatform(platform);
return !!ln.SUPPORTS_START_NAME[platform] && ln.util.arrayContainsValue(ln.SUPPORTS_START_NAME[platform], app);
};
/**
* Indicates if an app on a given platform supports specification of start location.
* @param {string} app - specified as a constant in `launchnavigator.APP`. e.g. `launchnavigator.APP.GOOGLE_MAPS`.
* @param {string} platform - specified as a constant in `launchnavigator.PLATFORM`. e.g. `launchnavigator.PLATFORM.IOS`.
* @return {boolean} - true if app/platform combination supports specification of start location.
*/
ln._supportsStart = function(app, platform){
ln.util.validateApp(app);
ln.util.validatePlatform(platform);
return !!ln.SUPPORTS_START[platform] && ln.util.arrayContainsValue(ln.SUPPORTS_START[platform], app);
};
/**
* Indicates if an app on a given platform supports specification of a custom nickname for destination location.
* @param {string} app - specified as a constant in `launchnavigator.APP`. e.g. `launchnavigator.APP.GOOGLE_MAPS`.
* @param {string} platform - specified as a constant in `launchnavigator.PLATFORM`. e.g. `launchnavigator.PLATFORM.IOS`.
* @return {boolean} - true if app/platform combination supports specification of destination location.
*/
ln._supportsDestName = function(app, platform){
ln.util.validateApp(app);
ln.util.validatePlatform(platform);
return !!ln.SUPPORTS_DEST_NAME[platform] && ln.util.arrayContainsValue(ln.SUPPORTS_DEST_NAME[platform], app);
};
/**
* Indicates if an app on a given platform supports specification of transport mode.
* @param {string} app - specified as a constant in `launchnavigator.APP`. e.g. `launchnavigator.APP.GOOGLE_MAPS`.
* @param {string} platform - specified as a constant in `launchnavigator.PLATFORM`. e.g. `launchnavigator.PLATFORM.IOS`.
* @return {boolean} - true if app/platform combination supports specification of transport mode.
*/
ln._supportsTransportMode = function(app, platform){
ln.util.validateApp(app);
ln.util.validatePlatform(platform);
return !!ln.TRANSPORT_MODES[platform] && ln.util.objectContainsKey(ln.TRANSPORT_MODES[platform], app);
};
/*******************
* Utility functions
*******************/
ln.util = {};
ln.util.arrayContainsValue = function (a, obj) {
var i = a.length;
while (i--) {
if (a[i] === obj) {
return true;
}
}
return false;
};
ln.util.objectContainsKey = function (o, key) {
for(var k in o){
if(k === key){
return true;
}
}
return false;
};
ln.util.objectContainsValue = function (o, value) {
for(var k in o){
if(o[k] === value){
return true;
}
}
return false;
};
ln.util.countKeysInObject = function (o){
var count = 0;
for(var k in o){
count++;
}
return count;
};
ln.util.isValidApp = function(app){
if(app === "none") return true; // native chooser
return ln.util.objectContainsValue(ln.APP, app);
};
ln.util.isValidPlatform = function(platform){
return ln.util.objectContainsValue(ln.PLATFORM, platform);
};
ln.util.isValidTransportMode = function(transportMode) {
return ln.util.objectContainsValue(ln.TRANSPORT_MODE, transportMode);
};
ln.util.validateApp = function(app){
if(!ln.util.isValidApp(app)){
throw new Error("'"+app+"' is not a recognised app");
}
};
ln.util.validatePlatform = function(platform){
if(!ln.util.isValidPlatform(platform)){
throw new Error("'"+platform+"' is not a recognised platform");
}
};
ln.util.validateTransportMode = function(transportMode){
if(!ln.util.isValidTransportMode(transportMode)){
throw new Error("'"+transportMode+"' is not a recognised transport mode");
}
};
ln.util.extractCoordsFromLocationString = function(location){
if(location && typeof(location) === "string" && location.match(ln.COORDS_REGEX)){
location = location.replace(/\s*/g,'');
var parts = location.split(",");
location = [parts[0], parts[1]];
}
return location;
};
ln.util.isValidLaunchMode = function(launchMode){
for(var LAUNCH_MODE in ln.LAUNCH_MODE){
if(launchMode === ln.LAUNCH_MODE[LAUNCH_MODE]) return true;
}
return false;
};
ln.util.validateLaunchMode = function(launchMode){
if(!ln.util.isValidLaunchMode(launchMode)){
throw new Error("'"+launchMode+"' is not a recognised launch mode");
}
};
ln.util.conformNavigateOptions = function(args){
var options;
if(args.length > 1 && typeof args[1] === "function"){
// assume (dest, success, error, opts)
options = (args.length > 3 && typeof args[3] === "object") ? args[3] : {};
options.successCallback = args[1];
if(args.length > 2 && typeof args[2] === "function"){
options.errorCallback = args[2];
}
}else{
// assume (dest, opts)
options = (args.length > 1 && typeof args[1] === "object") ? args[1] : {};
}
return options;
};
module.exports = ln;