foundation.util.keyboard.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. (function webpackUniversalModuleDefinition(root, factory) {
  2. if(typeof exports === 'object' && typeof module === 'object')
  3. module.exports = factory(require("./foundation.core"), require("jquery"));
  4. else if(typeof define === 'function' && define.amd)
  5. define(["./foundation.core", "jquery"], factory);
  6. else if(typeof exports === 'object')
  7. exports["foundation.util.keyboard"] = factory(require("./foundation.core"), require("jquery"));
  8. else
  9. root["__FOUNDATION_EXTERNAL__"] = root["__FOUNDATION_EXTERNAL__"] || {}, root["__FOUNDATION_EXTERNAL__"]["foundation.util.keyboard"] = factory(root["__FOUNDATION_EXTERNAL__"]["foundation.core"], root["jQuery"]);
  10. })(window, function(__WEBPACK_EXTERNAL_MODULE__foundation_core__, __WEBPACK_EXTERNAL_MODULE_jquery__) {
  11. return /******/ (function(modules) { // webpackBootstrap
  12. /******/ // The module cache
  13. /******/ var installedModules = {};
  14. /******/
  15. /******/ // The require function
  16. /******/ function __webpack_require__(moduleId) {
  17. /******/
  18. /******/ // Check if module is in cache
  19. /******/ if(installedModules[moduleId]) {
  20. /******/ return installedModules[moduleId].exports;
  21. /******/ }
  22. /******/ // Create a new module (and put it into the cache)
  23. /******/ var module = installedModules[moduleId] = {
  24. /******/ i: moduleId,
  25. /******/ l: false,
  26. /******/ exports: {}
  27. /******/ };
  28. /******/
  29. /******/ // Execute the module function
  30. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  31. /******/
  32. /******/ // Flag the module as loaded
  33. /******/ module.l = true;
  34. /******/
  35. /******/ // Return the exports of the module
  36. /******/ return module.exports;
  37. /******/ }
  38. /******/
  39. /******/
  40. /******/ // expose the modules object (__webpack_modules__)
  41. /******/ __webpack_require__.m = modules;
  42. /******/
  43. /******/ // expose the module cache
  44. /******/ __webpack_require__.c = installedModules;
  45. /******/
  46. /******/ // define getter function for harmony exports
  47. /******/ __webpack_require__.d = function(exports, name, getter) {
  48. /******/ if(!__webpack_require__.o(exports, name)) {
  49. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  50. /******/ }
  51. /******/ };
  52. /******/
  53. /******/ // define __esModule on exports
  54. /******/ __webpack_require__.r = function(exports) {
  55. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  56. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  57. /******/ }
  58. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  59. /******/ };
  60. /******/
  61. /******/ // create a fake namespace object
  62. /******/ // mode & 1: value is a module id, require it
  63. /******/ // mode & 2: merge all properties of value into the ns
  64. /******/ // mode & 4: return value when already ns object
  65. /******/ // mode & 8|1: behave like require
  66. /******/ __webpack_require__.t = function(value, mode) {
  67. /******/ if(mode & 1) value = __webpack_require__(value);
  68. /******/ if(mode & 8) return value;
  69. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  70. /******/ var ns = Object.create(null);
  71. /******/ __webpack_require__.r(ns);
  72. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  73. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  74. /******/ return ns;
  75. /******/ };
  76. /******/
  77. /******/ // getDefaultExport function for compatibility with non-harmony modules
  78. /******/ __webpack_require__.n = function(module) {
  79. /******/ var getter = module && module.__esModule ?
  80. /******/ function getDefault() { return module['default']; } :
  81. /******/ function getModuleExports() { return module; };
  82. /******/ __webpack_require__.d(getter, 'a', getter);
  83. /******/ return getter;
  84. /******/ };
  85. /******/
  86. /******/ // Object.prototype.hasOwnProperty.call
  87. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  88. /******/
  89. /******/ // __webpack_public_path__
  90. /******/ __webpack_require__.p = "";
  91. /******/
  92. /******/
  93. /******/ // Load entry module and return exports
  94. /******/ return __webpack_require__(__webpack_require__.s = 23);
  95. /******/ })
  96. /************************************************************************/
  97. /******/ ({
  98. /***/ "./foundation.core":
  99. /*!****************************************************************************************************************************************************************!*\
  100. !*** external {"root":["__FOUNDATION_EXTERNAL__","foundation.core"],"amd":"./foundation.core","commonjs":"./foundation.core","commonjs2":"./foundation.core"} ***!
  101. \****************************************************************************************************************************************************************/
  102. /*! no static exports found */
  103. /***/ (function(module, exports) {
  104. module.exports = __WEBPACK_EXTERNAL_MODULE__foundation_core__;
  105. /***/ }),
  106. /***/ "./js/entries/plugins/foundation.util.keyboard.js":
  107. /*!********************************************************!*\
  108. !*** ./js/entries/plugins/foundation.util.keyboard.js ***!
  109. \********************************************************/
  110. /*! exports provided: Foundation, Keyboard */
  111. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  112. "use strict";
  113. __webpack_require__.r(__webpack_exports__);
  114. /* harmony import */ var _foundation_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./foundation.core */ "./foundation.core");
  115. /* harmony import */ var _foundation_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_foundation_core__WEBPACK_IMPORTED_MODULE_0__);
  116. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "Foundation", function() { return _foundation_core__WEBPACK_IMPORTED_MODULE_0__["Foundation"]; });
  117. /* harmony import */ var _foundation_util_keyboard__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../foundation.util.keyboard */ "./js/foundation.util.keyboard.js");
  118. /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "Keyboard", function() { return _foundation_util_keyboard__WEBPACK_IMPORTED_MODULE_1__["Keyboard"]; });
  119. _foundation_core__WEBPACK_IMPORTED_MODULE_0__["Foundation"].Keyboard = _foundation_util_keyboard__WEBPACK_IMPORTED_MODULE_1__["Keyboard"];
  120. /***/ }),
  121. /***/ "./js/foundation.util.keyboard.js":
  122. /*!****************************************!*\
  123. !*** ./js/foundation.util.keyboard.js ***!
  124. \****************************************/
  125. /*! exports provided: Keyboard */
  126. /***/ (function(module, __webpack_exports__, __webpack_require__) {
  127. "use strict";
  128. __webpack_require__.r(__webpack_exports__);
  129. /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "Keyboard", function() { return Keyboard; });
  130. /* harmony import */ var jquery__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! jquery */ "jquery");
  131. /* harmony import */ var jquery__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(jquery__WEBPACK_IMPORTED_MODULE_0__);
  132. /* harmony import */ var _foundation_core_utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./foundation.core.utils */ "./foundation.core");
  133. /* harmony import */ var _foundation_core_utils__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_foundation_core_utils__WEBPACK_IMPORTED_MODULE_1__);
  134. /*******************************************
  135. * *
  136. * This util was created by Marius Olbertz *
  137. * Please thank Marius on GitHub /owlbertz *
  138. * or the web http://www.mariusolbertz.de/ *
  139. * *
  140. ******************************************/
  141. var keyCodes = {
  142. 9: 'TAB',
  143. 13: 'ENTER',
  144. 27: 'ESCAPE',
  145. 32: 'SPACE',
  146. 35: 'END',
  147. 36: 'HOME',
  148. 37: 'ARROW_LEFT',
  149. 38: 'ARROW_UP',
  150. 39: 'ARROW_RIGHT',
  151. 40: 'ARROW_DOWN'
  152. };
  153. var commands = {}; // Functions pulled out to be referenceable from internals
  154. function findFocusable($element) {
  155. if (!$element) {
  156. return false;
  157. }
  158. return $element.find('a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]').filter(function () {
  159. if (!jquery__WEBPACK_IMPORTED_MODULE_0___default()(this).is(':visible') || jquery__WEBPACK_IMPORTED_MODULE_0___default()(this).attr('tabindex') < 0) {
  160. return false;
  161. } //only have visible elements and those that have a tabindex greater or equal 0
  162. return true;
  163. });
  164. }
  165. function parseKey(event) {
  166. var key = keyCodes[event.which || event.keyCode] || String.fromCharCode(event.which).toUpperCase(); // Remove un-printable characters, e.g. for `fromCharCode` calls for CTRL only events
  167. key = key.replace(/\W+/, '');
  168. if (event.shiftKey) key = "SHIFT_".concat(key);
  169. if (event.ctrlKey) key = "CTRL_".concat(key);
  170. if (event.altKey) key = "ALT_".concat(key); // Remove trailing underscore, in case only modifiers were used (e.g. only `CTRL_ALT`)
  171. key = key.replace(/_$/, '');
  172. return key;
  173. }
  174. var Keyboard = {
  175. keys: getKeyCodes(keyCodes),
  176. /**
  177. * Parses the (keyboard) event and returns a String that represents its key
  178. * Can be used like Foundation.parseKey(event) === Foundation.keys.SPACE
  179. * @param {Event} event - the event generated by the event handler
  180. * @return String key - String that represents the key pressed
  181. */
  182. parseKey: parseKey,
  183. /**
  184. * Handles the given (keyboard) event
  185. * @param {Event} event - the event generated by the event handler
  186. * @param {String} component - Foundation component's name, e.g. Slider or Reveal
  187. * @param {Objects} functions - collection of functions that are to be executed
  188. */
  189. handleKey: function handleKey(event, component, functions) {
  190. var commandList = commands[component],
  191. keyCode = this.parseKey(event),
  192. cmds,
  193. command,
  194. fn;
  195. if (!commandList) return console.warn('Component not defined!'); // Ignore the event if it was already handled
  196. if (event.zfIsKeyHandled === true) return; // This component does not differentiate between ltr and rtl
  197. if (typeof commandList.ltr === 'undefined') {
  198. cmds = commandList; // use plain list
  199. } else {
  200. // merge ltr and rtl: if document is rtl, rtl overwrites ltr and vice versa
  201. if (Object(_foundation_core_utils__WEBPACK_IMPORTED_MODULE_1__["rtl"])()) cmds = jquery__WEBPACK_IMPORTED_MODULE_0___default.a.extend({}, commandList.ltr, commandList.rtl);else cmds = jquery__WEBPACK_IMPORTED_MODULE_0___default.a.extend({}, commandList.rtl, commandList.ltr);
  202. }
  203. command = cmds[keyCode];
  204. fn = functions[command]; // Execute the handler if found
  205. if (fn && typeof fn === 'function') {
  206. var returnValue = fn.apply(); // Mark the event as "handled" to prevent future handlings
  207. event.zfIsKeyHandled = true; // Execute function when event was handled
  208. if (functions.handled || typeof functions.handled === 'function') {
  209. functions.handled(returnValue);
  210. }
  211. } else {
  212. // Execute function when event was not handled
  213. if (functions.unhandled || typeof functions.unhandled === 'function') {
  214. functions.unhandled();
  215. }
  216. }
  217. },
  218. /**
  219. * Finds all focusable elements within the given `$element`
  220. * @param {jQuery} $element - jQuery object to search within
  221. * @return {jQuery} $focusable - all focusable elements within `$element`
  222. */
  223. findFocusable: findFocusable,
  224. /**
  225. * Returns the component name name
  226. * @param {Object} component - Foundation component, e.g. Slider or Reveal
  227. * @return String componentName
  228. */
  229. register: function register(componentName, cmds) {
  230. commands[componentName] = cmds;
  231. },
  232. // TODO9438: These references to Keyboard need to not require global. Will 'this' work in this context?
  233. //
  234. /**
  235. * Traps the focus in the given element.
  236. * @param {jQuery} $element jQuery object to trap the foucs into.
  237. */
  238. trapFocus: function trapFocus($element) {
  239. var $focusable = findFocusable($element),
  240. $firstFocusable = $focusable.eq(0),
  241. $lastFocusable = $focusable.eq(-1);
  242. $element.on('keydown.zf.trapfocus', function (event) {
  243. if (event.target === $lastFocusable[0] && parseKey(event) === 'TAB') {
  244. event.preventDefault();
  245. $firstFocusable.focus();
  246. } else if (event.target === $firstFocusable[0] && parseKey(event) === 'SHIFT_TAB') {
  247. event.preventDefault();
  248. $lastFocusable.focus();
  249. }
  250. });
  251. },
  252. /**
  253. * Releases the trapped focus from the given element.
  254. * @param {jQuery} $element jQuery object to release the focus for.
  255. */
  256. releaseFocus: function releaseFocus($element) {
  257. $element.off('keydown.zf.trapfocus');
  258. }
  259. };
  260. /*
  261. * Constants for easier comparing.
  262. * Can be used like Foundation.parseKey(event) === Foundation.keys.SPACE
  263. */
  264. function getKeyCodes(kcs) {
  265. var k = {};
  266. for (var kc in kcs) {
  267. k[kcs[kc]] = kcs[kc];
  268. }
  269. return k;
  270. }
  271. /***/ }),
  272. /***/ 23:
  273. /*!**************************************************************!*\
  274. !*** multi ./js/entries/plugins/foundation.util.keyboard.js ***!
  275. \**************************************************************/
  276. /*! no static exports found */
  277. /***/ (function(module, exports, __webpack_require__) {
  278. module.exports = __webpack_require__(/*! /Users/joeworkman/Development/foundation-sites/js/entries/plugins/foundation.util.keyboard.js */"./js/entries/plugins/foundation.util.keyboard.js");
  279. /***/ }),
  280. /***/ "jquery":
  281. /*!********************************************************************************************!*\
  282. !*** external {"root":["jQuery"],"amd":"jquery","commonjs":"jquery","commonjs2":"jquery"} ***!
  283. \********************************************************************************************/
  284. /*! no static exports found */
  285. /***/ (function(module, exports) {
  286. module.exports = __WEBPACK_EXTERNAL_MODULE_jquery__;
  287. /***/ })
  288. /******/ });
  289. });
  290. //# sourceMappingURL=foundation.util.keyboard.js.map