jquery.flot.browser.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /** ## jquery.flot.browser.js
  2. This plugin is used to make available some browser-related utility functions.
  3. ### Methods
  4. */
  5. (function ($) {
  6. 'use strict';
  7. var browser = {
  8. /**
  9. - getPageXY(e)
  10. Calculates the pageX and pageY using the screenX, screenY properties of the event
  11. and the scrolling of the page. This is needed because the pageX and pageY
  12. properties of the event are not correct while running tests in Edge. */
  13. getPageXY: function (e) {
  14. // This code is inspired from https://stackoverflow.com/a/3464890
  15. var doc = document.documentElement,
  16. pageX = e.clientX + (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0),
  17. pageY = e.clientY + (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0);
  18. return { X: pageX, Y: pageY };
  19. },
  20. /**
  21. - getPixelRatio(context)
  22. This function returns the current pixel ratio defined by the product of desktop
  23. zoom and page zoom.
  24. Additional info: https://www.html5rocks.com/en/tutorials/canvas/hidpi/
  25. */
  26. getPixelRatio: function(context) {
  27. var devicePixelRatio = window.devicePixelRatio || 1,
  28. backingStoreRatio =
  29. context.webkitBackingStorePixelRatio ||
  30. context.mozBackingStorePixelRatio ||
  31. context.msBackingStorePixelRatio ||
  32. context.oBackingStorePixelRatio ||
  33. context.backingStorePixelRatio || 1;
  34. return devicePixelRatio / backingStoreRatio;
  35. },
  36. /**
  37. - isSafari, isMobileSafari, isOpera, isFirefox, isIE, isEdge, isChrome, isBlink
  38. This is a collection of functions, used to check if the code is running in a
  39. particular browser or Javascript engine.
  40. */
  41. isSafari: function() {
  42. // *** https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  43. // Safari 3.0+ "[object HTMLElementConstructor]"
  44. return /constructor/i.test(window.top.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window.top['safari'] || (typeof window.top.safari !== 'undefined' && window.top.safari.pushNotification));
  45. },
  46. isMobileSafari: function() {
  47. //isMobileSafari adapted from https://stackoverflow.com/questions/3007480/determine-if-user-navigated-from-mobile-safari
  48. return navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/);
  49. },
  50. isOpera: function() {
  51. // *** https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  52. //Opera 8.0+
  53. return (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
  54. },
  55. isFirefox: function() {
  56. // *** https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  57. // Firefox 1.0+
  58. return typeof InstallTrigger !== 'undefined';
  59. },
  60. isIE: function() {
  61. // *** https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  62. // Internet Explorer 6-11
  63. return /*@cc_on!@*/false || !!document.documentMode;
  64. },
  65. isEdge: function() {
  66. // *** https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  67. // Edge 20+
  68. return !browser.isIE() && !!window.StyleMedia;
  69. },
  70. isChrome: function() {
  71. // *** https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  72. // Chrome 1+
  73. return !!window.chrome && !!window.chrome.webstore;
  74. },
  75. isBlink: function() {
  76. // *** https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
  77. return (browser.isChrome() || browser.isOpera()) && !!window.CSS;
  78. }
  79. };
  80. $.plot.browser = browser;
  81. })(jQuery);