v-click-outside-x.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*!
  2. {
  3. "author": "Graham Fairweather",
  4. "copywrite": "Copyright (c) 2018-present",
  5. "date": "2020-01-14T15:27:52.362Z",
  6. "describe": "",
  7. "description": "Vue directive to react on clicks outside an element.",
  8. "file": "v-click-outside-x.js",
  9. "hash": "985cfe535e0f8da2f484",
  10. "license": "MIT",
  11. "version": "4.1.0"
  12. }
  13. */
  14. (function webpackUniversalModuleDefinition(root, factory) {
  15. if(typeof exports === 'object' && typeof module === 'object')
  16. module.exports = factory();
  17. else if(typeof define === 'function' && define.amd)
  18. define([], factory);
  19. else if(typeof exports === 'object')
  20. exports["vClickOutsideX"] = factory();
  21. else
  22. root["vClickOutsideX"] = factory();
  23. })((function () {
  24. 'use strict';
  25. var ObjectCtr = {}.constructor;
  26. var objectPrototype = ObjectCtr.prototype;
  27. var defineProperty = ObjectCtr.defineProperty;
  28. var $globalThis;
  29. var getGlobalFallback = function() {
  30. if (typeof self !== 'undefined') {
  31. return self;
  32. }
  33. if (typeof window !== 'undefined') {
  34. return window;
  35. }
  36. if (typeof global !== 'undefined') {
  37. return global;
  38. }
  39. return void 0;
  40. };
  41. var returnThis = function() {
  42. return this;
  43. };
  44. try {
  45. if (defineProperty) {
  46. defineProperty(objectPrototype, '$$globalThis$$', {
  47. get: returnThis,
  48. configurable: true
  49. });
  50. } else {
  51. objectPrototype.__defineGetter__('$$globalThis$$', returnThis);
  52. }
  53. $globalThis = typeof $$globalThis$$ === 'undefined' ? getGlobalFallback() : $$globalThis$$;
  54. delete objectPrototype.$$globalThis$$;
  55. return $globalThis;
  56. } catch (error) {
  57. return getGlobalFallback();
  58. }
  59. }()), function() {
  60. return /******/ (function(modules) { // webpackBootstrap
  61. /******/ // The module cache
  62. /******/ var installedModules = {};
  63. /******/
  64. /******/ // The require function
  65. /******/ function __webpack_require__(moduleId) {
  66. /******/
  67. /******/ // Check if module is in cache
  68. /******/ if(installedModules[moduleId]) {
  69. /******/ return installedModules[moduleId].exports;
  70. /******/ }
  71. /******/ // Create a new module (and put it into the cache)
  72. /******/ var module = installedModules[moduleId] = {
  73. /******/ i: moduleId,
  74. /******/ l: false,
  75. /******/ exports: {}
  76. /******/ };
  77. /******/
  78. /******/ // Execute the module function
  79. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  80. /******/
  81. /******/ // Flag the module as loaded
  82. /******/ module.l = true;
  83. /******/
  84. /******/ // Return the exports of the module
  85. /******/ return module.exports;
  86. /******/ }
  87. /******/
  88. /******/
  89. /******/ // expose the modules object (__webpack_modules__)
  90. /******/ __webpack_require__.m = modules;
  91. /******/
  92. /******/ // expose the module cache
  93. /******/ __webpack_require__.c = installedModules;
  94. /******/
  95. /******/ // define getter function for harmony exports
  96. /******/ __webpack_require__.d = function(exports, name, getter) {
  97. /******/ if(!__webpack_require__.o(exports, name)) {
  98. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  99. /******/ }
  100. /******/ };
  101. /******/
  102. /******/ // define __esModule on exports
  103. /******/ __webpack_require__.r = function(exports) {
  104. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  105. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  106. /******/ }
  107. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  108. /******/ };
  109. /******/
  110. /******/ // create a fake namespace object
  111. /******/ // mode & 1: value is a module id, require it
  112. /******/ // mode & 2: merge all properties of value into the ns
  113. /******/ // mode & 4: return value when already ns object
  114. /******/ // mode & 8|1: behave like require
  115. /******/ __webpack_require__.t = function(value, mode) {
  116. /******/ if(mode & 1) value = __webpack_require__(value);
  117. /******/ if(mode & 8) return value;
  118. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  119. /******/ var ns = Object.create(null);
  120. /******/ __webpack_require__.r(ns);
  121. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  122. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  123. /******/ return ns;
  124. /******/ };
  125. /******/
  126. /******/ // getDefaultExport function for compatibility with non-harmony modules
  127. /******/ __webpack_require__.n = function(module) {
  128. /******/ var getter = module && module.__esModule ?
  129. /******/ function getDefault() { return module['default']; } :
  130. /******/ function getModuleExports() { return module; };
  131. /******/ __webpack_require__.d(getter, 'a', getter);
  132. /******/ return getter;
  133. /******/ };
  134. /******/
  135. /******/ // Object.prototype.hasOwnProperty.call
  136. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  137. /******/
  138. /******/ // __webpack_public_path__
  139. /******/ __webpack_require__.p = "";
  140. /******/
  141. /******/
  142. /******/ // Load entry module and return exports
  143. /******/ return __webpack_require__(__webpack_require__.s = 1);
  144. /******/ })
  145. /************************************************************************/
  146. /******/ ([
  147. /* 0 */
  148. /***/ (function(module) {
  149. module.exports = JSON.parse("{\"a\":\"4.1.0\"}");
  150. /***/ }),
  151. /* 1 */
  152. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  153. "use strict";
  154. __webpack_require__.r(__webpack_exports__);
  155. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "directive", function() { return directive; });
  156. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "install", function() { return install; });
  157. /* harmony import */ var _package_json__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
  158. var _package_json__WEBPACK_IMPORTED_MODULE_0___namespace = /*#__PURE__*/__webpack_require__.t(0, 1);
  159. 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); }
  160. 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; }
  161. 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; }
  162. 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; }
  163. /**
  164. * @typedef {import("../types/index.d.ts")} VClickOutsidePlugin
  165. */
  166. var CLICK = 'click';
  167. var captureInstances = Object.create(null);
  168. var nonCaptureInstances = Object.create(null);
  169. var captureEventHandlers = Object.create(null);
  170. var nonCaptureEventHandlers = Object.create(null);
  171. var instancesList = [captureInstances, nonCaptureInstances];
  172. /**
  173. * The common event handler for bot capture and non-capture events.
  174. *
  175. * @param {!object} context - The event context.
  176. * @param {!object} instances - The capture or non-capture registered instances.
  177. * @param {Event} event - The event object.
  178. * @param {string} arg - The event type.
  179. * @returns {undefined} Default.
  180. */
  181. var commonHandler = function onCommonEvent(context, instances, event, arg) {
  182. var target = event.target;
  183. var itemIteratee = function itemIteratee(item) {
  184. var el = item.el;
  185. if (el !== target && !el.contains(target)) {
  186. var binding = item.binding;
  187. if (binding.modifiers.stop) {
  188. event.stopPropagation();
  189. }
  190. if (binding.modifiers.prevent) {
  191. event.preventDefault();
  192. }
  193. binding.value.call(context, event);
  194. }
  195. };
  196. instances[arg].forEach(itemIteratee);
  197. };
  198. /**
  199. * Get the correct event handler: Capture or non-capture.
  200. *
  201. * @param {boolean} useCapture - Indicate which handler to use; 'true' to use
  202. * capture handler or 'false' for non-capture.
  203. * @param {string} arg - The event type.
  204. * @returns {Function} - The event handler.
  205. */
  206. var getEventHandler = function getEventHandler(useCapture, arg) {
  207. if (useCapture) {
  208. if (captureEventHandlers[arg]) {
  209. return captureEventHandlers[arg];
  210. }
  211. /**
  212. * Event handler for capture events.
  213. *
  214. * @param {Event} event - The event object.
  215. */
  216. captureEventHandlers[arg] = function onCaptureEvent(event) {
  217. commonHandler(this, captureInstances, event, arg);
  218. };
  219. return captureEventHandlers[arg];
  220. }
  221. if (nonCaptureEventHandlers[arg]) {
  222. return nonCaptureEventHandlers[arg];
  223. }
  224. /**
  225. * Event handler for non-capture events.
  226. *
  227. * @param {Event} event - The event object.
  228. */
  229. nonCaptureEventHandlers[arg] = function onNonCaptureEvent(event) {
  230. commonHandler(this, nonCaptureInstances, event, arg);
  231. };
  232. return nonCaptureEventHandlers[arg];
  233. };
  234. /**
  235. * The directive definition.
  236. * {@link https://vuejs.org/v2/guide/custom-directive.html|Custom directive}.
  237. *
  238. * @type {VClickOutsidePlugin.directive}
  239. * @property {!object} $captureInstances - Registered capture instances.
  240. * @property {!object} $nonCaptureInstances - Registered non-capture instances.
  241. * @property {Function} $_onCaptureEvent - Event handler for capture events.
  242. * @property {Function} $_onNonCaptureEvent - Event handler for non-capture events.
  243. * @property {Function} bind - Called only once, when the directive is first
  244. * bound to the element.
  245. * @property {Function} unbind - Called only once, when the directive is unbound
  246. * from the element.
  247. * @property {string} version - The version number of this release.
  248. */
  249. var directive = Object.defineProperties({}, {
  250. $captureInstances: {
  251. value: captureInstances
  252. },
  253. $nonCaptureInstances: {
  254. value: nonCaptureInstances
  255. },
  256. $captureEventHandlers: {
  257. value: captureEventHandlers
  258. },
  259. $nonCaptureEventHandlers: {
  260. value: nonCaptureEventHandlers
  261. },
  262. bind: {
  263. value: function bind(el, binding) {
  264. if (typeof binding.value !== 'function') {
  265. throw new TypeError('Binding value must be a function.');
  266. }
  267. var arg = binding.arg || CLICK;
  268. var normalisedBinding = _objectSpread({}, binding, {}, {
  269. arg: arg,
  270. modifiers: _objectSpread({}, {
  271. capture: false,
  272. prevent: false,
  273. stop: false
  274. }, {}, binding.modifiers)
  275. });
  276. var useCapture = normalisedBinding.modifiers.capture;
  277. var instances = useCapture ? captureInstances : nonCaptureInstances;
  278. if (!Array.isArray(instances[arg])) {
  279. instances[arg] = [];
  280. }
  281. if (instances[arg].push({
  282. el: el,
  283. binding: normalisedBinding
  284. }) === 1) {
  285. /* istanbul ignore next */
  286. if ((typeof document === "undefined" ? "undefined" : _typeof(document)) === 'object' && document) {
  287. document.addEventListener(arg, getEventHandler(useCapture, arg), useCapture);
  288. }
  289. }
  290. }
  291. },
  292. unbind: {
  293. value: function unbind(el) {
  294. var compareElements = function compareElements(item) {
  295. return item.el !== el;
  296. };
  297. var instancesIteratee = function instancesIteratee(instances) {
  298. var instanceKeys = Object.keys(instances);
  299. if (instanceKeys.length) {
  300. var useCapture = instances === captureInstances;
  301. var keysIteratee = function keysIteratee(eventName) {
  302. var newInstance = instances[eventName].filter(compareElements);
  303. if (newInstance.length) {
  304. instances[eventName] = newInstance;
  305. } else {
  306. /* istanbul ignore next */
  307. if ((typeof document === "undefined" ? "undefined" : _typeof(document)) === 'object' && document) {
  308. document.removeEventListener(eventName, getEventHandler(useCapture, eventName), useCapture);
  309. }
  310. delete instances[eventName];
  311. }
  312. };
  313. instanceKeys.forEach(keysIteratee);
  314. }
  315. };
  316. instancesList.forEach(instancesIteratee);
  317. }
  318. },
  319. /* Note: This needs to be manually updated to match package.json. */
  320. version: {
  321. enumerable: true,
  322. value: _package_json__WEBPACK_IMPORTED_MODULE_0__[/* version */ "a"]
  323. }
  324. });
  325. /**
  326. * A Vue.js plugin should expose an install method. The method will be called
  327. * with the Vue constructor as the first argument, along with possible options.
  328. * {@link https://vuejs.org/v2/guide/plugins.html#Writing-a-Plugin|Writing a plugin}.
  329. *
  330. * @type {VClickOutsidePlugin.install}
  331. * @param {import("vue")} Vue - The Vue constructor.
  332. */
  333. function install(Vue) {
  334. Vue.directive('click-outside', directive);
  335. }
  336. /***/ })
  337. /******/ ]);
  338. });
  339. //# sourceMappingURL=v-click-outside-x.js.map