v-click-outside-x.esm.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  2. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  3. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  4. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. import { version } from '../package.json';
  6. /**
  7. * @typedef {import("../types/index.d.ts")} VClickOutsidePlugin
  8. */
  9. var CLICK = 'click';
  10. var captureInstances = Object.create(null);
  11. var nonCaptureInstances = Object.create(null);
  12. var captureEventHandlers = Object.create(null);
  13. var nonCaptureEventHandlers = Object.create(null);
  14. var instancesList = [captureInstances, nonCaptureInstances];
  15. /**
  16. * The common event handler for bot capture and non-capture events.
  17. *
  18. * @param {!object} context - The event context.
  19. * @param {!object} instances - The capture or non-capture registered instances.
  20. * @param {Event} event - The event object.
  21. * @param {string} arg - The event type.
  22. * @returns {undefined} Default.
  23. */
  24. var commonHandler = function onCommonEvent(context, instances, event, arg) {
  25. var target = event.target;
  26. var itemIteratee = function itemIteratee(item) {
  27. var el = item.el;
  28. if (el !== target && !el.contains(target)) {
  29. var binding = item.binding;
  30. if (binding.modifiers.stop) {
  31. event.stopPropagation();
  32. }
  33. if (binding.modifiers.prevent) {
  34. event.preventDefault();
  35. }
  36. binding.value.call(context, event);
  37. }
  38. };
  39. instances[arg].forEach(itemIteratee);
  40. };
  41. /**
  42. * Get the correct event handler: Capture or non-capture.
  43. *
  44. * @param {boolean} useCapture - Indicate which handler to use; 'true' to use
  45. * capture handler or 'false' for non-capture.
  46. * @param {string} arg - The event type.
  47. * @returns {Function} - The event handler.
  48. */
  49. var getEventHandler = function getEventHandler(useCapture, arg) {
  50. if (useCapture) {
  51. if (captureEventHandlers[arg]) {
  52. return captureEventHandlers[arg];
  53. }
  54. /**
  55. * Event handler for capture events.
  56. *
  57. * @param {Event} event - The event object.
  58. */
  59. captureEventHandlers[arg] = function onCaptureEvent(event) {
  60. commonHandler(this, captureInstances, event, arg);
  61. };
  62. return captureEventHandlers[arg];
  63. }
  64. if (nonCaptureEventHandlers[arg]) {
  65. return nonCaptureEventHandlers[arg];
  66. }
  67. /**
  68. * Event handler for non-capture events.
  69. *
  70. * @param {Event} event - The event object.
  71. */
  72. nonCaptureEventHandlers[arg] = function onNonCaptureEvent(event) {
  73. commonHandler(this, nonCaptureInstances, event, arg);
  74. };
  75. return nonCaptureEventHandlers[arg];
  76. };
  77. /**
  78. * The directive definition.
  79. * {@link https://vuejs.org/v2/guide/custom-directive.html|Custom directive}.
  80. *
  81. * @type {VClickOutsidePlugin.directive}
  82. * @property {!object} $captureInstances - Registered capture instances.
  83. * @property {!object} $nonCaptureInstances - Registered non-capture instances.
  84. * @property {Function} $_onCaptureEvent - Event handler for capture events.
  85. * @property {Function} $_onNonCaptureEvent - Event handler for non-capture events.
  86. * @property {Function} bind - Called only once, when the directive is first
  87. * bound to the element.
  88. * @property {Function} unbind - Called only once, when the directive is unbound
  89. * from the element.
  90. * @property {string} version - The version number of this release.
  91. */
  92. export var directive = Object.defineProperties({}, {
  93. $captureInstances: {
  94. value: captureInstances
  95. },
  96. $nonCaptureInstances: {
  97. value: nonCaptureInstances
  98. },
  99. $captureEventHandlers: {
  100. value: captureEventHandlers
  101. },
  102. $nonCaptureEventHandlers: {
  103. value: nonCaptureEventHandlers
  104. },
  105. bind: {
  106. value: function bind(el, binding) {
  107. if (typeof binding.value !== 'function') {
  108. throw new TypeError('Binding value must be a function.');
  109. }
  110. var arg = binding.arg || CLICK;
  111. var normalisedBinding = _objectSpread({}, binding, {}, {
  112. arg: arg,
  113. modifiers: _objectSpread({}, {
  114. capture: false,
  115. prevent: false,
  116. stop: false
  117. }, {}, binding.modifiers)
  118. });
  119. var useCapture = normalisedBinding.modifiers.capture;
  120. var instances = useCapture ? captureInstances : nonCaptureInstances;
  121. if (!Array.isArray(instances[arg])) {
  122. instances[arg] = [];
  123. }
  124. if (instances[arg].push({
  125. el: el,
  126. binding: normalisedBinding
  127. }) === 1) {
  128. /* istanbul ignore next */
  129. if ((typeof document === "undefined" ? "undefined" : _typeof(document)) === 'object' && document) {
  130. document.addEventListener(arg, getEventHandler(useCapture, arg), useCapture);
  131. }
  132. }
  133. }
  134. },
  135. unbind: {
  136. value: function unbind(el) {
  137. var compareElements = function compareElements(item) {
  138. return item.el !== el;
  139. };
  140. var instancesIteratee = function instancesIteratee(instances) {
  141. var instanceKeys = Object.keys(instances);
  142. if (instanceKeys.length) {
  143. var useCapture = instances === captureInstances;
  144. var keysIteratee = function keysIteratee(eventName) {
  145. var newInstance = instances[eventName].filter(compareElements);
  146. if (newInstance.length) {
  147. instances[eventName] = newInstance;
  148. } else {
  149. /* istanbul ignore next */
  150. if ((typeof document === "undefined" ? "undefined" : _typeof(document)) === 'object' && document) {
  151. document.removeEventListener(eventName, getEventHandler(useCapture, eventName), useCapture);
  152. }
  153. delete instances[eventName];
  154. }
  155. };
  156. instanceKeys.forEach(keysIteratee);
  157. }
  158. };
  159. instancesList.forEach(instancesIteratee);
  160. }
  161. },
  162. /* Note: This needs to be manually updated to match package.json. */
  163. version: {
  164. enumerable: true,
  165. value: version
  166. }
  167. });
  168. /**
  169. * A Vue.js plugin should expose an install method. The method will be called
  170. * with the Vue constructor as the first argument, along with possible options.
  171. * {@link https://vuejs.org/v2/guide/plugins.html#Writing-a-Plugin|Writing a plugin}.
  172. *
  173. * @type {VClickOutsidePlugin.install}
  174. * @param {import("vue")} Vue - The Vue constructor.
  175. */
  176. export function install(Vue) {
  177. Vue.directive('click-outside', directive);
  178. }
  179. //# sourceMappingURL=v-click-outside-x.esm.js.map