Skip to content

Instantly share code, notes, and snippets.

@tegola
Last active August 29, 2015 14:20
Show Gist options
  • Save tegola/2ae7a1263cc6c31da307 to your computer and use it in GitHub Desktop.
Save tegola/2ae7a1263cc6c31da307 to your computer and use it in GitHub Desktop.
Cordova notification dialogs' wrapper for mixed web app/packaged app
/*
Custom wrappers for javascript's alert/confirm/prompt and their respective cordova counterparts:
https://github.com/apache/cordova-plugin-dialogs
Useful if you want to keep the same codebase on a web and a packaged app. Just keep in mind that javascript dialogs are synchronous, while cordova's are not.
Usage (for the prompt):
showPrompt("What's your name?", null, function(value){
if (value) {
console.log("Your name: "+ value);
} else {
console.log("No name selected");
}
});
*/
function showAlert(message){
if (navigator.notification) {
navigator.notification.alert(message);
} else {
alert(message);
}
};
function showConfirm(message, callback){
if (navigator.notification) {
navigator.notification.confirm(
message,
function(buttonIndex){
callback(buttonIndex == 1);
}
);
} else {
callback(confirm(message));
}
};
function showPrompt(message, value, callback){
if (navigator.notification) {
navigator.notification.prompt(
message,
function(results){
if (results.buttonIndex == 1) {
callback(results.input1);
}
},
null,
null,
value || null
);
} else {
callback(prompt(message, value));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment