forked from mdanishs/nativescript-toasts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
66 lines (62 loc) · 1.9 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
var app = require("tns-core-modules/application");
var frameModule = require("tns-core-modules/ui/frame");
/**
* The time duration for which toast will be displayed on the screen
* For Android the values will be mapped to Toast.LENGTH_LONG and Toast.LENGTH_SHORT
* For iOS the values will be mapped to the numeric values of Android enum which are 3.5 for LONG and 2 for SHORT
*/
var DURATION = {
LONG: "long",
SHORT: "short"
};
exports.DURATION = DURATION;
var POSITION = {
TOP: 'top',
BOTTOM: 'bottom',
CENTER: 'center'
};
exports.POSITION = POSITION;
/**
* show - displays the toast for iOS and Android
*
* @param {object} toastObject javascript object with properties text and duration. Duration by default is short for android.
* @return {void}
*/
exports.show = function (toastObject) {
var position;
if (app.ios) {
switch (toastObject.position) {
case POSITION.TOP:
position = "CSToastPositionTop";
break;
case POSITION.CENTER:
position = "CSToastPositionCenter";
break;
default:
position = "CSToastPositionBottom"; //default position = bottom
}
frameModule.topmost().ios.controller.view.makeToastDurationPosition(
toastObject.text,
toastObject.duration === DURATION.LONG ? 3.5 : 2.0,
position
);
} else if (app.android) {
switch (toastObject.position) {
case POSITION.TOP:
position = android.view.Gravity.TOP;
break;
case POSITION.CENTER:
position = android.view.Gravity.CENTER;
break;
default:
position = android.view.Gravity.BOTTOM; //default position = bottom
}
var toast = android.widget.Toast.makeText(
app.android.context,
toastObject.text,
toastObject.duration === DURATION.LONG ? android.widget.Toast.LENGTH_LONG : android.widget.Toast.LENGTH_SHORT
);
toast.setGravity(position, 0, 0);
toast.show();
}
};