visibilityApi.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /* Loading this module will cause, when TogetherJS is active, the
  5. session object to emit visibility-change with a `hidden` argument
  6. whenever the visibility changes, on browsers where we can detect
  7. it.
  8. */
  9. define(["util", "session"], function (util, session) {
  10. var visibilityApi = util.Module("visibilityApi");
  11. var hidden;
  12. var visibilityChange;
  13. if (document.hidden !== undefined) { // Opera 12.10 and Firefox 18 and later support
  14. hidden = "hidden";
  15. visibilityChange = "visibilitychange";
  16. } else if (document.mozHidden !== undefined) {
  17. hidden = "mozHidden";
  18. visibilityChange = "mozvisibilitychange";
  19. } else if (document.msHidden !== undefined) {
  20. hidden = "msHidden";
  21. visibilityChange = "msvisibilitychange";
  22. } else if (document.webkitHidden !== undefined) {
  23. hidden = "webkitHidden";
  24. visibilityChange = "webkitvisibilitychange";
  25. }
  26. session.on("start", function () {
  27. document.addEventListener(visibilityChange, change, false);
  28. });
  29. session.on("close", function () {
  30. document.removeEventListener(visibilityChange, change, false);
  31. });
  32. function change() {
  33. session.emit("visibility-change", document[hidden]);
  34. }
  35. visibilityApi.hidden = function () {
  36. return document[hidden];
  37. };
  38. return visibilityApi;
  39. });