diff --git a/Definitions/phonegap-2.2.d.ts b/Definitions/phonegap-2.2.d.ts index ceb87f325..8b2e30791 100644 --- a/Definitions/phonegap-2.2.d.ts +++ b/Definitions/phonegap-2.2.d.ts @@ -48,7 +48,233 @@ interface Camera { cleanup(cameraSuccess: (imageData: string) => void , cameraError: (message: string) => void ): void; } +interface CaptureAudioOptions { + limit: number; + duration: number; + mode: number; +} + +interface MediaFile { + name: string; + fullPath: string; + type: string; + lastModifiedDate: Date; + size: number; + + getFormatData(successCallback: Function, errorCallback?: Function): void; +} + +interface Capture { + captureAudio(captureSuccess: (mediaFiles: MediaFile[]) => void , captureError: (error: CaptureError) =>void , options?: CaptureAudioOptions); +} + +interface CompassOptions { + frequency?: number; + filter?: number; +} + +interface CompassHeading { + magneticHeading?: number; + trueHeading?: number; + headingAccuracy?: number; + timestamp?: number; +} + +interface CompassError { + code: number; +} + +interface Compass { + getCurrentHeading(compassSuccess: (heading: CompassHeading) => void , compassError: (error: CompassError) => void , compassOptions?: CompassOptions): void; + watchHeading(compassSuccess: (heading: CompassHeading) => void , compassError: (error: CompassError) => void , compassOptions?: CompassOptions): void; + clearWatch(watchID: number): void; +} + +interface Connection { + type: number; +} + +interface ContactAddress { + pref: bool; + type: string; + formatted: string; + streetAddress: string; + locality: string; + region: string; + postalCode: string; + country: string; +} + +interface ContactField { + type: string; + value: string; + pref: bool; +} + +interface Contact { + id: string; + displayName: string; + name: ContactName; + nickname: string; + phoneNumbers: ContactField[]; + emails: ContactField[]; + addresses: ContactAddress[]; + ims: ContactField[]; + organizations: ContactOrganization[]; + birthday: Date; + note: string; + photos: ContactField[]; + categories: ContactField[]; + urls: ContactField[]; +} + +interface ContactFindOptions { + filter?: string; + multiple?: bool; +} + +interface ContactName { + formatted: string; + familyName: string; + givenName: string; + middleName: string; + honorificPrefix: string; + honorificSuffix: string; +} + +interface ContactOrganization { + pref: bool; + type: string; + name: string; + department: string; + title: string; +} + +interface ContactError { + code: number; +} + +interface Contacts { + create(properties: any): void; + find(contactFields: string[], contactSuccess: (contacts: Contact[]) => void , contactError: (error: ContactError) => void , contactFindOptions?: ContactFindOptions): void; +} + +interface Device { + name: string; + cordova: string; + platform: string; + uuid: string; + version: string; +} + +interface File { +} + +interface GeolocationOptions { + enableHighAccuracy: bool; + timeout: number; + maximumAge: number; +} + +interface Geolocation { + getCurrentPosition(geolocationSuccess: (position: Position) => void , geolocationError?: PositionErrorCallback, geolocationOptions?: GeolocationOptions): void; + watchPosition(geolocationSuccess: (position: Position) => void , geolocationError?: PositionErrorCallback, geolocationOptions?: GeolocationOptions): void; + clearWatch(watchID: number): void; +} + +interface GlobalizationError { + code: number; + message: string; +} + +interface Globalization { + getPreferredLanguage(successCB, errorCB): void; + getLocaleName(successCB, errorCB): void; + dateToString(date, successCB, errorCB, options): void; + stringToDate(dateString, successCB, errorCB, options): void; + getDatePattern(successCB, errorCB, options): void; + getDateNames(successCB, errorCB, options): void; + isDayLightSavingsTime(date, successCB, errorCB): void; + getFirstDayOfWeek(successCB, errorCB): void; + numberToString(number, successCB, errorCB, options): void; + stringToNumber(string, successCB, errorCB, options): void; + getNumberPattern(successCB, errorCB, options): void; + getCurrencyPattern(currencyCode, successCB, errorCB): void; +} + +interface Media { + new (src: string, mediaSuccess: Function, mediaError?: MediaError, mediaStatus?: Function); + getCurrentPosition(mediaSuccess: Function, mediaError?: MediaError): void; + getDuration(): void; + play(): void; + pause(): void; + release(): void; + seekTo(milliseconds: number): void; + startRecord(): void; + stopRecord(): void; + stop(): void; +} + +interface Notification { + alert(message: string, alertCallback: Function, title?: string, buttonName?: string): void; + confirm(message: string, confirmCallback: Function, title?: string, buttonLabels?: string): void; + beep(times: number): void; + vibrate(milliseconds: number): void; +} + +interface Splashscreen { + show(): void; + hide(): void; +} + +interface Database { + transaction(); + changeVersion(); +} + +interface SQLResultSetRowList { + length: number; + item(index: number): any; +} + +interface SQLError { + code: number; + message: string; +} + +interface SQLResultSet { + insertId: number; + rowsAffected: number; + rows: SQLResultSetRowList; +} + +interface SQLTransaction { + executeSql(sql: string): SQLResultSet; +} + +interface LocalStorage { + key; + getItem; + setItem; + removeItem; + clear; +} + interface NavigatorPhonegap { accelerometer: Accelerometer; camera: Camera; + capture: Capture; + compass: Compass; + connection: Connection; + contacts: Contacts; + device: Device; + geolocation: Geolocation; + globalization: Globalization; + notification: Notification; + splashscreen: Splashscreen; +} + +interface WindowPhonegap { + openDatabase(database_name: string, database_version: string, database_displayname: string, database_size: number): Database; + localStorage: LocalStorage; } \ No newline at end of file diff --git a/README.md b/README.md index 720c029d2..83b68211a 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Complete * [Node.js](http://nodejs.org/) (from TypeScript samples) * [node_redis](https://github.com/mranney/node_redis) * [node-sqlserver](https://github.com/WindowsAzure/node-sqlserver) +* [PhoneGap](http://phonegap.com) * [PreloadJS](http://www.createjs.com/#!/PreloadJS) (by [Pedro Ferreira](https://bitbucket.org/drk4)) * [QUnit](http://qunitjs.com/) (by [Diullei Gomes](https://github.com/Diullei)) * [Raphael](http://raphaeljs.com/) (by [CheCoxshall](https://github.com/CheCoxshall)) @@ -54,7 +55,6 @@ Next * jQuery.Validate * google.visualization * Meteor -* PhoneGap * Isotope * Zepto * Socket.IO diff --git a/Tests/phonegap-tests.ts b/Tests/phonegap-tests.ts index b53bd0903..3e5fdd397 100644 --- a/Tests/phonegap-tests.ts +++ b/Tests/phonegap-tests.ts @@ -76,4 +76,340 @@ function test_camera() { function onFail(message) { alert('Failed because: ' + message); } +} + +function test_capture() { + var capture = navigator.device.capture; + function captureSuccess(mediaFiles) { + var i, len; + for (i = 0, len = mediaFiles.length; i < len; i += 1) { + uploadFile(mediaFiles[i]); + } + } + function captureError(error) { + var msg = 'An error occurred during capture: ' + error.code; + navigator.notification.alert(msg, null, 'Uh oh!'); + } + function captureAudio() { + navigator.device.capture.captureAudio(captureSuccess, captureError, { limit: 2 }); + } + function uploadFile(mediaFile) { + var ft = new FileTransfer(), + path = mediaFile.fullPath, + name = mediaFile.name; + ft.upload(path, + "http://my.domain.com/upload.php", + function (result) { + console.log('Upload success: ' + result.responseCode); + console.log(result.bytesSent + ' bytes sent'); + }, + function (error) { + console.log('Error uploading file ' + path + ': ' + error.code); + }, + { fileName: name }); + } + var options = { limit: 3, duration: 10 }; + navigator.device.capture.captureAudio(captureSuccess, captureError, options); + var captureSuccess = function (mediaFiles) { + var i, path, len; + for (i = 0, len = mediaFiles.length; i < len; i += 1) { + path = mediaFiles[i].fullPath; + } + }; + var captureError = function (error) { + navigator.notification.alert('Error code: ' + error.code, null, 'Capture Error'); + }; + navigator.device.capture.captureImage(captureSuccess, captureError, { limit: 2 }); + function captureSuccess(mediaFiles) { + var i, len; + for (i = 0, len = mediaFiles.length; i < len; i += 1) { + uploadFile(mediaFiles[i]); + } + } + function captureError(error) { + var msg = 'An error occurred during capture: ' + error.code; + navigator.notification.alert(msg, null, 'Uh oh!'); + } + function captureImage() { + navigator.device.capture.captureImage(captureSuccess, captureError, { limit: 2 }); + } + function uploadFile(mediaFile) { + var ft = new FileTransfer(), + path = mediaFile.fullPath, + name = mediaFile.name; + + ft.upload(path, + "http://my.domain.com/upload.php", + function (result) { + console.log('Upload success: ' + result.responseCode); + console.log(result.bytesSent + ' bytes sent'); + }, + function (error) { + console.log('Error uploading file ' + path + ': ' + error.code); + }, + { fileName: name }); + } + function captureSuccess(mediaFiles) { + var i, len; + for (i = 0, len = mediaFiles.length; i < len; i += 1) { + uploadFile(mediaFiles[i]); + } + } + function captureError(error) { + var msg = 'An error occurred during capture: ' + error.code; + navigator.notification.alert(msg, null, 'Uh oh!'); + } + function captureVideo() { + navigator.device.capture.captureVideo(captureSuccess, captureError, { limit: 2 }); + } + function uploadFile(mediaFile) { + var ft = new FileTransfer(), + path = mediaFile.fullPath, + name = mediaFile.name; + + ft.upload(path, + "http://my.domain.com/upload.php", + function (result) { + console.log('Upload success: ' + result.responseCode); + console.log(result.bytesSent + ' bytes sent'); + }, + function (error) { + console.log('Error uploading file ' + path + ': ' + error.code); + }, + { fileName: name }); + } +} + +function test_compass() { + document.addEventListener("deviceready", onDeviceReady, false); + function onDeviceReady() { + navigator.compass.getCurrentHeading(onSuccess, onError); + } + function onSuccess(heading) { + alert('Heading: ' + heading.magneticHeading); + } + function onError(compassError) { + alert('Compass Error: ' + compassError.code); + } + var watchID = null; + + + document.addEventListener("deviceready", onDeviceReady, false); + function onDeviceReady() { + startWatch(); + } + function startWatch() { + var options = { frequency: 3000 }; + watchID = navigator.compass.watchHeading(onSuccess, onError, options); + } + function stopWatch() { + if (watchID) { + navigator.compass.clearWatch(watchID); + watchID = null; + } + } + function onSuccess(heading) { + var element = document.getElementById('heading'); + element.innerHTML = 'Heading: ' + heading.magneticHeading; + } + function onError(compassError) { + alert('Compass error: ' + compassError.code); + } + + function startWatch() { + var options = { frequency: 3000 }; + watchID = navigator.compass.watchHeading(onSuccess, onError, options); + } + function stopWatch() { + if (watchID) { + navigator.compass.clearWatch(watchID); + watchID = null; + } + } + function onSuccess(heading) { + var element = document.getElementById('heading'); + element.innerHTML = 'Heading: ' + heading.magneticHeading; + } + function onError(compassError) { + alert('Compass error: ' + compassError.code); + } +} + +function test_connection() { + var networkState = navigator.connection.type; + var states = {}; + states[Connection.UNKNOWN] = 'Unknown connection'; + states[Connection.ETHERNET] = 'Ethernet connection'; + states[Connection.WIFI] = 'WiFi connection'; + states[Connection.CELL_2G] = 'Cell 2G connection'; + states[Connection.CELL_3G] = 'Cell 3G connection'; + states[Connection.CELL_4G] = 'Cell 4G connection'; + states[Connection.NONE] = 'No network connection'; + alert('Connection type: ' + states[networkState]); +} + +function test_contacts() { + var myContact = navigator.contacts.create({ "displayName": "Test User" }); + myContact.note = "This contact has a note."; + console.log("The contact, " + myContact.displayName + ", note: " + myContact.note); + + function onDeviceReady() { + var options = new ContactFindOptions(); + options.filter = "Bob"; + var fields = ["displayName", "name"]; + navigator.contacts.find(fields, onSuccess, onError, options); + } + function onSuccess(contacts) { + for (var i = 0; i < contacts.length; i++) { + console.log("Display Name = " + contacts[i].displayName); + } + } + function onError(contactError) { + alert('onError!'); + } + + var contact = navigator.contacts.create(); + contact.displayName = "Plumber"; + contact.nickname = "Plumber"; + var name = new ContactName(); + name.givenName = "Jane"; + name.familyName = "Doe"; + contact.name = name; + contact.save(onSuccess, onError); + + var clone = contact.clone(); + clone.name.givenName = "John"; + console.log("Original contact name = " + contact.name.givenName); + console.log("Cloned contact name = " + clone.name.givenName); + contact.remove(onSuccess, onError); + + function onDeviceReady() { + var contact = navigator.contacts.create(); + contact.displayName = "Plumber"; + contact.nickname = "Plumber"; + var name = new ContactName(); + name.givenName = "Jane"; + name.familyName = "Doe"; + contact.name = name; + contact.save(onSuccess, onError); + var clone = contact.clone(); + clone.name.givenName = "John"; + console.log("Original contact name = " + contact.name.givenName); + console.log("Cloned contact name = " + clone.name.givenName); + contact.remove(onSuccess, onError); + } + + var options = new ContactFindOptions(); + options.filter = ""; + var filter = ["displayName", "addresses"]; + navigator.contacts.find(filter, onSuccess, onError, options); + + for (var i = 0; i < contacts.length; i++) { + for (var j = 0; j < contacts[i].addresses.length; j++) { + alert("Pref: " + contacts[i].addresses[j].pref + "\n" + + "Type: " + contacts[i].addresses[j].type + "\n" + + "Formatted: " + contacts[i].addresses[j].formatted + "\n" + + "Street Address: " + contacts[i].addresses[j].streetAddress + "\n" + + "Locality: " + contacts[i].addresses[j].locality + "\n" + + "Region: " + contacts[i].addresses[j].region + "\n" + + "Postal Code: " + contacts[i].addresses[j].postalCode + "\n" + + "Country: " + contacts[i].addresses[j].country); + } + } + + var contact = navigator.contacts.create(); + var phoneNumbers = []; + phoneNumbers[0] = new ContactField('work', '212-555-1234', false); + phoneNumbers[1] = new ContactField('mobile', '917-555-5432', true); // preferred number + phoneNumbers[2] = new ContactField('home', '203-555-7890', false); + contact.phoneNumbers = phoneNumbers; + contact.save(); + + var options = new ContactFindOptions(); + options.filter = ""; + options.multiple = true; + filter = ["displayName"]; + navigator.contacts.find(filter, onSuccess, onError, options); +} + +function test_device() { + element.innerHTML = 'Device Name: ' + device.name + '
' + + 'Device Cordova: ' + device.cordova + '
' + + 'Device Platform: ' + device.platform + '
' + + 'Device UUID: ' + device.uuid + '
' + + 'Device Version: ' + device.version + '
'; +} + +function test_file() { + function onDeviceReady() { + window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); + } + function gotFS(fileSystem) { + fileSystem.root.getFile("readme.txt", null, gotFileEntry, fail); + } + function gotFileEntry(fileEntry) { + fileEntry.file(gotFile, fail); + } + function gotFile(file) { + readDataUrl(file); + readAsText(file); + } + function readDataUrl(file) { + var reader = new FileReader(); + reader.onloadend = function (evt) { + console.log("Read as data URL"); + console.log(evt.target.result); + }; + reader.readAsDataURL(file); + } + function readAsText(file) { + var reader = new FileReader(); + reader.onloadend = function (evt) { + console.log("Read as text"); + console.log(evt.target.result); + }; + reader.readAsText(file); + } + function fail(evt) { + console.log(evt.target.error.code); + } + + function onDeviceReady() { + window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); + } + function gotFS(fileSystem) { + fileSystem.root.getFile("readme.txt", { create: true, exclusive: false }, gotFileEntry, fail); + } + function gotFileEntry(fileEntry) { + fileEntry.createWriter(gotFileWriter, fail); + } + function gotFileWriter(writer) { + writer.onwriteend = function (evt) { + console.log("contents of file now 'some sample text'"); + writer.truncate(11); + writer.onwriteend = function (evt) { + console.log("contents of file now 'some sample'"); + writer.seek(4); + writer.write(" different text"); + writer.onwriteend = function (evt) { + console.log("contents of file now 'some different text'"); + } + }; + }; + writer.write("some sample text"); + } + function fail(error) { + console.log(error.code); + } + + function onDeviceReady() { + window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemSuccess, fail); + } + function onFileSystemSuccess(fileSystem) { + console.log(fileSystem.name); + console.log(fileSystem.root.name); + } + function fail(evt) { + console.log(evt.target.error.code); + } } \ No newline at end of file