Skip to content

Instantly share code, notes, and snippets.

@dkellycollins
Created February 24, 2016 23:10
Show Gist options
  • Save dkellycollins/3eefc5fd324238b62e54 to your computer and use it in GitHub Desktop.
Save dkellycollins/3eefc5fd324238b62e54 to your computer and use it in GitHub Desktop.
/**
* Provides the ability to fetch global plugin objects.
*/
class PluginService {
static $inject = ['$ionicPlatform', '$window', '$q', '$log'];
constructor(private $ionicPlatform: ionic.platform.IonicPlatformService,
private $window: ng.IWindowService,
private $q: ng.IQService,
private $log: ng.ILogService) {
}
/**
* Returns a promise that is resolved with the plugin with the given name.
* @param plugin {string} - The name or path to the plugin.
* @returns {IPromise<any>} - A promise that is resolved with the plugin.
*/
public getPlugin(plugin: string): ng.IPromise<any> {
return this.$ionicPlatform.ready()
.then(() => {
var pluginInstance = this.get(this.$window, plugin);
if(!pluginInstance) {
this.$log.error('Plugin [' + plugin + '] not found.');
return this.$q.reject('Plugin [' + plugin + '] not found.');
}
return this.$q.resolve(pluginInstance);
});
}
/**
* Returns the ionic keyboard plugin.
* @returns {ng.IPromise<any>} - A promise that is resolved with the ionic keyboard plugin.
*/
public keyboard(): ng.IPromise<any> {
return this.getPlugin('cordova.plugins.Keyboard');
}
private get(window: ng.IWindowService, plugin: string): any {
var objectPath = plugin.split('.');
var object = window;
var pathItem = objectPath.shift();
while(!!object && !!pathItem) {
object = object[pathItem];
pathItem = objectPath.shift();
}
return object;
}
}
angular.module('...')
.service('$pluginService', PluginService);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment