1 | /*
|
2 | *
|
3 | * Licensed to the Apache Software Foundation (ASF) under one
|
4 | * or more contributor license agreements. See the NOTICE file
|
5 | * distributed with this work for additional information
|
6 | * regarding copyright ownership. The ASF licenses this file
|
7 | * to you under the Apache License, Version 2.0 (the
|
8 | * "License"); you may not use this file except in compliance
|
9 | * with the License. You may obtain a copy of the License at
|
10 | *
|
11 | * http://www.apache.org/licenses/LICENSE-2.0
|
12 | *
|
13 | * Unless required by applicable law or agreed to in writing,
|
14 | * software distributed under the License is distributed on an
|
15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
16 | * KIND, either express or implied. See the License for the
|
17 | * specific language governing permissions and limitations
|
18 | * under the License.
|
19 | *
|
20 | */
|
21 |
|
22 | var exec = require('cordova/exec');
|
23 | var platform = require('cordova/platform');
|
24 |
|
25 | /**
|
26 | * Provides access to notifications on the device.
|
27 | */
|
28 |
|
29 | module.exports = {
|
30 |
|
31 | /**
|
32 | * Open a native alert dialog, with a customizable title and button text.
|
33 | *
|
34 | * @param {String} message Message to print in the body of the alert
|
35 | * @param {Function} completeCallback The callback that is called when user clicks on a button.
|
36 | * @param {String} title Title of the alert dialog (default: Alert)
|
37 | * @param {String} buttonLabel Label of the close button (default: OK)
|
38 | */
|
39 | alert: function (message, completeCallback, title, buttonLabel) {
|
40 | var _message = (typeof message === 'string' ? message : JSON.stringify(message));
|
41 | var _title = (typeof title === 'string' ? title : 'Alert');
|
42 | var _buttonLabel = (buttonLabel && typeof buttonLabel === 'string' ? buttonLabel : 'OK');
|
43 | exec(completeCallback, null, 'Notification', 'alert', [_message, _title, _buttonLabel]);
|
44 | },
|
45 |
|
46 | /**
|
47 | * Open a native confirm dialog, with a customizable title and button text.
|
48 | * The result that the user selects is returned to the result callback.
|
49 | *
|
50 | * @param {String} message Message to print in the body of the alert
|
51 | * @param {Function} resultCallback The callback that is called when user clicks on a button.
|
52 | * @param {String} title Title of the alert dialog (default: Confirm)
|
53 | * @param {Array} buttonLabels Array of the labels of the buttons (default: ['OK', 'Cancel'])
|
54 | */
|
55 | confirm: function (message, resultCallback, title, buttonLabels) {
|
56 | var _message = (typeof message === 'string' ? message : JSON.stringify(message));
|
57 | var _title = (typeof title === 'string' ? title : 'Confirm');
|
58 | var _buttonLabels = (buttonLabels || ['OK', 'Cancel']);
|
59 |
|
60 | // Strings are deprecated!
|
61 | if (typeof _buttonLabels === 'string') {
|
62 | console.log('Notification.confirm(string, function, string, string) is deprecated. Use Notification.confirm(string, function, string, array).');
|
63 | }
|
64 |
|
65 | _buttonLabels = convertButtonLabels(_buttonLabels);
|
66 |
|
67 | exec(resultCallback, null, 'Notification', 'confirm', [_message, _title, _buttonLabels]);
|
68 | },
|
69 |
|
70 | /**
|
71 | * Open a native prompt dialog, with a customizable title and button text.
|
72 | * The following results are returned to the result callback:
|
73 | * buttonIndex Index number of the button selected.
|
74 | * input1 The text entered in the prompt dialog box.
|
75 | *
|
76 | * @param {String} message Dialog message to display (default: "Prompt message")
|
77 | * @param {Function} resultCallback The callback that is called when user clicks on a button.
|
78 | * @param {String} title Title of the dialog (default: "Prompt")
|
79 | * @param {Array} buttonLabels Array of strings for the button labels (default: ["OK","Cancel"])
|
80 | * @param {String} defaultText Textbox input value (default: empty string)
|
81 | */
|
82 | prompt: function (message, resultCallback, title, buttonLabels, defaultText) {
|
83 | var _message = (typeof message === 'string' ? message : JSON.stringify(message));
|
84 | var _title = (typeof title === 'string' ? title : 'Prompt');
|
85 | var _buttonLabels = (buttonLabels || ['OK', 'Cancel']);
|
86 |
|
87 | // Strings are deprecated!
|
88 | if (typeof _buttonLabels === 'string') {
|
89 | console.log('Notification.prompt(string, function, string, string) is deprecated. Use Notification.confirm(string, function, string, array).');
|
90 | }
|
91 |
|
92 | _buttonLabels = convertButtonLabels(_buttonLabels);
|
93 |
|
94 | var _defaultText = (defaultText || '');
|
95 | exec(resultCallback, null, 'Notification', 'prompt', [_message, _title, _buttonLabels, _defaultText]);
|
96 | },
|
97 |
|
98 | /**
|
99 | * Causes the device to beep.
|
100 | * On Android, the default notification ringtone is played "count" times.
|
101 | *
|
102 | * @param {Integer} count The number of beeps.
|
103 | */
|
104 | beep: function (count) {
|
105 | var defaultedCount = count || 1;
|
106 | exec(null, null, 'Notification', 'beep', [ defaultedCount ]);
|
107 | }
|
108 | };
|
109 |
|
110 | function convertButtonLabels (buttonLabels) {
|
111 |
|
112 | // Some platforms take an array of button label names.
|
113 | // Other platforms take a comma separated list.
|
114 | // For compatibility, we convert to the desired type based on the platform.
|
115 | if (platform.id === 'amazon-fireos' || platform.id === 'android' || platform.id === 'ios' ||
|
116 | platform.id === 'windowsphone' || platform.id === 'firefoxos' || platform.id === 'ubuntu' ||
|
117 | platform.id === 'windows8' || platform.id === 'windows') {
|
118 |
|
119 | if (typeof buttonLabels === 'string') {
|
120 | buttonLabels = buttonLabels.split(','); // not crazy about changing the var type here
|
121 | }
|
122 | } else {
|
123 | if (Array.isArray(buttonLabels)) {
|
124 | var buttonLabelArray = buttonLabels;
|
125 | buttonLabels = buttonLabelArray.toString();
|
126 | }
|
127 | }
|
128 |
|
129 | return buttonLabels;
|
130 | }
|