Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

click-outside.js 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. function closeConditional() {
  2. return false;
  3. }
  4. function directive(e, el, binding) {
  5. // Args may not always be supplied
  6. binding.args = binding.args || {};
  7. // If no closeConditional was supplied assign a default
  8. var isActive = binding.args.closeConditional || closeConditional;
  9. // The include element callbacks below can be expensive
  10. // so we should avoid calling them when we're not active.
  11. // Explicitly check for false to allow fallback compatibility
  12. // with non-toggleable components
  13. if (!e || isActive(e) === false) return;
  14. // If click was triggered programmaticaly (domEl.click()) then
  15. // it shouldn't be treated as click-outside
  16. // Chrome/Firefox support isTrusted property
  17. // IE/Edge support pointerType property (empty if not triggered
  18. // by pointing device)
  19. if ('isTrusted' in e && !e.isTrusted || 'pointerType' in e && !e.pointerType) return;
  20. // Check if additional elements were passed to be included in check
  21. // (click must be outside all included elements, if any)
  22. var elements = (binding.args.include || function () {
  23. return [];
  24. })();
  25. // Add the root element for the component this directive was defined on
  26. elements.push(el);
  27. // Check if it's a click outside our elements, and then if our callback returns true.
  28. // Non-toggleable components should take action in their callback and return falsy.
  29. // Toggleable can return true if it wants to deactivate.
  30. // Note that, because we're in the capture phase, this callback will occur before
  31. // the bubbling click event on any outside elements.
  32. !elements.some(function (el) {
  33. return el.contains(e.target);
  34. }) && setTimeout(function () {
  35. isActive(e) && binding.value && binding.value(e);
  36. }, 0);
  37. }
  38. export default {
  39. // [data-app] may not be found
  40. // if using bind, inserted makes
  41. // sure that the root element is
  42. // available, iOS does not support
  43. // clicks on body
  44. inserted: function inserted(el, binding) {
  45. var onClick = function onClick(e) {
  46. return directive(e, el, binding);
  47. };
  48. // iOS does not recognize click events on document
  49. // or body, this is the entire purpose of the v-app
  50. // component and [data-app], stop removing this
  51. var app = document.querySelector('[data-app]') || document.body; // This is only for unit tests
  52. app.addEventListener('click', onClick, true);
  53. el._clickOutside = onClick;
  54. },
  55. unbind: function unbind(el) {
  56. if (!el._clickOutside) return;
  57. var app = document.querySelector('[data-app]') || document.body; // This is only for unit tests
  58. app && app.removeEventListener('click', el._clickOutside, true);
  59. delete el._clickOutside;
  60. }
  61. };
  62. //# sourceMappingURL=click-outside.js.map