From fa8094f520692ab98cf1c1570046f1236f544b37 Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Tue, 8 Sep 2015 12:04:16 -0500 Subject: [PATCH] feat(vibration): native plugin --- ionic/components/app/test/native/index.ts | 5 ++++- ionic/components/app/test/native/main.html | 2 ++ ionic/native/camera/camera.ts | 10 ++++++++- ionic/native/geolocation/geolocation.ts | 11 ++++++++-- ionic/native/plugin.ts | 25 ++++++++++++++++++++++ ionic/native/plugins.ts | 2 ++ ionic/native/vibration/vibration.ts | 21 ++++++++++++++++++ 7 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 ionic/native/plugin.ts create mode 100644 ionic/native/vibration/vibration.ts diff --git a/ionic/components/app/test/native/index.ts b/ionic/components/app/test/native/index.ts index b81f6f0a89..5d4b9f1d51 100644 --- a/ionic/components/app/test/native/index.ts +++ b/ionic/components/app/test/native/index.ts @@ -1,7 +1,7 @@ import {Component} from 'angular2/angular2'; import {Control, ControlGroup} from 'angular2/forms'; -import {App, Http, Camera, Geolocation} from 'ionic/ionic'; +import {App, Http, Camera, Geolocation, Vibration} from 'ionic/ionic'; let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; @@ -45,4 +45,7 @@ class IonicApp { alert('Unable to take picture') }) } + doVibrate() { + Vibration.vibrate(1000); + } } diff --git a/ionic/components/app/test/native/main.html b/ionic/components/app/test/native/main.html index c3bb3771f3..567e252430 100644 --- a/ionic/components/app/test/native/main.html +++ b/ionic/components/app/test/native/main.html @@ -13,5 +13,7 @@ Fetching location... {{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}} +

Vibration

+ diff --git a/ionic/native/camera/camera.ts b/ionic/native/camera/camera.ts index 3d27d96a59..a6a240e135 100644 --- a/ionic/native/camera/camera.ts +++ b/ionic/native/camera/camera.ts @@ -1,10 +1,18 @@ import * as util from 'ionic/util'; +import {NativePlugin} from '../plugin'; + +@NativePlugin({ + name: 'Camera', + platforms { + cordova: 'cordova-plugin-camera' + } +}) export class Camera { static getPicture(options) { return new Promise((resolve, reject) => { if (!navigator.camera) { - console.warn('Camera: no camera plugin installed. Pictures will not work.') + this.pluginWarn(); resolve(null); return; } diff --git a/ionic/native/geolocation/geolocation.ts b/ionic/native/geolocation/geolocation.ts index a20b3f5556..2cf79ae494 100644 --- a/ionic/native/geolocation/geolocation.ts +++ b/ionic/native/geolocation/geolocation.ts @@ -1,7 +1,14 @@ -import * as util from 'ionic/util'; - import * as Rx from 'rx'; +import * as util from 'ionic/util'; +import {NativePlugin} from '../plugin'; + +@NativePlugin({ + name: 'Geolocation', + platforms { + cordova: 'cordova-plugin-geolocation' + } +}) export class Geolocation { static getCurrentPosition(options) { return new Promise((resolve, reject) => { diff --git a/ionic/native/plugin.ts b/ionic/native/plugin.ts new file mode 100644 index 0000000000..b506519f1b --- /dev/null +++ b/ionic/native/plugin.ts @@ -0,0 +1,25 @@ +export class NativePluginDecorator { + constructor(cls, config) { + this.cls = cls; + this.config = config; + + cls.pluginWarn = () => { + let platformString = []; + for(var k in this.config.platforms) { + platformString.push('\t' + k + ': '+ this.config.platforms[k]); + } + console.warn('Plugin for ' + this.config.name + + ' not installed. For native functionality, please instead the correct plugin for your platform:\n' + + platformString.join('\n')); + } + } +} + +export function NativePlugin(config) { + return function(cls) { + var annotations = Reflect.getMetadata('annotations', cls) || []; + annotations.push(new NativePluginDecorator(cls, config)); + Reflect.defineMetadata('annotations', annotations, cls); + return cls; + } +} diff --git a/ionic/native/plugins.ts b/ionic/native/plugins.ts index 05cefc25e0..d8d776a0a0 100644 --- a/ionic/native/plugins.ts +++ b/ionic/native/plugins.ts @@ -1,2 +1,4 @@ +export * from './plugin' export * from './camera/camera' export * from './geolocation/geolocation' +export * from './vibration/vibration' diff --git a/ionic/native/vibration/vibration.ts b/ionic/native/vibration/vibration.ts new file mode 100644 index 0000000000..ba6c9bd2ce --- /dev/null +++ b/ionic/native/vibration/vibration.ts @@ -0,0 +1,21 @@ +import * as Rx from 'rx'; + +import * as util from 'ionic/util'; +import {NativePlugin} from '../plugin'; + +@NativePlugin({ + name: 'Vibration', + platforms { + cordova: 'cordova-plugin-vibration' + } +}) +export class Vibration { + static vibrate(pattern) { + this.pluginWarn(); + if(!navigator.vibrate) { + console.log('Vibrate (dev): ', pattern); + return; + } + navigator.vibrate(pattern); + } +}