diff --git a/ionic/components/app/test/native/index.ts b/ionic/components/app/test/native/index.ts new file mode 100644 index 0000000000..b81f6f0a89 --- /dev/null +++ b/ionic/components/app/test/native/index.ts @@ -0,0 +1,48 @@ +import {Component} from 'angular2/angular2'; +import {Control, ControlGroup} from 'angular2/forms'; + +import {App, Http, Camera, Geolocation} from 'ionic/ionic'; + +let testUrl = 'https://ionic-api-tester.herokuapp.com/json'; +let testUrl404 = 'https://ionic-api-tester.herokuapp.com/404'; + + +@App({ + templateUrl: 'main.html' +}) +class IonicApp { + constructor() { + } + 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') + }) + } +} diff --git a/ionic/components/app/test/native/main.html b/ionic/components/app/test/native/main.html new file mode 100644 index 0000000000..c3bb3771f3 --- /dev/null +++ b/ionic/components/app/test/native/main.html @@ -0,0 +1,17 @@ + + +

Camera

+ +

Geolocation

+ +
+ Fetching location... + {{location.coords.latitude}}, {{location.coords.longitude}} +
+ +
+ Fetching location... + {{trackLocation.coords.latitude}}, {{trackLocation.coords.longitude}} +
+
+
diff --git a/ionic/ionic.ts b/ionic/ionic.ts index 4766290aeb..aaa889dc98 100644 --- a/ionic/ionic.ts +++ b/ionic/ionic.ts @@ -23,3 +23,5 @@ export * from './animations/builtins' export * from './transitions/transition' export * from './transitions/ios-transition' export * from './transitions/md-transition' + +export * from './native/plugins' diff --git a/ionic/native/camera/camera.ts b/ionic/native/camera/camera.ts new file mode 100644 index 0000000000..3d27d96a59 --- /dev/null +++ b/ionic/native/camera/camera.ts @@ -0,0 +1,38 @@ +import * as util from 'ionic/util'; + +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.') + resolve(null); + return; + } + + var options = util.defaults({ + quality: 80, + destinationType: window.Camera.DestinationType.DATA_URL, + sourceType: window.Camera.PictureSourceType.CAMERA, + allowEdit: true, + encodingType: window.Camera.EncodingType.JPEG, + popoverOptions: window.CameraPopoverOptions, + saveToPhotoAlbum: false + }, options); + + navigator.camera.getPicture(function (imageData) { + resolve(imageData); + }, function (err) { + reject(err); + }, options); + }); + } + static cleanup() { + return new Promise((resolve, reject) => { + navigator.camera.cleanup(function () { + resolve(); + }, function (err) { + reject(err); + }); + }); + } +} diff --git a/ionic/native/geolocation/geolocation.ts b/ionic/native/geolocation/geolocation.ts new file mode 100644 index 0000000000..a20b3f5556 --- /dev/null +++ b/ionic/native/geolocation/geolocation.ts @@ -0,0 +1,39 @@ +import * as util from 'ionic/util'; + +import * as Rx from 'rx'; + +export class Geolocation { + static getCurrentPosition(options) { + return new Promise((resolve, reject) => { + navigator.geolocation.getCurrentPosition(function (result) { + resolve(result); + }, function (err) { + reject(err); + }, options); + }); + } + + static watchPosition(options) { + let watchID; + + let source = Rx.Observable.create((observer) => { + watchID = navigator.geolocation.watchPosition(function (result) { + observer.onNext(result) + }, function(err) { + observer.onError(err, observer); + }, options); + }) + + return { + source: source, + watchID: watchID, + clear: () => { + navigator.geolocation.clearWatch(watchID); + } + } + } + + static clearWatch(watchID) { + return navigator.geolocation.clearWatch(watchID); + } +} diff --git a/ionic/native/plugins.ts b/ionic/native/plugins.ts new file mode 100644 index 0000000000..05cefc25e0 --- /dev/null +++ b/ionic/native/plugins.ts @@ -0,0 +1,2 @@ +export * from './camera/camera' +export * from './geolocation/geolocation'