proximity.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*!
  2. {
  3. "name": "Proximity API",
  4. "property": "proximity",
  5. "authors": ["Cătălin Mariș"],
  6. "tags": ["events", "proximity"],
  7. "caniuse": "proximity",
  8. "notes": [{
  9. "name": "MDN Docs",
  10. "href": "https://developer.mozilla.org/en-US/docs/Web/API/Proximity_Events"
  11. },{
  12. "name": "W3C Spec",
  13. "href": "https://www.w3.org/TR/proximity/"
  14. }]
  15. }
  16. !*/
  17. /* DOC
  18. Detects support for an API that allows users to get proximity related information from the device's proximity sensor.
  19. */
  20. define(['Modernizr', 'addTest'], function(Modernizr, addTest) {
  21. Modernizr.addAsyncTest(function() {
  22. var timeout;
  23. var timeoutTime = 300;
  24. function advertiseSupport() {
  25. // Clean up after ourselves
  26. clearTimeout(timeout);
  27. window.removeEventListener('deviceproximity', advertiseSupport);
  28. // Advertise support as the browser supports
  29. // the API and the device has a proximity sensor
  30. addTest('proximity', true);
  31. }
  32. // Check if the browser has support for the API
  33. if ('ondeviceproximity' in window && 'onuserproximity' in window) {
  34. // Check if the device has a proximity sensor
  35. // ( devices without such a sensor support the events but
  36. // will never fire them resulting in a false positive )
  37. window.addEventListener('deviceproximity', advertiseSupport);
  38. // If the event doesn't fire in a reasonable amount of time,
  39. // it means that the device doesn't have a proximity sensor,
  40. // thus, we can advertise the "lack" of support
  41. timeout = setTimeout(function() {
  42. window.removeEventListener('deviceproximity', advertiseSupport);
  43. addTest('proximity', false);
  44. }, timeoutTime);
  45. } else {
  46. addTest('proximity', false);
  47. }
  48. });
  49. });