Created
June 27, 2014 13:01
-
-
Save dereklawless/cf9d5f5c5330c81faa63 to your computer and use it in GitHub Desktop.
Cordova iOS actionsheet plugin JavaScript wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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