baseframe-js
Version:
A suite of useful Javascript plugins and functions to help with Front-end Development on websites
202 lines (201 loc) • 9.86 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _cashDom = _interopRequireDefault(require("cash-dom"));
var _helpers = require("./util/helpers");
var _Store = _interopRequireDefault(require("./core/Store"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
var KEYS = {
esc: 'Escape',
left: 'ArrowLeft',
right: 'ArrowRight',
down: 'ArrowDown',
up: 'ArrowUp',
enter: 'Enter',
shift: 'Shift',
space: 'Space',
tab: 'Tab'
};
var VERSION = "1.3.0";
var DATA_NAME = 'AccessibleMenu';
var EVENT_NAME = 'accessibleMenu';
var DEFAULTS = {
keyDirections: ['horizontal', 'vertical', 'vertical'],
focusCss: 'focus',
focusInElems: 'a, [tabindex]',
focusLeaveElems: 'a, [tabindex], select, button'
};
var visible = function visible(i, el) {
return (0, _helpers.isVisible)(el);
};
var _AccessibleMenu_brand = /*#__PURE__*/new WeakSet();
var AccessibleMenu = exports["default"] = /*#__PURE__*/function () {
function AccessibleMenu(element, options) {
_classCallCheck(this, AccessibleMenu);
_classPrivateMethodInitSpec(this, _AccessibleMenu_brand);
_defineProperty(this, "element", void 0);
_defineProperty(this, "$element", void 0);
_defineProperty(this, "params", void 0);
var s = this;
var dataOptions = (0, _helpers.getDataOptions)(element, EVENT_NAME);
s.element = element;
s.$element = (0, _cashDom["default"])(element);
s.params = _cashDom["default"].extend({}, AccessibleMenu.defaults, options, dataOptions);
s.handleEvents();
return s;
}
return _createClass(AccessibleMenu, [{
key: "prev",
value: function prev(props) {
var s = this;
var p = props;
var l = p.$ulParents.length - 1;
var key = p.e.key;
if (key === KEYS.left && p.keyDirections[l] === "horizontal" || key === KEYS.up && p.keyDirections[l] === "vertical" || key === KEYS.left && p.keyDirections[l] === "vertical" && l > 1 && p.keyDirections[l - 1] === "vertical" && (0, _cashDom["default"])(p.activeElem).parent('li').index() === 0) {
_assertClassBrand(_AccessibleMenu_brand, s, _focusListItem).call(s, p.activeElem, p.$ulParents, p.focusCss, true, p.focusInElems);
p.e.preventDefault();
}
}
}, {
key: "next",
value: function next(props) {
var s = this;
var p = props;
var l = p.$ulParents.length - 1;
var atRootUl = p.$ulParents.length === 1;
var key = p.e.key;
if (
//go to sibling <li>
key === KEYS.right && p.keyDirections[l] === "horizontal" || key === KEYS.down && p.keyDirections[l] === "vertical") {
var isLastAtRoolLevel = atRootUl && (0, _cashDom["default"])(p.activeElem).closest('li').last();
var $currentLi = (0, _cashDom["default"])(p.activeElem).closest('li');
var isLastListItem = !$currentLi.next('li').length;
if (isLastAtRoolLevel && isLastListItem) {
_assertClassBrand(_AccessibleMenu_brand, s, _escapeFromUlAtRootNext).call(s, s.params.focusLeaveElems, p.$ulParents, p.activeElem);
} else {
_assertClassBrand(_AccessibleMenu_brand, s, _focusListItem).call(s, p.activeElem, p.$ulParents, p.focusCss, false, p.focusInElems);
p.e.preventDefault();
}
}
if (
//go to the nestled <li>
key === KEYS.right && p.keyDirections[l] === "vertical" || key === KEYS.down && p.keyDirections[l] === "horizontal") {
_assertClassBrand(_AccessibleMenu_brand, s, _focusNestledListItem).call(s, p.activeElem, p.focusCss, p.focusInElems);
p.e.preventDefault();
}
}
}, {
key: "handleEvents",
value: function handleEvents() {
var s = this;
var to = null;
(0, _cashDom["default"])(s.element).on('focusin.' + EVENT_NAME, this.params.focusInElems, function (e) {
to && clearTimeout(to);
(0, _cashDom["default"])(this).parent('li').addClass('focus').siblings('li').removeClass('focus');
}).on('mouseleave.' + EVENT_NAME, function () {
(0, _cashDom["default"])(this).find('li.focus').removeClass('focus');
}).on('focusout.' + EVENT_NAME, function () {
var _this = this;
to = setTimeout(function () {
(0, _cashDom["default"])(_this).find('li.focus').removeClass('focus');
}, 200);
});
(0, _cashDom["default"])(s.element).on('keydown.' + EVENT_NAME, function (e) {
var _s$params = s.params,
focusCss = _s$params.focusCss,
keyDirections = _s$params.keyDirections,
focusInElems = _s$params.focusInElems;
var activeElem = document.activeElement;
var $ulParents = (0, _cashDom["default"])(activeElem).parents('ul');
var props = {
e: e,
$ulParents: $ulParents,
activeElem: activeElem,
focusCss: focusCss,
keyDirections: keyDirections,
focusInElems: focusInElems
};
_assertClassBrand(_AccessibleMenu_brand, s, _escapeKey).call(s, e, $ulParents, focusCss, focusInElems);
s.prev(props);
s.next(props);
});
}
}], [{
key: "remove",
value: function remove(element, plugin) {
(0, _cashDom["default"])(element).each(function () {
var s = plugin || (0, _Store["default"])(this, DATA_NAME);
s.$element.off('focusin.' + EVENT_NAME);
s.$element.off('mouseleave.' + EVENT_NAME);
s.$element.off('blur.' + EVENT_NAME);
s.$element.off('keydown.' + EVENT_NAME);
(0, _Store["default"])(this, DATA_NAME, null);
});
}
}]);
}();
function _escapeKey(e, $ulParents, focusCss, focusInElems) {
if (e.key == KEYS.esc) {
if ($ulParents.length > 1) {
var $anchor = $ulParents.eq(0).closest('li').find(focusInElems).filter(visible);
$anchor[0].focus();
$anchor.parent('li').addClass(focusCss);
}
e.preventDefault();
}
}
function _focusListItem(activeElem, $ulParents, focusCss, prev, focusInElems) {
var $aeLi = (0, _cashDom["default"])(activeElem).parent('li');
var $el = $aeLi[prev ? 'prev' : 'next']('li').filter(visible);
if ($el.length) {
$el.addClass(focusCss).siblings('li').removeClass(focusCss);
$el.find(focusInElems)[0].focus();
} else {
if ($ulParents.length > 1) {
var $anchor = $ulParents.eq(0).parent('li').find('a').filter(visible);
if ($anchor.length) {
$anchor[0].focus();
$anchor.parent('li').eq(0).addClass(focusCss);
}
}
}
}
function _focusNestledListItem(activeElem, focusCss, focusInElems) {
var $el = (0, _cashDom["default"])(activeElem).parent('li').find('li').filter(visible);
if ($el.length) {
$el.addClass(focusCss).siblings('li').removeClass(focusCss);
$el.find(focusInElems).filter(visible)[0].focus();
}
}
function _escapeFromUlAtRootNext(focusLeaveElems, $ulParents, activeElem) {
var $rootUl = $ulParents.eq(0);
var focusableElems = document.querySelectorAll(focusLeaveElems);
var atCurrElem = false;
for (var i = 0, l = focusableElems.length; i < l; i++) {
var elem = focusableElems[i];
if (!atCurrElem && activeElem !== null && activeElem !== void 0 && activeElem.isSameNode(elem)) {
atCurrElem = true;
}
if (atCurrElem && !$rootUl.has(elem).length) {
if (elem instanceof HTMLElement) {
elem.focus();
break;
}
}
}
}
_defineProperty(AccessibleMenu, "defaults", DEFAULTS);
_defineProperty(AccessibleMenu, "version", VERSION);
_defineProperty(AccessibleMenu, "pluginName", DATA_NAME);