1
0

jquery-visibility.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*!
  2. * jquery-visibility v1.0.11
  3. * Page visibility shim for jQuery.
  4. *
  5. * Project Website: http://mths.be/visibility
  6. *
  7. * @version 1.0.11
  8. *
  9. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  10. * SPDX-FileCopyrightText: 2015 ownCloud, Inc.
  11. * SPDX-FileCopyrightText: Mathias Bynens
  12. * SPDX-FileCopyrightText: Jan Paepke
  13. * SPDX-License-Identifier: MIT
  14. */
  15. ;(function (root, factory) {
  16. if (typeof define === 'function' && define.amd) {
  17. // AMD. Register as an anonymous module.
  18. define(['jquery'], function ($) {
  19. return factory(root, $);
  20. });
  21. } else if (typeof exports === 'object') {
  22. // Node/CommonJS
  23. module.exports = factory(root, require('jquery'));
  24. } else {
  25. // Browser globals
  26. factory(root, jQuery);
  27. }
  28. }(this, function(window, $, undefined) {
  29. "use strict";
  30. var
  31. document = window.document,
  32. property, // property name of document, that stores page visibility
  33. vendorPrefixes = ['webkit', 'o', 'ms', 'moz', ''],
  34. $support = $.support || {},
  35. // In Opera, `'onfocusin' in document == true`, hence the extra `hasFocus` check to detect IE-like behavior
  36. eventName = 'onfocusin' in document && 'hasFocus' in document ?
  37. 'focusin focusout' :
  38. 'focus blur';
  39. var prefix;
  40. while ((prefix = vendorPrefixes.pop()) !== undefined) {
  41. property = (prefix ? prefix + 'H': 'h') + 'idden';
  42. $support.pageVisibility = document[property] !== undefined;
  43. if ($support.pageVisibility) {
  44. eventName = prefix + 'visibilitychange';
  45. break;
  46. }
  47. }
  48. // normalize to and update document hidden property
  49. function updateState() {
  50. if (property !== 'hidden') {
  51. document.hidden = $support.pageVisibility ? document[property] : undefined;
  52. }
  53. }
  54. updateState();
  55. $(/blur$/.test(eventName) ? window : document).on(eventName, function(event) {
  56. var type = event.type;
  57. var originalEvent = event.originalEvent;
  58. // Avoid errors from triggered native events for which `originalEvent` is
  59. // not available.
  60. if (!originalEvent) {
  61. return;
  62. }
  63. var toElement = originalEvent.toElement;
  64. // If it’s a `{focusin,focusout}` event (IE), `fromElement` and `toElement`
  65. // should both be `null` or `undefined`; else, the page visibility hasn’t
  66. // changed, but the user just clicked somewhere in the doc. In IE9, we need
  67. // to check the `relatedTarget` property instead.
  68. if (
  69. !/^focus./.test(type) || (
  70. toElement === undefined &&
  71. originalEvent.fromElement === undefined &&
  72. originalEvent.relatedTarget === undefined
  73. )
  74. ) {
  75. $(document).triggerHandler(
  76. property && document[property] || /^(?:blur|focusout)$/.test(type) ?
  77. 'hide' :
  78. 'show'
  79. );
  80. }
  81. // and update the current state
  82. updateState();
  83. });
  84. }));