diff --git a/demos/native/index.ts b/demos/native/index.ts index 4540eec0b6..6ede1ea4c5 100644 --- a/demos/native/index.ts +++ b/demos/native/index.ts @@ -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'; @@ -22,12 +23,13 @@ class MyApp { this.app = app; this.firstPage = CameraPage; - + this.plugins = [ {title: 'Camera', page: CameraPage}, {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}, diff --git a/demos/native/pages/dialogs.ts b/demos/native/pages/dialogs.ts new file mode 100644 index 0000000000..9a8390762c --- /dev/null +++ b/demos/native/pages/dialogs.ts @@ -0,0 +1,34 @@ +import {IonicView, Dialogs} from 'ionic/ionic'; + + +@IonicView({ + template: ` + + + + + Dialogs + + +

Device

+ + + +
+ ` +}) +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); + }); + } +} diff --git a/ionic/native/dialogs/dialogs.ts b/ionic/native/dialogs/dialogs.ts new file mode 100644 index 0000000000..1890478747 --- /dev/null +++ b/ionic/native/dialogs/dialogs.ts @@ -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); + } +} diff --git a/ionic/native/plugin.ts b/ionic/native/plugin.ts index 9b37805a92..2c5f660185 100644 --- a/ionic/native/plugin.ts +++ b/ionic/native/plugin.ts @@ -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; } } } diff --git a/ionic/native/plugins.ts b/ionic/native/plugins.ts index 48accba1c4..7a3b3c79c8 100644 --- a/ionic/native/plugins.ts +++ b/ionic/native/plugins.ts @@ -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'