ngCordova plugins: ActionSheet, Badge, File, FileTransfer

This commit is contained in:
pmccloghrylaing
2015-12-02 11:06:10 +11:00
committed by Phil McCloghry-Laing
parent 4910a7c52a
commit 64ed3fcbf0
9 changed files with 448 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
/// <reference path="tsd.d.ts" />
/// <reference path="../angularjs/angular.d.ts" />
module ngCordova {
'use strict';
angular.module('test')
// Adapted from http://ngcordova.com/docs/plugins/actionSheet/
.controller('ThisCtrl', function($cordovaActionSheet: ngCordova.IActionSheetService) {
var options = {
title: 'What do you want with this image?',
buttonLabels: ['Share via Facebook', 'Share via Twitter'],
addCancelButtonWithLabel: 'Cancel',
androidEnableCancelButton: true,
winphoneEnableCancelButton: true,
addDestructiveButtonWithLabel: 'Delete it'
};
document.addEventListener("deviceready", function() {
$cordovaActionSheet.show(options)
.then(function(btnIndex) {
var index: number = btnIndex;
});
}, false);
});
}
+22
View File
@@ -0,0 +1,22 @@
// Type definitions for ngCordova Action Sheet plugin
// Project: https://github.com/driftyco/ng-cordova
// Definitions by: Phil McCloghry-Laing <https://github.com/pmccloghrylaing>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../angularjs/angular.d.ts" />
declare module ngCordova {
export interface IActionSheetService {
show(options: ShowOptions): ng.IPromise<number>;
hide(): ng.IPromise<void>;
}
export interface ShowOptions {
title?: string;
buttonLabels?: string[];
addCancelButtonWithLabel?: string;
addDestructiveButtonWithLabel?: string;
androidEnableCancelButton?: boolean;
winphoneEnableCancelButton?: boolean;
}
}
+59
View File
@@ -0,0 +1,59 @@
/// <reference path="tsd.d.ts" />
/// <reference path="../angularjs/angular.d.ts" />
module ngCordova {
'use strict';
angular.module('test')
// Adapted from http://ngcordova.com/docs/plugins/badge/
.controller('ThisCtrl', function($cordovaBadge: ngCordova.IBadgeService) {
$cordovaBadge.hasPermission().then(function(yes) {
// You have permission
}, function(no) {
// You do not have permission
});
$cordovaBadge.set(3).then(function() {
// You have permission, badge set.
}, function(err) {
// You do not have permission.
});
$cordovaBadge.get().then(function(badge) {
// You have permission, badge returned.
var badgeNo: number = badge;
}, function(err) {
// You do not have permission.
});
$cordovaBadge.clear().then(function() {
// You have permission, badge cleared.
}, function(err) {
// You do not have permission.
});
$cordovaBadge.increase().then(function() {
// You have permission, badge increased.
}, function(err) {
// You do not have permission.
});
$cordovaBadge.increase(3).then(function() {
// You have permission, badge increased.
}, function(err) {
// You do not have permission.
});
$cordovaBadge.decrease().then(function() {
// You have permission, badge increased.
}, function(err) {
// You do not have permission.
});
$cordovaBadge.decrease(2).then(function() {
// You have permission, badge increased.
}, function(err) {
// You do not have permission.
});
});
}
+18
View File
@@ -0,0 +1,18 @@
// Type definitions for ngCordova badge plugin
// Project: https://github.com/driftyco/ng-cordova
// Definitions by: Phil McCloghry-Laing <https://github.com/pmccloghrylaing>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../angularjs/angular.d.ts" />
declare module ngCordova {
export interface IBadgeService {
hasPermission(): ng.IPromise<boolean>;
promptForPermission(): ng.IPromise<any>;
set(badge: number, callback?: Function, scope?: {}): ng.IPromise<any>;
get(): ng.IPromise<number>;
clear(callback?: Function, scope?: {}): ng.IPromise<any>;
increase(count?: number, callback?: Function, scope?: {}): ng.IPromise<any>;
decrease(count?: number, callback?: Function, scope?: {}): ng.IPromise<any>;
}
}
+184
View File
@@ -0,0 +1,184 @@
/// <reference path="tsd.d.ts" />
/// <reference path="../angularjs/angular.d.ts" />
/// <reference path="../cordova/cordova.d.ts" />
module ngCordova {
'use strict';
angular.module('test')
// Adapted from http://ngcordova.com/docs/plugins/file/
.controller('MyCtrl', function($scope: ng.IScope, $cordovaFile: ngCordova.IFileService) {
document.addEventListener('deviceready', function() {
$cordovaFile.getFreeDiskSpace()
.then(function(success) {
// success in kilobytes
var freeSpace: number = success;
}, function(error) {
// error
});
// CHECK
$cordovaFile.checkDir(cordova.file.dataDirectory, "dir/other_dir")
.then(function(success) {
// success
var dir: DirectoryEntry = success;
}, function(error) {
// error
});
$cordovaFile.checkFile(cordova.file.dataDirectory, "some_file.txt")
.then(function(success) {
// success
var fileResult: FileEntry = success;
}, function(error) {
// error
});
// CREATE
$cordovaFile.createDir(cordova.file.dataDirectory, "new_dir", false)
.then(function(success) {
// success
var dir: DirectoryEntry = success;
}, function(error) {
// error
});
$cordovaFile.createFile(cordova.file.dataDirectory, "new_file.txt", true)
.then(function(success) {
// success
var fileResult: FileEntry = success;
}, function(error) {
// error
});
// REMOVE
$cordovaFile.removeDir(cordova.file.dataDirectory, "some_dir")
.then(function(success) {
// success
if (success.success) {
var dirResult: DirectoryEntry = success.fileRemoved;
}
}, function(error) {
// error
});
$cordovaFile.removeFile(cordova.file.dataDirectory, "some_file.txt")
.then(function(success) {
// success
if (success.success) {
var fileResult: FileEntry = success.fileRemoved;
}
}, function(error) {
// error
});
$cordovaFile.removeRecursively(cordova.file.dataDirectory, "")
.then(function(success) {
// success
if (success.success) {
var dirResult: DirectoryEntry = success.fileRemoved;
}
}, function(error) {
// error
});
// WRITE
$cordovaFile.writeFile(cordova.file.dataDirectory, "file.txt", "text", true)
.then(function(success) {
// success
var endEvent: ProgressEvent = success;
}, function(error) {
// error
});
$cordovaFile.writeExistingFile(cordova.file.dataDirectory, "file.txt", "text")
.then(function(success) {
// success
var endEvent: ProgressEvent = success;
}, function(error) {
// error
});
// READ
$cordovaFile.readAsText(cordova.file.dataDirectory, "file.txt")
.then(function(success) {
// success
var text: string = success;
}, function(error) {
// error
});
$cordovaFile.readAsDataURL(cordova.file.dataDirectory, "file.txt")
.then(function(success) {
// success
var text: string = success;
}, function(error) {
// error
});
$cordovaFile.readAsBinaryString(cordova.file.dataDirectory, "file.txt")
.then(function(success) {
// success
var text: string = success;
}, function(error) {
// error
});
$cordovaFile.readAsArrayBuffer(cordova.file.dataDirectory, "file.txt")
.then(function(success) {
// success
var buffer: ArrayBuffer = success;
}, function(error) {
// error
});
// MOVE
$cordovaFile.moveDir(cordova.file.dataDirectory, "dir", cordova.file.tempDirectory, "new_dir")
.then(function(success) {
// success
var dirResult: DirectoryEntry = success;
}, function(error) {
// error
});
$cordovaFile.moveFile(cordova.file.dataDirectory, "file.txt", cordova.file.tempDirectory)
.then(function(success) {
// success
var fileResult: FileEntry = success;
}, function(error) {
// error
});
// COPY
$cordovaFile.copyDir(cordova.file.dataDirectory, "dir", cordova.file.tempDirectory, "new_dir")
.then(function(success) {
// success
var dirResult: DirectoryEntry = success;
}, function(error) {
// error
});
$cordovaFile.copyFile(cordova.file.dataDirectory, "file.txt", cordova.file.tempDirectory, "new_file.txt")
.then(function(success) {
// success
var fileResult: FileEntry = success;
}, function(error) {
// error
});
});
});
}
+51
View File
@@ -0,0 +1,51 @@
// Type definitions for ngCordova file plugin
// Project: https://github.com/driftyco/ng-cordova
// Definitions by: Phil McCloghry-Laing <https://github.com/pmccloghrylaing>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../angularjs/angular.d.ts" />
/// <reference path="../cordova/plugins/FileSystem.d.ts" />
declare module ngCordova {
export interface IFileService {
getFreeDiskSpace(): IFilePromise<number>;
checkDir(path: string, directory: string): IFilePromise<DirectoryEntry>;
checkFile(path: string, file: string): IFilePromise<FileEntry>;
createDir(path: string, directory: string, replace?: boolean): IFilePromise<DirectoryEntry>;
createFile(path: string, file: string, replace?: boolean): IFilePromise<FileEntry>;
removeDir(path: string, directory: string): IFilePromise<IFileRemoveResult<DirectoryEntry>>;
removeFile(path: string, file: string): IFilePromise<IFileRemoveResult<FileEntry>>;
removeRecursively(path: string, directory: string): IFilePromise<IFileRemoveResult<DirectoryEntry>>;
writeFile(path: string, file: string, text: string | Blob, replace?: boolean): IFilePromise<ProgressEvent>;
writeExistingFile(path: string, file: string, text: string | Blob): IFilePromise<ProgressEvent>;
readAsText(path: string, file: string): ng.IPromise<string>;
readAsDataURL(path: string, file: string): ng.IPromise<string>;
readAsBinaryString(path: string, file: string): ng.IPromise<string>;
readAsArrayBuffer(path: string, file: string): ng.IPromise<ArrayBuffer>;
moveDir(path: string, directory: string, newPath: string, newDirectory?: string): IFilePromise<DirectoryEntry>;
moveFile(path: string, file: string, newPath: string, newFile?: string): IFilePromise<FileEntry>;
copyDir(path: string, directory: string, newPath: string, newDirectory?: string): IFilePromise<DirectoryEntry>;
copyFile(path: string, file: string, newPath: string, newFile?: string): IFilePromise<FileEntry>;
}
export interface IFilePromise<T> extends ng.IPromise<T> {
then<TResult>(successCallback: (promiseValue: T) => ng.IPromise<TResult> | TResult, errorCallback?: (error: IFileError) => ng.IPromise<TResult> | TResult): ng.IPromise<TResult>;
catch<TResult>(onRejected: (error: IFileError) => ng.IPromise<TResult> | TResult): ng.IPromise<TResult>;
}
export interface IFileRemoveResult<TEntry> {
success: boolean;
fileRemoved: TEntry;
}
export interface IFileError extends FileError {
message: string;
}
}
+53
View File
@@ -0,0 +1,53 @@
/// <reference path="tsd.d.ts" />
/// <reference path="../angularjs/angular.d.ts" />
/// <reference path="../cordova/cordova.d.ts" />
module ngCordova {
'use strict';
angular.module('test')
// Adapted from http://ngcordova.com/docs/plugins/fileTransfer/
.controller('MyCtrl', function($scope: ng.IScope & { downloadProgress: number; }, $timeout: ng.ITimeoutService, $cordovaFileTransfer: ngCordova.IFileTransferService) {
document.addEventListener('deviceready', function() {
var url = "http://cdn.wall-pix.net/albums/art-space/00030109.jpg";
var targetPath = cordova.file.documentsDirectory + "testImage.png";
var trustHosts = true
var options = {};
$cordovaFileTransfer.download(url, targetPath, options, trustHosts)
.then(function(result) {
// Success!
var file: FileEntry = result;
}, function(err) {
// Error
}, function(progress) {
$timeout(function() {
$scope.downloadProgress = (progress.loaded / progress.total) * 100;
})
});
}, false);
document.addEventListener('deviceready', function() {
var url = "http://cdn.wall-pix.net/uploads";
var filePath = cordova.file.documentsDirectory + "testImage.png";
var trustHosts = true
var options = {};
$cordovaFileTransfer.upload(url, filePath, options, trustHosts)
.then(function(result) {
// Success!
var file: FileUploadResult = result;
}, function(err) {
// Error
}, function(progress) {
// constant progress updates
});
}, false);
});
}
+30
View File
@@ -0,0 +1,30 @@
// Type definitions for ngCordova file-transfer plugin
// Project: https://github.com/driftyco/ng-cordova
// Definitions by: Phil McCloghry-Laing <https://github.com/pmccloghrylaing>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../angularjs/angular.d.ts" />
/// <reference path="../cordova/plugins/FileTransfer.d.ts" />
/// <reference path="../cordova/plugins/FileSystem.d.ts" />
declare module ngCordova {
export interface IFileTransferService {
download(url: string, filePath: string, options?: IFileDownloadOptions, trustAllHosts?: boolean): IFileTransferPromise<FileEntry>;
upload(url: string, filePath: string, options?: IFileUploadOptions, trustAllHosts?: boolean): IFileTransferPromise<FileUploadResult>;
}
export interface IFileTransferPromise<T> extends ng.IPromise<T> {
then<TResult>(successCallback: (promiseValue: T) => ng.IPromise<TResult> | TResult, errorCallback?: (error: FileTransferError) => ng.IPromise<TResult> | TResult, notifyCallback?: (state: any) => any): ng.IPromise<TResult>;
catch<TResult>(onRejected: (error: FileTransferError) => ng.IPromise<TResult> | TResult): ng.IPromise<TResult>;
}
export interface IFileDownloadOptions extends FileDownloadOptions {
encodeURI?: boolean;
timeout?: number;
}
export interface IFileUploadOptions extends FileUploadOptions {
encodeURI?: boolean;
timeout?: number;
}
}
+4
View File
@@ -15,3 +15,7 @@
/// <reference path="datepicker.d.ts"/>
/// <reference path="app-version.d.ts"/>
/// <reference path="camera.d.ts"/>
/// <reference path="actionSheet.d.ts"/>
/// <reference path="badge.d.ts"/>
/// <reference path="file.d.ts"/>
/// <reference path="fileTransfer.d.ts"/>