UNPKG

8.55 kBMarkdownView Raw
1<!--
2# license: Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18-->
19
20# cordova-plugin-dialogs
21
22This plugin provides access to some native dialog UI elements
23via a global `navigator.notification` object.
24
25Although the object is attached to the global scoped `navigator`, it is not available until after the `deviceready` event.
26
27 document.addEventListener("deviceready", onDeviceReady, false);
28 function onDeviceReady() {
29 console.log(navigator.notification);
30 }
31
32:warning: Report issues on the [Apache Cordova issue tracker](https://issues.apache.org/jira/issues/?jql=project%20%3D%20CB%20AND%20status%20in%20%28Open%2C%20%22In%20Progress%22%2C%20Reopened%29%20AND%20resolution%20%3D%20Unresolved%20AND%20component%20%3D%20%22Plugin%20Dialogs%22%20ORDER%20BY%20priority%20DESC%2C%20summary%20ASC%2C%20updatedDate%20DESC)
33
34## Installation
35
36 cordova plugin add cordova-plugin-dialogs
37
38## Methods
39
40- `navigator.notification.alert`
41- `navigator.notification.confirm`
42- `navigator.notification.prompt`
43- `navigator.notification.beep`
44
45## navigator.notification.alert
46
47Shows a custom alert or dialog box. Most Cordova implementations use a native
48dialog box for this feature, but some platforms use the browser's `alert`
49function, which is typically less customizable.
50
51 navigator.notification.alert(message, alertCallback, [title], [buttonName])
52
53- __message__: Dialog message. _(String)_
54
55- __alertCallback__: Callback to invoke when alert dialog is dismissed. _(Function)_
56
57- __title__: Dialog title. _(String)_ (Optional, defaults to `Alert`)
58
59- __buttonName__: Button name. _(String)_ (Optional, defaults to `OK`)
60
61
62### Example
63
64 function alertDismissed() {
65 // do something
66 }
67
68 navigator.notification.alert(
69 'You are the winner!', // message
70 alertDismissed, // callback
71 'Game Over', // title
72 'Done' // buttonName
73 );
74
75### Supported Platforms
76
77- Amazon Fire OS
78- Android
79- BlackBerry 10
80- Firefox OS
81- iOS
82- Tizen
83- Windows Phone 7 and 8
84- Windows 8
85- Windows
86
87### Windows Phone 7 and 8 Quirks
88
89- There is no built-in browser alert, but you can bind one as follows to call `alert()` in the global scope:
90
91 window.alert = navigator.notification.alert;
92
93- Both `alert` and `confirm` are non-blocking calls, results of which are only available asynchronously.
94
95### Firefox OS Quirks:
96
97Both native-blocking `window.alert()` and non-blocking `navigator.notification.alert()` are available.
98
99### BlackBerry 10 Quirks
100`navigator.notification.alert('text', callback, 'title', 'text')` callback parameter is passed the number 1.
101
102## navigator.notification.confirm
103
104Displays a customizable confirmation dialog box.
105
106 navigator.notification.confirm(message, confirmCallback, [title], [buttonLabels])
107
108- __message__: Dialog message. _(String)_
109
110- __confirmCallback__: Callback to invoke with index of button pressed (1, 2, or 3) or when the dialog is dismissed without a button press (0). _(Function)_
111
112- __title__: Dialog title. _(String)_ (Optional, defaults to `Confirm`)
113
114- __buttonLabels__: Array of strings specifying button labels. _(Array)_ (Optional, defaults to [`OK,Cancel`])
115
116
117### confirmCallback
118
119The `confirmCallback` executes when the user presses one of the
120buttons in the confirmation dialog box.
121
122The callback takes the argument `buttonIndex` _(Number)_, which is the
123index of the pressed button. Note that the index uses one-based
124indexing, so the value is `1`, `2`, `3`, etc.
125
126### Example
127
128 function onConfirm(buttonIndex) {
129 alert('You selected button ' + buttonIndex);
130 }
131
132 navigator.notification.confirm(
133 'You are the winner!', // message
134 onConfirm, // callback to invoke with index of button pressed
135 'Game Over', // title
136 ['Restart','Exit'] // buttonLabels
137 );
138
139### Supported Platforms
140
141- Amazon Fire OS
142- Android
143- BlackBerry 10
144- Firefox OS
145- iOS
146- Tizen
147- Windows Phone 7 and 8
148- Windows 8
149- Windows
150
151### Windows Phone 7 and 8 Quirks
152
153- There is no built-in browser function for `window.confirm`, but you can bind it by assigning:
154
155 window.confirm = navigator.notification.confirm;
156
157- Calls to `alert` and `confirm` are non-blocking, so the result is only available asynchronously.
158
159### Windows Quirks
160
161- On Windows8/8.1 it is not possible to add more than three buttons to MessageDialog instance.
162
163- On Windows Phone 8.1 it's not possible to show dialog with more than two buttons.
164
165### Firefox OS Quirks:
166
167Both native-blocking `window.confirm()` and non-blocking `navigator.notification.confirm()` are available.
168
169## navigator.notification.prompt
170
171Displays a native dialog box that is more customizable than the browser's `prompt` function.
172
173 navigator.notification.prompt(message, promptCallback, [title], [buttonLabels], [defaultText])
174
175- __message__: Dialog message. _(String)_
176
177- __promptCallback__: Callback to invoke with index of button pressed (1, 2, or 3) or when the dialog is dismissed without a button press (0). _(Function)_
178
179- __title__: Dialog title _(String)_ (Optional, defaults to `Prompt`)
180
181- __buttonLabels__: Array of strings specifying button labels _(Array)_ (Optional, defaults to `["OK","Cancel"]`)
182
183- __defaultText__: Default textbox input value (`String`) (Optional, Default: empty string)
184
185### promptCallback
186
187The `promptCallback` executes when the user presses one of the buttons
188in the prompt dialog box. The `results` object passed to the callback
189contains the following properties:
190
191- __buttonIndex__: The index of the pressed button. _(Number)_ Note that the index uses one-based indexing, so the value is `1`, `2`, `3`, etc.
192
193
194
195- __input1__: The text entered in the prompt dialog box. _(String)_
196
197### Example
198
199 function onPrompt(results) {
200 alert("You selected button number " + results.buttonIndex + " and entered " + results.input1);
201 }
202
203 navigator.notification.prompt(
204 'Please enter your name', // message
205 onPrompt, // callback to invoke
206 'Registration', // title
207 ['Ok','Exit'], // buttonLabels
208 'Jane Doe' // defaultText
209 );
210
211### Supported Platforms
212
213- Amazon Fire OS
214- Android
215- Firefox OS
216- iOS
217- Windows Phone 7 and 8
218- Windows 8
219- Windows
220
221### Android Quirks
222
223- Android supports a maximum of three buttons, and ignores any more than that.
224
225- On Android 3.0 and later, buttons are displayed in reverse order for devices that use the Holo theme.
226
227### Windows Quirks
228
229- On Windows prompt dialog is html-based due to lack of such native api.
230
231### Firefox OS Quirks:
232
233Both native-blocking `window.prompt()` and non-blocking `navigator.notification.prompt()` are available.
234
235## navigator.notification.beep
236
237The device plays a beep sound.
238
239 navigator.notification.beep(times);
240
241- __times__: The number of times to repeat the beep. _(Number)_
242
243### Example
244
245 // Beep twice!
246 navigator.notification.beep(2);
247
248### Supported Platforms
249
250- Amazon Fire OS
251- Android
252- BlackBerry 10
253- iOS
254- Tizen
255- Windows Phone 7 and 8
256- Windows 8
257
258### Amazon Fire OS Quirks
259
260- Amazon Fire OS plays the default __Notification Sound__ specified under the __Settings/Display & Sound__ panel.
261
262### Android Quirks
263
264- Android plays the default __Notification ringtone__ specified under the __Settings/Sound & Display__ panel.
265
266### Windows Phone 7 and 8 Quirks
267
268- Relies on a generic beep file from the Cordova distribution.
269
270### Tizen Quirks
271
272- Tizen implements beeps by playing an audio file via the media API.
273
274- The beep file must be short, must be located in a `sounds` subdirectory of the application's root directory, and must be named `beep.wav`.