-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhy.js
391 lines (328 loc) · 13 KB
/
rhy.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* A module for JavaScript to native communication, and visa-versa.
*
* Support for the following platforms is implemented:
*
* - Android
* - iOS
*/
(function(Rhy, undefined) {
// Platform identifers.
Rhy.isAndroid = navigator.userAgent.match(/Android/i);
Rhy.isiOS = navigator.userAgent.match(/iPhone|iPad|iPod/i);
/*
* @function makeNativeRequest - A wrapper function to send a new
* request to the native code.
*
* @param {object} parameters - A dictionary of key-value pairs to
* send to the native code.
*/
function nativeRequest(parameters) {
// Here we check to see if the parameters has a platform specific
// object. If so, we only pass that on to the platform specific request
// handler. Else, we pass everything.
if (Rhy.isiOS) {
/*
if (parameters.IOS) {
parameters = parameters.IOS;
}
*/
return Rhy.iOS.request(parameters);
} else if (Rhy.isAndroid) {
if (parameters.ANDROID) {
parameters = parameters.ANDROID;
}
return Rhy.Android.request(parameters);
}
}
/*
* @function nativeRequest - This is here mostly for debugging, allows
* us to send any message to the native
* code.
* @param {object} parameters - The parameters to send to the native
* code.
*/
Rhy.nativeRequest = function(parameters) {
return nativeRequest(parameters);
};
/*
* @function getValuesForKeys - Useful to get a batch of values from the
* native code in a single operation.
* @param {array} keys - A list of keys whose values we want.
* @return {object} - A dictionary of key/value pairs where the
* the keys are the keys from the array
* passed in, and the values are the values
* returned from the native code.
*/
Rhy.getValuesForKeys = function(keys) {
return nativeRequest({
'getValuesForKeys' : keys,
})
};
/*
* @function showActivityIndicator - Show the activity indicator.
*/
Rhy.showActivityIndicator = function(title, message) {
var params = {}
params.showActivityIndicator = 'showActivityIndicator';
if(title) {
params.title = title;
}
if(message) {
params.message = message;
}
nativeRequest(params);
};
/*
* @function hideActivityIndicator - Hide the activity indicator.
*/
Rhy.hideActivityIndicator = function() {
nativeRequest({
hideActivityIndicator : 'hideActivityIndicator',
});
};
/*
* @function pushScreen - Go to the new screen given by
* the HTML file name.
* @param {string} fileName - The name of the HTML file to
* open on the next screen.
*/
Rhy.newScreen = function(fileName) {
nativeRequest({
newScreen : fileName
});
};
/*
* @function popScreen - Pop the given number of screens.
* Default is one.
* @optional
* @param {string} screens - The number of screens to pop.
* @param {function} callback - todo
*/
Rhy.popScreen = function(screens, callback) {
var params = {
popScreen : screens ? screens : 1,
};
if (callback) {
params.callback = callback;
}
nativeRequest(params);
};
/*
* @function createMenu - Create the given menu with the
* given screen open.
* @param {string} menuFileName - The menu file to open.
* @param {function} openFileName - The file to open.
*/
Rhy.createMenu = function(menuFileName, openFileName, menuMargin) {
// If the size was passed in, in rem units, e.g. '3.2rem', convert it to px.
if (menuMargin.indexOf('rem', menuMargin.length - 'rem'.length) !== -1) {
var size = getComputedStyle(document.documentElement).fontSize;
menuMargin = parseInt(parseFloat(menuMargin) * parseFloat(size));
}
nativeRequest({
createMenu : menuFileName,
openFileName : openFileName,
menuMargin : menuMargin,
});
};
/*
* @function openMenuOption - Open a screen that corresponds to a
* menu option.
*/
Rhy.openMenuOption = function(HTMLFile) {
return nativeRequest({
openMenuOption : HTMLFile,
});
};
/*
* @function toggleMenu - Open/close the menu.
*/
Rhy.toggleMenu = function() {
nativeRequest({
toggleMenu : 'toggleMenu',
});
};
/*
* @function shouldEnableMenu - Enable/disable the menu.
* @param shouldEnable - Pass true to enable, false to
* disable.
*/
Rhy.shouldEnableMenu = function(shouldEnable) {
nativeRequest({
shouldEnableMenu : shouldEnable,
});
};
/*
* @function handleMenu - This method handles menu visibility
* automatically.
* This is left empty intentionally,
* implementations must override this
* programatically.
* It's put here so it's know to exist.
* @example - An example of usage for a screen
* which does not need the menu enabled
* (i.e. a screen with a back button):
*
* Rhy.handleMenu = function() {
* $(function() {
* Rhy.shouldEnableMenu(false);
* });
* };
*/
Rhy.handleMenu = undefined;
/*
* @function openInBrowser - Open the given URL in the
* device's browser app.
* @param {string} URL - The URL to open.
*/
Rhy.openInBrowser = function(URL) {
nativeRequest({
openInBrowser : URL,
});
};
/*
* @function isAppInstalled - Check if the app identified by
* the given URL scheme is
* installed.
* @param {object}
* @optional - Only the supported platforms should
* be represented here.
* @param {object} ANDROID
* @param {String} packageName - The app's package name.
* @param {String} formFactor - The compatible form factor of
* the app (SMARTPHONE, TABLET or
* UNIVERSAL).
* @param {object} IOS
* @param {String} URLScheme - The app's URL Scheme.
* @param {String} formFactor - The compatible form factor of
* the app (SMARTPHONE, TABLET or
* UNIVERSAL).
* @param {object} WINDOWS
* @param {String} packageName - The app's package name.
* @param {function} callback - The function to be called with
* the native method's result.
*/
Rhy.isAppInstalled = function(options) {
// Here we need to add the command identifier so that it can be
// identified in the native code.
options.isAppInstalled = 'isAppInstalled';
return nativeRequest(options);
};
/*
* @function openApp - Open the given app (if installed,
* open the app, else open the app
* in the app store).
* @param {object}
* @optional - Only the supported platforms should
* be represented here.
* @param {object} ANDROID
* @param {String} packageName - The app's package name.
*
* @param {object} IOS
* @param {String} URLScheme - The app's URL Scheme.
* @param {String} appID - The app's ID in iTunes Connect.
* @param {object} WINDOWS
* @param {String} packageName - The app's package nam to check if
* it's installed.
* @param {String} appUri - The URI Asociated to the App, to
* launch it.
*
*/
Rhy.openApp = function(options) {
// Here we need to add the command identifier so that it can be
// identified in the native code.
options.openApp = 'openApp';
return nativeRequest(options);
};
/*
* @function formFactor - Get the kind of device (i.e. tablet
* or smartphone).
*
* @return - Either SMARTPHONE or TABLET.
*/
Rhy.formFactor = function() {
return nativeRequest({
formFactor: 'formFactor',
});
};
/*
* @function updateAppConfigKey - Set the value for the given key in the
* app's global persistant configuration
* object.
* @param key {string} - The key to set.
* @param value {string} - The value of the key.
*/
Rhy.updateAppConfigKey = function(key, value) {
var options = {updateAppConfigKey: key};
options[key] = value;
return nativeRequest(options);
};
/*
* @function callPhone - Call the given phone number if the
* device is capable of making calls.
* Else show the given message.
* @param {string} callPhone - The phone number.
* @optional
* @param {string} title - The title of the message to show if
* device is not capable of making
* calls.
* @param {string} message - The the message to show if device
* is not capable of making calls.
* @param {string} okButtonTitle - The title of the ok button.
*/
Rhy.callPhone = function(options) {
if (!options.title) {
options.title = "Message";
}
if (!options.message) {
options.message = "Cannot make call";
}
if (!options.okButtonTitle) {
options.okButtonTitle = "Ok";
}
if(!options.callPhone) {
options.callPhone = options.number;
}
nativeRequest(options);
};
Rhy.showNavigationBar = function(value) {
nativeRequest({
showNavigationBar: value,
});
}
Rhy.getValueForKey = function(key) {
return nativeRequest({
getValueForKey: key,
})
}
Rhy.setValueForKey = function(key, value) {
return nativeRequest({
setValueForKey: key,
value: value
})
}
Rhy.showAlert = function (title, message, okButtonTitle, callback) {
var options = {'showAlert': message, 'title': title, 'button_text': okButtonTitle};
if(callback) {
options.callback = callback;
}
return nativeRequest(options);
}
Rhy.showDatePicker = function() {
return nativeRequest();
}
Rhy.logout = function() {
return nativeRequest({'logout': 'YES'});
}
Rhy.showConfirmDialog = function(message, okbutton, cancelbutton, callback) {
return nativeRequest({'showConfirmDialog': message,
'buttonOk': okbutton,
'buttonCancel': cancelbutton,
'callback': callback});
}
// RHY.setiOSStatusBarColor = function (red, green, blue) {
// var options = {'setiOSStatusBarColor': red, 'green': green, 'blue': blue}
// return nativeRequest(options);
// }
})(window.Rhy = window.Rhy || {});