Last active
August 29, 2015 14:20
-
-
Save tegola/2ae7a1263cc6c31da307 to your computer and use it in GitHub Desktop.
Cordova notification dialogs' wrapper for mixed web app/packaged app
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
/* | |
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