mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2026-03-13 10:22:08 +08:00
feat(vibration): native plugin
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,5 +13,7 @@
|
||||
<b *ng-if="gettingTrackLocation">Fetching location...</b>
|
||||
<b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b>
|
||||
</div>
|
||||
<h2>Vibration</h2>
|
||||
<button primary (click)="doVibrate()">Vibrate</button>
|
||||
</ion-content>
|
||||
</ion-view>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
25
ionic/native/plugin.ts
Normal file
25
ionic/native/plugin.ts
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,4 @@
|
||||
export * from './plugin'
|
||||
export * from './camera/camera'
|
||||
export * from './geolocation/geolocation'
|
||||
export * from './vibration/vibration'
|
||||
|
||||
21
ionic/native/vibration/vibration.ts
Normal file
21
ionic/native/vibration/vibration.ts
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user