angular-adaptive-detection.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. (function () {
  2. 'use strict';
  3. /**
  4. * @ngdoc overview
  5. * @name adaptive.detection
  6. *
  7. * @description
  8. * The main module which holds everything together.
  9. */
  10. var adaptive = angular.module('adaptive.detection', []);
  11. /**
  12. * @ngdoc object
  13. * @name adaptive.detection.$detectionProvider
  14. *
  15. * @description
  16. * The `$detectionProvider` provides an interface to configure `$detection service for
  17. * runtime.
  18. */
  19. adaptive.provider('$detection', [function() {
  20. this.userAgent = navigator.userAgent;
  21. /**
  22. * @ngdoc function
  23. * @name adaptive.detection.$detectionProvider#setUserAgent
  24. * @methodOf adaptive.detection.$detectionProvider
  25. *
  26. * @description
  27. * Let's you configure a custom User Agent string during your apps configuration.
  28. *
  29. * <pre>
  30. * var app = angular.module('myApp', ['adaptive.detection']);
  31. *
  32. * app.config(['$detectionProvider', function ($detectionProvider) {
  33. * // sets custom User Agent
  34. * $detectionProvider.setUserAgent('angular browser');
  35. * }]);
  36. * </pre>
  37. *
  38. * @param {string} Custom User Agent string
  39. */
  40. this.setUserAgent = function(userAgent) {
  41. this.userAgent = userAgent;
  42. };
  43. /**
  44. * @ngdoc object
  45. * @name adaptive.detection.$detection
  46. *
  47. * @description
  48. * The `$detection` service can be injected anywhere in your app during runtime like
  49. * every other service. It provides methods to detect wheter a the current client is
  50. * for example and iOS device or an Android device.
  51. *
  52. * You are also able to retreive the current User Agent using this service.
  53. */
  54. this.$get = function() {
  55. var userAgent = this.userAgent;
  56. return {
  57. /**
  58. * @ngdoc function
  59. * @name adaptive.detection.$detection#getUserAgent
  60. * @methodOf adaptive.detection.$detection
  61. *
  62. * @description
  63. * Returns the current User Agent which was set with `$detectionProvider.setUserAgent'.
  64. *
  65. * @return {string} userAgent
  66. */
  67. getUserAgent: function(){
  68. return userAgent;
  69. },
  70. /**
  71. * @ngdoc function
  72. * @name adaptive.detection.$detection#isiOS
  73. * @methodOf adaptive.detection.$detection
  74. *
  75. * @description
  76. * Returns true if current device is an iOS device.
  77. *
  78. * @return {bool}
  79. */
  80. isiOS: function(){
  81. return (/(iPad|iPhone|iPod)/gi).test(userAgent);
  82. },
  83. /**
  84. * @ngdoc function
  85. * @name adaptive.detection.$detection#isAndroid
  86. * @methodOf adaptive.detection.$detection
  87. *
  88. * @description
  89. * Returns true if current device is an Android device.
  90. *
  91. * @return {bool}
  92. */
  93. isAndroid: function(){
  94. return (/(Android)/gi).test(userAgent);
  95. },
  96. /**
  97. * @ngdoc function
  98. * @name adaptive.detection.$detection#isWindowsPhone
  99. * @methodOf adaptive.detection.$detection
  100. *
  101. * @description
  102. * Returns true if current device is a Windows Phone device.
  103. *
  104. * @return {bool}
  105. */
  106. isWindowsPhone: function(){
  107. return (/(IEMobile)/gi).test(userAgent);
  108. },
  109. /**
  110. * @ngdoc function
  111. * @name adaptive.detection.$detection#isBB10
  112. * @methodOf adaptive.detection.$detection
  113. *
  114. * @description
  115. * Returns true if current device is a BlackBerry 10 device.
  116. *
  117. * @return {bool}
  118. */
  119. isBB10: function(){
  120. return (/(BB10)/gi).test(userAgent);
  121. },
  122. isMobile: function(){
  123. return this.isiOS() || this.isAndroid() || this.isBB10() || this.isWindowsPhone();
  124. }
  125. };
  126. };
  127. }]);
  128. })();