Dialogs plugin

This commit is contained in:
Max Lynch
2015-09-14 14:21:53 -05:00
parent fe028627b8
commit 7c005f2759
5 changed files with 131 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import {ContactsPage} from 'pages/contacts';
import {DevicePage} from 'pages/device';
import {DeviceMotionPage} from 'pages/device-motion';
import {DeviceOrientationPage} from 'pages/device-orientation';
import {DialogsPage} from 'pages/dialogs';
import {GeolocationPage} from 'pages/geolocation';
import {VibrationPage} from 'pages/vibration';
@ -28,6 +29,7 @@ class MyApp {
{title: 'Device', page: DevicePage},
{title: 'Device Motion', page: DeviceMotionPage},
{title: 'Device Orientation', page: DeviceOrientationPage},
{title: 'Dialogs', page: DialogsPage},
{title: 'Geolocation', page: GeolocationPage},
{title: 'Contacts', page: ContactsPage},
{title: 'Battery', page: BatteryPage},

View File

@ -0,0 +1,34 @@
import {IonicView, Dialogs} from 'ionic/ionic';
@IonicView({
template: `
<ion-navbar *navbar>
<a menu-toggle>
<icon menu></icon>
</a>
<ion-title>Dialogs</ion-title>
</ion-navbar>
<ion-content class="padding">
<h2>Device</h2>
<button primary outline (click)="doAlert()">Do Alert</button>
<button primary outline (click)="doConfirm()">Do Confirm</button>
<button primary outline (click)="doPrompt()">Do Prompt</button>
</ion-content>
`
})
export class DialogsPage {
doAlert() {
Dialogs.alert('Hello');
}
doConfirm() {
Dialogs.confirm('Do you want to click that?').then((resp) => {
console.log(resp);
});
}
doPrompt() {
Dialogs.prompt('What is your fav ice cream?').then((resp) => {
console.log(resp);
});
}
}

View 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);
}
}

View File

@ -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;
}
}
}

View File

@ -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'