mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-19 11:41:20 +08:00
Stuff
This commit is contained in:
@ -5,58 +5,29 @@ import {App, Http} from 'ionic/ionic';
|
||||
|
||||
import {Camera, Geolocation, Vibration, Battery, Device} from 'ionic/ionic';
|
||||
|
||||
let testUrl = 'https://ionic-api-tester.herokuapp.com/json';
|
||||
let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404';
|
||||
|
||||
import {CameraPage} from 'pages/camera';
|
||||
import {BatteryPage} from 'pages/battery';
|
||||
import {ContactsPage} from 'pages/contacts';
|
||||
import {DevicePage} from 'pages/device';
|
||||
import {DeviceMotionPage} from 'pages/device-motion';
|
||||
import {GeolocationPage} from 'pages/geolocation';
|
||||
import {VibrationPage} from 'pages/vibration';
|
||||
|
||||
@App({
|
||||
templateUrl: 'main.html'
|
||||
})
|
||||
class IonicApp {
|
||||
constructor() {
|
||||
}
|
||||
doDevice() {
|
||||
let device = Device.getDevice();
|
||||
console.log('Got device', device);
|
||||
}
|
||||
doGetLocation() {
|
||||
console.log('Getting location');
|
||||
this.gettingLocation = true;
|
||||
Geolocation.getCurrentPosition().then((pos) => {
|
||||
this.gettingLocation = false;
|
||||
console.log('Got location', pos);
|
||||
this.location = pos;
|
||||
}, (err) => {
|
||||
this.gettingLocation = false;
|
||||
console.error('Unable to get location', err);
|
||||
});
|
||||
}
|
||||
doTrackLocation() {
|
||||
this.gettingTrackLocation = true;
|
||||
Geolocation.watchPosition().source.subscribe((pos) => {
|
||||
this.gettingTrackLocation = false;
|
||||
console.log('Got location', pos);
|
||||
this.trackLocation = pos;
|
||||
}, (err) => {
|
||||
this.gettingTrackLocation = false;
|
||||
console.error('Unable to get location', pos);
|
||||
});
|
||||
}
|
||||
getPicture() {
|
||||
Camera.getPicture({
|
||||
|
||||
}).then(data => {
|
||||
console.log('Data', data);
|
||||
}, err => {
|
||||
alert('Unable to take picture')
|
||||
})
|
||||
}
|
||||
doVibrate() {
|
||||
Vibration.vibrate(1000);
|
||||
}
|
||||
doBatteryStatus() {
|
||||
Battery.getStatus().then((battery) => {
|
||||
this.battery = battery;
|
||||
});
|
||||
this.firstPage = CameraPage;
|
||||
console.log('First page', CameraPage);
|
||||
this.plugins = [
|
||||
{title: 'Camera', page: CameraPage},
|
||||
{title: 'Device', page: DevicePage},
|
||||
{title: 'Device Motion', page: DeviceMotionPage},
|
||||
{title: 'Geolocation', page: GeolocationPage},
|
||||
{title: 'Contacts', page: ContactsPage},
|
||||
{title: 'Battery', page: BatteryPage},
|
||||
{title: 'Vibration', page: VibrationPage},
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +1,12 @@
|
||||
<ion-view>
|
||||
<ion-content padding>
|
||||
<h2>Device</h2>
|
||||
<button primary outline (click)="doDevice()">Get Device</button>
|
||||
<div>
|
||||
</div>
|
||||
<h2>Camera</h2>
|
||||
<button primary outline (click)="getPicture()">Get Picture</button>
|
||||
<h2>Geolocation</h2>
|
||||
<button primary outline (click)="doGetLocation()">Get Location</button>
|
||||
<div>
|
||||
<b *ng-if="gettingLocation">Fetching location...</b>
|
||||
<b *ng-if="location">{{location.coords.latitude}}, {{location.coords.longitude}}</b>
|
||||
</div>
|
||||
<button primary outline (click)="doTrackLocation()">Track Location</button>
|
||||
<div>
|
||||
<b *ng-if="gettingTrackLocation">Fetching location...</b>
|
||||
<b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b>
|
||||
</div>
|
||||
<h2>Vibration</h2>
|
||||
<button primary outline (click)="doVibrate()">Vibrate</button>
|
||||
<h2>Battery</h2>
|
||||
<button primary outline (click)="doBatteryStatus()">Get Status</button>
|
||||
<div *ng-if="battery">
|
||||
Battery charging: <b>{{battery.charging}}</b><br>
|
||||
Battery level: <b>{{battery.level * 100}}</b>%<br>
|
||||
Battery charging time: <b>{{battery.chargingTime}}</b>s<br>
|
||||
Battery discharging time: <b>{{battery.dischargingTime}}</b>s<br>
|
||||
</div>
|
||||
<h2>Contacts</h2>
|
||||
<div>
|
||||
</div>
|
||||
|
||||
<ion-aside #aside [content]="content" id="mainMenu">
|
||||
<ion-toolbar><ion-title>Plugins</ion-title></ion-toolbar>
|
||||
<ion-content>
|
||||
<ion-list>
|
||||
<ion-item *ng-for="#p of plugins" (^click)="openPage(aside, p)">
|
||||
<span>{{p.title}}</span>
|
||||
</ion-item>
|
||||
</ion-list>
|
||||
</ion-content>
|
||||
</ion-view>
|
||||
</ion-aside>
|
||||
|
||||
<ion-nav #content swipe-back-enabled="false" [root]="firstPage"></ion-nav>
|
||||
|
31
ionic/components/app/test/native/pages/battery.ts
Normal file
31
ionic/components/app/test/native/pages/battery.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import {IonicView} from 'ionic/ionic';
|
||||
|
||||
|
||||
@IonicView({
|
||||
template: `
|
||||
<ion-navbar *navbar>
|
||||
<button aside-toggle>
|
||||
<icon menu></icon>
|
||||
</button>
|
||||
<ion-title>Battery</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
<h2>Battery</h2>
|
||||
<button primary outline (click)="doBatteryStatus()">Get Status</button>
|
||||
<div *ng-if="battery">
|
||||
Battery charging: <b>{{battery.charging}}</b><br>
|
||||
Battery level: <b>{{battery.level * 100}}</b>%<br>
|
||||
Battery charging time: <b>{{battery.chargingTime}}</b>s<br>
|
||||
Battery discharging time: <b>{{battery.dischargingTime}}</b>s<br>
|
||||
</div>
|
||||
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class BatteryPage {
|
||||
doBatteryStatus() {
|
||||
Battery.getStatus().then((battery) => {
|
||||
this.battery = battery;
|
||||
});
|
||||
}
|
||||
}
|
32
ionic/components/app/test/native/pages/camera.ts
Normal file
32
ionic/components/app/test/native/pages/camera.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import {IonicView} from 'ionic/ionic';
|
||||
|
||||
import {Camera} from 'ionic/ionic';
|
||||
|
||||
@IonicView({
|
||||
template: `
|
||||
<ion-navbar *navbar>
|
||||
<button aside-toggle>
|
||||
<icon menu></icon>
|
||||
</button>
|
||||
<ion-title>Camera</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
<h2>Camera</h2>
|
||||
<button primary outline (click)="getPicture()">Get Picture</button>
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class CameraPage {
|
||||
constructor() {
|
||||
|
||||
}
|
||||
getPicture() {
|
||||
Camera.getPicture({
|
||||
|
||||
}).then(data => {
|
||||
console.log('Data', data);
|
||||
}, err => {
|
||||
alert('Unable to take picture')
|
||||
})
|
||||
}
|
||||
}
|
22
ionic/components/app/test/native/pages/contacts.ts
Normal file
22
ionic/components/app/test/native/pages/contacts.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import {IonicView} from 'ionic/ionic';
|
||||
|
||||
import {Contacts} from 'ionic/ionic';
|
||||
|
||||
@IonicView({
|
||||
template: `
|
||||
<ion-navbar *navbar>
|
||||
<button aside-toggle>
|
||||
<icon menu></icon>
|
||||
</button>
|
||||
<ion-title>Contacts</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
<h2>Contacts</h2>
|
||||
<div>
|
||||
</div>
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class ContactsPage {
|
||||
|
||||
}
|
18
ionic/components/app/test/native/pages/device-motion.ts
Normal file
18
ionic/components/app/test/native/pages/device-motion.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import {IonicView} from 'ionic/ionic';
|
||||
|
||||
|
||||
@IonicView({
|
||||
template: `
|
||||
<ion-navbar *navbar>
|
||||
<button aside-toggle>
|
||||
<icon menu></icon>
|
||||
</button>
|
||||
<ion-title>Device Motion</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class DeviceMotionPage {
|
||||
|
||||
}
|
26
ionic/components/app/test/native/pages/device.ts
Normal file
26
ionic/components/app/test/native/pages/device.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import {IonicView} from 'ionic/ionic';
|
||||
|
||||
|
||||
@IonicView({
|
||||
template: `
|
||||
<ion-navbar *navbar>
|
||||
<button aside-toggle>
|
||||
<icon menu></icon>
|
||||
</button>
|
||||
<ion-title>Vibration</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
<h2>Device</h2>
|
||||
<button primary outline (click)="doDevice()">Get Device</button>
|
||||
<div>
|
||||
</div>
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class DevicePage {
|
||||
|
||||
doDevice() {
|
||||
let device = Device.getDevice();
|
||||
console.log('Got device', device);
|
||||
}
|
||||
}
|
52
ionic/components/app/test/native/pages/geolocation.ts
Normal file
52
ionic/components/app/test/native/pages/geolocation.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import {IonicView} from 'ionic/ionic';
|
||||
|
||||
import {Geolocation} from 'ionic/ionic';
|
||||
|
||||
@IonicView({
|
||||
template: `
|
||||
<ion-navbar *navbar>
|
||||
<button aside-toggle>
|
||||
<icon menu></icon>
|
||||
</button>
|
||||
<ion-title>Vibration</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
<h2>Geolocation</h2>
|
||||
<button primary outline (click)="doGetLocation()">Get Location</button>
|
||||
<div>
|
||||
<b *ng-if="gettingLocation">Fetching location...</b>
|
||||
<b *ng-if="location">{{location.coords.latitude}}, {{location.coords.longitude}}</b>
|
||||
</div>
|
||||
<button primary outline (click)="doTrackLocation()">Track Location</button>
|
||||
<div>
|
||||
<b *ng-if="gettingTrackLocation">Fetching location...</b>
|
||||
<b *ng-if="trackLocation">{{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}}</b>
|
||||
</div>
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class GeolocationPage {
|
||||
doGetLocation() {
|
||||
console.log('Getting location');
|
||||
this.gettingLocation = true;
|
||||
Geolocation.getCurrentPosition().then((pos) => {
|
||||
this.gettingLocation = false;
|
||||
console.log('Got location', pos);
|
||||
this.location = pos;
|
||||
}, (err) => {
|
||||
this.gettingLocation = false;
|
||||
console.error('Unable to get location', err);
|
||||
});
|
||||
}
|
||||
doTrackLocation() {
|
||||
this.gettingTrackLocation = true;
|
||||
Geolocation.watchPosition().source.subscribe((pos) => {
|
||||
this.gettingTrackLocation = false;
|
||||
console.log('Got location', pos);
|
||||
this.trackLocation = pos;
|
||||
}, (err) => {
|
||||
this.gettingTrackLocation = false;
|
||||
console.error('Unable to get location', pos);
|
||||
});
|
||||
}
|
||||
}
|
23
ionic/components/app/test/native/pages/vibration.ts
Normal file
23
ionic/components/app/test/native/pages/vibration.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import {IonicView} from 'ionic/ionic';
|
||||
|
||||
import {Vibration} from 'ionic/ionic';
|
||||
|
||||
@IonicView({
|
||||
template: `
|
||||
<ion-navbar *navbar>
|
||||
<button aside-toggle>
|
||||
<icon menu></icon>
|
||||
</button>
|
||||
<ion-title>Vibration</ion-title>
|
||||
</ion-navbar>
|
||||
<ion-content class="padding">
|
||||
<h2>Vibration</h2>
|
||||
<button primary outline (click)="doVibrate()">Vibrate</button>
|
||||
</ion-content>
|
||||
`
|
||||
})
|
||||
export class VibrationPage {
|
||||
doVibrate() {
|
||||
Vibration.vibrate(1000);
|
||||
}
|
||||
}
|
@ -20,9 +20,11 @@ export class Battery {
|
||||
|
||||
} else {
|
||||
|
||||
window.addEventListener('batterystatus', (battery) => {
|
||||
resolve(Battery._format(battery));
|
||||
});
|
||||
var fnCb = function fnCb(battery) {
|
||||
resolve(battery);
|
||||
window.removeEventListener('batterystatus', fnCb);
|
||||
}
|
||||
window.addEventListener('batterystatus', fnCb);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
79
ionic/native/device-motion/device-motion.ts
Normal file
79
ionic/native/device-motion/device-motion.ts
Normal file
@ -0,0 +1,79 @@
|
||||
import * as Rx from 'rx';
|
||||
|
||||
import * as util from 'ionic/util';
|
||||
import {NativePlugin} from '../plugin';
|
||||
import {Platform} from '../../platform/platform';
|
||||
|
||||
@NativePlugin({
|
||||
name: 'Device Motion',
|
||||
platforms: {
|
||||
cordova: 'cordova-plugin-device-motion'
|
||||
}
|
||||
})
|
||||
export class DeviceMotion {
|
||||
getCurrentAcceleration() {
|
||||
return new Promise((resolve, reject) => {
|
||||
if(navigator.accelerometer) {
|
||||
navigator.accelerometer.getCurrentAcceleration(function (result) {
|
||||
resolve(result);
|
||||
}, function (err) {
|
||||
reject(err);
|
||||
});
|
||||
} else if(window.DeviceMotionEvent || ('listenForDeviceMovement' in window)) {
|
||||
var fnCb = function fnCb(eventData) {
|
||||
resolve(eventData);
|
||||
window.removeEventListener('devicemotion', fnCb);
|
||||
}
|
||||
window.addEventListener('devicemotion', fnCb);
|
||||
} else {
|
||||
this.pluginWarn();
|
||||
reject('The Device does not support device motion events.');
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
watchAcceleration(options) {
|
||||
if(navigator.accelerometer) {
|
||||
let watchID;
|
||||
|
||||
let source = Rx.Observable.create((observer) => {
|
||||
|
||||
watchID = navigator.accelerometer.watchAcceleration(function (result) {
|
||||
observer.onNext(result);
|
||||
}, function (err) {
|
||||
observer.onError(err, observer);
|
||||
}, options);
|
||||
|
||||
});
|
||||
|
||||
return {
|
||||
source: source,
|
||||
watchID: watchID,
|
||||
clear: () => {
|
||||
navigator.accelerometer.clearWatch(watchID);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let watchID;
|
||||
|
||||
let source = Rx.Observable.create((observer) => {
|
||||
|
||||
var fnCb = function fnCb(eventData) {
|
||||
observer.onNext(eventData);
|
||||
};
|
||||
|
||||
window.addEventListener('devicemotion', cbFn);
|
||||
|
||||
});
|
||||
|
||||
return {
|
||||
source: source,
|
||||
watchID: watchID,
|
||||
clear: () => {
|
||||
window.removeEventListener('devicemotion', cbFn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -3,5 +3,6 @@ export * from './battery/battery'
|
||||
export * from './camera/camera'
|
||||
export * from './contacts/contacts'
|
||||
export * from './device/device'
|
||||
export * from './device-motion/device-motion'
|
||||
export * from './geolocation/geolocation'
|
||||
export * from './vibration/vibration'
|
||||
|
Reference in New Issue
Block a user