UNPKG

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