Skip to content

Instantly share code, notes, and snippets.

@dereklawless
Created June 27, 2014 13:01
Show Gist options
  • Save dereklawless/cf9d5f5c5330c81faa63 to your computer and use it in GitHub Desktop.
Save dereklawless/cf9d5f5c5330c81faa63 to your computer and use it in GitHub Desktop.
Cordova iOS actionsheet plugin JavaScript wrapper
define(function (undefined) {
'use strict';
return {
/**
* Initialises the actionsheet helper on the global scope.
*/
init: function () {
if (this.useNative()) {
console.log('Using native actionsheet');
window.actionsheet = {
create: function (options, callback) {
window.ActionSheet.create({
title: options.title,
items: options.items,
origin: options.origin,
destructiveButtonIndex: options.destructiveButtonIndex,
cancelButtonIndex: options.cancelButtonIndex
}, callback);
}
};
}
else {
console.log('Using browser prompt (emulating actionsheet)');
// Wrap browser prompt to match the callback pattern of the native actionsheet wrapper.
var prompt = window.prompt;
window.actionsheet = {
create: function (options, callback) {
var result = prompt(options.title + '\n\n' + 'Enter your chosen option:\n' + options.items);
if (callback && typeof(callback) === 'function') {
callback(result);
}
}
};
}
},
/**
* Determines whether to use device-native or browser-emulated functionality.
* @returns {boolean} - If true, indicates that native functionality can be used.
*/
useNative: function () {
return window && window.ActionSheet;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment