mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-18 19:21:34 +08:00
Dialogs plugin
This commit is contained in:
85
ionic/native/dialogs/dialogs.ts
Normal file
85
ionic/native/dialogs/dialogs.ts
Normal file
@ -0,0 +1,85 @@
|
||||
import * as Rx from 'rx';
|
||||
|
||||
import * as util from 'ionic/util';
|
||||
import {NativePlugin} from '../plugin';
|
||||
|
||||
/**
|
||||
* A native dialogs system. Native dialogs can give you a bit more
|
||||
* control over the UI than the browser built-ins, though the Dialogs
|
||||
* plugin will fall back to the built-ins when necessary.
|
||||
*/
|
||||
@NativePlugin({
|
||||
name: 'Dialogs',
|
||||
platforms: {
|
||||
cordova: 'cordova-plugin-dialogs'
|
||||
}
|
||||
})
|
||||
export class Dialogs {
|
||||
/**
|
||||
* Trigger an alert prompt.
|
||||
*
|
||||
* @param message the message to show
|
||||
* @param title the title to show
|
||||
* @param buttonName the button label to use (not available on browser fallback)
|
||||
* @return Promise
|
||||
*/
|
||||
static alert(message, title, buttonName) {
|
||||
return new Promise((resolve,reject) => {
|
||||
if(!navigator.notification) {
|
||||
this.pluginWarn();
|
||||
alert(message);
|
||||
resolve();
|
||||
} else {
|
||||
navigator.notification.alert(message, () => {
|
||||
resolve();
|
||||
}, title, buttonName);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger a confirm prompt.
|
||||
*
|
||||
* @param message the message to show
|
||||
* @param title the title to show
|
||||
* @param buttonLabels the button labels to use (not available on browser fallback)
|
||||
* @return Promise that resolves with the index of the button selected (zero indexed). 1 is OK on browser fallback
|
||||
*/
|
||||
static confirm(message, title, buttonLabels) {
|
||||
return new Promise((resolve,reject) => {
|
||||
if(!navigator.notification) {
|
||||
this.pluginWarn();
|
||||
var ok = confirm(message);
|
||||
// Use 2 as OK
|
||||
resolve(ok ? 2 : 0);
|
||||
} else {
|
||||
navigator.notification.confirm(message, (buttonIndex) => {
|
||||
resolve(buttonIndex - 1);
|
||||
}, title, buttonLabels);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static prompt(message, title, buttonLabels, defaultText) {
|
||||
return new Promise((resolve,reject) => {
|
||||
if(!navigator.notification) {
|
||||
this.pluginWarn();
|
||||
var response = prompt(message);
|
||||
// Use 1 as OK
|
||||
resolve(response);
|
||||
} else {
|
||||
navigator.notification.prompt(message, (results) => {
|
||||
resolve(results.input1, buttonIndex - 1);
|
||||
}, title, buttonLabels, defaultText);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Beep n times. Not available on browser.
|
||||
* @param times the number of times to beep
|
||||
*/
|
||||
static beep(times) {
|
||||
navigator.notification && navigator.notification.beep(times);
|
||||
}
|
||||
}
|
@ -19,6 +19,11 @@ export class NativePluginDecorator {
|
||||
};
|
||||
|
||||
cls.pluginWarn = () => {
|
||||
if(cls._pluginWarned) {
|
||||
// Only warn once
|
||||
return;
|
||||
}
|
||||
|
||||
let platformString = [];
|
||||
for(var k in this.config.platforms) {
|
||||
platformString.push('\t' + k + ': '+ this.config.platforms[k]);
|
||||
@ -26,6 +31,9 @@ export class NativePluginDecorator {
|
||||
console.warn('Plugin for ' + this.config.name +
|
||||
' not installed. For native functionality, please install the correct plugin for your platform:\n' +
|
||||
platformString.join('\n'));
|
||||
|
||||
// Set a flag so we don't warn again
|
||||
cls._pluginWarned = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ export * from './plugin'
|
||||
export * from './battery/battery'
|
||||
export * from './camera/camera'
|
||||
export * from './contacts/contacts'
|
||||
export * from './dialogs/dialogs'
|
||||
export * from './device/device'
|
||||
export * from './device-motion/device-motion'
|
||||
export * from './device-orientation/device-orientation'
|
||||
|
Reference in New Issue
Block a user