v-click-outside-x.js 11 KB

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