AppVersionPlugin.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*jslint indent: 2 */
  2. /*global window, jQuery, angular, cordova */
  3. "use strict";
  4. // Returns a jQuery or AngularJS deferred object, or pass a success and fail callbacks if you don't want to use jQuery or AngularJS
  5. var getPromisedCordovaExec = function (command, success, fail) {
  6. var toReturn, deferred, injector, $q;
  7. if (success === undefined) {
  8. if (window.jQuery) {
  9. deferred = jQuery.Deferred();
  10. success = deferred.resolve;
  11. fail = deferred.reject;
  12. toReturn = deferred;
  13. } else if (window.angular) {
  14. injector = angular.injector(["ng"]);
  15. $q = injector.get("$q");
  16. deferred = $q.defer();
  17. success = deferred.resolve;
  18. fail = deferred.reject;
  19. toReturn = deferred.promise;
  20. } else if (window.when && window.when.promise) {
  21. deferred = when.defer();
  22. success = deferred.resolve;
  23. fail = deferred.reject;
  24. toReturn = deferred.promise;
  25. } else if (window.Promise) {
  26. toReturn = new Promise(function(c, e) {
  27. success = c;
  28. fail = e;
  29. });
  30. } else if (window.WinJS && window.WinJS.Promise) {
  31. toReturn = new WinJS.Promise(function(c, e) {
  32. success = c;
  33. fail = e;
  34. });
  35. } else {
  36. return console.error('AppVersion either needs a success callback, or jQuery/AngularJS/Promise/WinJS.Promise defined for using promises');
  37. }
  38. }
  39. // 5th param is NOT optional. must be at least empty array
  40. cordova.exec(success, fail, "AppVersion", command, []);
  41. return toReturn;
  42. };
  43. var getAppVersion = function (success, fail) {
  44. return getPromisedCordovaExec('getVersionNumber', success, fail);
  45. };
  46. getAppVersion.getAppName = function (success, fail) {
  47. return getPromisedCordovaExec('getAppName', success, fail);
  48. };
  49. getAppVersion.getPackageName = function (success, fail) {
  50. return getPromisedCordovaExec('getPackageName', success, fail);
  51. };
  52. getAppVersion.getVersionNumber = function (success, fail) {
  53. return getPromisedCordovaExec('getVersionNumber', success, fail);
  54. };
  55. getAppVersion.getVersionCode = function (success, fail) {
  56. return getPromisedCordovaExec('getVersionCode', success, fail);
  57. };
  58. module.exports = getAppVersion;