mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-05 16:48:25 -04:00
7073 lines
779 KiB
JavaScript
7073 lines
779 KiB
JavaScript
|
|
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/*
|
||
|
|
* classList.js: Cross-browser full element.classList implementation.
|
||
|
|
* 2014-07-23
|
||
|
|
*
|
||
|
|
* By Eli Grey, http://eligrey.com
|
||
|
|
* Public Domain.
|
||
|
|
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/*global self, document, DOMException */
|
||
|
|
|
||
|
|
/*! @source http://purl.eligrey.com/github/classList.js/blob/master/classList.js*/
|
||
|
|
|
||
|
|
/* Copied from MDN:
|
||
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
|
||
|
|
*/
|
||
|
|
|
||
|
|
if ("document" in window.self) {
|
||
|
|
// Full polyfill for browsers with no classList support
|
||
|
|
// Including IE < Edge missing SVGElement.classList
|
||
|
|
if (!("classList" in document.createElement("_")) || document.createElementNS && !("classList" in document.createElementNS("http://www.w3.org/2000/svg", "g"))) {
|
||
|
|
(function (view) {
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
if (!('Element' in view)) return;
|
||
|
|
var classListProp = "classList",
|
||
|
|
protoProp = "prototype",
|
||
|
|
elemCtrProto = view.Element[protoProp],
|
||
|
|
objCtr = Object,
|
||
|
|
strTrim = String[protoProp].trim || function () {
|
||
|
|
return this.replace(/^\s+|\s+$/g, "");
|
||
|
|
},
|
||
|
|
arrIndexOf = Array[protoProp].indexOf || function (item) {
|
||
|
|
var i = 0,
|
||
|
|
len = this.length;
|
||
|
|
for (; i < len; i++) {
|
||
|
|
if (i in this && this[i] === item) {
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
// Vendors: please allow content code to instantiate DOMExceptions
|
||
|
|
,
|
||
|
|
DOMEx = function (type, message) {
|
||
|
|
this.name = type;
|
||
|
|
this.code = DOMException[type];
|
||
|
|
this.message = message;
|
||
|
|
},
|
||
|
|
checkTokenAndGetIndex = function (classList, token) {
|
||
|
|
if (token === "") {
|
||
|
|
throw new DOMEx("SYNTAX_ERR", "An invalid or illegal string was specified");
|
||
|
|
}
|
||
|
|
if (/\s/.test(token)) {
|
||
|
|
throw new DOMEx("INVALID_CHARACTER_ERR", "String contains an invalid character");
|
||
|
|
}
|
||
|
|
return arrIndexOf.call(classList, token);
|
||
|
|
},
|
||
|
|
ClassList = function (elem) {
|
||
|
|
var trimmedClasses = strTrim.call(elem.getAttribute("class") || ""),
|
||
|
|
classes = trimmedClasses ? trimmedClasses.split(/\s+/) : [],
|
||
|
|
i = 0,
|
||
|
|
len = classes.length;
|
||
|
|
for (; i < len; i++) {
|
||
|
|
this.push(classes[i]);
|
||
|
|
}
|
||
|
|
this._updateClassName = function () {
|
||
|
|
elem.setAttribute("class", this.toString());
|
||
|
|
};
|
||
|
|
},
|
||
|
|
classListProto = ClassList[protoProp] = [],
|
||
|
|
classListGetter = function () {
|
||
|
|
return new ClassList(this);
|
||
|
|
};
|
||
|
|
// Most DOMException implementations don't allow calling DOMException's toString()
|
||
|
|
// on non-DOMExceptions. Error's toString() is sufficient here.
|
||
|
|
DOMEx[protoProp] = Error[protoProp];
|
||
|
|
classListProto.item = function (i) {
|
||
|
|
return this[i] || null;
|
||
|
|
};
|
||
|
|
classListProto.contains = function (token) {
|
||
|
|
token += "";
|
||
|
|
return checkTokenAndGetIndex(this, token) !== -1;
|
||
|
|
};
|
||
|
|
classListProto.add = function () {
|
||
|
|
var tokens = arguments,
|
||
|
|
i = 0,
|
||
|
|
l = tokens.length,
|
||
|
|
token,
|
||
|
|
updated = false;
|
||
|
|
do {
|
||
|
|
token = tokens[i] + "";
|
||
|
|
if (checkTokenAndGetIndex(this, token) === -1) {
|
||
|
|
this.push(token);
|
||
|
|
updated = true;
|
||
|
|
}
|
||
|
|
} while (++i < l);
|
||
|
|
if (updated) {
|
||
|
|
this._updateClassName();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
classListProto.remove = function () {
|
||
|
|
var tokens = arguments,
|
||
|
|
i = 0,
|
||
|
|
l = tokens.length,
|
||
|
|
token,
|
||
|
|
updated = false,
|
||
|
|
index;
|
||
|
|
do {
|
||
|
|
token = tokens[i] + "";
|
||
|
|
index = checkTokenAndGetIndex(this, token);
|
||
|
|
while (index !== -1) {
|
||
|
|
this.splice(index, 1);
|
||
|
|
updated = true;
|
||
|
|
index = checkTokenAndGetIndex(this, token);
|
||
|
|
}
|
||
|
|
} while (++i < l);
|
||
|
|
if (updated) {
|
||
|
|
this._updateClassName();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
classListProto.toggle = function (token, force) {
|
||
|
|
token += "";
|
||
|
|
var result = this.contains(token),
|
||
|
|
method = result ? force !== true && "remove" : force !== false && "add";
|
||
|
|
if (method) {
|
||
|
|
this[method](token);
|
||
|
|
}
|
||
|
|
if (force === true || force === false) {
|
||
|
|
return force;
|
||
|
|
} else {
|
||
|
|
return !result;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
classListProto.toString = function () {
|
||
|
|
return this.join(" ");
|
||
|
|
};
|
||
|
|
if (objCtr.defineProperty) {
|
||
|
|
var classListPropDesc = {
|
||
|
|
get: classListGetter,
|
||
|
|
enumerable: true,
|
||
|
|
configurable: true
|
||
|
|
};
|
||
|
|
try {
|
||
|
|
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
|
||
|
|
} catch (ex) {
|
||
|
|
// IE 8 doesn't support enumerable:true
|
||
|
|
if (ex.number === -0x7FF5EC54) {
|
||
|
|
classListPropDesc.enumerable = false;
|
||
|
|
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else if (objCtr[protoProp].__defineGetter__) {
|
||
|
|
elemCtrProto.__defineGetter__(classListProp, classListGetter);
|
||
|
|
}
|
||
|
|
})(window.self);
|
||
|
|
} else {
|
||
|
|
// There is full or partial native classList support, so just check if we need
|
||
|
|
// to normalize the add/remove and toggle APIs.
|
||
|
|
|
||
|
|
(function () {
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
var testElement = document.createElement("_");
|
||
|
|
testElement.classList.add("c1", "c2");
|
||
|
|
|
||
|
|
// Polyfill for IE 10/11 and Firefox <26, where classList.add and
|
||
|
|
// classList.remove exist but support only one argument at a time.
|
||
|
|
if (!testElement.classList.contains("c2")) {
|
||
|
|
var createMethod = function (method) {
|
||
|
|
var original = DOMTokenList.prototype[method];
|
||
|
|
DOMTokenList.prototype[method] = function (token) {
|
||
|
|
var i,
|
||
|
|
len = arguments.length;
|
||
|
|
for (i = 0; i < len; i++) {
|
||
|
|
token = arguments[i];
|
||
|
|
original.call(this, token);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
};
|
||
|
|
createMethod('add');
|
||
|
|
createMethod('remove');
|
||
|
|
}
|
||
|
|
testElement.classList.toggle("c3", false);
|
||
|
|
|
||
|
|
// Polyfill for IE 10 and Firefox <24, where classList.toggle does not
|
||
|
|
// support the second argument.
|
||
|
|
if (testElement.classList.contains("c3")) {
|
||
|
|
var _toggle = DOMTokenList.prototype.toggle;
|
||
|
|
DOMTokenList.prototype.toggle = function (token, force) {
|
||
|
|
if (1 in arguments && !this.contains(token) === !force) {
|
||
|
|
return force;
|
||
|
|
} else {
|
||
|
|
return _toggle.call(this, token);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
testElement = null;
|
||
|
|
})();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
},{}],2:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
// element-closest | CC0-1.0 | github.com/jonathantneal/closest
|
||
|
|
|
||
|
|
(function (ElementProto) {
|
||
|
|
if (typeof ElementProto.matches !== 'function') {
|
||
|
|
ElementProto.matches = ElementProto.msMatchesSelector || ElementProto.mozMatchesSelector || ElementProto.webkitMatchesSelector || function matches(selector) {
|
||
|
|
var element = this;
|
||
|
|
var elements = (element.document || element.ownerDocument).querySelectorAll(selector);
|
||
|
|
var index = 0;
|
||
|
|
while (elements[index] && elements[index] !== element) {
|
||
|
|
++index;
|
||
|
|
}
|
||
|
|
return Boolean(elements[index]);
|
||
|
|
};
|
||
|
|
}
|
||
|
|
if (typeof ElementProto.closest !== 'function') {
|
||
|
|
ElementProto.closest = function closest(selector) {
|
||
|
|
var element = this;
|
||
|
|
while (element && element.nodeType === 1) {
|
||
|
|
if (element.matches(selector)) {
|
||
|
|
return element;
|
||
|
|
}
|
||
|
|
element = element.parentNode;
|
||
|
|
}
|
||
|
|
return null;
|
||
|
|
};
|
||
|
|
}
|
||
|
|
})(window.Element.prototype);
|
||
|
|
|
||
|
|
},{}],3:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/* global define, KeyboardEvent, module */
|
||
|
|
|
||
|
|
(function () {
|
||
|
|
var keyboardeventKeyPolyfill = {
|
||
|
|
polyfill: polyfill,
|
||
|
|
keys: {
|
||
|
|
3: 'Cancel',
|
||
|
|
6: 'Help',
|
||
|
|
8: 'Backspace',
|
||
|
|
9: 'Tab',
|
||
|
|
12: 'Clear',
|
||
|
|
13: 'Enter',
|
||
|
|
16: 'Shift',
|
||
|
|
17: 'Control',
|
||
|
|
18: 'Alt',
|
||
|
|
19: 'Pause',
|
||
|
|
20: 'CapsLock',
|
||
|
|
27: 'Escape',
|
||
|
|
28: 'Convert',
|
||
|
|
29: 'NonConvert',
|
||
|
|
30: 'Accept',
|
||
|
|
31: 'ModeChange',
|
||
|
|
32: ' ',
|
||
|
|
33: 'PageUp',
|
||
|
|
34: 'PageDown',
|
||
|
|
35: 'End',
|
||
|
|
36: 'Home',
|
||
|
|
37: 'ArrowLeft',
|
||
|
|
38: 'ArrowUp',
|
||
|
|
39: 'ArrowRight',
|
||
|
|
40: 'ArrowDown',
|
||
|
|
41: 'Select',
|
||
|
|
42: 'Print',
|
||
|
|
43: 'Execute',
|
||
|
|
44: 'PrintScreen',
|
||
|
|
45: 'Insert',
|
||
|
|
46: 'Delete',
|
||
|
|
48: ['0', ')'],
|
||
|
|
49: ['1', '!'],
|
||
|
|
50: ['2', '@'],
|
||
|
|
51: ['3', '#'],
|
||
|
|
52: ['4', '$'],
|
||
|
|
53: ['5', '%'],
|
||
|
|
54: ['6', '^'],
|
||
|
|
55: ['7', '&'],
|
||
|
|
56: ['8', '*'],
|
||
|
|
57: ['9', '('],
|
||
|
|
91: 'OS',
|
||
|
|
93: 'ContextMenu',
|
||
|
|
144: 'NumLock',
|
||
|
|
145: 'ScrollLock',
|
||
|
|
181: 'VolumeMute',
|
||
|
|
182: 'VolumeDown',
|
||
|
|
183: 'VolumeUp',
|
||
|
|
186: [';', ':'],
|
||
|
|
187: ['=', '+'],
|
||
|
|
188: [',', '<'],
|
||
|
|
189: ['-', '_'],
|
||
|
|
190: ['.', '>'],
|
||
|
|
191: ['/', '?'],
|
||
|
|
192: ['`', '~'],
|
||
|
|
219: ['[', '{'],
|
||
|
|
220: ['\\', '|'],
|
||
|
|
221: [']', '}'],
|
||
|
|
222: ["'", '"'],
|
||
|
|
224: 'Meta',
|
||
|
|
225: 'AltGraph',
|
||
|
|
246: 'Attn',
|
||
|
|
247: 'CrSel',
|
||
|
|
248: 'ExSel',
|
||
|
|
249: 'EraseEof',
|
||
|
|
250: 'Play',
|
||
|
|
251: 'ZoomOut'
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// Function keys (F1-24).
|
||
|
|
var i;
|
||
|
|
for (i = 1; i < 25; i++) {
|
||
|
|
keyboardeventKeyPolyfill.keys[111 + i] = 'F' + i;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Printable ASCII characters.
|
||
|
|
var letter = '';
|
||
|
|
for (i = 65; i < 91; i++) {
|
||
|
|
letter = String.fromCharCode(i);
|
||
|
|
keyboardeventKeyPolyfill.keys[i] = [letter.toLowerCase(), letter.toUpperCase()];
|
||
|
|
}
|
||
|
|
function polyfill() {
|
||
|
|
if (!('KeyboardEvent' in window) || 'key' in KeyboardEvent.prototype) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Polyfill `key` on `KeyboardEvent`.
|
||
|
|
var proto = {
|
||
|
|
get: function (x) {
|
||
|
|
var key = keyboardeventKeyPolyfill.keys[this.which || this.keyCode];
|
||
|
|
if (Array.isArray(key)) {
|
||
|
|
key = key[+this.shiftKey];
|
||
|
|
}
|
||
|
|
return key;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
Object.defineProperty(KeyboardEvent.prototype, 'key', proto);
|
||
|
|
return proto;
|
||
|
|
}
|
||
|
|
if (typeof define === 'function' && define.amd) {
|
||
|
|
define('keyboardevent-key-polyfill', keyboardeventKeyPolyfill);
|
||
|
|
} else if (typeof exports !== 'undefined' && typeof module !== 'undefined') {
|
||
|
|
module.exports = keyboardeventKeyPolyfill;
|
||
|
|
} else if (window) {
|
||
|
|
window.keyboardeventKeyPolyfill = keyboardeventKeyPolyfill;
|
||
|
|
}
|
||
|
|
})();
|
||
|
|
|
||
|
|
},{}],4:[function(require,module,exports){
|
||
|
|
/*
|
||
|
|
object-assign
|
||
|
|
(c) Sindre Sorhus
|
||
|
|
@license MIT
|
||
|
|
*/
|
||
|
|
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
/* eslint-disable no-unused-vars */
|
||
|
|
var getOwnPropertySymbols = Object.getOwnPropertySymbols;
|
||
|
|
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
||
|
|
var propIsEnumerable = Object.prototype.propertyIsEnumerable;
|
||
|
|
function toObject(val) {
|
||
|
|
if (val === null || val === undefined) {
|
||
|
|
throw new TypeError('Object.assign cannot be called with null or undefined');
|
||
|
|
}
|
||
|
|
return Object(val);
|
||
|
|
}
|
||
|
|
function shouldUseNative() {
|
||
|
|
try {
|
||
|
|
if (!Object.assign) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Detect buggy property enumeration order in older V8 versions.
|
||
|
|
|
||
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=4118
|
||
|
|
var test1 = new String('abc'); // eslint-disable-line no-new-wrappers
|
||
|
|
test1[5] = 'de';
|
||
|
|
if (Object.getOwnPropertyNames(test1)[0] === '5') {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=3056
|
||
|
|
var test2 = {};
|
||
|
|
for (var i = 0; i < 10; i++) {
|
||
|
|
test2['_' + String.fromCharCode(i)] = i;
|
||
|
|
}
|
||
|
|
var order2 = Object.getOwnPropertyNames(test2).map(function (n) {
|
||
|
|
return test2[n];
|
||
|
|
});
|
||
|
|
if (order2.join('') !== '0123456789') {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// https://bugs.chromium.org/p/v8/issues/detail?id=3056
|
||
|
|
var test3 = {};
|
||
|
|
'abcdefghijklmnopqrst'.split('').forEach(function (letter) {
|
||
|
|
test3[letter] = letter;
|
||
|
|
});
|
||
|
|
if (Object.keys(Object.assign({}, test3)).join('') !== 'abcdefghijklmnopqrst') {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
} catch (err) {
|
||
|
|
// We don't expect any of the above to throw, but better to be safe.
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
module.exports = shouldUseNative() ? Object.assign : function (target, source) {
|
||
|
|
var from;
|
||
|
|
var to = toObject(target);
|
||
|
|
var symbols;
|
||
|
|
for (var s = 1; s < arguments.length; s++) {
|
||
|
|
from = Object(arguments[s]);
|
||
|
|
for (var key in from) {
|
||
|
|
if (hasOwnProperty.call(from, key)) {
|
||
|
|
to[key] = from[key];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (getOwnPropertySymbols) {
|
||
|
|
symbols = getOwnPropertySymbols(from);
|
||
|
|
for (var i = 0; i < symbols.length; i++) {
|
||
|
|
if (propIsEnumerable.call(from, symbols[i])) {
|
||
|
|
to[symbols[i]] = from[symbols[i]];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return to;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],5:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const assign = require('object-assign');
|
||
|
|
const delegate = require('../delegate');
|
||
|
|
const delegateAll = require('../delegateAll');
|
||
|
|
const DELEGATE_PATTERN = /^(.+):delegate\((.+)\)$/;
|
||
|
|
const SPACE = ' ';
|
||
|
|
const getListeners = function (type, handler) {
|
||
|
|
var match = type.match(DELEGATE_PATTERN);
|
||
|
|
var selector;
|
||
|
|
if (match) {
|
||
|
|
type = match[1];
|
||
|
|
selector = match[2];
|
||
|
|
}
|
||
|
|
var options;
|
||
|
|
if (typeof handler === 'object') {
|
||
|
|
options = {
|
||
|
|
capture: popKey(handler, 'capture'),
|
||
|
|
passive: popKey(handler, 'passive')
|
||
|
|
};
|
||
|
|
}
|
||
|
|
var listener = {
|
||
|
|
selector: selector,
|
||
|
|
delegate: typeof handler === 'object' ? delegateAll(handler) : selector ? delegate(selector, handler) : handler,
|
||
|
|
options: options
|
||
|
|
};
|
||
|
|
if (type.indexOf(SPACE) > -1) {
|
||
|
|
return type.split(SPACE).map(function (_type) {
|
||
|
|
return assign({
|
||
|
|
type: _type
|
||
|
|
}, listener);
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
listener.type = type;
|
||
|
|
return [listener];
|
||
|
|
}
|
||
|
|
};
|
||
|
|
var popKey = function (obj, key) {
|
||
|
|
var value = obj[key];
|
||
|
|
delete obj[key];
|
||
|
|
return value;
|
||
|
|
};
|
||
|
|
module.exports = function behavior(events, props) {
|
||
|
|
const listeners = Object.keys(events).reduce(function (memo, type) {
|
||
|
|
var listeners = getListeners(type, events[type]);
|
||
|
|
return memo.concat(listeners);
|
||
|
|
}, []);
|
||
|
|
return assign({
|
||
|
|
add: function addBehavior(element) {
|
||
|
|
listeners.forEach(function (listener) {
|
||
|
|
element.addEventListener(listener.type, listener.delegate, listener.options);
|
||
|
|
});
|
||
|
|
},
|
||
|
|
remove: function removeBehavior(element) {
|
||
|
|
listeners.forEach(function (listener) {
|
||
|
|
element.removeEventListener(listener.type, listener.delegate, listener.options);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}, props);
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"../delegate":7,"../delegateAll":8,"object-assign":4}],6:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
module.exports = function compose(functions) {
|
||
|
|
return function (e) {
|
||
|
|
return functions.some(function (fn) {
|
||
|
|
return fn.call(this, e) === false;
|
||
|
|
}, this);
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],7:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
// polyfill Element.prototype.closest
|
||
|
|
require('element-closest');
|
||
|
|
module.exports = function delegate(selector, fn) {
|
||
|
|
return function delegation(event) {
|
||
|
|
var target = event.target.closest(selector);
|
||
|
|
if (target) {
|
||
|
|
return fn.call(target, event);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"element-closest":2}],8:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const delegate = require('../delegate');
|
||
|
|
const compose = require('../compose');
|
||
|
|
const SPLAT = '*';
|
||
|
|
module.exports = function delegateAll(selectors) {
|
||
|
|
const keys = Object.keys(selectors);
|
||
|
|
|
||
|
|
// XXX optimization: if there is only one handler and it applies to
|
||
|
|
// all elements (the "*" CSS selector), then just return that
|
||
|
|
// handler
|
||
|
|
if (keys.length === 1 && keys[0] === SPLAT) {
|
||
|
|
return selectors[SPLAT];
|
||
|
|
}
|
||
|
|
const delegates = keys.reduce(function (memo, selector) {
|
||
|
|
memo.push(delegate(selector, selectors[selector]));
|
||
|
|
return memo;
|
||
|
|
}, []);
|
||
|
|
return compose(delegates);
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"../compose":6,"../delegate":7}],9:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
module.exports = function ignore(element, fn) {
|
||
|
|
return function ignorance(e) {
|
||
|
|
if (element !== e.target && !element.contains(e.target)) {
|
||
|
|
return fn.call(this, e);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],10:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
behavior: require('./behavior'),
|
||
|
|
delegate: require('./delegate'),
|
||
|
|
delegateAll: require('./delegateAll'),
|
||
|
|
ignore: require('./ignore'),
|
||
|
|
keymap: require('./keymap')
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"./behavior":5,"./delegate":7,"./delegateAll":8,"./ignore":9,"./keymap":11}],11:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
require('keyboardevent-key-polyfill');
|
||
|
|
|
||
|
|
// these are the only relevant modifiers supported on all platforms,
|
||
|
|
// according to MDN:
|
||
|
|
// <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState>
|
||
|
|
const MODIFIERS = {
|
||
|
|
'Alt': 'altKey',
|
||
|
|
'Control': 'ctrlKey',
|
||
|
|
'Ctrl': 'ctrlKey',
|
||
|
|
'Shift': 'shiftKey'
|
||
|
|
};
|
||
|
|
const MODIFIER_SEPARATOR = '+';
|
||
|
|
const getEventKey = function (event, hasModifiers) {
|
||
|
|
var key = event.key;
|
||
|
|
if (hasModifiers) {
|
||
|
|
for (var modifier in MODIFIERS) {
|
||
|
|
if (event[MODIFIERS[modifier]] === true) {
|
||
|
|
key = [modifier, key].join(MODIFIER_SEPARATOR);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return key;
|
||
|
|
};
|
||
|
|
module.exports = function keymap(keys) {
|
||
|
|
const hasModifiers = Object.keys(keys).some(function (key) {
|
||
|
|
return key.indexOf(MODIFIER_SEPARATOR) > -1;
|
||
|
|
});
|
||
|
|
return function (event) {
|
||
|
|
var key = getEventKey(event, hasModifiers);
|
||
|
|
return [key, key.toLowerCase()].reduce(function (result, _key) {
|
||
|
|
if (_key in keys) {
|
||
|
|
result = keys[key].call(this, event);
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}, undefined);
|
||
|
|
};
|
||
|
|
};
|
||
|
|
module.exports.MODIFIERS = MODIFIERS;
|
||
|
|
|
||
|
|
},{"keyboardevent-key-polyfill":3}],12:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
module.exports = function once(listener, options) {
|
||
|
|
var wrapped = function wrappedOnce(e) {
|
||
|
|
e.currentTarget.removeEventListener(e.type, wrapped, options);
|
||
|
|
return listener.call(this, e);
|
||
|
|
};
|
||
|
|
return wrapped;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],13:[function(require,module,exports){
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
var RE_TRIM = /(^\s+)|(\s+$)/g;
|
||
|
|
var RE_SPLIT = /\s+/;
|
||
|
|
var trim = String.prototype.trim ? function (str) {
|
||
|
|
return str.trim();
|
||
|
|
} : function (str) {
|
||
|
|
return str.replace(RE_TRIM, '');
|
||
|
|
};
|
||
|
|
var queryById = function (id) {
|
||
|
|
return this.querySelector('[id="' + id.replace(/"/g, '\\"') + '"]');
|
||
|
|
};
|
||
|
|
module.exports = function resolveIds(ids, doc) {
|
||
|
|
if (typeof ids !== 'string') {
|
||
|
|
throw new Error('Expected a string but got ' + typeof ids);
|
||
|
|
}
|
||
|
|
if (!doc) {
|
||
|
|
doc = window.document;
|
||
|
|
}
|
||
|
|
var getElementById = doc.getElementById ? doc.getElementById.bind(doc) : queryById.bind(doc);
|
||
|
|
ids = trim(ids).split(RE_SPLIT);
|
||
|
|
|
||
|
|
// XXX we can short-circuit here because trimming and splitting a
|
||
|
|
// string of just whitespace produces an array containing a single,
|
||
|
|
// empty string
|
||
|
|
if (ids.length === 1 && ids[0] === '') {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
return ids.map(function (id) {
|
||
|
|
var el = getElementById(id);
|
||
|
|
if (!el) {
|
||
|
|
throw new Error('no element with id: "' + id + '"');
|
||
|
|
}
|
||
|
|
return el;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],14:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const toggleFormInput = require("../../uswds-core/src/js/utils/toggle-form-input");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const LINK = `.${PREFIX}-show-password`;
|
||
|
|
function toggle(event) {
|
||
|
|
event.preventDefault();
|
||
|
|
toggleFormInput(this);
|
||
|
|
}
|
||
|
|
module.exports = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[LINK]: toggle
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/toggle-form-input":55}],15:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const select = require("../../uswds-core/src/js/utils/select");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const toggle = require("../../uswds-core/src/js/utils/toggle");
|
||
|
|
const isElementInViewport = require("../../uswds-core/src/js/utils/is-in-viewport");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const ACCORDION = `.${PREFIX}-accordion, .${PREFIX}-accordion--bordered`;
|
||
|
|
const BUTTON = `.${PREFIX}-accordion__button[aria-controls]`;
|
||
|
|
const EXPANDED = "aria-expanded";
|
||
|
|
const MULTISELECTABLE = "data-allow-multiple";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get an Array of button elements belonging directly to the given
|
||
|
|
* accordion element.
|
||
|
|
* @param {HTMLElement} accordion
|
||
|
|
* @return {array<HTMLButtonElement>}
|
||
|
|
*/
|
||
|
|
const getAccordionButtons = accordion => {
|
||
|
|
const buttons = select(BUTTON, accordion);
|
||
|
|
return buttons.filter(button => button.closest(ACCORDION) === accordion);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Toggle a button's "pressed" state, optionally providing a target
|
||
|
|
* state.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} button
|
||
|
|
* @param {boolean?} expanded If no state is provided, the current
|
||
|
|
* state will be toggled (from false to true, and vice-versa).
|
||
|
|
* @return {boolean} the resulting state
|
||
|
|
*/
|
||
|
|
const toggleButton = (button, expanded) => {
|
||
|
|
const accordion = button.closest(ACCORDION);
|
||
|
|
let safeExpanded = expanded;
|
||
|
|
if (!accordion) {
|
||
|
|
throw new Error(`${BUTTON} is missing outer ${ACCORDION}`);
|
||
|
|
}
|
||
|
|
safeExpanded = toggle(button, expanded);
|
||
|
|
|
||
|
|
// XXX multiselectable is opt-in, to preserve legacy behavior
|
||
|
|
const multiselectable = accordion.hasAttribute(MULTISELECTABLE);
|
||
|
|
if (safeExpanded && !multiselectable) {
|
||
|
|
getAccordionButtons(accordion).forEach(other => {
|
||
|
|
if (other !== button) {
|
||
|
|
toggle(other, false);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param {HTMLButtonElement} button
|
||
|
|
* @return {boolean} true
|
||
|
|
*/
|
||
|
|
const showButton = button => toggleButton(button, true);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param {HTMLButtonElement} button
|
||
|
|
* @return {boolean} false
|
||
|
|
*/
|
||
|
|
const hideButton = button => toggleButton(button, false);
|
||
|
|
const accordion = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[BUTTON]() {
|
||
|
|
toggleButton(this);
|
||
|
|
if (this.getAttribute(EXPANDED) === "true") {
|
||
|
|
// We were just expanded, but if another accordion was also just
|
||
|
|
// collapsed, we may no longer be in the viewport. This ensures
|
||
|
|
// that we are still visible, so the user isn't confused.
|
||
|
|
if (!isElementInViewport(this)) this.scrollIntoView();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
select(BUTTON, root).forEach(button => {
|
||
|
|
const expanded = button.getAttribute(EXPANDED) === "true";
|
||
|
|
toggleButton(button, expanded);
|
||
|
|
});
|
||
|
|
},
|
||
|
|
ACCORDION,
|
||
|
|
BUTTON,
|
||
|
|
show: showButton,
|
||
|
|
hide: hideButton,
|
||
|
|
toggle: toggleButton,
|
||
|
|
getButtons: getAccordionButtons
|
||
|
|
});
|
||
|
|
module.exports = accordion;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/is-in-viewport":48,"../../uswds-core/src/js/utils/select":53,"../../uswds-core/src/js/utils/toggle":56}],16:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const HEADER = `.${PREFIX}-banner__header`;
|
||
|
|
const EXPANDED_CLASS = `${PREFIX}-banner__header--expanded`;
|
||
|
|
const toggleBanner = function toggleEl(event) {
|
||
|
|
event.preventDefault();
|
||
|
|
this.closest(HEADER).classList.toggle(EXPANDED_CLASS);
|
||
|
|
};
|
||
|
|
module.exports = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[`${HEADER} [aria-controls]`]: toggleBanner
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45}],17:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const select = require("../../uswds-core/src/js/utils/select");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const debounce = require("../../uswds-core/src/js/utils/debounce");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const CHARACTER_COUNT_CLASS = `${PREFIX}-character-count`;
|
||
|
|
const CHARACTER_COUNT = `.${CHARACTER_COUNT_CLASS}`;
|
||
|
|
const INPUT = `.${PREFIX}-character-count__field`;
|
||
|
|
const MESSAGE = `.${PREFIX}-character-count__message`;
|
||
|
|
const VALIDATION_MESSAGE = "The content is too long.";
|
||
|
|
const MESSAGE_INVALID_CLASS = `${PREFIX}-character-count__status--invalid`;
|
||
|
|
const STATUS_MESSAGE_CLASS = `${CHARACTER_COUNT_CLASS}__status`;
|
||
|
|
const STATUS_MESSAGE_SR_ONLY_CLASS = `${CHARACTER_COUNT_CLASS}__sr-status`;
|
||
|
|
const STATUS_MESSAGE = `.${STATUS_MESSAGE_CLASS}`;
|
||
|
|
const STATUS_MESSAGE_SR_ONLY = `.${STATUS_MESSAGE_SR_ONLY_CLASS}`;
|
||
|
|
const DEFAULT_STATUS_LABEL = `characters allowed`;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns the root and message element for an character count input
|
||
|
|
*
|
||
|
|
* @param {HTMLInputElement|HTMLTextAreaElement} inputEl The character count input element
|
||
|
|
* @returns {CharacterCountElements} elements The root and message element.
|
||
|
|
*/
|
||
|
|
const getCharacterCountElements = inputEl => {
|
||
|
|
const characterCountEl = inputEl.closest(CHARACTER_COUNT);
|
||
|
|
if (!characterCountEl) {
|
||
|
|
throw new Error(`${INPUT} is missing outer ${CHARACTER_COUNT}`);
|
||
|
|
}
|
||
|
|
const messageEl = characterCountEl.querySelector(MESSAGE);
|
||
|
|
if (!messageEl) {
|
||
|
|
throw new Error(`${CHARACTER_COUNT} is missing inner ${MESSAGE}`);
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
characterCountEl,
|
||
|
|
messageEl
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Move maxlength attribute to a data attribute on usa-character-count
|
||
|
|
*
|
||
|
|
* @param {HTMLInputElement|HTMLTextAreaElement} inputEl The character count input element
|
||
|
|
*/
|
||
|
|
const setDataLength = inputEl => {
|
||
|
|
const {
|
||
|
|
characterCountEl
|
||
|
|
} = getCharacterCountElements(inputEl);
|
||
|
|
const maxlength = inputEl.getAttribute("maxlength");
|
||
|
|
if (!maxlength) return;
|
||
|
|
inputEl.removeAttribute("maxlength");
|
||
|
|
characterCountEl.setAttribute("data-maxlength", maxlength);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create and append status messages for visual and screen readers
|
||
|
|
*
|
||
|
|
* @param {HTMLDivElement} characterCountEl - Div with `.usa-character-count` class
|
||
|
|
* @description Create two status messages for number of characters left;
|
||
|
|
* one visual status and another for screen readers
|
||
|
|
*/
|
||
|
|
const createStatusMessages = characterCountEl => {
|
||
|
|
const statusMessage = document.createElement("div");
|
||
|
|
const srStatusMessage = document.createElement("div");
|
||
|
|
const maxLength = characterCountEl.dataset.maxlength;
|
||
|
|
const defaultMessage = `${maxLength} ${DEFAULT_STATUS_LABEL}`;
|
||
|
|
statusMessage.classList.add(`${STATUS_MESSAGE_CLASS}`, "usa-hint");
|
||
|
|
srStatusMessage.classList.add(`${STATUS_MESSAGE_SR_ONLY_CLASS}`, "usa-sr-only");
|
||
|
|
statusMessage.setAttribute("aria-hidden", true);
|
||
|
|
srStatusMessage.setAttribute("aria-live", "polite");
|
||
|
|
statusMessage.textContent = defaultMessage;
|
||
|
|
srStatusMessage.textContent = defaultMessage;
|
||
|
|
characterCountEl.append(statusMessage, srStatusMessage);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Returns message with how many characters are left
|
||
|
|
*
|
||
|
|
* @param {number} currentLength - The number of characters used
|
||
|
|
* @param {number} maxLength - The total number of characters allowed
|
||
|
|
* @returns {string} A string description of how many characters are left
|
||
|
|
*/
|
||
|
|
const getCountMessage = (currentLength, maxLength) => {
|
||
|
|
let newMessage = "";
|
||
|
|
if (currentLength === 0) {
|
||
|
|
newMessage = `${maxLength} ${DEFAULT_STATUS_LABEL}`;
|
||
|
|
} else {
|
||
|
|
const difference = Math.abs(maxLength - currentLength);
|
||
|
|
const characters = `character${difference === 1 ? "" : "s"}`;
|
||
|
|
const guidance = currentLength > maxLength ? "over limit" : "left";
|
||
|
|
newMessage = `${difference} ${characters} ${guidance}`;
|
||
|
|
}
|
||
|
|
return newMessage;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Updates the character count status for screen readers after a 1000ms delay.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} msgEl - The screen reader status message element
|
||
|
|
* @param {string} statusMessage - A string of the current character status
|
||
|
|
*/
|
||
|
|
const srUpdateStatus = debounce((msgEl, statusMessage) => {
|
||
|
|
const srStatusMessage = msgEl;
|
||
|
|
srStatusMessage.textContent = statusMessage;
|
||
|
|
}, 1000);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the character count component
|
||
|
|
*
|
||
|
|
* @description On input, it will update visual status, screenreader
|
||
|
|
* status and update input validation (if over character length)
|
||
|
|
* @param {HTMLInputElement|HTMLTextAreaElement} inputEl The character count input element
|
||
|
|
*/
|
||
|
|
const updateCountMessage = inputEl => {
|
||
|
|
const {
|
||
|
|
characterCountEl
|
||
|
|
} = getCharacterCountElements(inputEl);
|
||
|
|
const currentLength = inputEl.value.length;
|
||
|
|
const maxLength = parseInt(characterCountEl.getAttribute("data-maxlength"), 10);
|
||
|
|
const statusMessage = characterCountEl.querySelector(STATUS_MESSAGE);
|
||
|
|
const srStatusMessage = characterCountEl.querySelector(STATUS_MESSAGE_SR_ONLY);
|
||
|
|
const currentStatusMessage = getCountMessage(currentLength, maxLength);
|
||
|
|
if (!maxLength) return;
|
||
|
|
const isOverLimit = currentLength && currentLength > maxLength;
|
||
|
|
statusMessage.textContent = currentStatusMessage;
|
||
|
|
srUpdateStatus(srStatusMessage, currentStatusMessage);
|
||
|
|
if (isOverLimit && !inputEl.validationMessage) {
|
||
|
|
inputEl.setCustomValidity(VALIDATION_MESSAGE);
|
||
|
|
}
|
||
|
|
if (!isOverLimit && inputEl.validationMessage === VALIDATION_MESSAGE) {
|
||
|
|
inputEl.setCustomValidity("");
|
||
|
|
}
|
||
|
|
statusMessage.classList.toggle(MESSAGE_INVALID_CLASS, isOverLimit);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Initialize component
|
||
|
|
*
|
||
|
|
* @description On init this function will create elements and update any
|
||
|
|
* attributes so it can tell the user how many characters are left.
|
||
|
|
* @param {HTMLInputElement|HTMLTextAreaElement} inputEl the components input
|
||
|
|
*/
|
||
|
|
const enhanceCharacterCount = inputEl => {
|
||
|
|
const {
|
||
|
|
characterCountEl,
|
||
|
|
messageEl
|
||
|
|
} = getCharacterCountElements(inputEl);
|
||
|
|
|
||
|
|
// Hide hint and remove aria-live for backwards compatibility
|
||
|
|
messageEl.classList.add("usa-sr-only");
|
||
|
|
messageEl.removeAttribute("aria-live");
|
||
|
|
setDataLength(inputEl);
|
||
|
|
createStatusMessages(characterCountEl);
|
||
|
|
};
|
||
|
|
const characterCount = behavior({
|
||
|
|
input: {
|
||
|
|
[INPUT]() {
|
||
|
|
updateCountMessage(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
select(INPUT, root).forEach(input => enhanceCharacterCount(input));
|
||
|
|
},
|
||
|
|
MESSAGE_INVALID_CLASS,
|
||
|
|
VALIDATION_MESSAGE,
|
||
|
|
STATUS_MESSAGE_CLASS,
|
||
|
|
STATUS_MESSAGE_SR_ONLY_CLASS,
|
||
|
|
DEFAULT_STATUS_LABEL,
|
||
|
|
createStatusMessages,
|
||
|
|
getCountMessage,
|
||
|
|
updateCountMessage
|
||
|
|
});
|
||
|
|
module.exports = characterCount;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/debounce":46,"../../uswds-core/src/js/utils/select":53}],18:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const keymap = require("receptor/keymap");
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const Sanitizer = require("../../uswds-core/src/js/utils/sanitizer");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const COMBO_BOX_CLASS = `${PREFIX}-combo-box`;
|
||
|
|
const COMBO_BOX_PRISTINE_CLASS = `${COMBO_BOX_CLASS}--pristine`;
|
||
|
|
const SELECT_CLASS = `${COMBO_BOX_CLASS}__select`;
|
||
|
|
const INPUT_CLASS = `${COMBO_BOX_CLASS}__input`;
|
||
|
|
const CLEAR_INPUT_BUTTON_CLASS = `${COMBO_BOX_CLASS}__clear-input`;
|
||
|
|
const CLEAR_INPUT_BUTTON_WRAPPER_CLASS = `${CLEAR_INPUT_BUTTON_CLASS}__wrapper`;
|
||
|
|
const INPUT_BUTTON_SEPARATOR_CLASS = `${COMBO_BOX_CLASS}__input-button-separator`;
|
||
|
|
const TOGGLE_LIST_BUTTON_CLASS = `${COMBO_BOX_CLASS}__toggle-list`;
|
||
|
|
const TOGGLE_LIST_BUTTON_WRAPPER_CLASS = `${TOGGLE_LIST_BUTTON_CLASS}__wrapper`;
|
||
|
|
const LIST_CLASS = `${COMBO_BOX_CLASS}__list`;
|
||
|
|
const LIST_OPTION_CLASS = `${COMBO_BOX_CLASS}__list-option`;
|
||
|
|
const LIST_OPTION_FOCUSED_CLASS = `${LIST_OPTION_CLASS}--focused`;
|
||
|
|
const LIST_OPTION_SELECTED_CLASS = `${LIST_OPTION_CLASS}--selected`;
|
||
|
|
const STATUS_CLASS = `${COMBO_BOX_CLASS}__status`;
|
||
|
|
const COMBO_BOX = `.${COMBO_BOX_CLASS}`;
|
||
|
|
const SELECT = `.${SELECT_CLASS}`;
|
||
|
|
const INPUT = `.${INPUT_CLASS}`;
|
||
|
|
const CLEAR_INPUT_BUTTON = `.${CLEAR_INPUT_BUTTON_CLASS}`;
|
||
|
|
const TOGGLE_LIST_BUTTON = `.${TOGGLE_LIST_BUTTON_CLASS}`;
|
||
|
|
const LIST = `.${LIST_CLASS}`;
|
||
|
|
const LIST_OPTION = `.${LIST_OPTION_CLASS}`;
|
||
|
|
const LIST_OPTION_FOCUSED = `.${LIST_OPTION_FOCUSED_CLASS}`;
|
||
|
|
const LIST_OPTION_SELECTED = `.${LIST_OPTION_SELECTED_CLASS}`;
|
||
|
|
const STATUS = `.${STATUS_CLASS}`;
|
||
|
|
const DEFAULT_FILTER = ".*{{query}}.*";
|
||
|
|
const noop = () => {};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* set the value of the element and dispatch a change event
|
||
|
|
*
|
||
|
|
* @param {HTMLInputElement|HTMLSelectElement} el The element to update
|
||
|
|
* @param {string} value The new value of the element
|
||
|
|
*/
|
||
|
|
const changeElementValue = function (el) {
|
||
|
|
let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
||
|
|
const elementToChange = el;
|
||
|
|
elementToChange.value = value;
|
||
|
|
const event = new CustomEvent("change", {
|
||
|
|
bubbles: true,
|
||
|
|
cancelable: true,
|
||
|
|
detail: {
|
||
|
|
value
|
||
|
|
}
|
||
|
|
});
|
||
|
|
elementToChange.dispatchEvent(event);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The elements within the combo box.
|
||
|
|
* @typedef {Object} ComboBoxContext
|
||
|
|
* @property {HTMLElement} comboBoxEl
|
||
|
|
* @property {HTMLSelectElement} selectEl
|
||
|
|
* @property {HTMLInputElement} inputEl
|
||
|
|
* @property {HTMLUListElement} listEl
|
||
|
|
* @property {HTMLDivElement} statusEl
|
||
|
|
* @property {HTMLLIElement} focusedOptionEl
|
||
|
|
* @property {HTMLLIElement} selectedOptionEl
|
||
|
|
* @property {HTMLButtonElement} toggleListBtnEl
|
||
|
|
* @property {HTMLButtonElement} clearInputBtnEl
|
||
|
|
* @property {boolean} isPristine
|
||
|
|
* @property {boolean} disableFiltering
|
||
|
|
*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get an object of elements belonging directly to the given
|
||
|
|
* combo box component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el the element within the combo box
|
||
|
|
* @returns {ComboBoxContext} elements
|
||
|
|
*/
|
||
|
|
const getComboBoxContext = el => {
|
||
|
|
const comboBoxEl = el.closest(COMBO_BOX);
|
||
|
|
if (!comboBoxEl) {
|
||
|
|
throw new Error(`Element is missing outer ${COMBO_BOX}`);
|
||
|
|
}
|
||
|
|
const selectEl = comboBoxEl.querySelector(SELECT);
|
||
|
|
const inputEl = comboBoxEl.querySelector(INPUT);
|
||
|
|
const listEl = comboBoxEl.querySelector(LIST);
|
||
|
|
const statusEl = comboBoxEl.querySelector(STATUS);
|
||
|
|
const focusedOptionEl = comboBoxEl.querySelector(LIST_OPTION_FOCUSED);
|
||
|
|
const selectedOptionEl = comboBoxEl.querySelector(LIST_OPTION_SELECTED);
|
||
|
|
const toggleListBtnEl = comboBoxEl.querySelector(TOGGLE_LIST_BUTTON);
|
||
|
|
const clearInputBtnEl = comboBoxEl.querySelector(CLEAR_INPUT_BUTTON);
|
||
|
|
const isPristine = comboBoxEl.classList.contains(COMBO_BOX_PRISTINE_CLASS);
|
||
|
|
const disableFiltering = comboBoxEl.dataset.disableFiltering === "true";
|
||
|
|
return {
|
||
|
|
comboBoxEl,
|
||
|
|
selectEl,
|
||
|
|
inputEl,
|
||
|
|
listEl,
|
||
|
|
statusEl,
|
||
|
|
focusedOptionEl,
|
||
|
|
selectedOptionEl,
|
||
|
|
toggleListBtnEl,
|
||
|
|
clearInputBtnEl,
|
||
|
|
isPristine,
|
||
|
|
disableFiltering
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Disable the combo-box component
|
||
|
|
*
|
||
|
|
* @param {HTMLInputElement} el An element within the combo box component
|
||
|
|
*/
|
||
|
|
const disable = el => {
|
||
|
|
const {
|
||
|
|
inputEl,
|
||
|
|
toggleListBtnEl,
|
||
|
|
clearInputBtnEl
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
clearInputBtnEl.hidden = true;
|
||
|
|
clearInputBtnEl.disabled = true;
|
||
|
|
toggleListBtnEl.disabled = true;
|
||
|
|
inputEl.disabled = true;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enable the combo-box component
|
||
|
|
*
|
||
|
|
* @param {HTMLInputElement} el An element within the combo box component
|
||
|
|
*/
|
||
|
|
const enable = el => {
|
||
|
|
const {
|
||
|
|
inputEl,
|
||
|
|
toggleListBtnEl,
|
||
|
|
clearInputBtnEl
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
clearInputBtnEl.hidden = false;
|
||
|
|
clearInputBtnEl.disabled = false;
|
||
|
|
toggleListBtnEl.disabled = false;
|
||
|
|
inputEl.disabled = false;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enhance a select element into a combo box component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} _comboBoxEl The initial element of the combo box component
|
||
|
|
*/
|
||
|
|
const enhanceComboBox = _comboBoxEl => {
|
||
|
|
const comboBoxEl = _comboBoxEl.closest(COMBO_BOX);
|
||
|
|
if (comboBoxEl.dataset.enhanced) return;
|
||
|
|
const selectEl = comboBoxEl.querySelector("select");
|
||
|
|
if (!selectEl) {
|
||
|
|
throw new Error(`${COMBO_BOX} is missing inner select`);
|
||
|
|
}
|
||
|
|
const selectId = selectEl.id;
|
||
|
|
const selectLabel = document.querySelector(`label[for="${selectId}"]`);
|
||
|
|
const listId = `${selectId}--list`;
|
||
|
|
const listIdLabel = `${selectId}-label`;
|
||
|
|
const assistiveHintID = `${selectId}--assistiveHint`;
|
||
|
|
const additionalAttributes = [];
|
||
|
|
const {
|
||
|
|
defaultValue
|
||
|
|
} = comboBoxEl.dataset;
|
||
|
|
const {
|
||
|
|
placeholder
|
||
|
|
} = comboBoxEl.dataset;
|
||
|
|
let selectedOption;
|
||
|
|
if (placeholder) {
|
||
|
|
additionalAttributes.push({
|
||
|
|
placeholder
|
||
|
|
});
|
||
|
|
}
|
||
|
|
if (defaultValue) {
|
||
|
|
for (let i = 0, len = selectEl.options.length; i < len; i += 1) {
|
||
|
|
const optionEl = selectEl.options[i];
|
||
|
|
if (optionEl.value === defaultValue) {
|
||
|
|
selectedOption = optionEl;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Throw error if combobox is missing a label or label is missing
|
||
|
|
* `for` attribute. Otherwise, set the ID to match the <ul> aria-labelledby
|
||
|
|
*/
|
||
|
|
if (!selectLabel || !selectLabel.matches(`label[for="${selectId}"]`)) {
|
||
|
|
throw new Error(`${COMBO_BOX} for ${selectId} is either missing a label or a "for" attribute`);
|
||
|
|
} else {
|
||
|
|
selectLabel.setAttribute("id", listIdLabel);
|
||
|
|
}
|
||
|
|
selectLabel.setAttribute("id", listIdLabel);
|
||
|
|
selectEl.setAttribute("aria-hidden", "true");
|
||
|
|
selectEl.setAttribute("tabindex", "-1");
|
||
|
|
selectEl.classList.add("usa-sr-only", SELECT_CLASS);
|
||
|
|
selectEl.id = "";
|
||
|
|
selectEl.value = "";
|
||
|
|
["required", "aria-label", "aria-labelledby"].forEach(name => {
|
||
|
|
if (selectEl.hasAttribute(name)) {
|
||
|
|
const value = selectEl.getAttribute(name);
|
||
|
|
additionalAttributes.push({
|
||
|
|
[name]: value
|
||
|
|
});
|
||
|
|
selectEl.removeAttribute(name);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// sanitize doesn't like functions in template literals
|
||
|
|
const input = document.createElement("input");
|
||
|
|
input.setAttribute("id", selectId);
|
||
|
|
input.setAttribute("aria-owns", listId);
|
||
|
|
input.setAttribute("aria-controls", listId);
|
||
|
|
input.setAttribute("aria-autocomplete", "list");
|
||
|
|
input.setAttribute("aria-describedby", assistiveHintID);
|
||
|
|
input.setAttribute("aria-expanded", "false");
|
||
|
|
input.setAttribute("autocapitalize", "off");
|
||
|
|
input.setAttribute("autocomplete", "off");
|
||
|
|
input.setAttribute("class", INPUT_CLASS);
|
||
|
|
input.setAttribute("type", "text");
|
||
|
|
input.setAttribute("role", "combobox");
|
||
|
|
additionalAttributes.forEach(attr => Object.keys(attr).forEach(key => {
|
||
|
|
const value = Sanitizer.escapeHTML`${attr[key]}`;
|
||
|
|
input.setAttribute(key, value);
|
||
|
|
}));
|
||
|
|
comboBoxEl.insertAdjacentElement("beforeend", input);
|
||
|
|
comboBoxEl.insertAdjacentHTML("beforeend", Sanitizer.escapeHTML`
|
||
|
|
<span class="${CLEAR_INPUT_BUTTON_WRAPPER_CLASS}" tabindex="-1">
|
||
|
|
<button type="button" class="${CLEAR_INPUT_BUTTON_CLASS}" aria-label="Clear the select contents"> </button>
|
||
|
|
</span>
|
||
|
|
<span class="${INPUT_BUTTON_SEPARATOR_CLASS}"> </span>
|
||
|
|
<span class="${TOGGLE_LIST_BUTTON_WRAPPER_CLASS}" tabindex="-1">
|
||
|
|
<button type="button" tabindex="-1" class="${TOGGLE_LIST_BUTTON_CLASS}" aria-label="Toggle the dropdown list"> </button>
|
||
|
|
</span>
|
||
|
|
<ul
|
||
|
|
tabindex="-1"
|
||
|
|
id="${listId}"
|
||
|
|
class="${LIST_CLASS}"
|
||
|
|
role="listbox"
|
||
|
|
aria-labelledby="${listIdLabel}"
|
||
|
|
hidden>
|
||
|
|
</ul>
|
||
|
|
<div class="${STATUS_CLASS} usa-sr-only" role="status"></div>
|
||
|
|
<span id="${assistiveHintID}" class="usa-sr-only">
|
||
|
|
When autocomplete results are available use up and down arrows to review and enter to select.
|
||
|
|
Touch device users, explore by touch or with swipe gestures.
|
||
|
|
</span>`);
|
||
|
|
if (selectedOption) {
|
||
|
|
const {
|
||
|
|
inputEl
|
||
|
|
} = getComboBoxContext(comboBoxEl);
|
||
|
|
changeElementValue(selectEl, selectedOption.value);
|
||
|
|
changeElementValue(inputEl, selectedOption.text);
|
||
|
|
comboBoxEl.classList.add(COMBO_BOX_PRISTINE_CLASS);
|
||
|
|
}
|
||
|
|
if (selectEl.disabled) {
|
||
|
|
disable(comboBoxEl);
|
||
|
|
selectEl.disabled = false;
|
||
|
|
}
|
||
|
|
comboBoxEl.dataset.enhanced = "true";
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Manage the focused element within the list options when
|
||
|
|
* navigating via keyboard.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An anchor element within the combo box component
|
||
|
|
* @param {HTMLElement} nextEl An element within the combo box component
|
||
|
|
* @param {Object} options options
|
||
|
|
* @param {boolean} options.skipFocus skip focus of highlighted item
|
||
|
|
* @param {boolean} options.preventScroll should skip procedure to scroll to element
|
||
|
|
*/
|
||
|
|
const highlightOption = function (el, nextEl) {
|
||
|
|
let {
|
||
|
|
skipFocus,
|
||
|
|
preventScroll
|
||
|
|
} = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
||
|
|
const {
|
||
|
|
inputEl,
|
||
|
|
listEl,
|
||
|
|
focusedOptionEl
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
if (focusedOptionEl) {
|
||
|
|
focusedOptionEl.classList.remove(LIST_OPTION_FOCUSED_CLASS);
|
||
|
|
focusedOptionEl.setAttribute("tabIndex", "-1");
|
||
|
|
}
|
||
|
|
if (nextEl) {
|
||
|
|
inputEl.setAttribute("aria-activedescendant", nextEl.id);
|
||
|
|
nextEl.setAttribute("tabIndex", "0");
|
||
|
|
nextEl.classList.add(LIST_OPTION_FOCUSED_CLASS);
|
||
|
|
if (!preventScroll) {
|
||
|
|
const optionBottom = nextEl.offsetTop + nextEl.offsetHeight;
|
||
|
|
const currentBottom = listEl.scrollTop + listEl.offsetHeight;
|
||
|
|
if (optionBottom > currentBottom) {
|
||
|
|
listEl.scrollTop = optionBottom - listEl.offsetHeight;
|
||
|
|
}
|
||
|
|
if (nextEl.offsetTop < listEl.scrollTop) {
|
||
|
|
listEl.scrollTop = nextEl.offsetTop;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (!skipFocus) {
|
||
|
|
nextEl.focus({
|
||
|
|
preventScroll
|
||
|
|
});
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
inputEl.setAttribute("aria-activedescendant", "");
|
||
|
|
inputEl.focus();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Generate a dynamic regular expression based off of a replaceable and possibly filtered value.
|
||
|
|
*
|
||
|
|
* @param {string} el An element within the combo box component
|
||
|
|
* @param {string} query The value to use in the regular expression
|
||
|
|
* @param {object} extras An object of regular expressions to replace and filter the query
|
||
|
|
*/
|
||
|
|
const generateDynamicRegExp = function (filter) {
|
||
|
|
let query = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
||
|
|
let extras = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
||
|
|
const escapeRegExp = text => text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
|
||
|
|
let find = filter.replace(/{{(.*?)}}/g, (m, $1) => {
|
||
|
|
const key = $1.trim();
|
||
|
|
const queryFilter = extras[key];
|
||
|
|
if (key !== "query" && queryFilter) {
|
||
|
|
const matcher = new RegExp(queryFilter, "i");
|
||
|
|
const matches = query.match(matcher);
|
||
|
|
if (matches) {
|
||
|
|
return escapeRegExp(matches[1]);
|
||
|
|
}
|
||
|
|
return "";
|
||
|
|
}
|
||
|
|
return escapeRegExp(query);
|
||
|
|
});
|
||
|
|
find = `^(?:${find})$`;
|
||
|
|
return new RegExp(find, "i");
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display the option list of a combo box component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the combo box component
|
||
|
|
*/
|
||
|
|
const displayList = el => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
selectEl,
|
||
|
|
inputEl,
|
||
|
|
listEl,
|
||
|
|
statusEl,
|
||
|
|
isPristine,
|
||
|
|
disableFiltering
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
let selectedItemId;
|
||
|
|
let firstFoundId;
|
||
|
|
const listOptionBaseId = `${listEl.id}--option-`;
|
||
|
|
const inputValue = (inputEl.value || "").toLowerCase();
|
||
|
|
const filter = comboBoxEl.dataset.filter || DEFAULT_FILTER;
|
||
|
|
const regex = generateDynamicRegExp(filter, inputValue, comboBoxEl.dataset);
|
||
|
|
const options = [];
|
||
|
|
for (let i = 0, len = selectEl.options.length; i < len; i += 1) {
|
||
|
|
const optionEl = selectEl.options[i];
|
||
|
|
const optionId = `${listOptionBaseId}${options.length}`;
|
||
|
|
if (optionEl.value && (disableFiltering || isPristine || !inputValue || regex.test(optionEl.text))) {
|
||
|
|
if (selectEl.value && optionEl.value === selectEl.value) {
|
||
|
|
selectedItemId = optionId;
|
||
|
|
}
|
||
|
|
if (disableFiltering && !firstFoundId && regex.test(optionEl.text)) {
|
||
|
|
firstFoundId = optionId;
|
||
|
|
}
|
||
|
|
options.push(optionEl);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
const numOptions = options.length;
|
||
|
|
const optionHtml = options.map((option, index) => {
|
||
|
|
const optionId = `${listOptionBaseId}${index}`;
|
||
|
|
const classes = [LIST_OPTION_CLASS];
|
||
|
|
let tabindex = "-1";
|
||
|
|
let ariaSelected = "false";
|
||
|
|
if (optionId === selectedItemId) {
|
||
|
|
classes.push(LIST_OPTION_SELECTED_CLASS, LIST_OPTION_FOCUSED_CLASS);
|
||
|
|
tabindex = "0";
|
||
|
|
ariaSelected = "true";
|
||
|
|
}
|
||
|
|
if (!selectedItemId && index === 0) {
|
||
|
|
classes.push(LIST_OPTION_FOCUSED_CLASS);
|
||
|
|
tabindex = "0";
|
||
|
|
}
|
||
|
|
const li = document.createElement("li");
|
||
|
|
li.setAttribute("aria-setsize", options.length);
|
||
|
|
li.setAttribute("aria-posinset", index + 1);
|
||
|
|
li.setAttribute("aria-selected", ariaSelected);
|
||
|
|
li.setAttribute("id", optionId);
|
||
|
|
li.setAttribute("class", classes.join(" "));
|
||
|
|
li.setAttribute("tabindex", tabindex);
|
||
|
|
li.setAttribute("role", "option");
|
||
|
|
li.setAttribute("data-value", option.value);
|
||
|
|
li.textContent = option.text;
|
||
|
|
return li;
|
||
|
|
});
|
||
|
|
const noResults = document.createElement("li");
|
||
|
|
noResults.setAttribute("class", `${LIST_OPTION_CLASS}--no-results`);
|
||
|
|
noResults.textContent = "No results found";
|
||
|
|
listEl.hidden = false;
|
||
|
|
if (numOptions) {
|
||
|
|
listEl.innerHTML = "";
|
||
|
|
optionHtml.forEach(item => listEl.insertAdjacentElement("beforeend", item));
|
||
|
|
} else {
|
||
|
|
listEl.innerHTML = "";
|
||
|
|
listEl.insertAdjacentElement("beforeend", noResults);
|
||
|
|
}
|
||
|
|
inputEl.setAttribute("aria-expanded", "true");
|
||
|
|
statusEl.textContent = numOptions ? `${numOptions} result${numOptions > 1 ? "s" : ""} available.` : "No results.";
|
||
|
|
let itemToFocus;
|
||
|
|
if (isPristine && selectedItemId) {
|
||
|
|
itemToFocus = listEl.querySelector(`#${selectedItemId}`);
|
||
|
|
} else if (disableFiltering && firstFoundId) {
|
||
|
|
itemToFocus = listEl.querySelector(`#${firstFoundId}`);
|
||
|
|
}
|
||
|
|
if (itemToFocus) {
|
||
|
|
highlightOption(listEl, itemToFocus, {
|
||
|
|
skipFocus: true
|
||
|
|
});
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Hide the option list of a combo box component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the combo box component
|
||
|
|
*/
|
||
|
|
const hideList = el => {
|
||
|
|
const {
|
||
|
|
inputEl,
|
||
|
|
listEl,
|
||
|
|
statusEl,
|
||
|
|
focusedOptionEl
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
statusEl.innerHTML = "";
|
||
|
|
inputEl.setAttribute("aria-expanded", "false");
|
||
|
|
inputEl.setAttribute("aria-activedescendant", "");
|
||
|
|
if (focusedOptionEl) {
|
||
|
|
focusedOptionEl.classList.remove(LIST_OPTION_FOCUSED_CLASS);
|
||
|
|
}
|
||
|
|
listEl.scrollTop = 0;
|
||
|
|
listEl.hidden = true;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Select an option list of the combo box component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} listOptionEl The list option being selected
|
||
|
|
*/
|
||
|
|
const selectItem = listOptionEl => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
selectEl,
|
||
|
|
inputEl
|
||
|
|
} = getComboBoxContext(listOptionEl);
|
||
|
|
changeElementValue(selectEl, listOptionEl.dataset.value);
|
||
|
|
changeElementValue(inputEl, listOptionEl.textContent);
|
||
|
|
comboBoxEl.classList.add(COMBO_BOX_PRISTINE_CLASS);
|
||
|
|
hideList(comboBoxEl);
|
||
|
|
inputEl.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Clear the input of the combo box
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} clearButtonEl The clear input button
|
||
|
|
*/
|
||
|
|
const clearInput = clearButtonEl => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
listEl,
|
||
|
|
selectEl,
|
||
|
|
inputEl
|
||
|
|
} = getComboBoxContext(clearButtonEl);
|
||
|
|
const listShown = !listEl.hidden;
|
||
|
|
if (selectEl.value) changeElementValue(selectEl);
|
||
|
|
if (inputEl.value) changeElementValue(inputEl);
|
||
|
|
comboBoxEl.classList.remove(COMBO_BOX_PRISTINE_CLASS);
|
||
|
|
if (listShown) displayList(comboBoxEl);
|
||
|
|
inputEl.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Reset the select based off of currently set select value
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the combo box component
|
||
|
|
*/
|
||
|
|
const resetSelection = el => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
selectEl,
|
||
|
|
inputEl
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
const selectValue = selectEl.value;
|
||
|
|
const inputValue = (inputEl.value || "").toLowerCase();
|
||
|
|
if (selectValue) {
|
||
|
|
for (let i = 0, len = selectEl.options.length; i < len; i += 1) {
|
||
|
|
const optionEl = selectEl.options[i];
|
||
|
|
if (optionEl.value === selectValue) {
|
||
|
|
if (inputValue !== optionEl.text) {
|
||
|
|
changeElementValue(inputEl, optionEl.text);
|
||
|
|
}
|
||
|
|
comboBoxEl.classList.add(COMBO_BOX_PRISTINE_CLASS);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (inputValue) {
|
||
|
|
changeElementValue(inputEl);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Select an option list of the combo box component based off of
|
||
|
|
* having a current focused list option or
|
||
|
|
* having test that completely matches a list option.
|
||
|
|
* Otherwise it clears the input and select.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the combo box component
|
||
|
|
*/
|
||
|
|
const completeSelection = el => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
selectEl,
|
||
|
|
inputEl,
|
||
|
|
statusEl
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
statusEl.textContent = "";
|
||
|
|
const inputValue = (inputEl.value || "").toLowerCase();
|
||
|
|
if (inputValue) {
|
||
|
|
for (let i = 0, len = selectEl.options.length; i < len; i += 1) {
|
||
|
|
const optionEl = selectEl.options[i];
|
||
|
|
if (optionEl.text.toLowerCase() === inputValue) {
|
||
|
|
changeElementValue(selectEl, optionEl.value);
|
||
|
|
changeElementValue(inputEl, optionEl.text);
|
||
|
|
comboBoxEl.classList.add(COMBO_BOX_PRISTINE_CLASS);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
resetSelection(comboBoxEl);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the escape event within the combo box component.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event An event within the combo box component
|
||
|
|
*/
|
||
|
|
const handleEscape = event => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
inputEl
|
||
|
|
} = getComboBoxContext(event.target);
|
||
|
|
hideList(comboBoxEl);
|
||
|
|
resetSelection(comboBoxEl);
|
||
|
|
inputEl.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the down event within the combo box component.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event An event within the combo box component
|
||
|
|
*/
|
||
|
|
const handleDownFromInput = event => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
listEl
|
||
|
|
} = getComboBoxContext(event.target);
|
||
|
|
if (listEl.hidden) {
|
||
|
|
displayList(comboBoxEl);
|
||
|
|
}
|
||
|
|
const nextOptionEl = listEl.querySelector(LIST_OPTION_FOCUSED) || listEl.querySelector(LIST_OPTION);
|
||
|
|
if (nextOptionEl) {
|
||
|
|
highlightOption(comboBoxEl, nextOptionEl);
|
||
|
|
}
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the enter event from an input element within the combo box component.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event An event within the combo box component
|
||
|
|
*/
|
||
|
|
const handleEnterFromInput = event => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
listEl
|
||
|
|
} = getComboBoxContext(event.target);
|
||
|
|
const listShown = !listEl.hidden;
|
||
|
|
completeSelection(comboBoxEl);
|
||
|
|
if (listShown) {
|
||
|
|
hideList(comboBoxEl);
|
||
|
|
}
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the down event within the combo box component.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event An event within the combo box component
|
||
|
|
*/
|
||
|
|
const handleDownFromListOption = event => {
|
||
|
|
const focusedOptionEl = event.target;
|
||
|
|
const nextOptionEl = focusedOptionEl.nextSibling;
|
||
|
|
if (nextOptionEl) {
|
||
|
|
highlightOption(focusedOptionEl, nextOptionEl);
|
||
|
|
}
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the tab event from an list option element within the combo box component.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event An event within the combo box component
|
||
|
|
*/
|
||
|
|
const handleTabFromListOption = event => {
|
||
|
|
selectItem(event.target);
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the enter event from list option within the combo box component.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event An event within the combo box component
|
||
|
|
*/
|
||
|
|
const handleEnterFromListOption = event => {
|
||
|
|
selectItem(event.target);
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the up event from list option within the combo box component.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event An event within the combo box component
|
||
|
|
*/
|
||
|
|
const handleUpFromListOption = event => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
listEl,
|
||
|
|
focusedOptionEl
|
||
|
|
} = getComboBoxContext(event.target);
|
||
|
|
const nextOptionEl = focusedOptionEl && focusedOptionEl.previousSibling;
|
||
|
|
const listShown = !listEl.hidden;
|
||
|
|
highlightOption(comboBoxEl, nextOptionEl);
|
||
|
|
if (listShown) {
|
||
|
|
event.preventDefault();
|
||
|
|
}
|
||
|
|
if (!nextOptionEl) {
|
||
|
|
hideList(comboBoxEl);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Select list option on the mouseover event.
|
||
|
|
*
|
||
|
|
* @param {MouseEvent} event The mouseover event
|
||
|
|
* @param {HTMLLIElement} listOptionEl An element within the combo box component
|
||
|
|
*/
|
||
|
|
const handleMouseover = listOptionEl => {
|
||
|
|
const isCurrentlyFocused = listOptionEl.classList.contains(LIST_OPTION_FOCUSED_CLASS);
|
||
|
|
if (isCurrentlyFocused) return;
|
||
|
|
highlightOption(listOptionEl, listOptionEl, {
|
||
|
|
preventScroll: true
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Toggle the list when the button is clicked
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the combo box component
|
||
|
|
*/
|
||
|
|
const toggleList = el => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
listEl,
|
||
|
|
inputEl
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
if (listEl.hidden) {
|
||
|
|
displayList(comboBoxEl);
|
||
|
|
} else {
|
||
|
|
hideList(comboBoxEl);
|
||
|
|
}
|
||
|
|
inputEl.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle click from input
|
||
|
|
*
|
||
|
|
* @param {HTMLInputElement} el An element within the combo box component
|
||
|
|
*/
|
||
|
|
const handleClickFromInput = el => {
|
||
|
|
const {
|
||
|
|
comboBoxEl,
|
||
|
|
listEl
|
||
|
|
} = getComboBoxContext(el);
|
||
|
|
if (listEl.hidden) {
|
||
|
|
displayList(comboBoxEl);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
const comboBox = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[INPUT]() {
|
||
|
|
if (this.disabled) return;
|
||
|
|
handleClickFromInput(this);
|
||
|
|
},
|
||
|
|
[TOGGLE_LIST_BUTTON]() {
|
||
|
|
if (this.disabled) return;
|
||
|
|
toggleList(this);
|
||
|
|
},
|
||
|
|
[LIST_OPTION]() {
|
||
|
|
if (this.disabled) return;
|
||
|
|
selectItem(this);
|
||
|
|
},
|
||
|
|
[CLEAR_INPUT_BUTTON]() {
|
||
|
|
if (this.disabled) return;
|
||
|
|
clearInput(this);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
focusout: {
|
||
|
|
[COMBO_BOX](event) {
|
||
|
|
if (!this.contains(event.relatedTarget)) {
|
||
|
|
resetSelection(this);
|
||
|
|
hideList(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
keydown: {
|
||
|
|
[COMBO_BOX]: keymap({
|
||
|
|
Escape: handleEscape
|
||
|
|
}),
|
||
|
|
[INPUT]: keymap({
|
||
|
|
Enter: handleEnterFromInput,
|
||
|
|
ArrowDown: handleDownFromInput,
|
||
|
|
Down: handleDownFromInput
|
||
|
|
}),
|
||
|
|
[LIST_OPTION]: keymap({
|
||
|
|
ArrowUp: handleUpFromListOption,
|
||
|
|
Up: handleUpFromListOption,
|
||
|
|
ArrowDown: handleDownFromListOption,
|
||
|
|
Down: handleDownFromListOption,
|
||
|
|
Enter: handleEnterFromListOption,
|
||
|
|
Tab: handleTabFromListOption,
|
||
|
|
"Shift+Tab": noop
|
||
|
|
})
|
||
|
|
},
|
||
|
|
input: {
|
||
|
|
[INPUT]() {
|
||
|
|
const comboBoxEl = this.closest(COMBO_BOX);
|
||
|
|
comboBoxEl.classList.remove(COMBO_BOX_PRISTINE_CLASS);
|
||
|
|
displayList(this);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
mouseover: {
|
||
|
|
[LIST_OPTION]() {
|
||
|
|
handleMouseover(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(COMBO_BOX, root).forEach(comboBoxEl => {
|
||
|
|
enhanceComboBox(comboBoxEl);
|
||
|
|
});
|
||
|
|
},
|
||
|
|
getComboBoxContext,
|
||
|
|
enhanceComboBox,
|
||
|
|
generateDynamicRegExp,
|
||
|
|
disable,
|
||
|
|
enable,
|
||
|
|
displayList,
|
||
|
|
hideList,
|
||
|
|
COMBO_BOX_CLASS
|
||
|
|
});
|
||
|
|
module.exports = comboBox;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/sanitizer":50,"../../uswds-core/src/js/utils/select-or-matches":52,"receptor/keymap":11}],19:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const keymap = require("receptor/keymap");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const select = require("../../uswds-core/src/js/utils/select");
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const activeElement = require("../../uswds-core/src/js/utils/active-element");
|
||
|
|
const isIosDevice = require("../../uswds-core/src/js/utils/is-ios-device");
|
||
|
|
const Sanitizer = require("../../uswds-core/src/js/utils/sanitizer");
|
||
|
|
const DATE_PICKER_CLASS = `${PREFIX}-date-picker`;
|
||
|
|
const DATE_PICKER_WRAPPER_CLASS = `${DATE_PICKER_CLASS}__wrapper`;
|
||
|
|
const DATE_PICKER_INITIALIZED_CLASS = `${DATE_PICKER_CLASS}--initialized`;
|
||
|
|
const DATE_PICKER_ACTIVE_CLASS = `${DATE_PICKER_CLASS}--active`;
|
||
|
|
const DATE_PICKER_INTERNAL_INPUT_CLASS = `${DATE_PICKER_CLASS}__internal-input`;
|
||
|
|
const DATE_PICKER_EXTERNAL_INPUT_CLASS = `${DATE_PICKER_CLASS}__external-input`;
|
||
|
|
const DATE_PICKER_BUTTON_CLASS = `${DATE_PICKER_CLASS}__button`;
|
||
|
|
const DATE_PICKER_CALENDAR_CLASS = `${DATE_PICKER_CLASS}__calendar`;
|
||
|
|
const DATE_PICKER_STATUS_CLASS = `${DATE_PICKER_CLASS}__status`;
|
||
|
|
const CALENDAR_DATE_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__date`;
|
||
|
|
const CALENDAR_DATE_FOCUSED_CLASS = `${CALENDAR_DATE_CLASS}--focused`;
|
||
|
|
const CALENDAR_DATE_SELECTED_CLASS = `${CALENDAR_DATE_CLASS}--selected`;
|
||
|
|
const CALENDAR_DATE_PREVIOUS_MONTH_CLASS = `${CALENDAR_DATE_CLASS}--previous-month`;
|
||
|
|
const CALENDAR_DATE_CURRENT_MONTH_CLASS = `${CALENDAR_DATE_CLASS}--current-month`;
|
||
|
|
const CALENDAR_DATE_NEXT_MONTH_CLASS = `${CALENDAR_DATE_CLASS}--next-month`;
|
||
|
|
const CALENDAR_DATE_RANGE_DATE_CLASS = `${CALENDAR_DATE_CLASS}--range-date`;
|
||
|
|
const CALENDAR_DATE_TODAY_CLASS = `${CALENDAR_DATE_CLASS}--today`;
|
||
|
|
const CALENDAR_DATE_RANGE_DATE_START_CLASS = `${CALENDAR_DATE_CLASS}--range-date-start`;
|
||
|
|
const CALENDAR_DATE_RANGE_DATE_END_CLASS = `${CALENDAR_DATE_CLASS}--range-date-end`;
|
||
|
|
const CALENDAR_DATE_WITHIN_RANGE_CLASS = `${CALENDAR_DATE_CLASS}--within-range`;
|
||
|
|
const CALENDAR_PREVIOUS_YEAR_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__previous-year`;
|
||
|
|
const CALENDAR_PREVIOUS_MONTH_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__previous-month`;
|
||
|
|
const CALENDAR_NEXT_YEAR_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__next-year`;
|
||
|
|
const CALENDAR_NEXT_MONTH_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__next-month`;
|
||
|
|
const CALENDAR_MONTH_SELECTION_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__month-selection`;
|
||
|
|
const CALENDAR_YEAR_SELECTION_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__year-selection`;
|
||
|
|
const CALENDAR_MONTH_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__month`;
|
||
|
|
const CALENDAR_MONTH_FOCUSED_CLASS = `${CALENDAR_MONTH_CLASS}--focused`;
|
||
|
|
const CALENDAR_MONTH_SELECTED_CLASS = `${CALENDAR_MONTH_CLASS}--selected`;
|
||
|
|
const CALENDAR_YEAR_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__year`;
|
||
|
|
const CALENDAR_YEAR_FOCUSED_CLASS = `${CALENDAR_YEAR_CLASS}--focused`;
|
||
|
|
const CALENDAR_YEAR_SELECTED_CLASS = `${CALENDAR_YEAR_CLASS}--selected`;
|
||
|
|
const CALENDAR_PREVIOUS_YEAR_CHUNK_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__previous-year-chunk`;
|
||
|
|
const CALENDAR_NEXT_YEAR_CHUNK_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__next-year-chunk`;
|
||
|
|
const CALENDAR_DATE_PICKER_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__date-picker`;
|
||
|
|
const CALENDAR_MONTH_PICKER_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__month-picker`;
|
||
|
|
const CALENDAR_YEAR_PICKER_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__year-picker`;
|
||
|
|
const CALENDAR_TABLE_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__table`;
|
||
|
|
const CALENDAR_ROW_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__row`;
|
||
|
|
const CALENDAR_CELL_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__cell`;
|
||
|
|
const CALENDAR_CELL_CENTER_ITEMS_CLASS = `${CALENDAR_CELL_CLASS}--center-items`;
|
||
|
|
const CALENDAR_MONTH_LABEL_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__month-label`;
|
||
|
|
const CALENDAR_DAY_OF_WEEK_CLASS = `${DATE_PICKER_CALENDAR_CLASS}__day-of-week`;
|
||
|
|
const DATE_PICKER = `.${DATE_PICKER_CLASS}`;
|
||
|
|
const DATE_PICKER_BUTTON = `.${DATE_PICKER_BUTTON_CLASS}`;
|
||
|
|
const DATE_PICKER_INTERNAL_INPUT = `.${DATE_PICKER_INTERNAL_INPUT_CLASS}`;
|
||
|
|
const DATE_PICKER_EXTERNAL_INPUT = `.${DATE_PICKER_EXTERNAL_INPUT_CLASS}`;
|
||
|
|
const DATE_PICKER_CALENDAR = `.${DATE_PICKER_CALENDAR_CLASS}`;
|
||
|
|
const DATE_PICKER_STATUS = `.${DATE_PICKER_STATUS_CLASS}`;
|
||
|
|
const CALENDAR_DATE = `.${CALENDAR_DATE_CLASS}`;
|
||
|
|
const CALENDAR_DATE_FOCUSED = `.${CALENDAR_DATE_FOCUSED_CLASS}`;
|
||
|
|
const CALENDAR_DATE_CURRENT_MONTH = `.${CALENDAR_DATE_CURRENT_MONTH_CLASS}`;
|
||
|
|
const CALENDAR_PREVIOUS_YEAR = `.${CALENDAR_PREVIOUS_YEAR_CLASS}`;
|
||
|
|
const CALENDAR_PREVIOUS_MONTH = `.${CALENDAR_PREVIOUS_MONTH_CLASS}`;
|
||
|
|
const CALENDAR_NEXT_YEAR = `.${CALENDAR_NEXT_YEAR_CLASS}`;
|
||
|
|
const CALENDAR_NEXT_MONTH = `.${CALENDAR_NEXT_MONTH_CLASS}`;
|
||
|
|
const CALENDAR_YEAR_SELECTION = `.${CALENDAR_YEAR_SELECTION_CLASS}`;
|
||
|
|
const CALENDAR_MONTH_SELECTION = `.${CALENDAR_MONTH_SELECTION_CLASS}`;
|
||
|
|
const CALENDAR_MONTH = `.${CALENDAR_MONTH_CLASS}`;
|
||
|
|
const CALENDAR_YEAR = `.${CALENDAR_YEAR_CLASS}`;
|
||
|
|
const CALENDAR_PREVIOUS_YEAR_CHUNK = `.${CALENDAR_PREVIOUS_YEAR_CHUNK_CLASS}`;
|
||
|
|
const CALENDAR_NEXT_YEAR_CHUNK = `.${CALENDAR_NEXT_YEAR_CHUNK_CLASS}`;
|
||
|
|
const CALENDAR_DATE_PICKER = `.${CALENDAR_DATE_PICKER_CLASS}`;
|
||
|
|
const CALENDAR_MONTH_PICKER = `.${CALENDAR_MONTH_PICKER_CLASS}`;
|
||
|
|
const CALENDAR_YEAR_PICKER = `.${CALENDAR_YEAR_PICKER_CLASS}`;
|
||
|
|
const CALENDAR_MONTH_FOCUSED = `.${CALENDAR_MONTH_FOCUSED_CLASS}`;
|
||
|
|
const CALENDAR_YEAR_FOCUSED = `.${CALENDAR_YEAR_FOCUSED_CLASS}`;
|
||
|
|
const VALIDATION_MESSAGE = "Please enter a valid date";
|
||
|
|
const MONTH_LABELS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
|
||
|
|
const DAY_OF_WEEK_LABELS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
|
||
|
|
const ENTER_KEYCODE = 13;
|
||
|
|
const YEAR_CHUNK = 12;
|
||
|
|
const DEFAULT_MIN_DATE = "0000-01-01";
|
||
|
|
const DEFAULT_EXTERNAL_DATE_FORMAT = "MM/DD/YYYY";
|
||
|
|
const INTERNAL_DATE_FORMAT = "YYYY-MM-DD";
|
||
|
|
const NOT_DISABLED_SELECTOR = ":not([disabled])";
|
||
|
|
const processFocusableSelectors = function () {
|
||
|
|
for (var _len = arguments.length, selectors = new Array(_len), _key = 0; _key < _len; _key++) {
|
||
|
|
selectors[_key] = arguments[_key];
|
||
|
|
}
|
||
|
|
return selectors.map(query => query + NOT_DISABLED_SELECTOR).join(", ");
|
||
|
|
};
|
||
|
|
const DATE_PICKER_FOCUSABLE = processFocusableSelectors(CALENDAR_PREVIOUS_YEAR, CALENDAR_PREVIOUS_MONTH, CALENDAR_YEAR_SELECTION, CALENDAR_MONTH_SELECTION, CALENDAR_NEXT_YEAR, CALENDAR_NEXT_MONTH, CALENDAR_DATE_FOCUSED);
|
||
|
|
const MONTH_PICKER_FOCUSABLE = processFocusableSelectors(CALENDAR_MONTH_FOCUSED);
|
||
|
|
const YEAR_PICKER_FOCUSABLE = processFocusableSelectors(CALENDAR_PREVIOUS_YEAR_CHUNK, CALENDAR_NEXT_YEAR_CHUNK, CALENDAR_YEAR_FOCUSED);
|
||
|
|
|
||
|
|
// #region Date Manipulation Functions
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Keep date within month. Month would only be over by 1 to 3 days
|
||
|
|
*
|
||
|
|
* @param {Date} dateToCheck the date object to check
|
||
|
|
* @param {number} month the correct month
|
||
|
|
* @returns {Date} the date, corrected if needed
|
||
|
|
*/
|
||
|
|
const keepDateWithinMonth = (dateToCheck, month) => {
|
||
|
|
if (month !== dateToCheck.getMonth()) {
|
||
|
|
dateToCheck.setDate(0);
|
||
|
|
}
|
||
|
|
return dateToCheck;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set date from month day year
|
||
|
|
*
|
||
|
|
* @param {number} year the year to set
|
||
|
|
* @param {number} month the month to set (zero-indexed)
|
||
|
|
* @param {number} date the date to set
|
||
|
|
* @returns {Date} the set date
|
||
|
|
*/
|
||
|
|
const setDate = (year, month, date) => {
|
||
|
|
const newDate = new Date(0);
|
||
|
|
newDate.setFullYear(year, month, date);
|
||
|
|
return newDate;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* todays date
|
||
|
|
*
|
||
|
|
* @returns {Date} todays date
|
||
|
|
*/
|
||
|
|
const today = () => {
|
||
|
|
const newDate = new Date();
|
||
|
|
const day = newDate.getDate();
|
||
|
|
const month = newDate.getMonth();
|
||
|
|
const year = newDate.getFullYear();
|
||
|
|
return setDate(year, month, day);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set date to first day of the month
|
||
|
|
*
|
||
|
|
* @param {number} date the date to adjust
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const startOfMonth = date => {
|
||
|
|
const newDate = new Date(0);
|
||
|
|
newDate.setFullYear(date.getFullYear(), date.getMonth(), 1);
|
||
|
|
return newDate;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set date to last day of the month
|
||
|
|
*
|
||
|
|
* @param {number} date the date to adjust
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const lastDayOfMonth = date => {
|
||
|
|
const newDate = new Date(0);
|
||
|
|
newDate.setFullYear(date.getFullYear(), date.getMonth() + 1, 0);
|
||
|
|
return newDate;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add days to date
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numDays the difference in days
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const addDays = (_date, numDays) => {
|
||
|
|
const newDate = new Date(_date.getTime());
|
||
|
|
newDate.setDate(newDate.getDate() + numDays);
|
||
|
|
return newDate;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Subtract days from date
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numDays the difference in days
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const subDays = (_date, numDays) => addDays(_date, -numDays);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add weeks to date
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numWeeks the difference in weeks
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const addWeeks = (_date, numWeeks) => addDays(_date, numWeeks * 7);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Subtract weeks from date
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numWeeks the difference in weeks
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const subWeeks = (_date, numWeeks) => addWeeks(_date, -numWeeks);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set date to the start of the week (Sunday)
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const startOfWeek = _date => {
|
||
|
|
const dayOfWeek = _date.getDay();
|
||
|
|
return subDays(_date, dayOfWeek);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set date to the end of the week (Saturday)
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numWeeks the difference in weeks
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const endOfWeek = _date => {
|
||
|
|
const dayOfWeek = _date.getDay();
|
||
|
|
return addDays(_date, 6 - dayOfWeek);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add months to date and keep date within month
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numMonths the difference in months
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const addMonths = (_date, numMonths) => {
|
||
|
|
const newDate = new Date(_date.getTime());
|
||
|
|
const dateMonth = (newDate.getMonth() + 12 + numMonths) % 12;
|
||
|
|
newDate.setMonth(newDate.getMonth() + numMonths);
|
||
|
|
keepDateWithinMonth(newDate, dateMonth);
|
||
|
|
return newDate;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Subtract months from date
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numMonths the difference in months
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const subMonths = (_date, numMonths) => addMonths(_date, -numMonths);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add years to date and keep date within month
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numYears the difference in years
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const addYears = (_date, numYears) => addMonths(_date, numYears * 12);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Subtract years from date
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} numYears the difference in years
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const subYears = (_date, numYears) => addYears(_date, -numYears);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set months of date
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} month zero-indexed month to set
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const setMonth = (_date, month) => {
|
||
|
|
const newDate = new Date(_date.getTime());
|
||
|
|
newDate.setMonth(month);
|
||
|
|
keepDateWithinMonth(newDate, month);
|
||
|
|
return newDate;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set year of date
|
||
|
|
*
|
||
|
|
* @param {Date} _date the date to adjust
|
||
|
|
* @param {number} year the year to set
|
||
|
|
* @returns {Date} the adjusted date
|
||
|
|
*/
|
||
|
|
const setYear = (_date, year) => {
|
||
|
|
const newDate = new Date(_date.getTime());
|
||
|
|
const month = newDate.getMonth();
|
||
|
|
newDate.setFullYear(year);
|
||
|
|
keepDateWithinMonth(newDate, month);
|
||
|
|
return newDate;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Return the earliest date
|
||
|
|
*
|
||
|
|
* @param {Date} dateA date to compare
|
||
|
|
* @param {Date} dateB date to compare
|
||
|
|
* @returns {Date} the earliest date
|
||
|
|
*/
|
||
|
|
const min = (dateA, dateB) => {
|
||
|
|
let newDate = dateA;
|
||
|
|
if (dateB < dateA) {
|
||
|
|
newDate = dateB;
|
||
|
|
}
|
||
|
|
return new Date(newDate.getTime());
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Return the latest date
|
||
|
|
*
|
||
|
|
* @param {Date} dateA date to compare
|
||
|
|
* @param {Date} dateB date to compare
|
||
|
|
* @returns {Date} the latest date
|
||
|
|
*/
|
||
|
|
const max = (dateA, dateB) => {
|
||
|
|
let newDate = dateA;
|
||
|
|
if (dateB > dateA) {
|
||
|
|
newDate = dateB;
|
||
|
|
}
|
||
|
|
return new Date(newDate.getTime());
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if dates are the in the same year
|
||
|
|
*
|
||
|
|
* @param {Date} dateA date to compare
|
||
|
|
* @param {Date} dateB date to compare
|
||
|
|
* @returns {boolean} are dates in the same year
|
||
|
|
*/
|
||
|
|
const isSameYear = (dateA, dateB) => dateA && dateB && dateA.getFullYear() === dateB.getFullYear();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if dates are the in the same month
|
||
|
|
*
|
||
|
|
* @param {Date} dateA date to compare
|
||
|
|
* @param {Date} dateB date to compare
|
||
|
|
* @returns {boolean} are dates in the same month
|
||
|
|
*/
|
||
|
|
const isSameMonth = (dateA, dateB) => isSameYear(dateA, dateB) && dateA.getMonth() === dateB.getMonth();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if dates are the same date
|
||
|
|
*
|
||
|
|
* @param {Date} dateA the date to compare
|
||
|
|
* @param {Date} dateA the date to compare
|
||
|
|
* @returns {boolean} are dates the same date
|
||
|
|
*/
|
||
|
|
const isSameDay = (dateA, dateB) => isSameMonth(dateA, dateB) && dateA.getDate() === dateB.getDate();
|
||
|
|
|
||
|
|
/**
|
||
|
|
* return a new date within minimum and maximum date
|
||
|
|
*
|
||
|
|
* @param {Date} date date to check
|
||
|
|
* @param {Date} minDate minimum date to allow
|
||
|
|
* @param {Date} maxDate maximum date to allow
|
||
|
|
* @returns {Date} the date between min and max
|
||
|
|
*/
|
||
|
|
const keepDateBetweenMinAndMax = (date, minDate, maxDate) => {
|
||
|
|
let newDate = date;
|
||
|
|
if (date < minDate) {
|
||
|
|
newDate = minDate;
|
||
|
|
} else if (maxDate && date > maxDate) {
|
||
|
|
newDate = maxDate;
|
||
|
|
}
|
||
|
|
return new Date(newDate.getTime());
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if dates is valid.
|
||
|
|
*
|
||
|
|
* @param {Date} date date to check
|
||
|
|
* @param {Date} minDate minimum date to allow
|
||
|
|
* @param {Date} maxDate maximum date to allow
|
||
|
|
* @return {boolean} is there a day within the month within min and max dates
|
||
|
|
*/
|
||
|
|
const isDateWithinMinAndMax = (date, minDate, maxDate) => date >= minDate && (!maxDate || date <= maxDate);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if dates month is invalid.
|
||
|
|
*
|
||
|
|
* @param {Date} date date to check
|
||
|
|
* @param {Date} minDate minimum date to allow
|
||
|
|
* @param {Date} maxDate maximum date to allow
|
||
|
|
* @return {boolean} is the month outside min or max dates
|
||
|
|
*/
|
||
|
|
const isDatesMonthOutsideMinOrMax = (date, minDate, maxDate) => lastDayOfMonth(date) < minDate || maxDate && startOfMonth(date) > maxDate;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Check if dates year is invalid.
|
||
|
|
*
|
||
|
|
* @param {Date} date date to check
|
||
|
|
* @param {Date} minDate minimum date to allow
|
||
|
|
* @param {Date} maxDate maximum date to allow
|
||
|
|
* @return {boolean} is the month outside min or max dates
|
||
|
|
*/
|
||
|
|
const isDatesYearOutsideMinOrMax = (date, minDate, maxDate) => lastDayOfMonth(setMonth(date, 11)) < minDate || maxDate && startOfMonth(setMonth(date, 0)) > maxDate;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Parse a date with format M-D-YY
|
||
|
|
*
|
||
|
|
* @param {string} dateString the date string to parse
|
||
|
|
* @param {string} dateFormat the format of the date string
|
||
|
|
* @param {boolean} adjustDate should the date be adjusted
|
||
|
|
* @returns {Date} the parsed date
|
||
|
|
*/
|
||
|
|
const parseDateString = function (dateString) {
|
||
|
|
let dateFormat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : INTERNAL_DATE_FORMAT;
|
||
|
|
let adjustDate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
||
|
|
let date;
|
||
|
|
let month;
|
||
|
|
let day;
|
||
|
|
let year;
|
||
|
|
let parsed;
|
||
|
|
if (dateString) {
|
||
|
|
let monthStr;
|
||
|
|
let dayStr;
|
||
|
|
let yearStr;
|
||
|
|
if (dateFormat === DEFAULT_EXTERNAL_DATE_FORMAT) {
|
||
|
|
[monthStr, dayStr, yearStr] = dateString.split("/");
|
||
|
|
} else {
|
||
|
|
[yearStr, monthStr, dayStr] = dateString.split("-");
|
||
|
|
}
|
||
|
|
if (yearStr) {
|
||
|
|
parsed = parseInt(yearStr, 10);
|
||
|
|
if (!Number.isNaN(parsed)) {
|
||
|
|
year = parsed;
|
||
|
|
if (adjustDate) {
|
||
|
|
year = Math.max(0, year);
|
||
|
|
if (yearStr.length < 3) {
|
||
|
|
const currentYear = today().getFullYear();
|
||
|
|
const currentYearStub = currentYear - currentYear % 10 ** yearStr.length;
|
||
|
|
year = currentYearStub + parsed;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (monthStr) {
|
||
|
|
parsed = parseInt(monthStr, 10);
|
||
|
|
if (!Number.isNaN(parsed)) {
|
||
|
|
month = parsed;
|
||
|
|
if (adjustDate) {
|
||
|
|
month = Math.max(1, month);
|
||
|
|
month = Math.min(12, month);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (month && dayStr && year != null) {
|
||
|
|
parsed = parseInt(dayStr, 10);
|
||
|
|
if (!Number.isNaN(parsed)) {
|
||
|
|
day = parsed;
|
||
|
|
if (adjustDate) {
|
||
|
|
const lastDayOfTheMonth = setDate(year, month, 0).getDate();
|
||
|
|
day = Math.max(1, day);
|
||
|
|
day = Math.min(lastDayOfTheMonth, day);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (month && day && year != null) {
|
||
|
|
date = setDate(year, month - 1, day);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return date;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Format a date to format MM-DD-YYYY
|
||
|
|
*
|
||
|
|
* @param {Date} date the date to format
|
||
|
|
* @param {string} dateFormat the format of the date string
|
||
|
|
* @returns {string} the formatted date string
|
||
|
|
*/
|
||
|
|
const formatDate = function (date) {
|
||
|
|
let dateFormat = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : INTERNAL_DATE_FORMAT;
|
||
|
|
const padZeros = (value, length) => `0000${value}`.slice(-length);
|
||
|
|
const month = date.getMonth() + 1;
|
||
|
|
const day = date.getDate();
|
||
|
|
const year = date.getFullYear();
|
||
|
|
if (dateFormat === DEFAULT_EXTERNAL_DATE_FORMAT) {
|
||
|
|
return [padZeros(month, 2), padZeros(day, 2), padZeros(year, 4)].join("/");
|
||
|
|
}
|
||
|
|
return [padZeros(year, 4), padZeros(month, 2), padZeros(day, 2)].join("-");
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Date Manipulation Functions
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create a grid string from an array of html strings
|
||
|
|
*
|
||
|
|
* @param {string[]} htmlArray the array of html items
|
||
|
|
* @param {number} rowSize the length of a row
|
||
|
|
* @returns {string} the grid string
|
||
|
|
*/
|
||
|
|
const listToGridHtml = (htmlArray, rowSize) => {
|
||
|
|
const grid = [];
|
||
|
|
let row = [];
|
||
|
|
let i = 0;
|
||
|
|
while (i < htmlArray.length) {
|
||
|
|
row = [];
|
||
|
|
const tr = document.createElement("tr");
|
||
|
|
while (i < htmlArray.length && row.length < rowSize) {
|
||
|
|
const td = document.createElement("td");
|
||
|
|
td.insertAdjacentElement("beforeend", htmlArray[i]);
|
||
|
|
row.push(td);
|
||
|
|
i += 1;
|
||
|
|
}
|
||
|
|
row.forEach(element => {
|
||
|
|
tr.insertAdjacentElement("beforeend", element);
|
||
|
|
});
|
||
|
|
grid.push(tr);
|
||
|
|
}
|
||
|
|
return grid;
|
||
|
|
};
|
||
|
|
const createTableBody = grid => {
|
||
|
|
const tableBody = document.createElement("tbody");
|
||
|
|
grid.forEach(element => {
|
||
|
|
tableBody.insertAdjacentElement("beforeend", element);
|
||
|
|
});
|
||
|
|
return tableBody;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* set the value of the element and dispatch a change event
|
||
|
|
*
|
||
|
|
* @param {HTMLInputElement} el The element to update
|
||
|
|
* @param {string} value The new value of the element
|
||
|
|
*/
|
||
|
|
const changeElementValue = function (el) {
|
||
|
|
let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
|
||
|
|
const elementToChange = el;
|
||
|
|
elementToChange.value = value;
|
||
|
|
const event = new CustomEvent("change", {
|
||
|
|
bubbles: true,
|
||
|
|
cancelable: true,
|
||
|
|
detail: {
|
||
|
|
value
|
||
|
|
}
|
||
|
|
});
|
||
|
|
elementToChange.dispatchEvent(event);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The properties and elements within the date picker.
|
||
|
|
* @typedef {Object} DatePickerContext
|
||
|
|
* @property {HTMLDivElement} calendarEl
|
||
|
|
* @property {HTMLElement} datePickerEl
|
||
|
|
* @property {HTMLInputElement} internalInputEl
|
||
|
|
* @property {HTMLInputElement} externalInputEl
|
||
|
|
* @property {HTMLDivElement} statusEl
|
||
|
|
* @property {HTMLDivElement} firstYearChunkEl
|
||
|
|
* @property {Date} calendarDate
|
||
|
|
* @property {Date} minDate
|
||
|
|
* @property {Date} maxDate
|
||
|
|
* @property {Date} selectedDate
|
||
|
|
* @property {Date} rangeDate
|
||
|
|
* @property {Date} defaultDate
|
||
|
|
*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get an object of the properties and elements belonging directly to the given
|
||
|
|
* date picker component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el the element within the date picker
|
||
|
|
* @returns {DatePickerContext} elements
|
||
|
|
*/
|
||
|
|
const getDatePickerContext = el => {
|
||
|
|
const datePickerEl = el.closest(DATE_PICKER);
|
||
|
|
if (!datePickerEl) {
|
||
|
|
throw new Error(`Element is missing outer ${DATE_PICKER}`);
|
||
|
|
}
|
||
|
|
const internalInputEl = datePickerEl.querySelector(DATE_PICKER_INTERNAL_INPUT);
|
||
|
|
const externalInputEl = datePickerEl.querySelector(DATE_PICKER_EXTERNAL_INPUT);
|
||
|
|
const calendarEl = datePickerEl.querySelector(DATE_PICKER_CALENDAR);
|
||
|
|
const toggleBtnEl = datePickerEl.querySelector(DATE_PICKER_BUTTON);
|
||
|
|
const statusEl = datePickerEl.querySelector(DATE_PICKER_STATUS);
|
||
|
|
const firstYearChunkEl = datePickerEl.querySelector(CALENDAR_YEAR);
|
||
|
|
const inputDate = parseDateString(externalInputEl.value, DEFAULT_EXTERNAL_DATE_FORMAT, true);
|
||
|
|
const selectedDate = parseDateString(internalInputEl.value);
|
||
|
|
const calendarDate = parseDateString(calendarEl.dataset.value);
|
||
|
|
const minDate = parseDateString(datePickerEl.dataset.minDate);
|
||
|
|
const maxDate = parseDateString(datePickerEl.dataset.maxDate);
|
||
|
|
const rangeDate = parseDateString(datePickerEl.dataset.rangeDate);
|
||
|
|
const defaultDate = parseDateString(datePickerEl.dataset.defaultDate);
|
||
|
|
if (minDate && maxDate && minDate > maxDate) {
|
||
|
|
throw new Error("Minimum date cannot be after maximum date");
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
toggleBtnEl,
|
||
|
|
selectedDate,
|
||
|
|
maxDate,
|
||
|
|
firstYearChunkEl,
|
||
|
|
datePickerEl,
|
||
|
|
inputDate,
|
||
|
|
internalInputEl,
|
||
|
|
externalInputEl,
|
||
|
|
calendarEl,
|
||
|
|
rangeDate,
|
||
|
|
defaultDate,
|
||
|
|
statusEl
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Disable the date picker component
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const disable = el => {
|
||
|
|
const {
|
||
|
|
externalInputEl,
|
||
|
|
toggleBtnEl
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
toggleBtnEl.disabled = true;
|
||
|
|
externalInputEl.disabled = true;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enable the date picker component
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const enable = el => {
|
||
|
|
const {
|
||
|
|
externalInputEl,
|
||
|
|
toggleBtnEl
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
toggleBtnEl.disabled = false;
|
||
|
|
externalInputEl.disabled = false;
|
||
|
|
};
|
||
|
|
|
||
|
|
// #region Validation
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate the value in the input as a valid date of format M/D/YYYY
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const isDateInputInvalid = el => {
|
||
|
|
const {
|
||
|
|
externalInputEl,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const dateString = externalInputEl.value;
|
||
|
|
let isInvalid = false;
|
||
|
|
if (dateString) {
|
||
|
|
isInvalid = true;
|
||
|
|
const dateStringParts = dateString.split("/");
|
||
|
|
const [month, day, year] = dateStringParts.map(str => {
|
||
|
|
let value;
|
||
|
|
const parsed = parseInt(str, 10);
|
||
|
|
if (!Number.isNaN(parsed)) value = parsed;
|
||
|
|
return value;
|
||
|
|
});
|
||
|
|
if (month && day && year != null) {
|
||
|
|
const checkDate = setDate(year, month - 1, day);
|
||
|
|
if (checkDate.getMonth() === month - 1 && checkDate.getDate() === day && checkDate.getFullYear() === year && dateStringParts[2].length === 4 && isDateWithinMinAndMax(checkDate, minDate, maxDate)) {
|
||
|
|
isInvalid = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return isInvalid;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Validate the value in the input as a valid date of format M/D/YYYY
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const validateDateInput = el => {
|
||
|
|
const {
|
||
|
|
externalInputEl
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const isInvalid = isDateInputInvalid(externalInputEl);
|
||
|
|
if (isInvalid && !externalInputEl.validationMessage) {
|
||
|
|
externalInputEl.setCustomValidity(VALIDATION_MESSAGE);
|
||
|
|
}
|
||
|
|
if (!isInvalid && externalInputEl.validationMessage === VALIDATION_MESSAGE) {
|
||
|
|
externalInputEl.setCustomValidity("");
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Validation
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enable the date picker component
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const reconcileInputValues = el => {
|
||
|
|
const {
|
||
|
|
internalInputEl,
|
||
|
|
inputDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
let newValue = "";
|
||
|
|
if (inputDate && !isDateInputInvalid(el)) {
|
||
|
|
newValue = formatDate(inputDate);
|
||
|
|
}
|
||
|
|
if (internalInputEl.value !== newValue) {
|
||
|
|
changeElementValue(internalInputEl, newValue);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Select the value of the date picker inputs.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} el An element within the date picker component
|
||
|
|
* @param {string} dateString The date string to update in YYYY-MM-DD format
|
||
|
|
*/
|
||
|
|
const setCalendarValue = (el, dateString) => {
|
||
|
|
const parsedDate = parseDateString(dateString);
|
||
|
|
if (parsedDate) {
|
||
|
|
const formattedDate = formatDate(parsedDate, DEFAULT_EXTERNAL_DATE_FORMAT);
|
||
|
|
const {
|
||
|
|
datePickerEl,
|
||
|
|
internalInputEl,
|
||
|
|
externalInputEl
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
changeElementValue(internalInputEl, dateString);
|
||
|
|
changeElementValue(externalInputEl, formattedDate);
|
||
|
|
validateDateInput(datePickerEl);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enhance an input with the date picker elements
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el The initial wrapping element of the date picker component
|
||
|
|
*/
|
||
|
|
const enhanceDatePicker = el => {
|
||
|
|
const datePickerEl = el.closest(DATE_PICKER);
|
||
|
|
const {
|
||
|
|
defaultValue
|
||
|
|
} = datePickerEl.dataset;
|
||
|
|
const internalInputEl = datePickerEl.querySelector(`input`);
|
||
|
|
if (!internalInputEl) {
|
||
|
|
throw new Error(`${DATE_PICKER} is missing inner input`);
|
||
|
|
}
|
||
|
|
if (internalInputEl.value) {
|
||
|
|
internalInputEl.value = "";
|
||
|
|
}
|
||
|
|
const minDate = parseDateString(datePickerEl.dataset.minDate || internalInputEl.getAttribute("min"));
|
||
|
|
datePickerEl.dataset.minDate = minDate ? formatDate(minDate) : DEFAULT_MIN_DATE;
|
||
|
|
const maxDate = parseDateString(datePickerEl.dataset.maxDate || internalInputEl.getAttribute("max"));
|
||
|
|
if (maxDate) {
|
||
|
|
datePickerEl.dataset.maxDate = formatDate(maxDate);
|
||
|
|
}
|
||
|
|
const calendarWrapper = document.createElement("div");
|
||
|
|
calendarWrapper.classList.add(DATE_PICKER_WRAPPER_CLASS);
|
||
|
|
const externalInputEl = internalInputEl.cloneNode();
|
||
|
|
externalInputEl.classList.add(DATE_PICKER_EXTERNAL_INPUT_CLASS);
|
||
|
|
externalInputEl.type = "text";
|
||
|
|
calendarWrapper.appendChild(externalInputEl);
|
||
|
|
calendarWrapper.insertAdjacentHTML("beforeend", Sanitizer.escapeHTML`
|
||
|
|
<button type="button" class="${DATE_PICKER_BUTTON_CLASS}" aria-haspopup="true" aria-label="Toggle calendar"></button>
|
||
|
|
<div class="${DATE_PICKER_CALENDAR_CLASS}" role="dialog" aria-modal="true" hidden></div>
|
||
|
|
<div class="usa-sr-only ${DATE_PICKER_STATUS_CLASS}" role="status" aria-live="polite"></div>`);
|
||
|
|
internalInputEl.setAttribute("aria-hidden", "true");
|
||
|
|
internalInputEl.setAttribute("tabindex", "-1");
|
||
|
|
internalInputEl.style.display = "none";
|
||
|
|
internalInputEl.classList.add(DATE_PICKER_INTERNAL_INPUT_CLASS);
|
||
|
|
internalInputEl.removeAttribute("id");
|
||
|
|
internalInputEl.removeAttribute("name");
|
||
|
|
internalInputEl.required = false;
|
||
|
|
datePickerEl.appendChild(calendarWrapper);
|
||
|
|
datePickerEl.classList.add(DATE_PICKER_INITIALIZED_CLASS);
|
||
|
|
if (defaultValue) {
|
||
|
|
setCalendarValue(datePickerEl, defaultValue);
|
||
|
|
}
|
||
|
|
if (internalInputEl.disabled) {
|
||
|
|
disable(datePickerEl);
|
||
|
|
internalInputEl.disabled = false;
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// #region Calendar - Date Selection View
|
||
|
|
|
||
|
|
/**
|
||
|
|
* render the calendar.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the date picker component
|
||
|
|
* @param {Date} _dateToDisplay a date to render on the calendar
|
||
|
|
* @returns {HTMLElement} a reference to the new calendar element
|
||
|
|
*/
|
||
|
|
const renderCalendar = (el, _dateToDisplay) => {
|
||
|
|
const {
|
||
|
|
datePickerEl,
|
||
|
|
calendarEl,
|
||
|
|
statusEl,
|
||
|
|
selectedDate,
|
||
|
|
maxDate,
|
||
|
|
minDate,
|
||
|
|
rangeDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const todaysDate = today();
|
||
|
|
let dateToDisplay = _dateToDisplay || todaysDate;
|
||
|
|
const calendarWasHidden = calendarEl.hidden;
|
||
|
|
const focusedDate = addDays(dateToDisplay, 0);
|
||
|
|
const focusedMonth = dateToDisplay.getMonth();
|
||
|
|
const focusedYear = dateToDisplay.getFullYear();
|
||
|
|
const prevMonth = subMonths(dateToDisplay, 1);
|
||
|
|
const nextMonth = addMonths(dateToDisplay, 1);
|
||
|
|
const currentFormattedDate = formatDate(dateToDisplay);
|
||
|
|
const firstOfMonth = startOfMonth(dateToDisplay);
|
||
|
|
const prevButtonsDisabled = isSameMonth(dateToDisplay, minDate);
|
||
|
|
const nextButtonsDisabled = isSameMonth(dateToDisplay, maxDate);
|
||
|
|
const rangeConclusionDate = selectedDate || dateToDisplay;
|
||
|
|
const rangeStartDate = rangeDate && min(rangeConclusionDate, rangeDate);
|
||
|
|
const rangeEndDate = rangeDate && max(rangeConclusionDate, rangeDate);
|
||
|
|
const withinRangeStartDate = rangeDate && addDays(rangeStartDate, 1);
|
||
|
|
const withinRangeEndDate = rangeDate && subDays(rangeEndDate, 1);
|
||
|
|
const monthLabel = MONTH_LABELS[focusedMonth];
|
||
|
|
const generateDateHtml = dateToRender => {
|
||
|
|
const classes = [CALENDAR_DATE_CLASS];
|
||
|
|
const day = dateToRender.getDate();
|
||
|
|
const month = dateToRender.getMonth();
|
||
|
|
const year = dateToRender.getFullYear();
|
||
|
|
const dayOfWeek = dateToRender.getDay();
|
||
|
|
const formattedDate = formatDate(dateToRender);
|
||
|
|
let tabindex = "-1";
|
||
|
|
const isDisabled = !isDateWithinMinAndMax(dateToRender, minDate, maxDate);
|
||
|
|
const isSelected = isSameDay(dateToRender, selectedDate);
|
||
|
|
if (isSameMonth(dateToRender, prevMonth)) {
|
||
|
|
classes.push(CALENDAR_DATE_PREVIOUS_MONTH_CLASS);
|
||
|
|
}
|
||
|
|
if (isSameMonth(dateToRender, focusedDate)) {
|
||
|
|
classes.push(CALENDAR_DATE_CURRENT_MONTH_CLASS);
|
||
|
|
}
|
||
|
|
if (isSameMonth(dateToRender, nextMonth)) {
|
||
|
|
classes.push(CALENDAR_DATE_NEXT_MONTH_CLASS);
|
||
|
|
}
|
||
|
|
if (isSelected) {
|
||
|
|
classes.push(CALENDAR_DATE_SELECTED_CLASS);
|
||
|
|
}
|
||
|
|
if (isSameDay(dateToRender, todaysDate)) {
|
||
|
|
classes.push(CALENDAR_DATE_TODAY_CLASS);
|
||
|
|
}
|
||
|
|
if (rangeDate) {
|
||
|
|
if (isSameDay(dateToRender, rangeDate)) {
|
||
|
|
classes.push(CALENDAR_DATE_RANGE_DATE_CLASS);
|
||
|
|
}
|
||
|
|
if (isSameDay(dateToRender, rangeStartDate)) {
|
||
|
|
classes.push(CALENDAR_DATE_RANGE_DATE_START_CLASS);
|
||
|
|
}
|
||
|
|
if (isSameDay(dateToRender, rangeEndDate)) {
|
||
|
|
classes.push(CALENDAR_DATE_RANGE_DATE_END_CLASS);
|
||
|
|
}
|
||
|
|
if (isDateWithinMinAndMax(dateToRender, withinRangeStartDate, withinRangeEndDate)) {
|
||
|
|
classes.push(CALENDAR_DATE_WITHIN_RANGE_CLASS);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (isSameDay(dateToRender, focusedDate)) {
|
||
|
|
tabindex = "0";
|
||
|
|
classes.push(CALENDAR_DATE_FOCUSED_CLASS);
|
||
|
|
}
|
||
|
|
const monthStr = MONTH_LABELS[month];
|
||
|
|
const dayStr = DAY_OF_WEEK_LABELS[dayOfWeek];
|
||
|
|
const btn = document.createElement("button");
|
||
|
|
btn.setAttribute("type", "button");
|
||
|
|
btn.setAttribute("tabindex", tabindex);
|
||
|
|
btn.setAttribute("class", classes.join(" "));
|
||
|
|
btn.setAttribute("data-day", day);
|
||
|
|
btn.setAttribute("data-month", month + 1);
|
||
|
|
btn.setAttribute("data-year", year);
|
||
|
|
btn.setAttribute("data-value", formattedDate);
|
||
|
|
btn.setAttribute("aria-label", Sanitizer.escapeHTML`${day} ${monthStr} ${year} ${dayStr}`);
|
||
|
|
btn.setAttribute("aria-selected", isSelected ? "true" : "false");
|
||
|
|
if (isDisabled === true) {
|
||
|
|
btn.disabled = true;
|
||
|
|
}
|
||
|
|
btn.textContent = day;
|
||
|
|
return btn;
|
||
|
|
};
|
||
|
|
|
||
|
|
// set date to first rendered day
|
||
|
|
dateToDisplay = startOfWeek(firstOfMonth);
|
||
|
|
const days = [];
|
||
|
|
while (days.length < 28 || dateToDisplay.getMonth() === focusedMonth || days.length % 7 !== 0) {
|
||
|
|
days.push(generateDateHtml(dateToDisplay));
|
||
|
|
dateToDisplay = addDays(dateToDisplay, 1);
|
||
|
|
}
|
||
|
|
const datesGrid = listToGridHtml(days, 7);
|
||
|
|
const newCalendar = calendarEl.cloneNode();
|
||
|
|
newCalendar.dataset.value = currentFormattedDate;
|
||
|
|
newCalendar.style.top = `${datePickerEl.offsetHeight}px`;
|
||
|
|
newCalendar.hidden = false;
|
||
|
|
newCalendar.innerHTML = Sanitizer.escapeHTML`
|
||
|
|
<div tabindex="-1" class="${CALENDAR_DATE_PICKER_CLASS}">
|
||
|
|
<div class="${CALENDAR_ROW_CLASS}">
|
||
|
|
<div class="${CALENDAR_CELL_CLASS} ${CALENDAR_CELL_CENTER_ITEMS_CLASS}">
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
class="${CALENDAR_PREVIOUS_YEAR_CLASS}"
|
||
|
|
aria-label="Navigate back one year"
|
||
|
|
${prevButtonsDisabled ? `disabled="disabled"` : ""}
|
||
|
|
></button>
|
||
|
|
</div>
|
||
|
|
<div class="${CALENDAR_CELL_CLASS} ${CALENDAR_CELL_CENTER_ITEMS_CLASS}">
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
class="${CALENDAR_PREVIOUS_MONTH_CLASS}"
|
||
|
|
aria-label="Navigate back one month"
|
||
|
|
${prevButtonsDisabled ? `disabled="disabled"` : ""}
|
||
|
|
></button>
|
||
|
|
</div>
|
||
|
|
<div class="${CALENDAR_CELL_CLASS} ${CALENDAR_MONTH_LABEL_CLASS}">
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
class="${CALENDAR_MONTH_SELECTION_CLASS}" aria-label="${monthLabel}. Click to select month"
|
||
|
|
>${monthLabel}</button>
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
class="${CALENDAR_YEAR_SELECTION_CLASS}" aria-label="${focusedYear}. Click to select year"
|
||
|
|
>${focusedYear}</button>
|
||
|
|
</div>
|
||
|
|
<div class="${CALENDAR_CELL_CLASS} ${CALENDAR_CELL_CENTER_ITEMS_CLASS}">
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
class="${CALENDAR_NEXT_MONTH_CLASS}"
|
||
|
|
aria-label="Navigate forward one month"
|
||
|
|
${nextButtonsDisabled ? `disabled="disabled"` : ""}
|
||
|
|
></button>
|
||
|
|
</div>
|
||
|
|
<div class="${CALENDAR_CELL_CLASS} ${CALENDAR_CELL_CENTER_ITEMS_CLASS}">
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
class="${CALENDAR_NEXT_YEAR_CLASS}"
|
||
|
|
aria-label="Navigate forward one year"
|
||
|
|
${nextButtonsDisabled ? `disabled="disabled"` : ""}
|
||
|
|
></button>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
`;
|
||
|
|
const table = document.createElement("table");
|
||
|
|
table.setAttribute("class", CALENDAR_TABLE_CLASS);
|
||
|
|
table.setAttribute("role", "presentation");
|
||
|
|
const tableHead = document.createElement("thead");
|
||
|
|
table.insertAdjacentElement("beforeend", tableHead);
|
||
|
|
const tableHeadRow = document.createElement("tr");
|
||
|
|
tableHead.insertAdjacentElement("beforeend", tableHeadRow);
|
||
|
|
const daysOfWeek = {
|
||
|
|
Sunday: "S",
|
||
|
|
Monday: "M",
|
||
|
|
Tuesday: "T",
|
||
|
|
Wednesday: "W",
|
||
|
|
Thursday: "Th",
|
||
|
|
Friday: "Fr",
|
||
|
|
Saturday: "S"
|
||
|
|
};
|
||
|
|
Object.keys(daysOfWeek).forEach(key => {
|
||
|
|
const th = document.createElement("th");
|
||
|
|
th.setAttribute("class", CALENDAR_DAY_OF_WEEK_CLASS);
|
||
|
|
th.setAttribute("scope", "presentation");
|
||
|
|
th.setAttribute("aria-label", key);
|
||
|
|
th.textContent = daysOfWeek[key];
|
||
|
|
tableHeadRow.insertAdjacentElement("beforeend", th);
|
||
|
|
});
|
||
|
|
const tableBody = createTableBody(datesGrid);
|
||
|
|
table.insertAdjacentElement("beforeend", tableBody);
|
||
|
|
|
||
|
|
// Container for Years, Months, and Days
|
||
|
|
const datePickerCalendarContainer = newCalendar.querySelector(CALENDAR_DATE_PICKER);
|
||
|
|
datePickerCalendarContainer.insertAdjacentElement("beforeend", table);
|
||
|
|
calendarEl.parentNode.replaceChild(newCalendar, calendarEl);
|
||
|
|
datePickerEl.classList.add(DATE_PICKER_ACTIVE_CLASS);
|
||
|
|
const statuses = [];
|
||
|
|
if (isSameDay(selectedDate, focusedDate)) {
|
||
|
|
statuses.push("Selected date");
|
||
|
|
}
|
||
|
|
if (calendarWasHidden) {
|
||
|
|
statuses.push("You can navigate by day using left and right arrows", "Weeks by using up and down arrows", "Months by using page up and page down keys", "Years by using shift plus page up and shift plus page down", "Home and end keys navigate to the beginning and end of a week");
|
||
|
|
statusEl.textContent = "";
|
||
|
|
} else {
|
||
|
|
statuses.push(`${monthLabel} ${focusedYear}`);
|
||
|
|
}
|
||
|
|
statusEl.textContent = statuses.join(". ");
|
||
|
|
return newCalendar;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back one year and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} _buttonEl An element within the date picker component
|
||
|
|
*/
|
||
|
|
const displayPreviousYear = _buttonEl => {
|
||
|
|
if (_buttonEl.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(_buttonEl);
|
||
|
|
let date = subYears(calendarDate, 1);
|
||
|
|
date = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
const newCalendar = renderCalendar(calendarEl, date);
|
||
|
|
let nextToFocus = newCalendar.querySelector(CALENDAR_PREVIOUS_YEAR);
|
||
|
|
if (nextToFocus.disabled) {
|
||
|
|
nextToFocus = newCalendar.querySelector(CALENDAR_DATE_PICKER);
|
||
|
|
}
|
||
|
|
nextToFocus.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back one month and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} _buttonEl An element within the date picker component
|
||
|
|
*/
|
||
|
|
const displayPreviousMonth = _buttonEl => {
|
||
|
|
if (_buttonEl.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(_buttonEl);
|
||
|
|
let date = subMonths(calendarDate, 1);
|
||
|
|
date = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
const newCalendar = renderCalendar(calendarEl, date);
|
||
|
|
let nextToFocus = newCalendar.querySelector(CALENDAR_PREVIOUS_MONTH);
|
||
|
|
if (nextToFocus.disabled) {
|
||
|
|
nextToFocus = newCalendar.querySelector(CALENDAR_DATE_PICKER);
|
||
|
|
}
|
||
|
|
nextToFocus.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward one month and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} _buttonEl An element within the date picker component
|
||
|
|
*/
|
||
|
|
const displayNextMonth = _buttonEl => {
|
||
|
|
if (_buttonEl.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(_buttonEl);
|
||
|
|
let date = addMonths(calendarDate, 1);
|
||
|
|
date = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
const newCalendar = renderCalendar(calendarEl, date);
|
||
|
|
let nextToFocus = newCalendar.querySelector(CALENDAR_NEXT_MONTH);
|
||
|
|
if (nextToFocus.disabled) {
|
||
|
|
nextToFocus = newCalendar.querySelector(CALENDAR_DATE_PICKER);
|
||
|
|
}
|
||
|
|
nextToFocus.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward one year and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} _buttonEl An element within the date picker component
|
||
|
|
*/
|
||
|
|
const displayNextYear = _buttonEl => {
|
||
|
|
if (_buttonEl.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(_buttonEl);
|
||
|
|
let date = addYears(calendarDate, 1);
|
||
|
|
date = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
const newCalendar = renderCalendar(calendarEl, date);
|
||
|
|
let nextToFocus = newCalendar.querySelector(CALENDAR_NEXT_YEAR);
|
||
|
|
if (nextToFocus.disabled) {
|
||
|
|
nextToFocus = newCalendar.querySelector(CALENDAR_DATE_PICKER);
|
||
|
|
}
|
||
|
|
nextToFocus.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Hide the calendar of a date picker component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const hideCalendar = el => {
|
||
|
|
const {
|
||
|
|
datePickerEl,
|
||
|
|
calendarEl,
|
||
|
|
statusEl
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
datePickerEl.classList.remove(DATE_PICKER_ACTIVE_CLASS);
|
||
|
|
calendarEl.hidden = true;
|
||
|
|
statusEl.textContent = "";
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Select a date within the date picker component.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} calendarDateEl A date element within the date picker component
|
||
|
|
*/
|
||
|
|
const selectDate = calendarDateEl => {
|
||
|
|
if (calendarDateEl.disabled) return;
|
||
|
|
const {
|
||
|
|
datePickerEl,
|
||
|
|
externalInputEl
|
||
|
|
} = getDatePickerContext(calendarDateEl);
|
||
|
|
setCalendarValue(calendarDateEl, calendarDateEl.dataset.value);
|
||
|
|
hideCalendar(datePickerEl);
|
||
|
|
externalInputEl.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Toggle the calendar.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const toggleCalendar = el => {
|
||
|
|
if (el.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
inputDate,
|
||
|
|
minDate,
|
||
|
|
maxDate,
|
||
|
|
defaultDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
if (calendarEl.hidden) {
|
||
|
|
const dateToDisplay = keepDateBetweenMinAndMax(inputDate || defaultDate || today(), minDate, maxDate);
|
||
|
|
const newCalendar = renderCalendar(calendarEl, dateToDisplay);
|
||
|
|
newCalendar.querySelector(CALENDAR_DATE_FOCUSED).focus();
|
||
|
|
} else {
|
||
|
|
hideCalendar(el);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the calendar when visible.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el an element within the date picker
|
||
|
|
*/
|
||
|
|
const updateCalendarIfVisible = el => {
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
inputDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const calendarShown = !calendarEl.hidden;
|
||
|
|
if (calendarShown && inputDate) {
|
||
|
|
const dateToDisplay = keepDateBetweenMinAndMax(inputDate, minDate, maxDate);
|
||
|
|
renderCalendar(calendarEl, dateToDisplay);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Calendar - Date Selection View
|
||
|
|
|
||
|
|
// #region Calendar - Month Selection View
|
||
|
|
/**
|
||
|
|
* Display the month selection screen in the date picker.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} el An element within the date picker component
|
||
|
|
* @returns {HTMLElement} a reference to the new calendar element
|
||
|
|
*/
|
||
|
|
const displayMonthSelection = (el, monthToDisplay) => {
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
statusEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const selectedMonth = calendarDate.getMonth();
|
||
|
|
const focusedMonth = monthToDisplay == null ? selectedMonth : monthToDisplay;
|
||
|
|
const months = MONTH_LABELS.map((month, index) => {
|
||
|
|
const monthToCheck = setMonth(calendarDate, index);
|
||
|
|
const isDisabled = isDatesMonthOutsideMinOrMax(monthToCheck, minDate, maxDate);
|
||
|
|
let tabindex = "-1";
|
||
|
|
const classes = [CALENDAR_MONTH_CLASS];
|
||
|
|
const isSelected = index === selectedMonth;
|
||
|
|
if (index === focusedMonth) {
|
||
|
|
tabindex = "0";
|
||
|
|
classes.push(CALENDAR_MONTH_FOCUSED_CLASS);
|
||
|
|
}
|
||
|
|
if (isSelected) {
|
||
|
|
classes.push(CALENDAR_MONTH_SELECTED_CLASS);
|
||
|
|
}
|
||
|
|
const btn = document.createElement("button");
|
||
|
|
btn.setAttribute("type", "button");
|
||
|
|
btn.setAttribute("tabindex", tabindex);
|
||
|
|
btn.setAttribute("class", classes.join(" "));
|
||
|
|
btn.setAttribute("data-value", index);
|
||
|
|
btn.setAttribute("data-label", month);
|
||
|
|
btn.setAttribute("aria-selected", isSelected ? "true" : "false");
|
||
|
|
if (isDisabled === true) {
|
||
|
|
btn.disabled = true;
|
||
|
|
}
|
||
|
|
btn.textContent = month;
|
||
|
|
return btn;
|
||
|
|
});
|
||
|
|
const monthsHtml = document.createElement("div");
|
||
|
|
monthsHtml.setAttribute("tabindex", "-1");
|
||
|
|
monthsHtml.setAttribute("class", CALENDAR_MONTH_PICKER_CLASS);
|
||
|
|
const table = document.createElement("table");
|
||
|
|
table.setAttribute("class", CALENDAR_TABLE_CLASS);
|
||
|
|
table.setAttribute("role", "presentation");
|
||
|
|
const monthsGrid = listToGridHtml(months, 3);
|
||
|
|
const tableBody = createTableBody(monthsGrid);
|
||
|
|
table.insertAdjacentElement("beforeend", tableBody);
|
||
|
|
monthsHtml.insertAdjacentElement("beforeend", table);
|
||
|
|
const newCalendar = calendarEl.cloneNode();
|
||
|
|
newCalendar.insertAdjacentElement("beforeend", monthsHtml);
|
||
|
|
calendarEl.parentNode.replaceChild(newCalendar, calendarEl);
|
||
|
|
statusEl.textContent = "Select a month.";
|
||
|
|
return newCalendar;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Select a month in the date picker component.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} monthEl An month element within the date picker component
|
||
|
|
*/
|
||
|
|
const selectMonth = monthEl => {
|
||
|
|
if (monthEl.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(monthEl);
|
||
|
|
const selectedMonth = parseInt(monthEl.dataset.value, 10);
|
||
|
|
let date = setMonth(calendarDate, selectedMonth);
|
||
|
|
date = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
const newCalendar = renderCalendar(calendarEl, date);
|
||
|
|
newCalendar.querySelector(CALENDAR_DATE_FOCUSED).focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Calendar - Month Selection View
|
||
|
|
|
||
|
|
// #region Calendar - Year Selection View
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Display the year selection screen in the date picker.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} el An element within the date picker component
|
||
|
|
* @param {number} yearToDisplay year to display in year selection
|
||
|
|
* @returns {HTMLElement} a reference to the new calendar element
|
||
|
|
*/
|
||
|
|
const displayYearSelection = (el, yearToDisplay) => {
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
statusEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const selectedYear = calendarDate.getFullYear();
|
||
|
|
const focusedYear = yearToDisplay == null ? selectedYear : yearToDisplay;
|
||
|
|
let yearToChunk = focusedYear;
|
||
|
|
yearToChunk -= yearToChunk % YEAR_CHUNK;
|
||
|
|
yearToChunk = Math.max(0, yearToChunk);
|
||
|
|
const prevYearChunkDisabled = isDatesYearOutsideMinOrMax(setYear(calendarDate, yearToChunk - 1), minDate, maxDate);
|
||
|
|
const nextYearChunkDisabled = isDatesYearOutsideMinOrMax(setYear(calendarDate, yearToChunk + YEAR_CHUNK), minDate, maxDate);
|
||
|
|
const years = [];
|
||
|
|
let yearIndex = yearToChunk;
|
||
|
|
while (years.length < YEAR_CHUNK) {
|
||
|
|
const isDisabled = isDatesYearOutsideMinOrMax(setYear(calendarDate, yearIndex), minDate, maxDate);
|
||
|
|
let tabindex = "-1";
|
||
|
|
const classes = [CALENDAR_YEAR_CLASS];
|
||
|
|
const isSelected = yearIndex === selectedYear;
|
||
|
|
if (yearIndex === focusedYear) {
|
||
|
|
tabindex = "0";
|
||
|
|
classes.push(CALENDAR_YEAR_FOCUSED_CLASS);
|
||
|
|
}
|
||
|
|
if (isSelected) {
|
||
|
|
classes.push(CALENDAR_YEAR_SELECTED_CLASS);
|
||
|
|
}
|
||
|
|
const btn = document.createElement("button");
|
||
|
|
btn.setAttribute("type", "button");
|
||
|
|
btn.setAttribute("tabindex", tabindex);
|
||
|
|
btn.setAttribute("class", classes.join(" "));
|
||
|
|
btn.setAttribute("data-value", yearIndex);
|
||
|
|
btn.setAttribute("aria-selected", isSelected ? "true" : "false");
|
||
|
|
if (isDisabled === true) {
|
||
|
|
btn.disabled = true;
|
||
|
|
}
|
||
|
|
btn.textContent = yearIndex;
|
||
|
|
years.push(btn);
|
||
|
|
yearIndex += 1;
|
||
|
|
}
|
||
|
|
const newCalendar = calendarEl.cloneNode();
|
||
|
|
|
||
|
|
// create the years calendar wrapper
|
||
|
|
const yearsCalendarWrapper = document.createElement("div");
|
||
|
|
yearsCalendarWrapper.setAttribute("tabindex", "-1");
|
||
|
|
yearsCalendarWrapper.setAttribute("class", CALENDAR_YEAR_PICKER_CLASS);
|
||
|
|
|
||
|
|
// create table parent
|
||
|
|
const yearsTableParent = document.createElement("table");
|
||
|
|
yearsTableParent.setAttribute("role", "presentation");
|
||
|
|
yearsTableParent.setAttribute("class", CALENDAR_TABLE_CLASS);
|
||
|
|
|
||
|
|
// create table body and table row
|
||
|
|
const yearsHTMLTableBody = document.createElement("tbody");
|
||
|
|
const yearsHTMLTableBodyRow = document.createElement("tr");
|
||
|
|
|
||
|
|
// create previous button
|
||
|
|
const previousYearsBtn = document.createElement("button");
|
||
|
|
previousYearsBtn.setAttribute("type", "button");
|
||
|
|
previousYearsBtn.setAttribute("class", CALENDAR_PREVIOUS_YEAR_CHUNK_CLASS);
|
||
|
|
previousYearsBtn.setAttribute("aria-label", `Navigate back ${YEAR_CHUNK} years`);
|
||
|
|
if (prevYearChunkDisabled === true) {
|
||
|
|
previousYearsBtn.disabled = true;
|
||
|
|
}
|
||
|
|
previousYearsBtn.innerHTML = Sanitizer.escapeHTML` `;
|
||
|
|
|
||
|
|
// create next button
|
||
|
|
const nextYearsBtn = document.createElement("button");
|
||
|
|
nextYearsBtn.setAttribute("type", "button");
|
||
|
|
nextYearsBtn.setAttribute("class", CALENDAR_NEXT_YEAR_CHUNK_CLASS);
|
||
|
|
nextYearsBtn.setAttribute("aria-label", `Navigate forward ${YEAR_CHUNK} years`);
|
||
|
|
if (nextYearChunkDisabled === true) {
|
||
|
|
nextYearsBtn.disabled = true;
|
||
|
|
}
|
||
|
|
nextYearsBtn.innerHTML = Sanitizer.escapeHTML` `;
|
||
|
|
|
||
|
|
// create the actual years table
|
||
|
|
const yearsTable = document.createElement("table");
|
||
|
|
yearsTable.setAttribute("class", CALENDAR_TABLE_CLASS);
|
||
|
|
yearsTable.setAttribute("role", "presentation");
|
||
|
|
|
||
|
|
// create the years child table
|
||
|
|
const yearsGrid = listToGridHtml(years, 3);
|
||
|
|
const yearsTableBody = createTableBody(yearsGrid);
|
||
|
|
|
||
|
|
// append the grid to the years child table
|
||
|
|
yearsTable.insertAdjacentElement("beforeend", yearsTableBody);
|
||
|
|
|
||
|
|
// create the prev button td and append the prev button
|
||
|
|
const yearsHTMLTableBodyDetailPrev = document.createElement("td");
|
||
|
|
yearsHTMLTableBodyDetailPrev.insertAdjacentElement("beforeend", previousYearsBtn);
|
||
|
|
|
||
|
|
// create the years td and append the years child table
|
||
|
|
const yearsHTMLTableBodyYearsDetail = document.createElement("td");
|
||
|
|
yearsHTMLTableBodyYearsDetail.setAttribute("colspan", "3");
|
||
|
|
yearsHTMLTableBodyYearsDetail.insertAdjacentElement("beforeend", yearsTable);
|
||
|
|
|
||
|
|
// create the next button td and append the next button
|
||
|
|
const yearsHTMLTableBodyDetailNext = document.createElement("td");
|
||
|
|
yearsHTMLTableBodyDetailNext.insertAdjacentElement("beforeend", nextYearsBtn);
|
||
|
|
|
||
|
|
// append the three td to the years child table row
|
||
|
|
yearsHTMLTableBodyRow.insertAdjacentElement("beforeend", yearsHTMLTableBodyDetailPrev);
|
||
|
|
yearsHTMLTableBodyRow.insertAdjacentElement("beforeend", yearsHTMLTableBodyYearsDetail);
|
||
|
|
yearsHTMLTableBodyRow.insertAdjacentElement("beforeend", yearsHTMLTableBodyDetailNext);
|
||
|
|
|
||
|
|
// append the table row to the years child table body
|
||
|
|
yearsHTMLTableBody.insertAdjacentElement("beforeend", yearsHTMLTableBodyRow);
|
||
|
|
|
||
|
|
// append the years table body to the years parent table
|
||
|
|
yearsTableParent.insertAdjacentElement("beforeend", yearsHTMLTableBody);
|
||
|
|
|
||
|
|
// append the parent table to the calendar wrapper
|
||
|
|
yearsCalendarWrapper.insertAdjacentElement("beforeend", yearsTableParent);
|
||
|
|
|
||
|
|
// append the years calender to the new calendar
|
||
|
|
newCalendar.insertAdjacentElement("beforeend", yearsCalendarWrapper);
|
||
|
|
|
||
|
|
// replace calendar
|
||
|
|
calendarEl.parentNode.replaceChild(newCalendar, calendarEl);
|
||
|
|
statusEl.textContent = Sanitizer.escapeHTML`Showing years ${yearToChunk} to ${yearToChunk + YEAR_CHUNK - 1}. Select a year.`;
|
||
|
|
return newCalendar;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back by years and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const displayPreviousYearChunk = el => {
|
||
|
|
if (el.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const yearEl = calendarEl.querySelector(CALENDAR_YEAR_FOCUSED);
|
||
|
|
const selectedYear = parseInt(yearEl.textContent, 10);
|
||
|
|
let adjustedYear = selectedYear - YEAR_CHUNK;
|
||
|
|
adjustedYear = Math.max(0, adjustedYear);
|
||
|
|
const date = setYear(calendarDate, adjustedYear);
|
||
|
|
const cappedDate = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
const newCalendar = displayYearSelection(calendarEl, cappedDate.getFullYear());
|
||
|
|
let nextToFocus = newCalendar.querySelector(CALENDAR_PREVIOUS_YEAR_CHUNK);
|
||
|
|
if (nextToFocus.disabled) {
|
||
|
|
nextToFocus = newCalendar.querySelector(CALENDAR_YEAR_PICKER);
|
||
|
|
}
|
||
|
|
nextToFocus.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward by years and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} el An element within the date picker component
|
||
|
|
*/
|
||
|
|
const displayNextYearChunk = el => {
|
||
|
|
if (el.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const yearEl = calendarEl.querySelector(CALENDAR_YEAR_FOCUSED);
|
||
|
|
const selectedYear = parseInt(yearEl.textContent, 10);
|
||
|
|
let adjustedYear = selectedYear + YEAR_CHUNK;
|
||
|
|
adjustedYear = Math.max(0, adjustedYear);
|
||
|
|
const date = setYear(calendarDate, adjustedYear);
|
||
|
|
const cappedDate = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
const newCalendar = displayYearSelection(calendarEl, cappedDate.getFullYear());
|
||
|
|
let nextToFocus = newCalendar.querySelector(CALENDAR_NEXT_YEAR_CHUNK);
|
||
|
|
if (nextToFocus.disabled) {
|
||
|
|
nextToFocus = newCalendar.querySelector(CALENDAR_YEAR_PICKER);
|
||
|
|
}
|
||
|
|
nextToFocus.focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Select a year in the date picker component.
|
||
|
|
*
|
||
|
|
* @param {HTMLButtonElement} yearEl A year element within the date picker component
|
||
|
|
*/
|
||
|
|
const selectYear = yearEl => {
|
||
|
|
if (yearEl.disabled) return;
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(yearEl);
|
||
|
|
const selectedYear = parseInt(yearEl.innerHTML, 10);
|
||
|
|
let date = setYear(calendarDate, selectedYear);
|
||
|
|
date = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
const newCalendar = renderCalendar(calendarEl, date);
|
||
|
|
newCalendar.querySelector(CALENDAR_DATE_FOCUSED).focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Calendar - Year Selection View
|
||
|
|
|
||
|
|
// #region Calendar Event Handling
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Hide the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleEscapeFromCalendar = event => {
|
||
|
|
const {
|
||
|
|
datePickerEl,
|
||
|
|
externalInputEl
|
||
|
|
} = getDatePickerContext(event.target);
|
||
|
|
hideCalendar(datePickerEl);
|
||
|
|
externalInputEl.focus();
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Calendar Event Handling
|
||
|
|
|
||
|
|
// #region Calendar Date Event Handling
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Adjust the date and display the calendar if needed.
|
||
|
|
*
|
||
|
|
* @param {function} adjustDateFn function that returns the adjusted date
|
||
|
|
*/
|
||
|
|
const adjustCalendar = adjustDateFn => event => {
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(event.target);
|
||
|
|
const date = adjustDateFn(calendarDate);
|
||
|
|
const cappedDate = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
if (!isSameDay(calendarDate, cappedDate)) {
|
||
|
|
const newCalendar = renderCalendar(calendarEl, cappedDate);
|
||
|
|
newCalendar.querySelector(CALENDAR_DATE_FOCUSED).focus();
|
||
|
|
}
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back one week and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleUpFromDate = adjustCalendar(date => subWeeks(date, 1));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward one week and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleDownFromDate = adjustCalendar(date => addWeeks(date, 1));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back one day and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleLeftFromDate = adjustCalendar(date => subDays(date, 1));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward one day and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleRightFromDate = adjustCalendar(date => addDays(date, 1));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to the start of the week and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleHomeFromDate = adjustCalendar(date => startOfWeek(date));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to the end of the week and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleEndFromDate = adjustCalendar(date => endOfWeek(date));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward one month and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handlePageDownFromDate = adjustCalendar(date => addMonths(date, 1));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back one month and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handlePageUpFromDate = adjustCalendar(date => subMonths(date, 1));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward one year and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleShiftPageDownFromDate = adjustCalendar(date => addYears(date, 1));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back one year and display the calendar.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleShiftPageUpFromDate = adjustCalendar(date => subYears(date, 1));
|
||
|
|
|
||
|
|
/**
|
||
|
|
* display the calendar for the mouseover date.
|
||
|
|
*
|
||
|
|
* @param {MouseEvent} event The mouseover event
|
||
|
|
* @param {HTMLButtonElement} dateEl A date element within the date picker component
|
||
|
|
*/
|
||
|
|
const handleMouseoverFromDate = dateEl => {
|
||
|
|
if (dateEl.disabled) return;
|
||
|
|
const calendarEl = dateEl.closest(DATE_PICKER_CALENDAR);
|
||
|
|
const currentCalendarDate = calendarEl.dataset.value;
|
||
|
|
const hoverDate = dateEl.dataset.value;
|
||
|
|
if (hoverDate === currentCalendarDate) return;
|
||
|
|
const dateToDisplay = parseDateString(hoverDate);
|
||
|
|
const newCalendar = renderCalendar(calendarEl, dateToDisplay);
|
||
|
|
newCalendar.querySelector(CALENDAR_DATE_FOCUSED).focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Calendar Date Event Handling
|
||
|
|
|
||
|
|
// #region Calendar Month Event Handling
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Adjust the month and display the month selection screen if needed.
|
||
|
|
*
|
||
|
|
* @param {function} adjustMonthFn function that returns the adjusted month
|
||
|
|
*/
|
||
|
|
const adjustMonthSelectionScreen = adjustMonthFn => event => {
|
||
|
|
const monthEl = event.target;
|
||
|
|
const selectedMonth = parseInt(monthEl.dataset.value, 10);
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(monthEl);
|
||
|
|
const currentDate = setMonth(calendarDate, selectedMonth);
|
||
|
|
let adjustedMonth = adjustMonthFn(selectedMonth);
|
||
|
|
adjustedMonth = Math.max(0, Math.min(11, adjustedMonth));
|
||
|
|
const date = setMonth(calendarDate, adjustedMonth);
|
||
|
|
const cappedDate = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
if (!isSameMonth(currentDate, cappedDate)) {
|
||
|
|
const newCalendar = displayMonthSelection(calendarEl, cappedDate.getMonth());
|
||
|
|
newCalendar.querySelector(CALENDAR_MONTH_FOCUSED).focus();
|
||
|
|
}
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back three months and display the month selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleUpFromMonth = adjustMonthSelectionScreen(month => month - 3);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward three months and display the month selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleDownFromMonth = adjustMonthSelectionScreen(month => month + 3);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back one month and display the month selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleLeftFromMonth = adjustMonthSelectionScreen(month => month - 1);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward one month and display the month selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleRightFromMonth = adjustMonthSelectionScreen(month => month + 1);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to the start of the row of months and display the month selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleHomeFromMonth = adjustMonthSelectionScreen(month => month - month % 3);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to the end of the row of months and display the month selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleEndFromMonth = adjustMonthSelectionScreen(month => month + 2 - month % 3);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to the last month (December) and display the month selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handlePageDownFromMonth = adjustMonthSelectionScreen(() => 11);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to the first month (January) and display the month selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handlePageUpFromMonth = adjustMonthSelectionScreen(() => 0);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* update the focus on a month when the mouse moves.
|
||
|
|
*
|
||
|
|
* @param {MouseEvent} event The mouseover event
|
||
|
|
* @param {HTMLButtonElement} monthEl A month element within the date picker component
|
||
|
|
*/
|
||
|
|
const handleMouseoverFromMonth = monthEl => {
|
||
|
|
if (monthEl.disabled) return;
|
||
|
|
if (monthEl.classList.contains(CALENDAR_MONTH_FOCUSED_CLASS)) return;
|
||
|
|
const focusMonth = parseInt(monthEl.dataset.value, 10);
|
||
|
|
const newCalendar = displayMonthSelection(monthEl, focusMonth);
|
||
|
|
newCalendar.querySelector(CALENDAR_MONTH_FOCUSED).focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Calendar Month Event Handling
|
||
|
|
|
||
|
|
// #region Calendar Year Event Handling
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Adjust the year and display the year selection screen if needed.
|
||
|
|
*
|
||
|
|
* @param {function} adjustYearFn function that returns the adjusted year
|
||
|
|
*/
|
||
|
|
const adjustYearSelectionScreen = adjustYearFn => event => {
|
||
|
|
const yearEl = event.target;
|
||
|
|
const selectedYear = parseInt(yearEl.dataset.value, 10);
|
||
|
|
const {
|
||
|
|
calendarEl,
|
||
|
|
calendarDate,
|
||
|
|
minDate,
|
||
|
|
maxDate
|
||
|
|
} = getDatePickerContext(yearEl);
|
||
|
|
const currentDate = setYear(calendarDate, selectedYear);
|
||
|
|
let adjustedYear = adjustYearFn(selectedYear);
|
||
|
|
adjustedYear = Math.max(0, adjustedYear);
|
||
|
|
const date = setYear(calendarDate, adjustedYear);
|
||
|
|
const cappedDate = keepDateBetweenMinAndMax(date, minDate, maxDate);
|
||
|
|
if (!isSameYear(currentDate, cappedDate)) {
|
||
|
|
const newCalendar = displayYearSelection(calendarEl, cappedDate.getFullYear());
|
||
|
|
newCalendar.querySelector(CALENDAR_YEAR_FOCUSED).focus();
|
||
|
|
}
|
||
|
|
event.preventDefault();
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back three years and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleUpFromYear = adjustYearSelectionScreen(year => year - 3);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward three years and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleDownFromYear = adjustYearSelectionScreen(year => year + 3);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate back one year and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleLeftFromYear = adjustYearSelectionScreen(year => year - 1);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward one year and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleRightFromYear = adjustYearSelectionScreen(year => year + 1);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to the start of the row of years and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleHomeFromYear = adjustYearSelectionScreen(year => year - year % 3);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to the end of the row of years and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handleEndFromYear = adjustYearSelectionScreen(year => year + 2 - year % 3);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate to back 12 years and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handlePageUpFromYear = adjustYearSelectionScreen(year => year - YEAR_CHUNK);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Navigate forward 12 years and display the year selection screen.
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
*/
|
||
|
|
const handlePageDownFromYear = adjustYearSelectionScreen(year => year + YEAR_CHUNK);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* update the focus on a year when the mouse moves.
|
||
|
|
*
|
||
|
|
* @param {MouseEvent} event The mouseover event
|
||
|
|
* @param {HTMLButtonElement} dateEl A year element within the date picker component
|
||
|
|
*/
|
||
|
|
const handleMouseoverFromYear = yearEl => {
|
||
|
|
if (yearEl.disabled) return;
|
||
|
|
if (yearEl.classList.contains(CALENDAR_YEAR_FOCUSED_CLASS)) return;
|
||
|
|
const focusYear = parseInt(yearEl.dataset.value, 10);
|
||
|
|
const newCalendar = displayYearSelection(yearEl, focusYear);
|
||
|
|
newCalendar.querySelector(CALENDAR_YEAR_FOCUSED).focus();
|
||
|
|
};
|
||
|
|
|
||
|
|
// #endregion Calendar Year Event Handling
|
||
|
|
|
||
|
|
// #region Focus Handling Event Handling
|
||
|
|
|
||
|
|
const tabHandler = focusable => {
|
||
|
|
const getFocusableContext = el => {
|
||
|
|
const {
|
||
|
|
calendarEl
|
||
|
|
} = getDatePickerContext(el);
|
||
|
|
const focusableElements = select(focusable, calendarEl);
|
||
|
|
const firstTabIndex = 0;
|
||
|
|
const lastTabIndex = focusableElements.length - 1;
|
||
|
|
const firstTabStop = focusableElements[firstTabIndex];
|
||
|
|
const lastTabStop = focusableElements[lastTabIndex];
|
||
|
|
const focusIndex = focusableElements.indexOf(activeElement());
|
||
|
|
const isLastTab = focusIndex === lastTabIndex;
|
||
|
|
const isFirstTab = focusIndex === firstTabIndex;
|
||
|
|
const isNotFound = focusIndex === -1;
|
||
|
|
return {
|
||
|
|
focusableElements,
|
||
|
|
isNotFound,
|
||
|
|
firstTabStop,
|
||
|
|
isFirstTab,
|
||
|
|
lastTabStop,
|
||
|
|
isLastTab
|
||
|
|
};
|
||
|
|
};
|
||
|
|
return {
|
||
|
|
tabAhead(event) {
|
||
|
|
const {
|
||
|
|
firstTabStop,
|
||
|
|
isLastTab,
|
||
|
|
isNotFound
|
||
|
|
} = getFocusableContext(event.target);
|
||
|
|
if (isLastTab || isNotFound) {
|
||
|
|
event.preventDefault();
|
||
|
|
firstTabStop.focus();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
tabBack(event) {
|
||
|
|
const {
|
||
|
|
lastTabStop,
|
||
|
|
isFirstTab,
|
||
|
|
isNotFound
|
||
|
|
} = getFocusableContext(event.target);
|
||
|
|
if (isFirstTab || isNotFound) {
|
||
|
|
event.preventDefault();
|
||
|
|
lastTabStop.focus();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
};
|
||
|
|
const datePickerTabEventHandler = tabHandler(DATE_PICKER_FOCUSABLE);
|
||
|
|
const monthPickerTabEventHandler = tabHandler(MONTH_PICKER_FOCUSABLE);
|
||
|
|
const yearPickerTabEventHandler = tabHandler(YEAR_PICKER_FOCUSABLE);
|
||
|
|
|
||
|
|
// #endregion Focus Handling Event Handling
|
||
|
|
|
||
|
|
// #region Date Picker Event Delegation Registration / Component
|
||
|
|
|
||
|
|
const datePickerEvents = {
|
||
|
|
[CLICK]: {
|
||
|
|
[DATE_PICKER_BUTTON]() {
|
||
|
|
toggleCalendar(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_DATE]() {
|
||
|
|
selectDate(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_MONTH]() {
|
||
|
|
selectMonth(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_YEAR]() {
|
||
|
|
selectYear(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_PREVIOUS_MONTH]() {
|
||
|
|
displayPreviousMonth(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_NEXT_MONTH]() {
|
||
|
|
displayNextMonth(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_PREVIOUS_YEAR]() {
|
||
|
|
displayPreviousYear(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_NEXT_YEAR]() {
|
||
|
|
displayNextYear(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_PREVIOUS_YEAR_CHUNK]() {
|
||
|
|
displayPreviousYearChunk(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_NEXT_YEAR_CHUNK]() {
|
||
|
|
displayNextYearChunk(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_MONTH_SELECTION]() {
|
||
|
|
const newCalendar = displayMonthSelection(this);
|
||
|
|
newCalendar.querySelector(CALENDAR_MONTH_FOCUSED).focus();
|
||
|
|
},
|
||
|
|
[CALENDAR_YEAR_SELECTION]() {
|
||
|
|
const newCalendar = displayYearSelection(this);
|
||
|
|
newCalendar.querySelector(CALENDAR_YEAR_FOCUSED).focus();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
keyup: {
|
||
|
|
[DATE_PICKER_CALENDAR](event) {
|
||
|
|
const keydown = this.dataset.keydownKeyCode;
|
||
|
|
if (`${event.keyCode}` !== keydown) {
|
||
|
|
event.preventDefault();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
keydown: {
|
||
|
|
[DATE_PICKER_EXTERNAL_INPUT](event) {
|
||
|
|
if (event.keyCode === ENTER_KEYCODE) {
|
||
|
|
validateDateInput(this);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
[CALENDAR_DATE]: keymap({
|
||
|
|
Up: handleUpFromDate,
|
||
|
|
ArrowUp: handleUpFromDate,
|
||
|
|
Down: handleDownFromDate,
|
||
|
|
ArrowDown: handleDownFromDate,
|
||
|
|
Left: handleLeftFromDate,
|
||
|
|
ArrowLeft: handleLeftFromDate,
|
||
|
|
Right: handleRightFromDate,
|
||
|
|
ArrowRight: handleRightFromDate,
|
||
|
|
Home: handleHomeFromDate,
|
||
|
|
End: handleEndFromDate,
|
||
|
|
PageDown: handlePageDownFromDate,
|
||
|
|
PageUp: handlePageUpFromDate,
|
||
|
|
"Shift+PageDown": handleShiftPageDownFromDate,
|
||
|
|
"Shift+PageUp": handleShiftPageUpFromDate,
|
||
|
|
Tab: datePickerTabEventHandler.tabAhead
|
||
|
|
}),
|
||
|
|
[CALENDAR_DATE_PICKER]: keymap({
|
||
|
|
Tab: datePickerTabEventHandler.tabAhead,
|
||
|
|
"Shift+Tab": datePickerTabEventHandler.tabBack
|
||
|
|
}),
|
||
|
|
[CALENDAR_MONTH]: keymap({
|
||
|
|
Up: handleUpFromMonth,
|
||
|
|
ArrowUp: handleUpFromMonth,
|
||
|
|
Down: handleDownFromMonth,
|
||
|
|
ArrowDown: handleDownFromMonth,
|
||
|
|
Left: handleLeftFromMonth,
|
||
|
|
ArrowLeft: handleLeftFromMonth,
|
||
|
|
Right: handleRightFromMonth,
|
||
|
|
ArrowRight: handleRightFromMonth,
|
||
|
|
Home: handleHomeFromMonth,
|
||
|
|
End: handleEndFromMonth,
|
||
|
|
PageDown: handlePageDownFromMonth,
|
||
|
|
PageUp: handlePageUpFromMonth
|
||
|
|
}),
|
||
|
|
[CALENDAR_MONTH_PICKER]: keymap({
|
||
|
|
Tab: monthPickerTabEventHandler.tabAhead,
|
||
|
|
"Shift+Tab": monthPickerTabEventHandler.tabBack
|
||
|
|
}),
|
||
|
|
[CALENDAR_YEAR]: keymap({
|
||
|
|
Up: handleUpFromYear,
|
||
|
|
ArrowUp: handleUpFromYear,
|
||
|
|
Down: handleDownFromYear,
|
||
|
|
ArrowDown: handleDownFromYear,
|
||
|
|
Left: handleLeftFromYear,
|
||
|
|
ArrowLeft: handleLeftFromYear,
|
||
|
|
Right: handleRightFromYear,
|
||
|
|
ArrowRight: handleRightFromYear,
|
||
|
|
Home: handleHomeFromYear,
|
||
|
|
End: handleEndFromYear,
|
||
|
|
PageDown: handlePageDownFromYear,
|
||
|
|
PageUp: handlePageUpFromYear
|
||
|
|
}),
|
||
|
|
[CALENDAR_YEAR_PICKER]: keymap({
|
||
|
|
Tab: yearPickerTabEventHandler.tabAhead,
|
||
|
|
"Shift+Tab": yearPickerTabEventHandler.tabBack
|
||
|
|
}),
|
||
|
|
[DATE_PICKER_CALENDAR](event) {
|
||
|
|
this.dataset.keydownKeyCode = event.keyCode;
|
||
|
|
},
|
||
|
|
[DATE_PICKER](event) {
|
||
|
|
const keyMap = keymap({
|
||
|
|
Escape: handleEscapeFromCalendar
|
||
|
|
});
|
||
|
|
keyMap(event);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
focusout: {
|
||
|
|
[DATE_PICKER_EXTERNAL_INPUT]() {
|
||
|
|
validateDateInput(this);
|
||
|
|
},
|
||
|
|
[DATE_PICKER](event) {
|
||
|
|
if (!this.contains(event.relatedTarget)) {
|
||
|
|
hideCalendar(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
input: {
|
||
|
|
[DATE_PICKER_EXTERNAL_INPUT]() {
|
||
|
|
reconcileInputValues(this);
|
||
|
|
updateCalendarIfVisible(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
if (!isIosDevice()) {
|
||
|
|
datePickerEvents.mouseover = {
|
||
|
|
[CALENDAR_DATE_CURRENT_MONTH]() {
|
||
|
|
handleMouseoverFromDate(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_MONTH]() {
|
||
|
|
handleMouseoverFromMonth(this);
|
||
|
|
},
|
||
|
|
[CALENDAR_YEAR]() {
|
||
|
|
handleMouseoverFromYear(this);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
}
|
||
|
|
const datePicker = behavior(datePickerEvents, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(DATE_PICKER, root).forEach(datePickerEl => {
|
||
|
|
enhanceDatePicker(datePickerEl);
|
||
|
|
});
|
||
|
|
},
|
||
|
|
getDatePickerContext,
|
||
|
|
disable,
|
||
|
|
enable,
|
||
|
|
isDateInputInvalid,
|
||
|
|
setCalendarValue,
|
||
|
|
validateDateInput,
|
||
|
|
renderCalendar,
|
||
|
|
updateCalendarIfVisible
|
||
|
|
});
|
||
|
|
|
||
|
|
// #endregion Date Picker Event Delegation Registration / Component
|
||
|
|
|
||
|
|
module.exports = datePicker;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/active-element":44,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/is-ios-device":49,"../../uswds-core/src/js/utils/sanitizer":50,"../../uswds-core/src/js/utils/select":53,"../../uswds-core/src/js/utils/select-or-matches":52,"receptor/keymap":11}],20:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const select = require("../../uswds-core/src/js/utils/select");
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const {
|
||
|
|
getDatePickerContext,
|
||
|
|
isDateInputInvalid,
|
||
|
|
updateCalendarIfVisible
|
||
|
|
} = require("../../usa-date-picker/src/index");
|
||
|
|
const DATE_PICKER_CLASS = `${PREFIX}-date-picker`;
|
||
|
|
const DATE_RANGE_PICKER_CLASS = `${PREFIX}-date-range-picker`;
|
||
|
|
const DATE_RANGE_PICKER_RANGE_START_CLASS = `${DATE_RANGE_PICKER_CLASS}__range-start`;
|
||
|
|
const DATE_RANGE_PICKER_RANGE_END_CLASS = `${DATE_RANGE_PICKER_CLASS}__range-end`;
|
||
|
|
const DATE_PICKER = `.${DATE_PICKER_CLASS}`;
|
||
|
|
const DATE_RANGE_PICKER = `.${DATE_RANGE_PICKER_CLASS}`;
|
||
|
|
const DATE_RANGE_PICKER_RANGE_START = `.${DATE_RANGE_PICKER_RANGE_START_CLASS}`;
|
||
|
|
const DATE_RANGE_PICKER_RANGE_END = `.${DATE_RANGE_PICKER_RANGE_END_CLASS}`;
|
||
|
|
const DEFAULT_MIN_DATE = "0000-01-01";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The properties and elements within the date range picker.
|
||
|
|
* @typedef {Object} DateRangePickerContext
|
||
|
|
* @property {HTMLElement} dateRangePickerEl
|
||
|
|
* @property {HTMLElement} rangeStartEl
|
||
|
|
* @property {HTMLElement} rangeEndEl
|
||
|
|
*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get an object of the properties and elements belonging directly to the given
|
||
|
|
* date picker component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el the element within the date picker
|
||
|
|
* @returns {DateRangePickerContext} elements
|
||
|
|
*/
|
||
|
|
const getDateRangePickerContext = el => {
|
||
|
|
const dateRangePickerEl = el.closest(DATE_RANGE_PICKER);
|
||
|
|
if (!dateRangePickerEl) {
|
||
|
|
throw new Error(`Element is missing outer ${DATE_RANGE_PICKER}`);
|
||
|
|
}
|
||
|
|
const rangeStartEl = dateRangePickerEl.querySelector(DATE_RANGE_PICKER_RANGE_START);
|
||
|
|
const rangeEndEl = dateRangePickerEl.querySelector(DATE_RANGE_PICKER_RANGE_END);
|
||
|
|
return {
|
||
|
|
dateRangePickerEl,
|
||
|
|
rangeStartEl,
|
||
|
|
rangeEndEl
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* handle update from range start date picker
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el an element within the date range picker
|
||
|
|
*/
|
||
|
|
const handleRangeStartUpdate = el => {
|
||
|
|
const {
|
||
|
|
dateRangePickerEl,
|
||
|
|
rangeStartEl,
|
||
|
|
rangeEndEl
|
||
|
|
} = getDateRangePickerContext(el);
|
||
|
|
const {
|
||
|
|
internalInputEl
|
||
|
|
} = getDatePickerContext(rangeStartEl);
|
||
|
|
const updatedDate = internalInputEl.value;
|
||
|
|
if (updatedDate && !isDateInputInvalid(internalInputEl)) {
|
||
|
|
rangeEndEl.dataset.minDate = updatedDate;
|
||
|
|
rangeEndEl.dataset.rangeDate = updatedDate;
|
||
|
|
rangeEndEl.dataset.defaultDate = updatedDate;
|
||
|
|
} else {
|
||
|
|
rangeEndEl.dataset.minDate = dateRangePickerEl.dataset.minDate || "";
|
||
|
|
rangeEndEl.dataset.rangeDate = "";
|
||
|
|
rangeEndEl.dataset.defaultDate = "";
|
||
|
|
}
|
||
|
|
updateCalendarIfVisible(rangeEndEl);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* handle update from range start date picker
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el an element within the date range picker
|
||
|
|
*/
|
||
|
|
const handleRangeEndUpdate = el => {
|
||
|
|
const {
|
||
|
|
dateRangePickerEl,
|
||
|
|
rangeStartEl,
|
||
|
|
rangeEndEl
|
||
|
|
} = getDateRangePickerContext(el);
|
||
|
|
const {
|
||
|
|
internalInputEl
|
||
|
|
} = getDatePickerContext(rangeEndEl);
|
||
|
|
const updatedDate = internalInputEl.value;
|
||
|
|
if (updatedDate && !isDateInputInvalid(internalInputEl)) {
|
||
|
|
rangeStartEl.dataset.maxDate = updatedDate;
|
||
|
|
rangeStartEl.dataset.rangeDate = updatedDate;
|
||
|
|
rangeStartEl.dataset.defaultDate = updatedDate;
|
||
|
|
} else {
|
||
|
|
rangeStartEl.dataset.maxDate = dateRangePickerEl.dataset.maxDate || "";
|
||
|
|
rangeStartEl.dataset.rangeDate = "";
|
||
|
|
rangeStartEl.dataset.defaultDate = "";
|
||
|
|
}
|
||
|
|
updateCalendarIfVisible(rangeStartEl);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enhance an input with the date picker elements
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el The initial wrapping element of the date range picker component
|
||
|
|
*/
|
||
|
|
const enhanceDateRangePicker = el => {
|
||
|
|
const dateRangePickerEl = el.closest(DATE_RANGE_PICKER);
|
||
|
|
const [rangeStart, rangeEnd] = select(DATE_PICKER, dateRangePickerEl);
|
||
|
|
if (!rangeStart) {
|
||
|
|
throw new Error(`${DATE_RANGE_PICKER} is missing inner two '${DATE_PICKER}' elements`);
|
||
|
|
}
|
||
|
|
if (!rangeEnd) {
|
||
|
|
throw new Error(`${DATE_RANGE_PICKER} is missing second '${DATE_PICKER}' element`);
|
||
|
|
}
|
||
|
|
rangeStart.classList.add(DATE_RANGE_PICKER_RANGE_START_CLASS);
|
||
|
|
rangeEnd.classList.add(DATE_RANGE_PICKER_RANGE_END_CLASS);
|
||
|
|
if (!dateRangePickerEl.dataset.minDate) {
|
||
|
|
dateRangePickerEl.dataset.minDate = DEFAULT_MIN_DATE;
|
||
|
|
}
|
||
|
|
const {
|
||
|
|
minDate
|
||
|
|
} = dateRangePickerEl.dataset;
|
||
|
|
rangeStart.dataset.minDate = minDate;
|
||
|
|
rangeEnd.dataset.minDate = minDate;
|
||
|
|
const {
|
||
|
|
maxDate
|
||
|
|
} = dateRangePickerEl.dataset;
|
||
|
|
if (maxDate) {
|
||
|
|
rangeStart.dataset.maxDate = maxDate;
|
||
|
|
rangeEnd.dataset.maxDate = maxDate;
|
||
|
|
}
|
||
|
|
handleRangeStartUpdate(dateRangePickerEl);
|
||
|
|
handleRangeEndUpdate(dateRangePickerEl);
|
||
|
|
};
|
||
|
|
const dateRangePicker = behavior({
|
||
|
|
"input change": {
|
||
|
|
[DATE_RANGE_PICKER_RANGE_START]() {
|
||
|
|
handleRangeStartUpdate(this);
|
||
|
|
},
|
||
|
|
[DATE_RANGE_PICKER_RANGE_END]() {
|
||
|
|
handleRangeEndUpdate(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(DATE_RANGE_PICKER, root).forEach(dateRangePickerEl => {
|
||
|
|
enhanceDateRangePicker(dateRangePickerEl);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
module.exports = dateRangePicker;
|
||
|
|
|
||
|
|
},{"../../usa-date-picker/src/index":19,"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/select":53,"../../uswds-core/src/js/utils/select-or-matches":52}],21:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const Sanitizer = require("../../uswds-core/src/js/utils/sanitizer");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const DROPZONE_CLASS = `${PREFIX}-file-input`;
|
||
|
|
const DROPZONE = `.${DROPZONE_CLASS}`;
|
||
|
|
const INPUT_CLASS = `${PREFIX}-file-input__input`;
|
||
|
|
const TARGET_CLASS = `${PREFIX}-file-input__target`;
|
||
|
|
const INPUT = `.${INPUT_CLASS}`;
|
||
|
|
const BOX_CLASS = `${PREFIX}-file-input__box`;
|
||
|
|
const INSTRUCTIONS_CLASS = `${PREFIX}-file-input__instructions`;
|
||
|
|
const PREVIEW_CLASS = `${PREFIX}-file-input__preview`;
|
||
|
|
const PREVIEW_HEADING_CLASS = `${PREFIX}-file-input__preview-heading`;
|
||
|
|
const DISABLED_CLASS = `${PREFIX}-file-input--disabled`;
|
||
|
|
const CHOOSE_CLASS = `${PREFIX}-file-input__choose`;
|
||
|
|
const ACCEPTED_FILE_MESSAGE_CLASS = `${PREFIX}-file-input__accepted-files-message`;
|
||
|
|
const DRAG_TEXT_CLASS = `${PREFIX}-file-input__drag-text`;
|
||
|
|
const DRAG_CLASS = `${PREFIX}-file-input--drag`;
|
||
|
|
const LOADING_CLASS = "is-loading";
|
||
|
|
const HIDDEN_CLASS = "display-none";
|
||
|
|
const INVALID_FILE_CLASS = "has-invalid-file";
|
||
|
|
const GENERIC_PREVIEW_CLASS_NAME = `${PREFIX}-file-input__preview-image`;
|
||
|
|
const GENERIC_PREVIEW_CLASS = `${GENERIC_PREVIEW_CLASS_NAME}--generic`;
|
||
|
|
const PDF_PREVIEW_CLASS = `${GENERIC_PREVIEW_CLASS_NAME}--pdf`;
|
||
|
|
const WORD_PREVIEW_CLASS = `${GENERIC_PREVIEW_CLASS_NAME}--word`;
|
||
|
|
const VIDEO_PREVIEW_CLASS = `${GENERIC_PREVIEW_CLASS_NAME}--video`;
|
||
|
|
const EXCEL_PREVIEW_CLASS = `${GENERIC_PREVIEW_CLASS_NAME}--excel`;
|
||
|
|
const SPACER_GIF = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
|
||
|
|
let TYPE_IS_VALID = Boolean(true); // logic gate for change listener
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The properties and elements within the file input.
|
||
|
|
* @typedef {Object} FileInputContext
|
||
|
|
* @property {HTMLDivElement} dropZoneEl
|
||
|
|
* @property {HTMLInputElement} inputEl
|
||
|
|
*/
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get an object of the properties and elements belonging directly to the given
|
||
|
|
* file input component.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el the element within the file input
|
||
|
|
* @returns {FileInputContext} elements
|
||
|
|
*/
|
||
|
|
const getFileInputContext = el => {
|
||
|
|
const dropZoneEl = el.closest(DROPZONE);
|
||
|
|
if (!dropZoneEl) {
|
||
|
|
throw new Error(`Element is missing outer ${DROPZONE}`);
|
||
|
|
}
|
||
|
|
const inputEl = dropZoneEl.querySelector(INPUT);
|
||
|
|
return {
|
||
|
|
dropZoneEl,
|
||
|
|
inputEl
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Disable the file input component
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the file input component
|
||
|
|
*/
|
||
|
|
const disable = el => {
|
||
|
|
const {
|
||
|
|
dropZoneEl,
|
||
|
|
inputEl
|
||
|
|
} = getFileInputContext(el);
|
||
|
|
inputEl.disabled = true;
|
||
|
|
dropZoneEl.classList.add(DISABLED_CLASS);
|
||
|
|
dropZoneEl.setAttribute("aria-disabled", "true");
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enable the file input component
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the file input component
|
||
|
|
*/
|
||
|
|
const enable = el => {
|
||
|
|
const {
|
||
|
|
dropZoneEl,
|
||
|
|
inputEl
|
||
|
|
} = getFileInputContext(el);
|
||
|
|
inputEl.disabled = false;
|
||
|
|
dropZoneEl.classList.remove(DISABLED_CLASS);
|
||
|
|
dropZoneEl.removeAttribute("aria-disabled");
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
*
|
||
|
|
* @param {String} s special characters
|
||
|
|
* @returns {String} replaces specified values
|
||
|
|
*/
|
||
|
|
const replaceName = s => {
|
||
|
|
const c = s.charCodeAt(0);
|
||
|
|
if (c === 32) return "-";
|
||
|
|
if (c >= 65 && c <= 90) return `img_${s.toLowerCase()}`;
|
||
|
|
return `__${("000", c.toString(16)).slice(-4)}`;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Creates an ID name for each file that strips all invalid characters.
|
||
|
|
* @param {String} name - name of the file added to file input (searchvalue)
|
||
|
|
* @returns {String} same characters as the name with invalid chars removed (newvalue)
|
||
|
|
*/
|
||
|
|
const makeSafeForID = name => name.replace(/[^a-z0-9]/g, replaceName);
|
||
|
|
|
||
|
|
// Takes a generated safe ID and creates a unique ID.
|
||
|
|
const createUniqueID = name => `${name}-${Math.floor(Date.now().toString() / 1000)}`;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Builds full file input component
|
||
|
|
* @param {HTMLElement} fileInputEl - original file input on page
|
||
|
|
* @returns {HTMLElement|HTMLElement} - Instructions, target area div
|
||
|
|
*/
|
||
|
|
const buildFileInput = fileInputEl => {
|
||
|
|
const acceptsMultiple = fileInputEl.hasAttribute("multiple");
|
||
|
|
const fileInputParent = document.createElement("div");
|
||
|
|
const dropTarget = document.createElement("div");
|
||
|
|
const box = document.createElement("div");
|
||
|
|
const instructions = document.createElement("div");
|
||
|
|
const disabled = fileInputEl.hasAttribute("disabled");
|
||
|
|
let defaultAriaLabel;
|
||
|
|
|
||
|
|
// Adds class names and other attributes
|
||
|
|
fileInputEl.classList.remove(DROPZONE_CLASS);
|
||
|
|
fileInputEl.classList.add(INPUT_CLASS);
|
||
|
|
fileInputParent.classList.add(DROPZONE_CLASS);
|
||
|
|
box.classList.add(BOX_CLASS);
|
||
|
|
instructions.classList.add(INSTRUCTIONS_CLASS);
|
||
|
|
instructions.setAttribute("aria-hidden", "true");
|
||
|
|
dropTarget.classList.add(TARGET_CLASS);
|
||
|
|
// Encourage screenreader to read out aria changes immediately following upload status change
|
||
|
|
fileInputEl.setAttribute("aria-live", "polite");
|
||
|
|
|
||
|
|
// Adds child elements to the DOM
|
||
|
|
fileInputEl.parentNode.insertBefore(dropTarget, fileInputEl);
|
||
|
|
fileInputEl.parentNode.insertBefore(fileInputParent, dropTarget);
|
||
|
|
dropTarget.appendChild(fileInputEl);
|
||
|
|
fileInputParent.appendChild(dropTarget);
|
||
|
|
fileInputEl.parentNode.insertBefore(instructions, fileInputEl);
|
||
|
|
fileInputEl.parentNode.insertBefore(box, fileInputEl);
|
||
|
|
|
||
|
|
// Disabled styling
|
||
|
|
if (disabled) {
|
||
|
|
disable(fileInputEl);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Sets instruction test and aria-label based on whether or not multiple files are accepted
|
||
|
|
if (acceptsMultiple) {
|
||
|
|
defaultAriaLabel = "No files selected";
|
||
|
|
instructions.innerHTML = Sanitizer.escapeHTML`<span class="${DRAG_TEXT_CLASS}">Drag files here or </span><span class="${CHOOSE_CLASS}">choose from folder</span>`;
|
||
|
|
fileInputEl.setAttribute("aria-label", defaultAriaLabel);
|
||
|
|
fileInputEl.setAttribute("data-default-aria-label", defaultAriaLabel);
|
||
|
|
} else {
|
||
|
|
defaultAriaLabel = "No file selected";
|
||
|
|
instructions.innerHTML = Sanitizer.escapeHTML`<span class="${DRAG_TEXT_CLASS}">Drag file here or </span><span class="${CHOOSE_CLASS}">choose from folder</span>`;
|
||
|
|
fileInputEl.setAttribute("aria-label", defaultAriaLabel);
|
||
|
|
fileInputEl.setAttribute("data-default-aria-label", defaultAriaLabel);
|
||
|
|
}
|
||
|
|
|
||
|
|
// IE11 and Edge do not support drop files on file inputs, so we've removed text that indicates that
|
||
|
|
if (/rv:11.0/i.test(navigator.userAgent) || /Edge\/\d./i.test(navigator.userAgent)) {
|
||
|
|
fileInputParent.querySelector(`.${DRAG_TEXT_CLASS}`).outerHTML = "";
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
instructions,
|
||
|
|
dropTarget
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Removes image previews, we want to start with a clean list every time files are added to the file input
|
||
|
|
* @param {HTMLElement} dropTarget - target area div that encases the input
|
||
|
|
* @param {HTMLElement} instructions - text to inform users to drag or select files
|
||
|
|
*/
|
||
|
|
const removeOldPreviews = (dropTarget, instructions, inputAriaLabel) => {
|
||
|
|
const filePreviews = dropTarget.querySelectorAll(`.${PREVIEW_CLASS}`);
|
||
|
|
const fileInputElement = dropTarget.querySelector(INPUT);
|
||
|
|
const currentPreviewHeading = dropTarget.querySelector(`.${PREVIEW_HEADING_CLASS}`);
|
||
|
|
const currentErrorMessage = dropTarget.querySelector(`.${ACCEPTED_FILE_MESSAGE_CLASS}`);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* finds the parent of the passed node and removes the child
|
||
|
|
* @param {HTMLElement} node
|
||
|
|
*/
|
||
|
|
const removeImages = node => {
|
||
|
|
node.parentNode.removeChild(node);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Remove the heading above the previews
|
||
|
|
if (currentPreviewHeading) {
|
||
|
|
currentPreviewHeading.outerHTML = "";
|
||
|
|
}
|
||
|
|
|
||
|
|
// Remove existing error messages
|
||
|
|
if (currentErrorMessage) {
|
||
|
|
currentErrorMessage.outerHTML = "";
|
||
|
|
dropTarget.classList.remove(INVALID_FILE_CLASS);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Get rid of existing previews if they exist, show instructions
|
||
|
|
if (filePreviews !== null) {
|
||
|
|
if (instructions) {
|
||
|
|
instructions.classList.remove(HIDDEN_CLASS);
|
||
|
|
}
|
||
|
|
fileInputElement.setAttribute("aria-label", inputAriaLabel);
|
||
|
|
Array.prototype.forEach.call(filePreviews, removeImages);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* When new files are applied to file input, this function generates previews
|
||
|
|
* and removes old ones.
|
||
|
|
* @param {event} e
|
||
|
|
* @param {HTMLElement} fileInputEl - file input element
|
||
|
|
* @param {HTMLElement} instructions - text to inform users to drag or select files
|
||
|
|
* @param {HTMLElement} dropTarget - target area div that encases the input
|
||
|
|
*/
|
||
|
|
|
||
|
|
const handleChange = (e, fileInputEl, instructions, dropTarget) => {
|
||
|
|
const fileNames = e.target.files;
|
||
|
|
const filePreviewsHeading = document.createElement("div");
|
||
|
|
const inputAriaLabel = fileInputEl.dataset.defaultAriaLabel;
|
||
|
|
const fileStore = [];
|
||
|
|
|
||
|
|
// First, get rid of existing previews
|
||
|
|
removeOldPreviews(dropTarget, instructions, inputAriaLabel);
|
||
|
|
|
||
|
|
// Then, iterate through files list and:
|
||
|
|
// 1. Add selected file list names to aria-label
|
||
|
|
// 2. Create previews
|
||
|
|
for (let i = 0; i < fileNames.length; i += 1) {
|
||
|
|
const reader = new FileReader();
|
||
|
|
const fileName = fileNames[i].name;
|
||
|
|
|
||
|
|
// Push updated file names into the store array
|
||
|
|
fileStore.push(fileName);
|
||
|
|
|
||
|
|
// read out the store array via aria-label, wording options vary based on file count
|
||
|
|
if (i === 0) {
|
||
|
|
fileInputEl.setAttribute("aria-label", `You have selected the file: ${fileName}`);
|
||
|
|
} else if (i >= 1) {
|
||
|
|
fileInputEl.setAttribute("aria-label", `You have selected ${fileNames.length} files: ${fileStore.join(", ")}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Starts with a loading image while preview is created
|
||
|
|
reader.onloadstart = function createLoadingImage() {
|
||
|
|
const imageId = createUniqueID(makeSafeForID(fileName));
|
||
|
|
instructions.insertAdjacentHTML("afterend", Sanitizer.escapeHTML`<div class="${PREVIEW_CLASS}" aria-hidden="true">
|
||
|
|
<img id="${imageId}" src="${SPACER_GIF}" alt="" class="${GENERIC_PREVIEW_CLASS_NAME} ${LOADING_CLASS}"/>${fileName}
|
||
|
|
<div>`);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Not all files will be able to generate previews. In case this happens, we provide several types "generic previews" based on the file extension.
|
||
|
|
reader.onloadend = function createFilePreview() {
|
||
|
|
const imageId = createUniqueID(makeSafeForID(fileName));
|
||
|
|
const previewImage = document.getElementById(imageId);
|
||
|
|
if (fileName.indexOf(".pdf") > 0) {
|
||
|
|
previewImage.setAttribute("onerror", `this.onerror=null;this.src="${SPACER_GIF}"; this.classList.add("${PDF_PREVIEW_CLASS}")`);
|
||
|
|
} else if (fileName.indexOf(".doc") > 0 || fileName.indexOf(".pages") > 0) {
|
||
|
|
previewImage.setAttribute("onerror", `this.onerror=null;this.src="${SPACER_GIF}"; this.classList.add("${WORD_PREVIEW_CLASS}")`);
|
||
|
|
} else if (fileName.indexOf(".xls") > 0 || fileName.indexOf(".numbers") > 0) {
|
||
|
|
previewImage.setAttribute("onerror", `this.onerror=null;this.src="${SPACER_GIF}"; this.classList.add("${EXCEL_PREVIEW_CLASS}")`);
|
||
|
|
} else if (fileName.indexOf(".mov") > 0 || fileName.indexOf(".mp4") > 0) {
|
||
|
|
previewImage.setAttribute("onerror", `this.onerror=null;this.src="${SPACER_GIF}"; this.classList.add("${VIDEO_PREVIEW_CLASS}")`);
|
||
|
|
} else {
|
||
|
|
previewImage.setAttribute("onerror", `this.onerror=null;this.src="${SPACER_GIF}"; this.classList.add("${GENERIC_PREVIEW_CLASS}")`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Removes loader and displays preview
|
||
|
|
previewImage.classList.remove(LOADING_CLASS);
|
||
|
|
previewImage.src = reader.result;
|
||
|
|
};
|
||
|
|
if (fileNames[i]) {
|
||
|
|
reader.readAsDataURL(fileNames[i]);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Adds heading above file previews, pluralizes if there are multiple
|
||
|
|
if (i === 0) {
|
||
|
|
dropTarget.insertBefore(filePreviewsHeading, instructions);
|
||
|
|
filePreviewsHeading.innerHTML = `Selected file <span class="usa-file-input__choose">Change file</span>`;
|
||
|
|
} else if (i >= 1) {
|
||
|
|
dropTarget.insertBefore(filePreviewsHeading, instructions);
|
||
|
|
filePreviewsHeading.innerHTML = Sanitizer.escapeHTML`${i + 1} files selected <span class="usa-file-input__choose">Change files</span>`;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Hides null state content and sets preview heading class
|
||
|
|
if (filePreviewsHeading) {
|
||
|
|
instructions.classList.add(HIDDEN_CLASS);
|
||
|
|
filePreviewsHeading.classList.add(PREVIEW_HEADING_CLASS);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* When using an Accept attribute, invalid files will be hidden from
|
||
|
|
* file browser, but they can still be dragged to the input. This
|
||
|
|
* function prevents them from being dragged and removes error states
|
||
|
|
* when correct files are added.
|
||
|
|
* @param {event} e
|
||
|
|
* @param {HTMLElement} fileInputEl - file input element
|
||
|
|
* @param {HTMLElement} instructions - text to inform users to drag or select files
|
||
|
|
* @param {HTMLElement} dropTarget - target area div that encases the input
|
||
|
|
*/
|
||
|
|
const preventInvalidFiles = (e, fileInputEl, instructions, dropTarget) => {
|
||
|
|
const acceptedFilesAttr = fileInputEl.getAttribute("accept");
|
||
|
|
dropTarget.classList.remove(INVALID_FILE_CLASS);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* We can probably move away from this once IE11 support stops, and replace
|
||
|
|
* with a simple es `.includes`
|
||
|
|
* check if element is in array
|
||
|
|
* check if 1 or more alphabets are in string
|
||
|
|
* if element is present return the position value and -1 otherwise
|
||
|
|
* @param {Object} file
|
||
|
|
* @param {String} value
|
||
|
|
* @returns {Boolean}
|
||
|
|
*/
|
||
|
|
const isIncluded = (file, value) => {
|
||
|
|
let returnValue = false;
|
||
|
|
const pos = file.indexOf(value);
|
||
|
|
if (pos >= 0) {
|
||
|
|
returnValue = true;
|
||
|
|
}
|
||
|
|
return returnValue;
|
||
|
|
};
|
||
|
|
|
||
|
|
// Runs if only specific files are accepted
|
||
|
|
if (acceptedFilesAttr) {
|
||
|
|
const acceptedFiles = acceptedFilesAttr.split(",");
|
||
|
|
const errorMessage = document.createElement("div");
|
||
|
|
|
||
|
|
// If multiple files are dragged, this iterates through them and look for any files that are not accepted.
|
||
|
|
let allFilesAllowed = true;
|
||
|
|
const scannedFiles = e.target.files || e.dataTransfer.files;
|
||
|
|
for (let i = 0; i < scannedFiles.length; i += 1) {
|
||
|
|
const file = scannedFiles[i];
|
||
|
|
if (allFilesAllowed) {
|
||
|
|
for (let j = 0; j < acceptedFiles.length; j += 1) {
|
||
|
|
const fileType = acceptedFiles[j];
|
||
|
|
allFilesAllowed = file.name.indexOf(fileType) > 0 || isIncluded(file.type, fileType.replace(/\*/g, ""));
|
||
|
|
if (allFilesAllowed) {
|
||
|
|
TYPE_IS_VALID = true;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else break;
|
||
|
|
}
|
||
|
|
|
||
|
|
// If dragged files are not accepted, this removes them from the value of the input and creates and error state
|
||
|
|
if (!allFilesAllowed) {
|
||
|
|
removeOldPreviews(dropTarget, instructions);
|
||
|
|
fileInputEl.value = ""; // eslint-disable-line no-param-reassign
|
||
|
|
dropTarget.insertBefore(errorMessage, fileInputEl);
|
||
|
|
errorMessage.textContent = fileInputEl.dataset.errormessage || `This is not a valid file type.`;
|
||
|
|
errorMessage.classList.add(ACCEPTED_FILE_MESSAGE_CLASS);
|
||
|
|
dropTarget.classList.add(INVALID_FILE_CLASS);
|
||
|
|
TYPE_IS_VALID = false;
|
||
|
|
e.preventDefault();
|
||
|
|
e.stopPropagation();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 1. passes through gate for preventing invalid files
|
||
|
|
* 2. handles updates if file is valid
|
||
|
|
* @param {event} event
|
||
|
|
* @param {HTMLElement} element
|
||
|
|
* @param {HTMLElement} instructionsEl
|
||
|
|
* @param {HTMLElement} target
|
||
|
|
*/
|
||
|
|
const handleUpload = (event, element, instructionsEl, dropTargetEl) => {
|
||
|
|
preventInvalidFiles(event, element, instructionsEl, dropTargetEl);
|
||
|
|
if (TYPE_IS_VALID === true) {
|
||
|
|
handleChange(event, element, instructionsEl, dropTargetEl);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
const fileInput = behavior({}, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(DROPZONE, root).forEach(fileInputEl => {
|
||
|
|
const {
|
||
|
|
instructions,
|
||
|
|
dropTarget
|
||
|
|
} = buildFileInput(fileInputEl);
|
||
|
|
dropTarget.addEventListener("dragover", function handleDragOver() {
|
||
|
|
this.classList.add(DRAG_CLASS);
|
||
|
|
}, false);
|
||
|
|
dropTarget.addEventListener("dragleave", function handleDragLeave() {
|
||
|
|
this.classList.remove(DRAG_CLASS);
|
||
|
|
}, false);
|
||
|
|
dropTarget.addEventListener("drop", function handleDrop() {
|
||
|
|
this.classList.remove(DRAG_CLASS);
|
||
|
|
}, false);
|
||
|
|
fileInputEl.addEventListener("change", e => handleUpload(e, fileInputEl, instructions, dropTarget), false);
|
||
|
|
});
|
||
|
|
},
|
||
|
|
teardown(root) {
|
||
|
|
selectOrMatches(INPUT, root).forEach(fileInputEl => {
|
||
|
|
const fileInputTopElement = fileInputEl.parentElement.parentElement;
|
||
|
|
fileInputTopElement.parentElement.replaceChild(fileInputEl, fileInputTopElement);
|
||
|
|
// eslint-disable-next-line no-param-reassign
|
||
|
|
fileInputEl.className = DROPZONE_CLASS;
|
||
|
|
});
|
||
|
|
},
|
||
|
|
getFileInputContext,
|
||
|
|
disable,
|
||
|
|
enable
|
||
|
|
});
|
||
|
|
module.exports = fileInput;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/sanitizer":50,"../../uswds-core/src/js/utils/select-or-matches":52}],22:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const SCOPE = `.${PREFIX}-footer--big`;
|
||
|
|
const NAV = `${SCOPE} nav`;
|
||
|
|
const BUTTON = `${NAV} .${PREFIX}-footer__primary-link`;
|
||
|
|
const HIDE_MAX_WIDTH = 480;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Expands selected footer menu panel, while collapsing others
|
||
|
|
*/
|
||
|
|
function showPanel() {
|
||
|
|
if (window.innerWidth < HIDE_MAX_WIDTH) {
|
||
|
|
const isOpen = this.getAttribute("aria-expanded") === "true";
|
||
|
|
const thisFooter = this.closest(SCOPE);
|
||
|
|
|
||
|
|
// Close all other menus
|
||
|
|
thisFooter.querySelectorAll(BUTTON).forEach(button => {
|
||
|
|
button.setAttribute("aria-expanded", false);
|
||
|
|
});
|
||
|
|
this.setAttribute("aria-expanded", !isOpen);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Swaps the <h4> element for a <button> element (and vice-versa) and sets id
|
||
|
|
* of menu list
|
||
|
|
*
|
||
|
|
* @param {Boolean} isMobile - If the footer is in mobile configuration
|
||
|
|
*/
|
||
|
|
function toggleHtmlTag(isMobile) {
|
||
|
|
const bigFooter = document.querySelector(SCOPE);
|
||
|
|
if (!bigFooter) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const primaryLinks = bigFooter.querySelectorAll(BUTTON);
|
||
|
|
primaryLinks.forEach(currentElement => {
|
||
|
|
const currentElementClasses = currentElement.getAttribute("class");
|
||
|
|
const preservedHtmlTag = currentElement.getAttribute("data-tag") || currentElement.tagName;
|
||
|
|
const newElementType = isMobile ? "button" : preservedHtmlTag;
|
||
|
|
|
||
|
|
// Create the new element
|
||
|
|
const newElement = document.createElement(newElementType);
|
||
|
|
newElement.setAttribute("class", currentElementClasses);
|
||
|
|
newElement.classList.toggle(`${PREFIX}-footer__primary-link--button`, isMobile);
|
||
|
|
newElement.textContent = currentElement.textContent;
|
||
|
|
if (isMobile) {
|
||
|
|
newElement.setAttribute("data-tag", currentElement.tagName);
|
||
|
|
const menuId = `${PREFIX}-footer-menu-list-${Math.floor(Math.random() * 100000)}`;
|
||
|
|
newElement.setAttribute("aria-controls", menuId);
|
||
|
|
newElement.setAttribute("aria-expanded", "false");
|
||
|
|
currentElement.nextElementSibling.setAttribute("id", menuId);
|
||
|
|
newElement.setAttribute("type", "button");
|
||
|
|
}
|
||
|
|
|
||
|
|
// Insert the new element and delete the old
|
||
|
|
currentElement.after(newElement);
|
||
|
|
currentElement.remove();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
const resize = event => {
|
||
|
|
toggleHtmlTag(event.matches);
|
||
|
|
};
|
||
|
|
module.exports = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[BUTTON]: showPanel
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
// export for use elsewhere
|
||
|
|
HIDE_MAX_WIDTH,
|
||
|
|
init() {
|
||
|
|
toggleHtmlTag(window.innerWidth < HIDE_MAX_WIDTH);
|
||
|
|
this.mediaQueryList = window.matchMedia(`(max-width: ${HIDE_MAX_WIDTH - 0.1}px)`);
|
||
|
|
this.mediaQueryList.addListener(resize);
|
||
|
|
},
|
||
|
|
teardown() {
|
||
|
|
this.mediaQueryList.removeListener(resize);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45}],23:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const keymap = require("receptor/keymap");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const select = require("../../uswds-core/src/js/utils/select");
|
||
|
|
const toggle = require("../../uswds-core/src/js/utils/toggle");
|
||
|
|
const FocusTrap = require("../../uswds-core/src/js/utils/focus-trap");
|
||
|
|
const accordion = require("../../usa-accordion/src/index");
|
||
|
|
const ScrollBarWidth = require("../../uswds-core/src/js/utils/scrollbar-width");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const BODY = "body";
|
||
|
|
const HEADER = `.${PREFIX}-header`;
|
||
|
|
const NAV = `.${PREFIX}-nav`;
|
||
|
|
const NAV_CONTAINER = `.${PREFIX}-nav-container`;
|
||
|
|
const NAV_PRIMARY = `.${PREFIX}-nav__primary`;
|
||
|
|
const NAV_PRIMARY_ITEM = `.${PREFIX}-nav__primary-item`;
|
||
|
|
const NAV_CONTROL = `button.${PREFIX}-nav__link`;
|
||
|
|
const NAV_LINKS = `${NAV} a`;
|
||
|
|
const NON_NAV_HIDDEN_ATTRIBUTE = `data-nav-hidden`;
|
||
|
|
const OPENERS = `.${PREFIX}-menu-btn`;
|
||
|
|
const CLOSE_BUTTON = `.${PREFIX}-nav__close`;
|
||
|
|
const OVERLAY = `.${PREFIX}-overlay`;
|
||
|
|
const CLOSERS = `${CLOSE_BUTTON}, .${PREFIX}-overlay`;
|
||
|
|
const TOGGLES = [NAV, OVERLAY].join(", ");
|
||
|
|
const NON_NAV_ELEMENTS = `body *:not(${HEADER}, ${NAV_CONTAINER}, ${NAV}, ${NAV} *):not([aria-hidden])`;
|
||
|
|
const NON_NAV_HIDDEN = `[${NON_NAV_HIDDEN_ATTRIBUTE}]`;
|
||
|
|
const ACTIVE_CLASS = "usa-js-mobile-nav--active";
|
||
|
|
const VISIBLE_CLASS = "is-visible";
|
||
|
|
let navigation;
|
||
|
|
let navActive;
|
||
|
|
let nonNavElements;
|
||
|
|
const isActive = () => document.body.classList.contains(ACTIVE_CLASS);
|
||
|
|
const SCROLLBAR_WIDTH = ScrollBarWidth();
|
||
|
|
const INITIAL_PADDING = window.getComputedStyle(document.body).getPropertyValue("padding-right");
|
||
|
|
const TEMPORARY_PADDING = `${parseInt(INITIAL_PADDING.replace(/px/, ""), 10) + parseInt(SCROLLBAR_WIDTH.replace(/px/, ""), 10)}px`;
|
||
|
|
const hideNonNavItems = () => {
|
||
|
|
const headerParent = document.querySelector(`${HEADER}`).parentNode;
|
||
|
|
nonNavElements = document.querySelectorAll(NON_NAV_ELEMENTS);
|
||
|
|
nonNavElements.forEach(nonNavElement => {
|
||
|
|
if (nonNavElement !== headerParent) {
|
||
|
|
nonNavElement.setAttribute("aria-hidden", true);
|
||
|
|
nonNavElement.setAttribute(NON_NAV_HIDDEN_ATTRIBUTE, "");
|
||
|
|
}
|
||
|
|
});
|
||
|
|
};
|
||
|
|
const showNonNavItems = () => {
|
||
|
|
nonNavElements = document.querySelectorAll(NON_NAV_HIDDEN);
|
||
|
|
if (!nonNavElements) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Remove aria-hidden from non-header elements
|
||
|
|
nonNavElements.forEach(nonNavElement => {
|
||
|
|
nonNavElement.removeAttribute("aria-hidden");
|
||
|
|
nonNavElement.removeAttribute(NON_NAV_HIDDEN_ATTRIBUTE);
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
// Toggle all non-header elements #3527.
|
||
|
|
const toggleNonNavItems = active => {
|
||
|
|
if (active) {
|
||
|
|
hideNonNavItems();
|
||
|
|
} else {
|
||
|
|
showNonNavItems();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
const toggleNav = active => {
|
||
|
|
const {
|
||
|
|
body
|
||
|
|
} = document;
|
||
|
|
const safeActive = typeof active === "boolean" ? active : !isActive();
|
||
|
|
body.classList.toggle(ACTIVE_CLASS, safeActive);
|
||
|
|
select(TOGGLES).forEach(el => el.classList.toggle(VISIBLE_CLASS, safeActive));
|
||
|
|
navigation.focusTrap.update(safeActive);
|
||
|
|
const closeButton = body.querySelector(CLOSE_BUTTON);
|
||
|
|
const menuButton = document.querySelector(OPENERS);
|
||
|
|
body.style.paddingRight = body.style.paddingRight === TEMPORARY_PADDING ? INITIAL_PADDING : TEMPORARY_PADDING;
|
||
|
|
toggleNonNavItems(safeActive);
|
||
|
|
if (safeActive && closeButton) {
|
||
|
|
// The mobile nav was just activated. Focus on the close button, which is
|
||
|
|
// just before all the nav elements in the tab order.
|
||
|
|
closeButton.focus();
|
||
|
|
} else if (!safeActive && menuButton && getComputedStyle(menuButton).display !== "none") {
|
||
|
|
// The mobile nav was just deactivated. We don't want the focus to
|
||
|
|
// disappear into the void, so focus on the menu button if it's
|
||
|
|
// visible (this may have been what the user was just focused on,
|
||
|
|
// if they triggered the mobile nav by mistake).
|
||
|
|
menuButton.focus();
|
||
|
|
}
|
||
|
|
return safeActive;
|
||
|
|
};
|
||
|
|
const resize = () => {
|
||
|
|
const closer = document.body.querySelector(CLOSE_BUTTON);
|
||
|
|
if (isActive() && closer && closer.getBoundingClientRect().width === 0) {
|
||
|
|
// When the mobile nav is active, and the close box isn't visible,
|
||
|
|
// we know the user's viewport has been resized to be larger.
|
||
|
|
// Let's make the page state consistent by deactivating the mobile nav.
|
||
|
|
navigation.toggleNav.call(closer, false);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
const onMenuClose = () => navigation.toggleNav.call(navigation, false);
|
||
|
|
const hideActiveNavDropdown = () => {
|
||
|
|
if (!navActive) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
toggle(navActive, false);
|
||
|
|
navActive = null;
|
||
|
|
};
|
||
|
|
const focusNavButton = event => {
|
||
|
|
const parentNavItem = event.target.closest(NAV_PRIMARY_ITEM);
|
||
|
|
|
||
|
|
// Only shift focus if within dropdown
|
||
|
|
if (!event.target.matches(NAV_CONTROL)) {
|
||
|
|
const navControl = parentNavItem.querySelector(NAV_CONTROL);
|
||
|
|
if (navControl) {
|
||
|
|
navControl.focus();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
const handleEscape = event => {
|
||
|
|
hideActiveNavDropdown();
|
||
|
|
focusNavButton(event);
|
||
|
|
};
|
||
|
|
navigation = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[NAV_CONTROL]() {
|
||
|
|
// If another nav is open, close it
|
||
|
|
if (navActive !== this) {
|
||
|
|
hideActiveNavDropdown();
|
||
|
|
}
|
||
|
|
// store a reference to the last clicked nav link element, so we
|
||
|
|
// can hide the dropdown if another element on the page is clicked
|
||
|
|
if (!navActive) {
|
||
|
|
navActive = this;
|
||
|
|
toggle(navActive, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Do this so the event handler on the body doesn't fire
|
||
|
|
return false;
|
||
|
|
},
|
||
|
|
[BODY]: hideActiveNavDropdown,
|
||
|
|
[OPENERS]: toggleNav,
|
||
|
|
[CLOSERS]: toggleNav,
|
||
|
|
[NAV_LINKS]() {
|
||
|
|
// A navigation link has been clicked! We want to collapse any
|
||
|
|
// hierarchical navigation UI it's a part of, so that the user
|
||
|
|
// can focus on whatever they've just selected.
|
||
|
|
|
||
|
|
// Some navigation links are inside accordions; when they're
|
||
|
|
// clicked, we want to collapse those accordions.
|
||
|
|
const acc = this.closest(accordion.ACCORDION);
|
||
|
|
if (acc) {
|
||
|
|
accordion.getButtons(acc).forEach(btn => accordion.hide(btn));
|
||
|
|
}
|
||
|
|
|
||
|
|
// If the mobile navigation menu is active, we want to hide it.
|
||
|
|
if (isActive()) {
|
||
|
|
navigation.toggleNav.call(navigation, false);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
keydown: {
|
||
|
|
[NAV_PRIMARY]: keymap({
|
||
|
|
Escape: handleEscape
|
||
|
|
})
|
||
|
|
},
|
||
|
|
focusout: {
|
||
|
|
[NAV_PRIMARY](event) {
|
||
|
|
const nav = event.target.closest(NAV_PRIMARY);
|
||
|
|
if (!nav.contains(event.relatedTarget)) {
|
||
|
|
hideActiveNavDropdown();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
const trapContainer = root.matches(NAV) ? root : root.querySelector(NAV);
|
||
|
|
if (trapContainer) {
|
||
|
|
navigation.focusTrap = FocusTrap(trapContainer, {
|
||
|
|
Escape: onMenuClose
|
||
|
|
});
|
||
|
|
}
|
||
|
|
resize();
|
||
|
|
window.addEventListener("resize", resize, false);
|
||
|
|
},
|
||
|
|
teardown() {
|
||
|
|
window.removeEventListener("resize", resize, false);
|
||
|
|
navActive = false;
|
||
|
|
},
|
||
|
|
focusTrap: null,
|
||
|
|
toggleNav
|
||
|
|
});
|
||
|
|
module.exports = navigation;
|
||
|
|
|
||
|
|
},{"../../usa-accordion/src/index":15,"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/focus-trap":47,"../../uswds-core/src/js/utils/scrollbar-width":51,"../../uswds-core/src/js/utils/select":53,"../../uswds-core/src/js/utils/toggle":56,"receptor/keymap":11}],24:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const once = require("receptor/once");
|
||
|
|
const keymap = require("receptor/keymap");
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const Sanitizer = require("../../uswds-core/src/js/utils/sanitizer");
|
||
|
|
const CURRENT_CLASS = `${PREFIX}-current`;
|
||
|
|
const IN_PAGE_NAV_TITLE_TEXT = "On this page";
|
||
|
|
const IN_PAGE_NAV_TITLE_HEADING_LEVEL = "h4";
|
||
|
|
const IN_PAGE_NAV_SCROLL_OFFSET = 0;
|
||
|
|
const IN_PAGE_NAV_ROOT_MARGIN = "0px 0px 0px 0px";
|
||
|
|
const IN_PAGE_NAV_THRESHOLD = "1";
|
||
|
|
const IN_PAGE_NAV_CLASS = `${PREFIX}-in-page-nav`;
|
||
|
|
const IN_PAGE_NAV_ANCHOR_CLASS = `${PREFIX}-anchor`;
|
||
|
|
const IN_PAGE_NAV_NAV_CLASS = `${IN_PAGE_NAV_CLASS}__nav`;
|
||
|
|
const IN_PAGE_NAV_LIST_CLASS = `${IN_PAGE_NAV_CLASS}__list`;
|
||
|
|
const IN_PAGE_NAV_ITEM_CLASS = `${IN_PAGE_NAV_CLASS}__item`;
|
||
|
|
const IN_PAGE_NAV_LINK_CLASS = `${IN_PAGE_NAV_CLASS}__link`;
|
||
|
|
const IN_PAGE_NAV_TITLE_CLASS = `${IN_PAGE_NAV_CLASS}__heading`;
|
||
|
|
const SUB_ITEM_CLASS = `${IN_PAGE_NAV_ITEM_CLASS}--sub-item`;
|
||
|
|
const MAIN_ELEMENT = "main";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Set the active link state for the currently observed section
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the in-page nav component
|
||
|
|
*/
|
||
|
|
const setActive = el => {
|
||
|
|
const allLinks = document.querySelectorAll(`.${IN_PAGE_NAV_LINK_CLASS}`);
|
||
|
|
el.map(i => {
|
||
|
|
if (i.isIntersecting === true && i.intersectionRatio >= 1) {
|
||
|
|
allLinks.forEach(link => link.classList.remove(CURRENT_CLASS));
|
||
|
|
document.querySelector(`a[href="#${i.target.id}"]`).classList.add(CURRENT_CLASS);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Return a node list of section headings
|
||
|
|
*
|
||
|
|
* @return {HTMLElement[]} - An array of DOM nodes
|
||
|
|
*/
|
||
|
|
const getSectionHeadings = () => {
|
||
|
|
const sectionHeadings = document.querySelectorAll(`${MAIN_ELEMENT} h2, ${MAIN_ELEMENT} h3`);
|
||
|
|
return sectionHeadings;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Return a node list of section anchor tags
|
||
|
|
*
|
||
|
|
* @return {HTMLElement[]} - An array of DOM nodes
|
||
|
|
*/
|
||
|
|
const getSectionAnchors = () => {
|
||
|
|
const sectionAnchors = document.querySelectorAll(`.${IN_PAGE_NAV_ANCHOR_CLASS}`);
|
||
|
|
return sectionAnchors;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Generates a unique ID for the given heading element.
|
||
|
|
*
|
||
|
|
* @param {HTMLHeadingElement} heading
|
||
|
|
*
|
||
|
|
* @return {string} - Unique ID
|
||
|
|
*/
|
||
|
|
const getHeadingId = heading => {
|
||
|
|
const baseId = heading.textContent.toLowerCase()
|
||
|
|
// Replace non-alphanumeric characters with dashes
|
||
|
|
.replace(/[^a-z\d]/g, "-")
|
||
|
|
// Replace a sequence of two or more dashes with a single dash
|
||
|
|
.replace(/-{2,}/g, "-")
|
||
|
|
// Trim leading or trailing dash (there should only ever be one)
|
||
|
|
.replace(/^-|-$/g, "");
|
||
|
|
let id;
|
||
|
|
let suffix = 0;
|
||
|
|
do {
|
||
|
|
id = baseId;
|
||
|
|
|
||
|
|
// To avoid conflicts with existing IDs on the page, loop and append an
|
||
|
|
// incremented suffix until a unique ID is found.
|
||
|
|
suffix += 1;
|
||
|
|
if (suffix > 1) {
|
||
|
|
id += `-${suffix}`;
|
||
|
|
}
|
||
|
|
} while (document.getElementById(id));
|
||
|
|
return id;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Return a section id/anchor hash without the number sign
|
||
|
|
*
|
||
|
|
* @return {String} - Id value with the number sign removed
|
||
|
|
*/
|
||
|
|
const getSectionId = value => {
|
||
|
|
let id;
|
||
|
|
|
||
|
|
// Check if value is an event or element and get the cleaned up id
|
||
|
|
if (value && value.nodeType === 1) {
|
||
|
|
id = value.getAttribute("href").replace("#", "");
|
||
|
|
} else {
|
||
|
|
id = value.target.hash.replace("#", "");
|
||
|
|
}
|
||
|
|
return id;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Scroll smoothly to a section based on the passed in element
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} - Id value with the number sign removed
|
||
|
|
*/
|
||
|
|
const handleScrollToSection = el => {
|
||
|
|
const inPageNavEl = document.querySelector(`.${IN_PAGE_NAV_CLASS}`);
|
||
|
|
const inPageNavScrollOffset = inPageNavEl.dataset.scrollOffset || IN_PAGE_NAV_SCROLL_OFFSET;
|
||
|
|
window.scroll({
|
||
|
|
behavior: "smooth",
|
||
|
|
top: el.offsetTop - inPageNavScrollOffset,
|
||
|
|
block: "start"
|
||
|
|
});
|
||
|
|
if (window.location.hash.slice(1) !== el.id) {
|
||
|
|
window.history.pushState(null, "", `#${el.id}`);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Scrolls the page to the section corresponding to the current hash fragment, if one exists.
|
||
|
|
*/
|
||
|
|
const scrollToCurrentSection = () => {
|
||
|
|
const hashFragment = window.location.hash.slice(1);
|
||
|
|
if (hashFragment) {
|
||
|
|
const anchorTag = document.getElementById(hashFragment);
|
||
|
|
if (anchorTag) {
|
||
|
|
handleScrollToSection(anchorTag);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Create the in-page navigation component
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} inPageNavEl The in-page nav element
|
||
|
|
*/
|
||
|
|
const createInPageNav = inPageNavEl => {
|
||
|
|
const inPageNavTitleText = Sanitizer.escapeHTML`${inPageNavEl.dataset.titleText || IN_PAGE_NAV_TITLE_TEXT}`;
|
||
|
|
const inPageNavTitleHeadingLevel = Sanitizer.escapeHTML`${inPageNavEl.dataset.titleHeadingLevel || IN_PAGE_NAV_TITLE_HEADING_LEVEL}`;
|
||
|
|
const inPageNavRootMargin = Sanitizer.escapeHTML`${inPageNavEl.dataset.rootMargin || IN_PAGE_NAV_ROOT_MARGIN}`;
|
||
|
|
const inPageNavThreshold = Sanitizer.escapeHTML`${inPageNavEl.dataset.threshold || IN_PAGE_NAV_THRESHOLD}`;
|
||
|
|
const options = {
|
||
|
|
root: null,
|
||
|
|
rootMargin: inPageNavRootMargin,
|
||
|
|
threshold: [inPageNavThreshold]
|
||
|
|
};
|
||
|
|
const sectionHeadings = getSectionHeadings();
|
||
|
|
const inPageNav = document.createElement("nav");
|
||
|
|
inPageNav.setAttribute("aria-label", inPageNavTitleText);
|
||
|
|
inPageNav.classList.add(IN_PAGE_NAV_NAV_CLASS);
|
||
|
|
const inPageNavTitle = document.createElement(inPageNavTitleHeadingLevel);
|
||
|
|
inPageNavTitle.classList.add(IN_PAGE_NAV_TITLE_CLASS);
|
||
|
|
inPageNavTitle.setAttribute("tabindex", "0");
|
||
|
|
inPageNavTitle.textContent = inPageNavTitleText;
|
||
|
|
inPageNav.appendChild(inPageNavTitle);
|
||
|
|
const inPageNavList = document.createElement("ul");
|
||
|
|
inPageNavList.classList.add(IN_PAGE_NAV_LIST_CLASS);
|
||
|
|
inPageNav.appendChild(inPageNavList);
|
||
|
|
sectionHeadings.forEach(el => {
|
||
|
|
const listItem = document.createElement("li");
|
||
|
|
const navLinks = document.createElement("a");
|
||
|
|
const anchorTag = document.createElement("a");
|
||
|
|
const textContentOfLink = el.textContent;
|
||
|
|
const tag = el.tagName.toLowerCase();
|
||
|
|
listItem.classList.add(IN_PAGE_NAV_ITEM_CLASS);
|
||
|
|
if (tag === "h3") {
|
||
|
|
listItem.classList.add(SUB_ITEM_CLASS);
|
||
|
|
}
|
||
|
|
const headingId = getHeadingId(el);
|
||
|
|
navLinks.setAttribute("href", `#${headingId}`);
|
||
|
|
navLinks.setAttribute("class", IN_PAGE_NAV_LINK_CLASS);
|
||
|
|
navLinks.textContent = textContentOfLink;
|
||
|
|
anchorTag.setAttribute("id", headingId);
|
||
|
|
anchorTag.setAttribute("class", IN_PAGE_NAV_ANCHOR_CLASS);
|
||
|
|
el.insertAdjacentElement("afterbegin", anchorTag);
|
||
|
|
inPageNavList.appendChild(listItem);
|
||
|
|
listItem.appendChild(navLinks);
|
||
|
|
});
|
||
|
|
inPageNavEl.appendChild(inPageNav);
|
||
|
|
const anchorTags = getSectionAnchors();
|
||
|
|
const observeSections = new window.IntersectionObserver(setActive, options);
|
||
|
|
anchorTags.forEach(tag => {
|
||
|
|
observeSections.observe(tag);
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle click from link
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el An element within the in-page nav component
|
||
|
|
*/
|
||
|
|
const handleClickFromLink = el => {
|
||
|
|
const elementToScrollTo = document.querySelector(el.hash);
|
||
|
|
handleScrollToSection(elementToScrollTo);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Handle the enter event from a link within the in-page nav component
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event An event within the in-page nav component
|
||
|
|
*/
|
||
|
|
const handleEnterFromLink = event => {
|
||
|
|
const id = getSectionId(event);
|
||
|
|
const targetAnchor = document.getElementById(id);
|
||
|
|
const target = targetAnchor.parentElement;
|
||
|
|
if (target) {
|
||
|
|
target.setAttribute("tabindex", 0);
|
||
|
|
target.focus();
|
||
|
|
target.addEventListener("blur", once(() => {
|
||
|
|
target.setAttribute("tabindex", -1);
|
||
|
|
}));
|
||
|
|
} else {
|
||
|
|
// throw an error?
|
||
|
|
}
|
||
|
|
handleScrollToSection(targetAnchor);
|
||
|
|
};
|
||
|
|
const inPageNavigation = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[`.${IN_PAGE_NAV_LINK_CLASS}`](event) {
|
||
|
|
event.preventDefault();
|
||
|
|
if (this.disabled) return;
|
||
|
|
handleClickFromLink(this);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
keydown: {
|
||
|
|
[`.${IN_PAGE_NAV_LINK_CLASS}`]: keymap({
|
||
|
|
Enter: handleEnterFromLink
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(`.${IN_PAGE_NAV_CLASS}`, root).forEach(inPageNavEl => {
|
||
|
|
createInPageNav(inPageNavEl);
|
||
|
|
scrollToCurrentSection();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
module.exports = inPageNavigation;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/sanitizer":50,"../../uswds-core/src/js/utils/select-or-matches":52,"receptor/keymap":11,"receptor/once":12}],25:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const MASKED_CLASS = `${PREFIX}-masked`;
|
||
|
|
const MASKED = `.${MASKED_CLASS}`;
|
||
|
|
const MASK = `${PREFIX}-input-mask`;
|
||
|
|
const MASK_CONTENT = `${MASK}--content`;
|
||
|
|
const PLACEHOLDER = "placeholder";
|
||
|
|
const CONTEXT = "form";
|
||
|
|
|
||
|
|
// User defined Values
|
||
|
|
const maskedNumber = "_#dDmMyY9";
|
||
|
|
const maskedLetter = "A";
|
||
|
|
|
||
|
|
// replaces each masked input with a shell containing the input and it's mask.
|
||
|
|
const createMaskedInputShell = input => {
|
||
|
|
const placeholder = input.getAttribute(`${PLACEHOLDER}`);
|
||
|
|
if (placeholder) {
|
||
|
|
input.setAttribute("maxlength", placeholder.length);
|
||
|
|
input.setAttribute("data-placeholder", placeholder);
|
||
|
|
input.removeAttribute(`${PLACEHOLDER}`);
|
||
|
|
} else {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const shell = document.createElement("span");
|
||
|
|
shell.classList.add(MASK);
|
||
|
|
shell.setAttribute("data-mask", placeholder);
|
||
|
|
const content = document.createElement("span");
|
||
|
|
content.classList.add(MASK_CONTENT);
|
||
|
|
content.setAttribute("aria-hidden", "true");
|
||
|
|
content.id = `${input.id}Mask`;
|
||
|
|
content.textContent = placeholder;
|
||
|
|
shell.appendChild(content);
|
||
|
|
input.closest(CONTEXT).insertBefore(shell, input);
|
||
|
|
shell.appendChild(input);
|
||
|
|
};
|
||
|
|
const setValueOfMask = el => {
|
||
|
|
const {
|
||
|
|
value
|
||
|
|
} = el;
|
||
|
|
const placeholderVal = `${el.dataset.placeholder.substr(value.length)}`;
|
||
|
|
const theIEl = document.createElement("i");
|
||
|
|
theIEl.textContent = value;
|
||
|
|
return [theIEl, placeholderVal];
|
||
|
|
};
|
||
|
|
const strippedValue = (isCharsetPresent, value) => isCharsetPresent ? value.replace(/\W/g, "") : value.replace(/\D/g, "");
|
||
|
|
const isInteger = value => !Number.isNaN(parseInt(value, 10));
|
||
|
|
const isLetter = value => value ? value.match(/[A-Z]/i) : false;
|
||
|
|
const handleCurrentValue = el => {
|
||
|
|
const isCharsetPresent = el.dataset.charset;
|
||
|
|
const placeholder = isCharsetPresent || el.dataset.placeholder;
|
||
|
|
const {
|
||
|
|
value
|
||
|
|
} = el;
|
||
|
|
const len = placeholder.length;
|
||
|
|
let newValue = "";
|
||
|
|
let i;
|
||
|
|
let charIndex;
|
||
|
|
const strippedVal = strippedValue(isCharsetPresent, value);
|
||
|
|
for (i = 0, charIndex = 0; i < len; i += 1) {
|
||
|
|
const isInt = isInteger(strippedVal[charIndex]);
|
||
|
|
const isLet = isLetter(strippedVal[charIndex]);
|
||
|
|
const matchesNumber = maskedNumber.indexOf(placeholder[i]) >= 0;
|
||
|
|
const matchesLetter = maskedLetter.indexOf(placeholder[i]) >= 0;
|
||
|
|
if (matchesNumber && isInt || isCharsetPresent && matchesLetter && isLet) {
|
||
|
|
newValue += strippedVal[charIndex];
|
||
|
|
charIndex += 1;
|
||
|
|
} else if (!isCharsetPresent && !isInt && matchesNumber || isCharsetPresent && (matchesLetter && !isLet || matchesNumber && !isInt)) {
|
||
|
|
return newValue;
|
||
|
|
} else {
|
||
|
|
newValue += placeholder[i];
|
||
|
|
}
|
||
|
|
// break if no characters left and the pattern is non-special character
|
||
|
|
if (strippedVal[charIndex] === undefined) {
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return newValue;
|
||
|
|
};
|
||
|
|
const handleValueChange = el => {
|
||
|
|
const inputEl = el;
|
||
|
|
const id = inputEl.getAttribute("id");
|
||
|
|
inputEl.value = handleCurrentValue(inputEl);
|
||
|
|
const maskVal = setValueOfMask(el);
|
||
|
|
const maskEl = document.getElementById(`${id}Mask`);
|
||
|
|
maskEl.textContent = "";
|
||
|
|
maskEl.replaceChildren(maskVal[0], maskVal[1]);
|
||
|
|
};
|
||
|
|
const inputMaskEvents = {
|
||
|
|
keyup: {
|
||
|
|
[MASKED]() {
|
||
|
|
handleValueChange(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
const inputMask = behavior(inputMaskEvents, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(MASKED, root).forEach(maskedInput => {
|
||
|
|
createMaskedInputShell(maskedInput);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
module.exports = inputMask;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/select-or-matches":52}],26:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const select = require("../../uswds-core/src/js/utils/select");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const CONTAINER = `.${PREFIX}-input-group`;
|
||
|
|
const INPUT = `${CONTAINER} .${PREFIX}-input`;
|
||
|
|
const DECORATION = `${CONTAINER} .${PREFIX}-input-prefix, ${CONTAINER} .${PREFIX}-input-suffix`;
|
||
|
|
const FOCUS_CLASS = "is-focused";
|
||
|
|
function setFocus(el) {
|
||
|
|
el.closest(CONTAINER).querySelector(`.${PREFIX}-input`).focus();
|
||
|
|
}
|
||
|
|
function handleFocus() {
|
||
|
|
this.closest(CONTAINER).classList.add(FOCUS_CLASS);
|
||
|
|
}
|
||
|
|
function handleBlur() {
|
||
|
|
this.closest(CONTAINER).classList.remove(FOCUS_CLASS);
|
||
|
|
}
|
||
|
|
const inputPrefixSuffix = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[DECORATION]() {
|
||
|
|
setFocus(this);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
select(INPUT, root).forEach(inputEl => {
|
||
|
|
inputEl.addEventListener("focus", handleFocus, false);
|
||
|
|
inputEl.addEventListener("blur", handleBlur, false);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
module.exports = inputPrefixSuffix;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/select":53}],27:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const keymap = require("receptor/keymap");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const toggle = require("../../uswds-core/src/js/utils/toggle");
|
||
|
|
const FocusTrap = require("../../uswds-core/src/js/utils/focus-trap");
|
||
|
|
const accordion = require("../../usa-accordion/src/index");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const BODY = "body";
|
||
|
|
const LANGUAGE = `.${PREFIX}-language`;
|
||
|
|
const LANGUAGE_SUB = `.${PREFIX}-language__submenu`;
|
||
|
|
const LANGUAGE_PRIMARY = `.${PREFIX}-language__primary`;
|
||
|
|
const LANGUAGE_PRIMARY_ITEM = `.${PREFIX}-language__primary-item`;
|
||
|
|
const LANGUAGE_CONTROL = `button.${PREFIX}-language__link`;
|
||
|
|
const LANGUAGE_LINKS = `${LANGUAGE} a`;
|
||
|
|
let languageSelector;
|
||
|
|
let languageActive;
|
||
|
|
const onLanguageClose = () => languageSelector.toggleLanguage.call(languageSelector, false);
|
||
|
|
const hideActiveLanguageDropdown = () => {
|
||
|
|
if (!languageActive) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
toggle(languageActive, false);
|
||
|
|
languageActive = null;
|
||
|
|
};
|
||
|
|
const focusLanguageButton = event => {
|
||
|
|
const parentLanguageItem = event.target.closest(LANGUAGE_PRIMARY_ITEM);
|
||
|
|
if (!event.target.matches(LANGUAGE_CONTROL)) {
|
||
|
|
parentLanguageItem.querySelector(LANGUAGE_CONTROL).focus();
|
||
|
|
}
|
||
|
|
};
|
||
|
|
const handleEscape = event => {
|
||
|
|
hideActiveLanguageDropdown();
|
||
|
|
focusLanguageButton(event);
|
||
|
|
};
|
||
|
|
languageSelector = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[LANGUAGE_CONTROL]() {
|
||
|
|
if (languageActive !== this) {
|
||
|
|
hideActiveLanguageDropdown();
|
||
|
|
}
|
||
|
|
if (languageActive === this) {
|
||
|
|
hideActiveLanguageDropdown();
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
if (!languageActive) {
|
||
|
|
languageActive = this;
|
||
|
|
toggle(languageActive, true);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
},
|
||
|
|
[BODY]: hideActiveLanguageDropdown,
|
||
|
|
[LANGUAGE_LINKS]() {
|
||
|
|
const acc = this.closest(accordion.ACCORDION);
|
||
|
|
if (acc) {
|
||
|
|
accordion.getButtons(acc).forEach(btn => accordion.hide(btn));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
keydown: {
|
||
|
|
[LANGUAGE_PRIMARY]: keymap({
|
||
|
|
Escape: handleEscape
|
||
|
|
})
|
||
|
|
},
|
||
|
|
focusout: {
|
||
|
|
[LANGUAGE_PRIMARY](event) {
|
||
|
|
const language = event.target.closest(LANGUAGE_PRIMARY);
|
||
|
|
if (!language.contains(event.relatedTarget)) {
|
||
|
|
hideActiveLanguageDropdown();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
const trapContainer = root.matches(LANGUAGE_SUB) ? root : root.querySelector(LANGUAGE_SUB);
|
||
|
|
if (trapContainer) {
|
||
|
|
languageSelector.focusTrap = FocusTrap(trapContainer, {
|
||
|
|
Escape: onLanguageClose
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
teardown() {
|
||
|
|
languageActive = false;
|
||
|
|
},
|
||
|
|
focusTrap: null
|
||
|
|
});
|
||
|
|
module.exports = languageSelector;
|
||
|
|
|
||
|
|
},{"../../usa-accordion/src/index":15,"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/focus-trap":47,"../../uswds-core/src/js/utils/toggle":56,"receptor/keymap":11}],28:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const FocusTrap = require("../../uswds-core/src/js/utils/focus-trap");
|
||
|
|
const ScrollBarWidth = require("../../uswds-core/src/js/utils/scrollbar-width");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const MODAL_CLASSNAME = `${PREFIX}-modal`;
|
||
|
|
const OVERLAY_CLASSNAME = `${MODAL_CLASSNAME}-overlay`;
|
||
|
|
const WRAPPER_CLASSNAME = `${MODAL_CLASSNAME}-wrapper`;
|
||
|
|
const OPENER_ATTRIBUTE = "data-open-modal";
|
||
|
|
const CLOSER_ATTRIBUTE = "data-close-modal";
|
||
|
|
const FORCE_ACTION_ATTRIBUTE = "data-force-action";
|
||
|
|
const NON_MODAL_HIDDEN_ATTRIBUTE = `data-modal-hidden`;
|
||
|
|
const MODAL = `.${MODAL_CLASSNAME}`;
|
||
|
|
const INITIAL_FOCUS = `.${WRAPPER_CLASSNAME} *[data-focus]`;
|
||
|
|
const CLOSE_BUTTON = `${WRAPPER_CLASSNAME} *[${CLOSER_ATTRIBUTE}]`;
|
||
|
|
const OPENERS = `*[${OPENER_ATTRIBUTE}][aria-controls]`;
|
||
|
|
const CLOSERS = `${CLOSE_BUTTON}, .${OVERLAY_CLASSNAME}:not([${FORCE_ACTION_ATTRIBUTE}])`;
|
||
|
|
const NON_MODALS = `body > *:not(.${WRAPPER_CLASSNAME}):not([aria-hidden])`;
|
||
|
|
const NON_MODALS_HIDDEN = `[${NON_MODAL_HIDDEN_ATTRIBUTE}]`;
|
||
|
|
const ACTIVE_CLASS = "usa-js-modal--active";
|
||
|
|
const PREVENT_CLICK_CLASS = "usa-js-no-click";
|
||
|
|
const VISIBLE_CLASS = "is-visible";
|
||
|
|
const HIDDEN_CLASS = "is-hidden";
|
||
|
|
let modal;
|
||
|
|
const isActive = () => document.body.classList.contains(ACTIVE_CLASS);
|
||
|
|
const SCROLLBAR_WIDTH = ScrollBarWidth();
|
||
|
|
const INITIAL_PADDING = window.getComputedStyle(document.body).getPropertyValue("padding-right");
|
||
|
|
const TEMPORARY_PADDING = `${parseInt(INITIAL_PADDING.replace(/px/, ""), 10) + parseInt(SCROLLBAR_WIDTH.replace(/px/, ""), 10)}px`;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Is bound to escape key, closes modal when
|
||
|
|
*/
|
||
|
|
const onMenuClose = () => {
|
||
|
|
modal.toggleModal.call(modal, false);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Toggle the visibility of a modal window
|
||
|
|
*
|
||
|
|
* @param {KeyboardEvent} event the keydown event
|
||
|
|
* @returns {boolean} safeActive if mobile is open
|
||
|
|
*/
|
||
|
|
function toggleModal(event) {
|
||
|
|
let originalOpener;
|
||
|
|
let clickedElement = event.target;
|
||
|
|
const {
|
||
|
|
body
|
||
|
|
} = document;
|
||
|
|
const safeActive = !isActive();
|
||
|
|
const modalId = clickedElement ? clickedElement.getAttribute("aria-controls") : document.querySelector(".usa-modal-wrapper.is-visible");
|
||
|
|
const targetModal = safeActive ? document.getElementById(modalId) : document.querySelector(".usa-modal-wrapper.is-visible");
|
||
|
|
|
||
|
|
// if there is no modal we return early
|
||
|
|
if (!targetModal) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
const openFocusEl = targetModal.querySelector(INITIAL_FOCUS) ? targetModal.querySelector(INITIAL_FOCUS) : targetModal.querySelector(".usa-modal");
|
||
|
|
const returnFocus = document.getElementById(targetModal.getAttribute("data-opener"));
|
||
|
|
const menuButton = body.querySelector(OPENERS);
|
||
|
|
const forceUserAction = targetModal.getAttribute(FORCE_ACTION_ATTRIBUTE);
|
||
|
|
|
||
|
|
// Sets the clicked element to the close button
|
||
|
|
// so esc key always closes modal
|
||
|
|
if (event.type === "keydown" && targetModal !== null) {
|
||
|
|
clickedElement = targetModal.querySelector(CLOSE_BUTTON);
|
||
|
|
}
|
||
|
|
|
||
|
|
// When we're not hitting the escape key…
|
||
|
|
if (clickedElement) {
|
||
|
|
// Make sure we click the opener
|
||
|
|
// If it doesn't have an ID, make one
|
||
|
|
// Store id as data attribute on modal
|
||
|
|
if (clickedElement.hasAttribute(OPENER_ATTRIBUTE)) {
|
||
|
|
if (this.getAttribute("id") === null) {
|
||
|
|
originalOpener = `modal-${Math.floor(Math.random() * 900000) + 100000}`;
|
||
|
|
this.setAttribute("id", originalOpener);
|
||
|
|
} else {
|
||
|
|
originalOpener = this.getAttribute("id");
|
||
|
|
}
|
||
|
|
targetModal.setAttribute("data-opener", originalOpener);
|
||
|
|
}
|
||
|
|
|
||
|
|
// This basically stops the propagation if the element
|
||
|
|
// is inside the modal and not a close button or
|
||
|
|
// element inside a close button
|
||
|
|
if (clickedElement.closest(`.${MODAL_CLASSNAME}`)) {
|
||
|
|
if (clickedElement.hasAttribute(CLOSER_ATTRIBUTE) || clickedElement.closest(`[${CLOSER_ATTRIBUTE}]`)) {
|
||
|
|
// do nothing. move on.
|
||
|
|
} else {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
body.classList.toggle(ACTIVE_CLASS, safeActive);
|
||
|
|
targetModal.classList.toggle(VISIBLE_CLASS, safeActive);
|
||
|
|
targetModal.classList.toggle(HIDDEN_CLASS, !safeActive);
|
||
|
|
|
||
|
|
// If user is forced to take an action, adding
|
||
|
|
// a class to the body that prevents clicking underneath
|
||
|
|
// overlay
|
||
|
|
if (forceUserAction) {
|
||
|
|
body.classList.toggle(PREVENT_CLICK_CLASS, safeActive);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Account for content shifting from body overflow: hidden
|
||
|
|
// We only check paddingRight in case apps are adding other properties
|
||
|
|
// to the body element
|
||
|
|
body.style.paddingRight = body.style.paddingRight === TEMPORARY_PADDING ? INITIAL_PADDING : TEMPORARY_PADDING;
|
||
|
|
|
||
|
|
// Handle the focus actions
|
||
|
|
if (safeActive && openFocusEl) {
|
||
|
|
// The modal window is opened. Focus is set to close button.
|
||
|
|
|
||
|
|
// Binds escape key if we're not forcing
|
||
|
|
// the user to take an action
|
||
|
|
if (forceUserAction) {
|
||
|
|
modal.focusTrap = FocusTrap(targetModal);
|
||
|
|
} else {
|
||
|
|
modal.focusTrap = FocusTrap(targetModal, {
|
||
|
|
Escape: onMenuClose
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handles focus setting and interactions
|
||
|
|
modal.focusTrap.update(safeActive);
|
||
|
|
openFocusEl.focus();
|
||
|
|
|
||
|
|
// Hides everything that is not the modal from screen readers
|
||
|
|
document.querySelectorAll(NON_MODALS).forEach(nonModal => {
|
||
|
|
nonModal.setAttribute("aria-hidden", "true");
|
||
|
|
nonModal.setAttribute(NON_MODAL_HIDDEN_ATTRIBUTE, "");
|
||
|
|
});
|
||
|
|
} else if (!safeActive && menuButton && returnFocus) {
|
||
|
|
// The modal window is closed.
|
||
|
|
// Non-modals now accesible to screen reader
|
||
|
|
document.querySelectorAll(NON_MODALS_HIDDEN).forEach(nonModal => {
|
||
|
|
nonModal.removeAttribute("aria-hidden");
|
||
|
|
nonModal.removeAttribute(NON_MODAL_HIDDEN_ATTRIBUTE);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Focus is returned to the opener
|
||
|
|
returnFocus.focus();
|
||
|
|
modal.focusTrap.update(safeActive);
|
||
|
|
}
|
||
|
|
return safeActive;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Builds modal window from base HTML
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} baseComponent the modal html in the DOM
|
||
|
|
*/
|
||
|
|
const setUpModal = baseComponent => {
|
||
|
|
const modalContent = baseComponent;
|
||
|
|
const modalWrapper = document.createElement("div");
|
||
|
|
const overlayDiv = document.createElement("div");
|
||
|
|
const modalID = baseComponent.getAttribute("id");
|
||
|
|
const ariaLabelledBy = baseComponent.getAttribute("aria-labelledby");
|
||
|
|
const ariaDescribedBy = baseComponent.getAttribute("aria-describedby");
|
||
|
|
const forceUserAction = baseComponent.hasAttribute(FORCE_ACTION_ATTRIBUTE) ? baseComponent.hasAttribute(FORCE_ACTION_ATTRIBUTE) : false;
|
||
|
|
// Create placeholder where modal is for cleanup
|
||
|
|
const originalLocationPlaceHolder = document.createElement("div");
|
||
|
|
originalLocationPlaceHolder.setAttribute(`data-placeholder-for`, modalID);
|
||
|
|
originalLocationPlaceHolder.style.display = "none";
|
||
|
|
originalLocationPlaceHolder.setAttribute("aria-hidden", "true");
|
||
|
|
for (let attributeIndex = 0; attributeIndex < modalContent.attributes.length; attributeIndex += 1) {
|
||
|
|
const attribute = modalContent.attributes[attributeIndex];
|
||
|
|
originalLocationPlaceHolder.setAttribute(`data-original-${attribute.name}`, attribute.value);
|
||
|
|
}
|
||
|
|
modalContent.after(originalLocationPlaceHolder);
|
||
|
|
|
||
|
|
// Rebuild the modal element
|
||
|
|
modalContent.parentNode.insertBefore(modalWrapper, modalContent);
|
||
|
|
modalWrapper.appendChild(modalContent);
|
||
|
|
modalContent.parentNode.insertBefore(overlayDiv, modalContent);
|
||
|
|
overlayDiv.appendChild(modalContent);
|
||
|
|
|
||
|
|
// Add classes and attributes
|
||
|
|
modalWrapper.classList.add(HIDDEN_CLASS);
|
||
|
|
modalWrapper.classList.add(WRAPPER_CLASSNAME);
|
||
|
|
overlayDiv.classList.add(OVERLAY_CLASSNAME);
|
||
|
|
|
||
|
|
// Set attributes
|
||
|
|
modalWrapper.setAttribute("role", "dialog");
|
||
|
|
modalWrapper.setAttribute("id", modalID);
|
||
|
|
if (ariaLabelledBy) {
|
||
|
|
modalWrapper.setAttribute("aria-labelledby", ariaLabelledBy);
|
||
|
|
}
|
||
|
|
if (ariaDescribedBy) {
|
||
|
|
modalWrapper.setAttribute("aria-describedby", ariaDescribedBy);
|
||
|
|
}
|
||
|
|
if (forceUserAction) {
|
||
|
|
modalWrapper.setAttribute(FORCE_ACTION_ATTRIBUTE, "true");
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update the base element HTML
|
||
|
|
baseComponent.removeAttribute("id");
|
||
|
|
baseComponent.removeAttribute("aria-labelledby");
|
||
|
|
baseComponent.removeAttribute("aria-describedby");
|
||
|
|
baseComponent.setAttribute("tabindex", "-1");
|
||
|
|
|
||
|
|
// Add aria-controls
|
||
|
|
const modalClosers = modalWrapper.querySelectorAll(CLOSERS);
|
||
|
|
modalClosers.forEach(el => {
|
||
|
|
el.setAttribute("aria-controls", modalID);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Move all modals to the end of the DOM. Doing this allows us to
|
||
|
|
// more easily find the elements to hide from screen readers
|
||
|
|
// when the modal is open.
|
||
|
|
document.body.appendChild(modalWrapper);
|
||
|
|
};
|
||
|
|
const cleanUpModal = baseComponent => {
|
||
|
|
const modalContent = baseComponent;
|
||
|
|
const modalWrapper = modalContent.parentElement.parentElement;
|
||
|
|
const modalID = modalWrapper.getAttribute("id");
|
||
|
|
const originalLocationPlaceHolder = document.querySelector(`[data-placeholder-for="${modalID}"]`);
|
||
|
|
if (originalLocationPlaceHolder) {
|
||
|
|
for (let attributeIndex = 0; attributeIndex < originalLocationPlaceHolder.attributes.length; attributeIndex += 1) {
|
||
|
|
const attribute = originalLocationPlaceHolder.attributes[attributeIndex];
|
||
|
|
if (attribute.name.startsWith("data-original-")) {
|
||
|
|
// data-original- is 14 long
|
||
|
|
modalContent.setAttribute(attribute.name.substr(14), attribute.value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
originalLocationPlaceHolder.after(modalContent);
|
||
|
|
originalLocationPlaceHolder.parentElement.removeChild(originalLocationPlaceHolder);
|
||
|
|
}
|
||
|
|
modalWrapper.parentElement.removeChild(modalWrapper);
|
||
|
|
};
|
||
|
|
modal = {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(MODAL, root).forEach(modalWindow => {
|
||
|
|
const modalId = modalWindow.id;
|
||
|
|
setUpModal(modalWindow);
|
||
|
|
|
||
|
|
// this will query all openers and closers including the overlay
|
||
|
|
document.querySelectorAll(`[aria-controls="${modalId}"]`).forEach(item => {
|
||
|
|
// Turn anchor links into buttons because of
|
||
|
|
// VoiceOver on Safari
|
||
|
|
if (item.nodeName === "A") {
|
||
|
|
item.setAttribute("role", "button");
|
||
|
|
item.addEventListener("click", e => e.preventDefault());
|
||
|
|
}
|
||
|
|
|
||
|
|
// Can uncomment when aria-haspopup="dialog" is supported
|
||
|
|
// https://a11ysupport.io/tech/aria/aria-haspopup_attribute
|
||
|
|
// Most screen readers support aria-haspopup, but might announce
|
||
|
|
// as opening a menu if "dialog" is not supported.
|
||
|
|
// item.setAttribute("aria-haspopup", "dialog");
|
||
|
|
|
||
|
|
item.addEventListener("click", toggleModal);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
},
|
||
|
|
teardown(root) {
|
||
|
|
selectOrMatches(MODAL, root).forEach(modalWindow => {
|
||
|
|
cleanUpModal(modalWindow);
|
||
|
|
const modalId = modalWindow.id;
|
||
|
|
document.querySelectorAll(`[aria-controls="${modalId}"]`).forEach(item => item.removeEventListener("click", toggleModal));
|
||
|
|
});
|
||
|
|
},
|
||
|
|
focusTrap: null,
|
||
|
|
toggleModal,
|
||
|
|
on(root) {
|
||
|
|
this.init(root);
|
||
|
|
},
|
||
|
|
off(root) {
|
||
|
|
this.teardown(root);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
module.exports = modal;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/utils/focus-trap":47,"../../uswds-core/src/js/utils/scrollbar-width":51,"../../uswds-core/src/js/utils/select-or-matches":52}],29:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const ignore = require("receptor/ignore");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const select = require("../../uswds-core/src/js/utils/select");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const BUTTON = ".js-search-button";
|
||
|
|
const FORM = ".js-search-form";
|
||
|
|
const INPUT = "[type=search]";
|
||
|
|
const CONTEXT = "header"; // XXX
|
||
|
|
|
||
|
|
let lastButton;
|
||
|
|
const getForm = button => {
|
||
|
|
const context = button.closest(CONTEXT);
|
||
|
|
return context ? context.querySelector(FORM) : document.querySelector(FORM);
|
||
|
|
};
|
||
|
|
const toggleSearch = (button, active) => {
|
||
|
|
const form = getForm(button);
|
||
|
|
if (!form) {
|
||
|
|
throw new Error(`No ${FORM} found for search toggle in ${CONTEXT}!`);
|
||
|
|
}
|
||
|
|
|
||
|
|
/* eslint-disable no-param-reassign */
|
||
|
|
button.hidden = active;
|
||
|
|
form.hidden = !active;
|
||
|
|
/* eslint-enable */
|
||
|
|
|
||
|
|
if (!active) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const input = form.querySelector(INPUT);
|
||
|
|
if (input) {
|
||
|
|
input.focus();
|
||
|
|
}
|
||
|
|
// when the user clicks _outside_ of the form w/ignore(): hide the
|
||
|
|
// search, then remove the listener
|
||
|
|
const listener = ignore(form, () => {
|
||
|
|
if (lastButton) {
|
||
|
|
hideSearch.call(lastButton); // eslint-disable-line no-use-before-define
|
||
|
|
}
|
||
|
|
|
||
|
|
document.body.removeEventListener(CLICK, listener);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Normally we would just run this code without a timeout, but
|
||
|
|
// IE11 and Edge will actually call the listener *immediately* because
|
||
|
|
// they are currently handling this exact type of event, so we'll
|
||
|
|
// make sure the browser is done handling the current click event,
|
||
|
|
// if any, before we attach the listener.
|
||
|
|
setTimeout(() => {
|
||
|
|
document.body.addEventListener(CLICK, listener);
|
||
|
|
}, 0);
|
||
|
|
};
|
||
|
|
function showSearch() {
|
||
|
|
toggleSearch(this, true);
|
||
|
|
lastButton = this;
|
||
|
|
}
|
||
|
|
function hideSearch() {
|
||
|
|
toggleSearch(this, false);
|
||
|
|
lastButton = undefined;
|
||
|
|
}
|
||
|
|
const search = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[BUTTON]: showSearch
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(target) {
|
||
|
|
select(BUTTON, target).forEach(button => {
|
||
|
|
toggleSearch(button, false);
|
||
|
|
});
|
||
|
|
},
|
||
|
|
teardown() {
|
||
|
|
// forget the last button clicked
|
||
|
|
lastButton = undefined;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
module.exports = search;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/select":53,"receptor/ignore":9}],30:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const once = require("receptor/once");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const LINK = `.${PREFIX}-skipnav[href^="#"], .${PREFIX}-footer__return-to-top [href^="#"]`;
|
||
|
|
const MAINCONTENT = "main-content";
|
||
|
|
function setTabindex() {
|
||
|
|
// NB: we know because of the selector we're delegating to below that the
|
||
|
|
// href already begins with '#'
|
||
|
|
const id = encodeURI(this.getAttribute("href"));
|
||
|
|
const target = document.getElementById(id === "#" ? MAINCONTENT : id.slice(1));
|
||
|
|
if (target) {
|
||
|
|
target.style.outline = "0";
|
||
|
|
target.setAttribute("tabindex", 0);
|
||
|
|
target.focus();
|
||
|
|
target.addEventListener("blur", once(() => {
|
||
|
|
target.setAttribute("tabindex", -1);
|
||
|
|
}));
|
||
|
|
} else {
|
||
|
|
// throw an error?
|
||
|
|
}
|
||
|
|
}
|
||
|
|
module.exports = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[LINK]: setTabindex
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"receptor/once":12}],31:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const select = require("../../uswds-core/src/js/utils/select");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const {
|
||
|
|
CLICK
|
||
|
|
} = require("../../uswds-core/src/js/events");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const Sanitizer = require("../../uswds-core/src/js/utils/sanitizer");
|
||
|
|
const TABLE = `.${PREFIX}-table`;
|
||
|
|
const SORTED = "aria-sort";
|
||
|
|
const ASCENDING = "ascending";
|
||
|
|
const DESCENDING = "descending";
|
||
|
|
const SORT_OVERRIDE = "data-sort-value";
|
||
|
|
const SORT_BUTTON_CLASS = `${PREFIX}-table__header__button`;
|
||
|
|
const SORT_BUTTON = `.${SORT_BUTTON_CLASS}`;
|
||
|
|
const SORTABLE_HEADER = `th[data-sortable]`;
|
||
|
|
const ANNOUNCEMENT_REGION = `.${PREFIX}-table__announcement-region[aria-live="polite"]`;
|
||
|
|
|
||
|
|
/** Gets the data-sort-value attribute value, if provided — otherwise, gets
|
||
|
|
* the innerText or textContent — of the child element (HTMLTableCellElement)
|
||
|
|
* at the specified index of the given table row
|
||
|
|
*
|
||
|
|
* @param {number} index
|
||
|
|
* @param {array<HTMLTableRowElement>} tr
|
||
|
|
* @return {boolean}
|
||
|
|
*/
|
||
|
|
const getCellValue = (tr, index) => tr.children[index].getAttribute(SORT_OVERRIDE) || tr.children[index].innerText || tr.children[index].textContent;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Compares the values of two row array items at the given index, then sorts by the given direction
|
||
|
|
* @param {number} index
|
||
|
|
* @param {string} direction
|
||
|
|
* @return {boolean}
|
||
|
|
*/
|
||
|
|
const compareFunction = (index, isAscending) => (thisRow, nextRow) => {
|
||
|
|
// get values to compare from data attribute or cell content
|
||
|
|
const value1 = getCellValue(isAscending ? thisRow : nextRow, index);
|
||
|
|
const value2 = getCellValue(isAscending ? nextRow : thisRow, index);
|
||
|
|
|
||
|
|
// if neither value is empty, and if both values are already numbers, compare numerically
|
||
|
|
if (value1 && value2 && !Number.isNaN(Number(value1)) && !Number.isNaN(Number(value2))) {
|
||
|
|
return value1 - value2;
|
||
|
|
}
|
||
|
|
// Otherwise, compare alphabetically based on current user locale
|
||
|
|
return value1.toString().localeCompare(value2, navigator.language, {
|
||
|
|
numeric: true,
|
||
|
|
ignorePunctuation: true
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get an Array of column headers elements belonging directly to the given
|
||
|
|
* table element.
|
||
|
|
* @param {HTMLTableElement} table
|
||
|
|
* @return {array<HTMLTableHeaderCellElement>}
|
||
|
|
*/
|
||
|
|
const getColumnHeaders = table => {
|
||
|
|
const headers = select(SORTABLE_HEADER, table);
|
||
|
|
return headers.filter(header => header.closest(TABLE) === table);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the button label within the given header element, resetting it
|
||
|
|
* to the default state (ready to sort ascending) if it's no longer sorted
|
||
|
|
* @param {HTMLTableHeaderCellElement} header
|
||
|
|
*/
|
||
|
|
const updateSortLabel = header => {
|
||
|
|
const headerName = header.innerText;
|
||
|
|
const sortedAscending = header.getAttribute(SORTED) === ASCENDING;
|
||
|
|
const isSorted = header.getAttribute(SORTED) === ASCENDING || header.getAttribute(SORTED) === DESCENDING || false;
|
||
|
|
const headerLabel = `${headerName}', sortable column, currently ${isSorted ? `${sortedAscending ? `sorted ${ASCENDING}` : `sorted ${DESCENDING}`}` : "unsorted"}`;
|
||
|
|
const headerButtonLabel = `Click to sort by ${headerName} in ${sortedAscending ? DESCENDING : ASCENDING} order.`;
|
||
|
|
header.setAttribute("aria-label", headerLabel);
|
||
|
|
header.querySelector(SORT_BUTTON).setAttribute("title", headerButtonLabel);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove the aria-sort attribute on the given header element, and reset the label and button icon
|
||
|
|
* @param {HTMLTableHeaderCellElement} header
|
||
|
|
*/
|
||
|
|
const unsetSort = header => {
|
||
|
|
header.removeAttribute(SORTED);
|
||
|
|
updateSortLabel(header);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Sort rows either ascending or descending, based on a given header's aria-sort attribute
|
||
|
|
* @param {HTMLTableHeaderCellElement} header
|
||
|
|
* @param {boolean} isAscending
|
||
|
|
* @return {boolean} true
|
||
|
|
*/
|
||
|
|
const sortRows = (header, isAscending) => {
|
||
|
|
header.setAttribute(SORTED, isAscending === true ? DESCENDING : ASCENDING);
|
||
|
|
updateSortLabel(header);
|
||
|
|
const tbody = header.closest(TABLE).querySelector("tbody");
|
||
|
|
|
||
|
|
// We can use Array.from() and Array.sort() instead once we drop IE11 support, likely in the summer of 2021
|
||
|
|
//
|
||
|
|
// Array.from(tbody.querySelectorAll('tr').sort(
|
||
|
|
// compareFunction(
|
||
|
|
// Array.from(header.parentNode.children).indexOf(header),
|
||
|
|
// !isAscending)
|
||
|
|
// )
|
||
|
|
// .forEach(tr => tbody.appendChild(tr) );
|
||
|
|
|
||
|
|
// [].slice.call() turns array-like sets into true arrays so that we can sort them
|
||
|
|
const allRows = [].slice.call(tbody.querySelectorAll("tr"));
|
||
|
|
const allHeaders = [].slice.call(header.parentNode.children);
|
||
|
|
const thisHeaderIndex = allHeaders.indexOf(header);
|
||
|
|
allRows.sort(compareFunction(thisHeaderIndex, !isAscending)).forEach(tr => {
|
||
|
|
[].slice.call(tr.children).forEach(td => td.removeAttribute("data-sort-active"));
|
||
|
|
tr.children[thisHeaderIndex].setAttribute("data-sort-active", true);
|
||
|
|
tbody.appendChild(tr);
|
||
|
|
});
|
||
|
|
return true;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Update the live region immediately following the table whenever sort changes.
|
||
|
|
* @param {HTMLTableElement} table
|
||
|
|
* @param {HTMLTableHeaderCellElement} sortedHeader
|
||
|
|
*/
|
||
|
|
|
||
|
|
const updateLiveRegion = (table, sortedHeader) => {
|
||
|
|
const caption = table.querySelector("caption").innerText;
|
||
|
|
const sortedAscending = sortedHeader.getAttribute(SORTED) === ASCENDING;
|
||
|
|
const headerLabel = sortedHeader.innerText;
|
||
|
|
const liveRegion = table.nextElementSibling;
|
||
|
|
if (liveRegion && liveRegion.matches(ANNOUNCEMENT_REGION)) {
|
||
|
|
const sortAnnouncement = `The table named "${caption}" is now sorted by ${headerLabel} in ${sortedAscending ? ASCENDING : DESCENDING} order.`;
|
||
|
|
liveRegion.innerText = sortAnnouncement;
|
||
|
|
} else {
|
||
|
|
throw new Error(`Table containing a sortable column header is not followed by an aria-live region.`);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Toggle a header's sort state, optionally providing a target
|
||
|
|
* state.
|
||
|
|
*
|
||
|
|
* @param {HTMLTableHeaderCellElement} header
|
||
|
|
* @param {boolean?} isAscending If no state is provided, the current
|
||
|
|
* state will be toggled (from false to true, and vice-versa).
|
||
|
|
*/
|
||
|
|
const toggleSort = (header, isAscending) => {
|
||
|
|
const table = header.closest(TABLE);
|
||
|
|
let safeAscending = isAscending;
|
||
|
|
if (typeof safeAscending !== "boolean") {
|
||
|
|
safeAscending = header.getAttribute(SORTED) === ASCENDING;
|
||
|
|
}
|
||
|
|
if (!table) {
|
||
|
|
throw new Error(`${SORTABLE_HEADER} is missing outer ${TABLE}`);
|
||
|
|
}
|
||
|
|
safeAscending = sortRows(header, isAscending);
|
||
|
|
if (safeAscending) {
|
||
|
|
getColumnHeaders(table).forEach(otherHeader => {
|
||
|
|
if (otherHeader !== header) {
|
||
|
|
unsetSort(otherHeader);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
updateLiveRegion(table, header);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
** Inserts a button with icon inside a sortable header
|
||
|
|
* @param {HTMLTableHeaderCellElement} header
|
||
|
|
*/
|
||
|
|
|
||
|
|
const createHeaderButton = header => {
|
||
|
|
const buttonEl = document.createElement("button");
|
||
|
|
buttonEl.setAttribute("tabindex", "0");
|
||
|
|
buttonEl.classList.add(SORT_BUTTON_CLASS);
|
||
|
|
// ICON_SOURCE
|
||
|
|
buttonEl.innerHTML = Sanitizer.escapeHTML`
|
||
|
|
<svg class="${PREFIX}-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
||
|
|
<g class="descending" fill="transparent">
|
||
|
|
<path d="M17 17L15.59 15.59L12.9999 18.17V2H10.9999V18.17L8.41 15.58L7 17L11.9999 22L17 17Z" />
|
||
|
|
</g>
|
||
|
|
<g class="ascending" fill="transparent">
|
||
|
|
<path transform="rotate(180, 12, 12)" d="M17 17L15.59 15.59L12.9999 18.17V2H10.9999V18.17L8.41 15.58L7 17L11.9999 22L17 17Z" />
|
||
|
|
</g>
|
||
|
|
<g class="unsorted" fill="transparent">
|
||
|
|
<polygon points="15.17 15 13 17.17 13 6.83 15.17 9 16.58 7.59 12 3 7.41 7.59 8.83 9 11 6.83 11 17.17 8.83 15 7.42 16.41 12 21 16.59 16.41 15.17 15"/>
|
||
|
|
</g>
|
||
|
|
</svg>
|
||
|
|
`;
|
||
|
|
header.appendChild(buttonEl);
|
||
|
|
updateSortLabel(header);
|
||
|
|
};
|
||
|
|
const table = behavior({
|
||
|
|
[CLICK]: {
|
||
|
|
[SORT_BUTTON](event) {
|
||
|
|
event.preventDefault();
|
||
|
|
toggleSort(event.target.closest(SORTABLE_HEADER), event.target.closest(SORTABLE_HEADER).getAttribute(SORTED) === ASCENDING);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
const sortableHeaders = select(SORTABLE_HEADER, root);
|
||
|
|
sortableHeaders.forEach(header => createHeaderButton(header));
|
||
|
|
const firstSorted = sortableHeaders.filter(header => header.getAttribute(SORTED) === ASCENDING || header.getAttribute(SORTED) === DESCENDING)[0];
|
||
|
|
if (typeof firstSorted === "undefined") {
|
||
|
|
// no sortable headers found
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const sortDir = firstSorted.getAttribute(SORTED);
|
||
|
|
if (sortDir === ASCENDING) {
|
||
|
|
toggleSort(firstSorted, true);
|
||
|
|
} else if (sortDir === DESCENDING) {
|
||
|
|
toggleSort(firstSorted, false);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
TABLE,
|
||
|
|
SORTABLE_HEADER,
|
||
|
|
SORT_BUTTON
|
||
|
|
});
|
||
|
|
module.exports = table;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/events":36,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/sanitizer":50,"../../uswds-core/src/js/utils/select":53}],32:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const {
|
||
|
|
COMBO_BOX_CLASS,
|
||
|
|
enhanceComboBox
|
||
|
|
} = require("../../usa-combo-box/src/index");
|
||
|
|
const TIME_PICKER_CLASS = `${PREFIX}-time-picker`;
|
||
|
|
const TIME_PICKER = `.${TIME_PICKER_CLASS}`;
|
||
|
|
const MAX_TIME = 60 * 24 - 1;
|
||
|
|
const MIN_TIME = 0;
|
||
|
|
const DEFAULT_STEP = 30;
|
||
|
|
const MIN_STEP = 1;
|
||
|
|
const FILTER_DATASET = {
|
||
|
|
filter: "0?{{ hourQueryFilter }}:{{minuteQueryFilter}}.*{{ apQueryFilter }}m?",
|
||
|
|
apQueryFilter: "([ap])",
|
||
|
|
hourQueryFilter: "([1-9][0-2]?)",
|
||
|
|
minuteQueryFilter: "[\\d]+:([0-9]{0,2})"
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Parse a string of hh:mm into minutes
|
||
|
|
*
|
||
|
|
* @param {string} timeStr the time string to parse
|
||
|
|
* @returns {number} the number of minutes
|
||
|
|
*/
|
||
|
|
const parseTimeString = timeStr => {
|
||
|
|
let minutes;
|
||
|
|
if (timeStr) {
|
||
|
|
const [hours, mins] = timeStr.split(":").map(str => {
|
||
|
|
let value;
|
||
|
|
const parsed = parseInt(str, 10);
|
||
|
|
if (!Number.isNaN(parsed)) value = parsed;
|
||
|
|
return value;
|
||
|
|
});
|
||
|
|
if (hours != null && mins != null) {
|
||
|
|
minutes = hours * 60 + mins;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return minutes;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Enhance an input with the date picker elements
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el The initial wrapping element of the date picker component
|
||
|
|
*/
|
||
|
|
const transformTimePicker = el => {
|
||
|
|
const timePickerEl = el.closest(TIME_PICKER);
|
||
|
|
const initialInputEl = timePickerEl.querySelector(`input`);
|
||
|
|
if (!initialInputEl) {
|
||
|
|
throw new Error(`${TIME_PICKER} is missing inner input`);
|
||
|
|
}
|
||
|
|
const selectEl = document.createElement("select");
|
||
|
|
["id", "name", "required", "aria-label", "aria-labelledby"].forEach(name => {
|
||
|
|
if (initialInputEl.hasAttribute(name)) {
|
||
|
|
const value = initialInputEl.getAttribute(name);
|
||
|
|
selectEl.setAttribute(name, value);
|
||
|
|
initialInputEl.removeAttribute(name);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
const padZeros = (value, length) => `0000${value}`.slice(-length);
|
||
|
|
const getTimeContext = minutes => {
|
||
|
|
const minute = minutes % 60;
|
||
|
|
const hour24 = Math.floor(minutes / 60);
|
||
|
|
const hour12 = hour24 % 12 || 12;
|
||
|
|
const ampm = hour24 < 12 ? "am" : "pm";
|
||
|
|
return {
|
||
|
|
minute,
|
||
|
|
hour24,
|
||
|
|
hour12,
|
||
|
|
ampm
|
||
|
|
};
|
||
|
|
};
|
||
|
|
const minTime = Math.max(MIN_TIME, parseTimeString(timePickerEl.dataset.minTime) || MIN_TIME);
|
||
|
|
const maxTime = Math.min(MAX_TIME, parseTimeString(timePickerEl.dataset.maxTime) || MAX_TIME);
|
||
|
|
const step = Math.floor(Math.max(MIN_STEP, timePickerEl.dataset.step || DEFAULT_STEP));
|
||
|
|
let defaultValue;
|
||
|
|
for (let time = minTime; time <= maxTime; time += step) {
|
||
|
|
const {
|
||
|
|
minute,
|
||
|
|
hour24,
|
||
|
|
hour12,
|
||
|
|
ampm
|
||
|
|
} = getTimeContext(time);
|
||
|
|
const option = document.createElement("option");
|
||
|
|
option.value = `${padZeros(hour24, 2)}:${padZeros(minute, 2)}`;
|
||
|
|
option.text = `${hour12}:${padZeros(minute, 2)}${ampm}`;
|
||
|
|
if (option.text === initialInputEl.value) {
|
||
|
|
defaultValue = option.value;
|
||
|
|
}
|
||
|
|
selectEl.appendChild(option);
|
||
|
|
}
|
||
|
|
timePickerEl.classList.add(COMBO_BOX_CLASS);
|
||
|
|
|
||
|
|
// combo box properties
|
||
|
|
Object.keys(FILTER_DATASET).forEach(key => {
|
||
|
|
timePickerEl.dataset[key] = FILTER_DATASET[key];
|
||
|
|
});
|
||
|
|
timePickerEl.dataset.disableFiltering = "true";
|
||
|
|
timePickerEl.dataset.defaultValue = defaultValue;
|
||
|
|
timePickerEl.appendChild(selectEl);
|
||
|
|
initialInputEl.remove();
|
||
|
|
};
|
||
|
|
const timePicker = behavior({}, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(TIME_PICKER, root).forEach(timePickerEl => {
|
||
|
|
transformTimePicker(timePickerEl);
|
||
|
|
enhanceComboBox(timePickerEl);
|
||
|
|
});
|
||
|
|
},
|
||
|
|
FILTER_DATASET
|
||
|
|
});
|
||
|
|
module.exports = timePicker;
|
||
|
|
|
||
|
|
},{"../../usa-combo-box/src/index":18,"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/select-or-matches":52}],33:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
// Tooltips
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const isElementInViewport = require("../../uswds-core/src/js/utils/is-in-viewport");
|
||
|
|
const TOOLTIP = `.${PREFIX}-tooltip`;
|
||
|
|
const TOOLTIP_TRIGGER = `.${PREFIX}-tooltip__trigger`;
|
||
|
|
const TOOLTIP_TRIGGER_CLASS = `${PREFIX}-tooltip__trigger`;
|
||
|
|
const TOOLTIP_CLASS = `${PREFIX}-tooltip`;
|
||
|
|
const TOOLTIP_BODY_CLASS = `${PREFIX}-tooltip__body`;
|
||
|
|
const SET_CLASS = "is-set";
|
||
|
|
const VISIBLE_CLASS = "is-visible";
|
||
|
|
const TRIANGLE_SIZE = 5;
|
||
|
|
const ADJUST_WIDTH_CLASS = `${PREFIX}-tooltip__body--wrap`;
|
||
|
|
|
||
|
|
/**
|
||
|
|
*
|
||
|
|
* @param {DOMElement} trigger - The tooltip trigger
|
||
|
|
* @returns {object} Elements for initialized tooltip; includes trigger, wrapper, and body
|
||
|
|
*/
|
||
|
|
const getTooltipElements = trigger => {
|
||
|
|
const wrapper = trigger.parentNode;
|
||
|
|
const body = wrapper.querySelector(`.${TOOLTIP_BODY_CLASS}`);
|
||
|
|
return {
|
||
|
|
trigger,
|
||
|
|
wrapper,
|
||
|
|
body
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Shows the tooltip
|
||
|
|
* @param {HTMLElement} tooltipTrigger - the element that initializes the tooltip
|
||
|
|
*/
|
||
|
|
const showToolTip = (tooltipBody, tooltipTrigger, position) => {
|
||
|
|
tooltipBody.setAttribute("aria-hidden", "false");
|
||
|
|
|
||
|
|
// This sets up the tooltip body. The opacity is 0, but
|
||
|
|
// we can begin running the calculations below.
|
||
|
|
tooltipBody.classList.add(SET_CLASS);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Position the tooltip body when the trigger is hovered
|
||
|
|
* Removes old positioning classnames and reapplies. This allows
|
||
|
|
* positioning to change in case the user resizes browser or DOM manipulation
|
||
|
|
* causes tooltip to get clipped from viewport
|
||
|
|
*
|
||
|
|
* @param {string} setPos - can be "top", "bottom", "right", "left"
|
||
|
|
*/
|
||
|
|
const setPositionClass = setPos => {
|
||
|
|
tooltipBody.classList.remove(`${TOOLTIP_BODY_CLASS}--top`);
|
||
|
|
tooltipBody.classList.remove(`${TOOLTIP_BODY_CLASS}--bottom`);
|
||
|
|
tooltipBody.classList.remove(`${TOOLTIP_BODY_CLASS}--right`);
|
||
|
|
tooltipBody.classList.remove(`${TOOLTIP_BODY_CLASS}--left`);
|
||
|
|
tooltipBody.classList.add(`${TOOLTIP_BODY_CLASS}--${setPos}`);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Removes old positioning styles. This allows
|
||
|
|
* re-positioning to change without inheriting other
|
||
|
|
* dynamic styles
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} e - this is the tooltip body
|
||
|
|
*/
|
||
|
|
const resetPositionStyles = e => {
|
||
|
|
// we don't override anything in the stylesheet when finding alt positions
|
||
|
|
e.style.top = null;
|
||
|
|
e.style.bottom = null;
|
||
|
|
e.style.right = null;
|
||
|
|
e.style.left = null;
|
||
|
|
e.style.margin = null;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* get margin offset calculations
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} target - this is the tooltip body
|
||
|
|
* @param {String} propertyValue - this is the tooltip body
|
||
|
|
*/
|
||
|
|
|
||
|
|
const offsetMargin = (target, propertyValue) => parseInt(window.getComputedStyle(target).getPropertyValue(propertyValue), 10);
|
||
|
|
|
||
|
|
// offsetLeft = the left position, and margin of the element, the left
|
||
|
|
// padding, scrollbar and border of the offsetParent element
|
||
|
|
// offsetWidth = The offsetWidth property returns the viewable width of an
|
||
|
|
// element in pixels, including padding, border and scrollbar, but not
|
||
|
|
// the margin.
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Calculate margin offset
|
||
|
|
* tooltip trigger margin(position) offset + tooltipBody offsetWidth
|
||
|
|
* @param {String} marginPosition
|
||
|
|
* @param {Number} tooltipBodyOffset
|
||
|
|
* @param {HTMLElement} trigger
|
||
|
|
*/
|
||
|
|
const calculateMarginOffset = (marginPosition, tooltipBodyOffset, trigger) => {
|
||
|
|
const offset = offsetMargin(trigger, `margin-${marginPosition}`) > 0 ? tooltipBodyOffset - offsetMargin(trigger, `margin-${marginPosition}`) : tooltipBodyOffset;
|
||
|
|
return offset;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Positions tooltip at the top
|
||
|
|
* @param {HTMLElement} e - this is the tooltip body
|
||
|
|
*/
|
||
|
|
const positionTop = e => {
|
||
|
|
resetPositionStyles(e); // ensures we start from the same point
|
||
|
|
// get details on the elements object with
|
||
|
|
|
||
|
|
const topMargin = calculateMarginOffset("top", e.offsetHeight, tooltipTrigger);
|
||
|
|
const leftMargin = calculateMarginOffset("left", e.offsetWidth, tooltipTrigger);
|
||
|
|
setPositionClass("top");
|
||
|
|
e.style.left = `50%`; // center the element
|
||
|
|
e.style.top = `-${TRIANGLE_SIZE}px`; // consider the pseudo element
|
||
|
|
// apply our margins based on the offset
|
||
|
|
e.style.margin = `-${topMargin}px 0 0 -${leftMargin / 2}px`;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Positions tooltip at the bottom
|
||
|
|
* @param {HTMLElement} e - this is the tooltip body
|
||
|
|
*/
|
||
|
|
const positionBottom = e => {
|
||
|
|
resetPositionStyles(e);
|
||
|
|
const leftMargin = calculateMarginOffset("left", e.offsetWidth, tooltipTrigger);
|
||
|
|
setPositionClass("bottom");
|
||
|
|
e.style.left = `50%`;
|
||
|
|
e.style.margin = `${TRIANGLE_SIZE}px 0 0 -${leftMargin / 2}px`;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Positions tooltip at the right
|
||
|
|
* @param {HTMLElement} e - this is the tooltip body
|
||
|
|
*/
|
||
|
|
const positionRight = e => {
|
||
|
|
resetPositionStyles(e);
|
||
|
|
const topMargin = calculateMarginOffset("top", e.offsetHeight, tooltipTrigger);
|
||
|
|
setPositionClass("right");
|
||
|
|
e.style.top = `50%`;
|
||
|
|
e.style.left = `${tooltipTrigger.offsetLeft + tooltipTrigger.offsetWidth + TRIANGLE_SIZE}px`;
|
||
|
|
e.style.margin = `-${topMargin / 2}px 0 0 0`;
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Positions tooltip at the right
|
||
|
|
* @param {HTMLElement} e - this is the tooltip body
|
||
|
|
*/
|
||
|
|
const positionLeft = e => {
|
||
|
|
resetPositionStyles(e);
|
||
|
|
const topMargin = calculateMarginOffset("top", e.offsetHeight, tooltipTrigger);
|
||
|
|
|
||
|
|
// we have to check for some utility margins
|
||
|
|
const leftMargin = calculateMarginOffset("left", tooltipTrigger.offsetLeft > e.offsetWidth ? tooltipTrigger.offsetLeft - e.offsetWidth : e.offsetWidth, tooltipTrigger);
|
||
|
|
setPositionClass("left");
|
||
|
|
e.style.top = `50%`;
|
||
|
|
e.style.left = `-${TRIANGLE_SIZE}px`;
|
||
|
|
e.style.margin = `-${topMargin / 2}px 0 0 ${tooltipTrigger.offsetLeft > e.offsetWidth ? leftMargin : -leftMargin}px`; // adjust the margin
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* We try to set the position based on the
|
||
|
|
* original intention, but make adjustments
|
||
|
|
* if the element is clipped out of the viewport
|
||
|
|
* we constrain the width only as a last resort
|
||
|
|
* @param {HTMLElement} element(alias tooltipBody)
|
||
|
|
* @param {Number} attempt (--flag)
|
||
|
|
*/
|
||
|
|
|
||
|
|
const maxAttempts = 2;
|
||
|
|
function findBestPosition(element) {
|
||
|
|
let attempt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
||
|
|
// create array of optional positions
|
||
|
|
const positions = [positionTop, positionBottom, positionRight, positionLeft];
|
||
|
|
let hasVisiblePosition = false;
|
||
|
|
|
||
|
|
// we take a recursive approach
|
||
|
|
function tryPositions(i) {
|
||
|
|
if (i < positions.length) {
|
||
|
|
const pos = positions[i];
|
||
|
|
pos(element);
|
||
|
|
if (!isElementInViewport(element)) {
|
||
|
|
// eslint-disable-next-line no-param-reassign
|
||
|
|
tryPositions(i += 1);
|
||
|
|
} else {
|
||
|
|
hasVisiblePosition = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
tryPositions(0);
|
||
|
|
// if we can't find a position we compress it and try again
|
||
|
|
if (!hasVisiblePosition) {
|
||
|
|
element.classList.add(ADJUST_WIDTH_CLASS);
|
||
|
|
if (attempt <= maxAttempts) {
|
||
|
|
// eslint-disable-next-line no-param-reassign
|
||
|
|
findBestPosition(element, attempt += 1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
switch (position) {
|
||
|
|
case "top":
|
||
|
|
positionTop(tooltipBody);
|
||
|
|
if (!isElementInViewport(tooltipBody)) {
|
||
|
|
findBestPosition(tooltipBody);
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case "bottom":
|
||
|
|
positionBottom(tooltipBody);
|
||
|
|
if (!isElementInViewport(tooltipBody)) {
|
||
|
|
findBestPosition(tooltipBody);
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case "right":
|
||
|
|
positionRight(tooltipBody);
|
||
|
|
if (!isElementInViewport(tooltipBody)) {
|
||
|
|
findBestPosition(tooltipBody);
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case "left":
|
||
|
|
positionLeft(tooltipBody);
|
||
|
|
if (!isElementInViewport(tooltipBody)) {
|
||
|
|
findBestPosition(tooltipBody);
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
// skip default case
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Actually show the tooltip. The VISIBLE_CLASS
|
||
|
|
* will change the opacity to 1
|
||
|
|
*/
|
||
|
|
setTimeout(() => {
|
||
|
|
tooltipBody.classList.add(VISIBLE_CLASS);
|
||
|
|
}, 20);
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Removes all the properties to show and position the tooltip,
|
||
|
|
* and resets the tooltip position to the original intention
|
||
|
|
* in case the window is resized or the element is moved through
|
||
|
|
* DOM manipulation.
|
||
|
|
* @param {HTMLElement} tooltipBody - The body of the tooltip
|
||
|
|
*/
|
||
|
|
const hideToolTip = tooltipBody => {
|
||
|
|
tooltipBody.classList.remove(VISIBLE_CLASS);
|
||
|
|
tooltipBody.classList.remove(SET_CLASS);
|
||
|
|
tooltipBody.classList.remove(ADJUST_WIDTH_CLASS);
|
||
|
|
tooltipBody.setAttribute("aria-hidden", "true");
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Setup the tooltip component
|
||
|
|
* @param {HTMLElement} tooltipTrigger The element that creates the tooltip
|
||
|
|
*/
|
||
|
|
const setUpAttributes = tooltipTrigger => {
|
||
|
|
const tooltipID = `tooltip-${Math.floor(Math.random() * 900000) + 100000}`;
|
||
|
|
const tooltipContent = tooltipTrigger.getAttribute("title");
|
||
|
|
const wrapper = document.createElement("span");
|
||
|
|
const tooltipBody = document.createElement("span");
|
||
|
|
const position = tooltipTrigger.getAttribute("data-position") ? tooltipTrigger.getAttribute("data-position") : "top";
|
||
|
|
const additionalClasses = tooltipTrigger.getAttribute("data-classes");
|
||
|
|
|
||
|
|
// Set up tooltip attributes
|
||
|
|
tooltipTrigger.setAttribute("aria-describedby", tooltipID);
|
||
|
|
tooltipTrigger.setAttribute("tabindex", "0");
|
||
|
|
tooltipTrigger.removeAttribute("title");
|
||
|
|
tooltipTrigger.classList.remove(TOOLTIP_CLASS);
|
||
|
|
tooltipTrigger.classList.add(TOOLTIP_TRIGGER_CLASS);
|
||
|
|
|
||
|
|
// insert wrapper before el in the DOM tree
|
||
|
|
tooltipTrigger.parentNode.insertBefore(wrapper, tooltipTrigger);
|
||
|
|
|
||
|
|
// set up the wrapper
|
||
|
|
wrapper.appendChild(tooltipTrigger);
|
||
|
|
wrapper.classList.add(TOOLTIP_CLASS);
|
||
|
|
wrapper.appendChild(tooltipBody);
|
||
|
|
|
||
|
|
// Apply additional class names to wrapper element
|
||
|
|
if (additionalClasses) {
|
||
|
|
const classesArray = additionalClasses.split(" ");
|
||
|
|
classesArray.forEach(classname => wrapper.classList.add(classname));
|
||
|
|
}
|
||
|
|
|
||
|
|
// set up the tooltip body
|
||
|
|
tooltipBody.classList.add(TOOLTIP_BODY_CLASS);
|
||
|
|
tooltipBody.setAttribute("id", tooltipID);
|
||
|
|
tooltipBody.setAttribute("role", "tooltip");
|
||
|
|
tooltipBody.setAttribute("aria-hidden", "true");
|
||
|
|
|
||
|
|
// place the text in the tooltip
|
||
|
|
tooltipBody.textContent = tooltipContent;
|
||
|
|
return {
|
||
|
|
tooltipBody,
|
||
|
|
position,
|
||
|
|
tooltipContent,
|
||
|
|
wrapper
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
// Setup our function to run on various events
|
||
|
|
const tooltip = behavior({
|
||
|
|
"mouseover focusin": {
|
||
|
|
[TOOLTIP](e) {
|
||
|
|
const trigger = e.target;
|
||
|
|
const elementType = trigger.nodeName;
|
||
|
|
|
||
|
|
// Initialize tooltip if it hasn't already
|
||
|
|
if (elementType === "BUTTON" && trigger.hasAttribute("title")) {
|
||
|
|
setUpAttributes(trigger);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
[TOOLTIP_TRIGGER](e) {
|
||
|
|
const {
|
||
|
|
trigger,
|
||
|
|
body
|
||
|
|
} = getTooltipElements(e.target);
|
||
|
|
showToolTip(body, trigger, trigger.dataset.position);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
"mouseout focusout": {
|
||
|
|
[TOOLTIP_TRIGGER](e) {
|
||
|
|
const {
|
||
|
|
body
|
||
|
|
} = getTooltipElements(e.target);
|
||
|
|
hideToolTip(body);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(TOOLTIP, root).forEach(tooltipTrigger => {
|
||
|
|
setUpAttributes(tooltipTrigger);
|
||
|
|
});
|
||
|
|
},
|
||
|
|
setup: setUpAttributes,
|
||
|
|
getTooltipElements,
|
||
|
|
show: showToolTip,
|
||
|
|
hide: hideToolTip
|
||
|
|
});
|
||
|
|
module.exports = tooltip;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/is-in-viewport":48,"../../uswds-core/src/js/utils/select-or-matches":52}],34:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const behavior = require("../../uswds-core/src/js/utils/behavior");
|
||
|
|
const validate = require("../../uswds-core/src/js/utils/validate-input");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../../uswds-core/src/js/config");
|
||
|
|
const selectOrMatches = require("../../uswds-core/src/js/utils/select-or-matches");
|
||
|
|
const VALIDATE_INPUT = "input[data-validation-element]";
|
||
|
|
const CHECKLIST_ITEM = `.${PREFIX}-checklist__item`;
|
||
|
|
|
||
|
|
// Trigger validation on input change
|
||
|
|
const handleChange = el => validate(el);
|
||
|
|
|
||
|
|
// Create container to hold aria readout
|
||
|
|
const createStatusElement = input => {
|
||
|
|
const validationContainer = input.parentNode;
|
||
|
|
const inputID = input.getAttribute("id");
|
||
|
|
const statusSummaryID = `${inputID}-sr-summary`;
|
||
|
|
input.setAttribute("aria-describedby", statusSummaryID);
|
||
|
|
const statusSummaryContainer = document.createElement("span");
|
||
|
|
statusSummaryContainer.setAttribute("data-validation-status", "");
|
||
|
|
statusSummaryContainer.classList.add("usa-sr-only");
|
||
|
|
statusSummaryContainer.setAttribute("aria-live", "polite");
|
||
|
|
statusSummaryContainer.setAttribute("aria-atomic", true);
|
||
|
|
statusSummaryContainer.setAttribute("id", statusSummaryID);
|
||
|
|
validationContainer.append(statusSummaryContainer);
|
||
|
|
};
|
||
|
|
|
||
|
|
// Set up checklist items with initial aria-label (incomplete) values
|
||
|
|
const createInitialStatus = input => {
|
||
|
|
const validationContainer = input.parentNode;
|
||
|
|
const checklistItems = validationContainer.querySelectorAll(CHECKLIST_ITEM);
|
||
|
|
const validationElement = input.getAttribute("data-validation-element");
|
||
|
|
input.setAttribute("aria-controls", validationElement);
|
||
|
|
checklistItems.forEach(listItem => {
|
||
|
|
let currentStatus = "status incomplete";
|
||
|
|
if (input.hasAttribute("data-validation-incomplete")) {
|
||
|
|
currentStatus = input.getAttribute("data-validation-incomplete");
|
||
|
|
}
|
||
|
|
const itemStatus = `${listItem.textContent} ${currentStatus} `;
|
||
|
|
listItem.setAttribute("tabindex", "0");
|
||
|
|
listItem.setAttribute("aria-label", itemStatus);
|
||
|
|
});
|
||
|
|
};
|
||
|
|
const enhanceValidation = input => {
|
||
|
|
createStatusElement(input);
|
||
|
|
createInitialStatus(input);
|
||
|
|
};
|
||
|
|
const validator = behavior({
|
||
|
|
"input change": {
|
||
|
|
[VALIDATE_INPUT](event) {
|
||
|
|
handleChange(event.target);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
init(root) {
|
||
|
|
selectOrMatches(VALIDATE_INPUT, root).forEach(input => enhanceValidation(input));
|
||
|
|
}
|
||
|
|
});
|
||
|
|
module.exports = validator;
|
||
|
|
|
||
|
|
},{"../../uswds-core/src/js/config":35,"../../uswds-core/src/js/utils/behavior":45,"../../uswds-core/src/js/utils/select-or-matches":52,"../../uswds-core/src/js/utils/validate-input":57}],35:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
prefix: "usa"
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],36:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
module.exports = {
|
||
|
|
// This used to be conditionally dependent on whether the
|
||
|
|
// browser supported touch events; if it did, `CLICK` was set to
|
||
|
|
// `touchstart`. However, this had downsides:
|
||
|
|
//
|
||
|
|
// * It pre-empted mobile browsers' default behavior of detecting
|
||
|
|
// whether a touch turned into a scroll, thereby preventing
|
||
|
|
// users from using some of our components as scroll surfaces.
|
||
|
|
//
|
||
|
|
// * Some devices, such as the Microsoft Surface Pro, support *both*
|
||
|
|
// touch and clicks. This meant the conditional effectively dropped
|
||
|
|
// support for the user's mouse, frustrating users who preferred
|
||
|
|
// it on those systems.
|
||
|
|
CLICK: "click"
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],37:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const accordion = require("../../../usa-accordion/src/index");
|
||
|
|
const banner = require("../../../usa-banner/src/index");
|
||
|
|
const characterCount = require("../../../usa-character-count/src/index");
|
||
|
|
const comboBox = require("../../../usa-combo-box/src/index");
|
||
|
|
const datePicker = require("../../../usa-date-picker/src/index");
|
||
|
|
const dateRangePicker = require("../../../usa-date-range-picker/src/index");
|
||
|
|
const fileInput = require("../../../usa-file-input/src/index");
|
||
|
|
const footer = require("../../../usa-footer/src/index");
|
||
|
|
const inPageNavigation = require("../../../usa-in-page-navigation/src/index");
|
||
|
|
const inputMask = require("../../../usa-input-mask/src/index");
|
||
|
|
const inputPrefixSuffix = require("../../../usa-input-prefix-suffix/src/index");
|
||
|
|
const languageSelector = require("../../../usa-language-selector/src/index");
|
||
|
|
const modal = require("../../../usa-modal/src/index");
|
||
|
|
const navigation = require("../../../usa-header/src/index");
|
||
|
|
const password = require("../../../_usa-password/src/index");
|
||
|
|
const search = require("../../../usa-search/src/index");
|
||
|
|
const skipnav = require("../../../usa-skipnav/src/index");
|
||
|
|
const table = require("../../../usa-table/src/index");
|
||
|
|
const timePicker = require("../../../usa-time-picker/src/index");
|
||
|
|
const tooltip = require("../../../usa-tooltip/src/index");
|
||
|
|
const validator = require("../../../usa-validation/src/index");
|
||
|
|
module.exports = {
|
||
|
|
accordion,
|
||
|
|
banner,
|
||
|
|
characterCount,
|
||
|
|
comboBox,
|
||
|
|
datePicker,
|
||
|
|
dateRangePicker,
|
||
|
|
fileInput,
|
||
|
|
footer,
|
||
|
|
inPageNavigation,
|
||
|
|
inputMask,
|
||
|
|
inputPrefixSuffix,
|
||
|
|
languageSelector,
|
||
|
|
modal,
|
||
|
|
navigation,
|
||
|
|
password,
|
||
|
|
search,
|
||
|
|
skipnav,
|
||
|
|
table,
|
||
|
|
timePicker,
|
||
|
|
tooltip,
|
||
|
|
validator
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"../../../_usa-password/src/index":14,"../../../usa-accordion/src/index":15,"../../../usa-banner/src/index":16,"../../../usa-character-count/src/index":17,"../../../usa-combo-box/src/index":18,"../../../usa-date-picker/src/index":19,"../../../usa-date-range-picker/src/index":20,"../../../usa-file-input/src/index":21,"../../../usa-footer/src/index":22,"../../../usa-header/src/index":23,"../../../usa-in-page-navigation/src/index":24,"../../../usa-input-mask/src/index":25,"../../../usa-input-prefix-suffix/src/index":26,"../../../usa-language-selector/src/index":27,"../../../usa-modal/src/index":28,"../../../usa-search/src/index":29,"../../../usa-skipnav/src/index":30,"../../../usa-table/src/index":31,"../../../usa-time-picker/src/index":32,"../../../usa-tooltip/src/index":33,"../../../usa-validation/src/index":34}],38:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/* eslint-disable consistent-return */
|
||
|
|
/* eslint-disable func-names */
|
||
|
|
(function () {
|
||
|
|
if (typeof window.CustomEvent === "function") return false;
|
||
|
|
function CustomEvent(event, _params) {
|
||
|
|
const params = _params || {
|
||
|
|
bubbles: false,
|
||
|
|
cancelable: false,
|
||
|
|
detail: null
|
||
|
|
};
|
||
|
|
const evt = document.createEvent("CustomEvent");
|
||
|
|
evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
|
||
|
|
return evt;
|
||
|
|
}
|
||
|
|
window.CustomEvent = CustomEvent;
|
||
|
|
})();
|
||
|
|
|
||
|
|
},{}],39:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const elproto = window.HTMLElement.prototype;
|
||
|
|
const HIDDEN = "hidden";
|
||
|
|
if (!(HIDDEN in elproto)) {
|
||
|
|
Object.defineProperty(elproto, HIDDEN, {
|
||
|
|
get() {
|
||
|
|
return this.hasAttribute(HIDDEN);
|
||
|
|
},
|
||
|
|
set(value) {
|
||
|
|
if (value) {
|
||
|
|
this.setAttribute(HIDDEN, "");
|
||
|
|
} else {
|
||
|
|
this.removeAttribute(HIDDEN);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
},{}],40:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
// polyfills HTMLElement.prototype.classList and DOMTokenList
|
||
|
|
require("classlist-polyfill");
|
||
|
|
// polyfills HTMLElement.prototype.hidden
|
||
|
|
require("./element-hidden");
|
||
|
|
// polyfills Number.isNaN()
|
||
|
|
require("./number-is-nan");
|
||
|
|
// polyfills CustomEvent
|
||
|
|
require("./custom-event");
|
||
|
|
// polyfills svg4everybody
|
||
|
|
require("./svg4everybody");
|
||
|
|
|
||
|
|
},{"./custom-event":38,"./element-hidden":39,"./number-is-nan":41,"./svg4everybody":42,"classlist-polyfill":1}],41:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
Number.isNaN = Number.isNaN || function isNaN(input) {
|
||
|
|
// eslint-disable-next-line no-self-compare
|
||
|
|
return typeof input === "number" && input !== input;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],42:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/* eslint-disable */
|
||
|
|
!function (factory) {
|
||
|
|
module.exports = factory();
|
||
|
|
}(function () {
|
||
|
|
/*! svg4everybody v2.1.9 | github.com/jonathantneal/svg4everybody */
|
||
|
|
function embed(parent, svg, target, use) {
|
||
|
|
// if the target exists
|
||
|
|
if (target) {
|
||
|
|
// create a document fragment to hold the contents of the target
|
||
|
|
var fragment = document.createDocumentFragment(),
|
||
|
|
viewBox = !svg.hasAttribute("viewBox") && target.getAttribute("viewBox");
|
||
|
|
// conditionally set the viewBox on the svg
|
||
|
|
viewBox && svg.setAttribute("viewBox", viewBox);
|
||
|
|
// copy the contents of the clone into the fragment
|
||
|
|
for (
|
||
|
|
// clone the target
|
||
|
|
var clone = document.importNode ? document.importNode(target, !0) : target.cloneNode(!0), g = document.createElementNS(svg.namespaceURI || "http://www.w3.org/2000/svg", "g"); clone.childNodes.length;) {
|
||
|
|
g.appendChild(clone.firstChild);
|
||
|
|
}
|
||
|
|
if (use) {
|
||
|
|
for (var i = 0; use.attributes.length > i; i++) {
|
||
|
|
var attr = use.attributes[i];
|
||
|
|
"xlink:href" !== attr.name && "href" !== attr.name && g.setAttribute(attr.name, attr.value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
fragment.appendChild(g),
|
||
|
|
// append the fragment into the svg
|
||
|
|
parent.appendChild(fragment);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
function loadreadystatechange(xhr, use) {
|
||
|
|
// listen to changes in the request
|
||
|
|
xhr.onreadystatechange = function () {
|
||
|
|
// if the request is ready
|
||
|
|
if (4 === xhr.readyState) {
|
||
|
|
// get the cached html document
|
||
|
|
var cachedDocument = xhr._cachedDocument;
|
||
|
|
// ensure the cached html document based on the xhr response
|
||
|
|
cachedDocument || (cachedDocument = xhr._cachedDocument = document.implementation.createHTMLDocument(""), cachedDocument.body.innerHTML = xhr.responseText,
|
||
|
|
// ensure domains are the same, otherwise we'll have issues appending the
|
||
|
|
// element in IE 11
|
||
|
|
cachedDocument.domain !== document.domain && (cachedDocument.domain = document.domain), xhr._cachedTarget = {}),
|
||
|
|
// clear the xhr embeds list and embed each item
|
||
|
|
xhr._embeds.splice(0).map(function (item) {
|
||
|
|
// get the cached target
|
||
|
|
var target = xhr._cachedTarget[item.id];
|
||
|
|
// ensure the cached target
|
||
|
|
target || (target = xhr._cachedTarget[item.id] = cachedDocument.getElementById(item.id)),
|
||
|
|
// embed the target into the svg
|
||
|
|
embed(item.parent, item.svg, target, use);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
},
|
||
|
|
// test the ready state change immediately
|
||
|
|
xhr.onreadystatechange();
|
||
|
|
}
|
||
|
|
function svg4everybody(rawopts) {
|
||
|
|
function oninterval() {
|
||
|
|
// if all <use>s in the array are being bypassed, don't proceed.
|
||
|
|
if (numberOfSvgUseElementsToBypass && uses.length - numberOfSvgUseElementsToBypass <= 0) {
|
||
|
|
return void requestAnimationFrame(oninterval, 67);
|
||
|
|
}
|
||
|
|
// if there are <use>s to process, proceed.
|
||
|
|
// reset the bypass counter, since the counter will be incremented for every bypassed element,
|
||
|
|
// even ones that were counted before.
|
||
|
|
numberOfSvgUseElementsToBypass = 0;
|
||
|
|
// while the index exists in the live <use> collection
|
||
|
|
for (
|
||
|
|
// get the cached <use> index
|
||
|
|
var index = 0; index < uses.length;) {
|
||
|
|
// get the current <use>
|
||
|
|
var use = uses[index],
|
||
|
|
parent = use.parentNode,
|
||
|
|
svg = getSVGAncestor(parent),
|
||
|
|
src = use.getAttribute("xlink:href") || use.getAttribute("href");
|
||
|
|
if (!src && opts.attributeName && (src = use.getAttribute(opts.attributeName)), svg && src) {
|
||
|
|
if (polyfill) {
|
||
|
|
if (!opts.validate || opts.validate(src, svg, use)) {
|
||
|
|
// remove the <use> element
|
||
|
|
parent.removeChild(use);
|
||
|
|
// parse the src and get the url and id
|
||
|
|
var srcSplit = src.split("#"),
|
||
|
|
url = srcSplit.shift(),
|
||
|
|
id = srcSplit.join("#");
|
||
|
|
// if the link is external
|
||
|
|
if (url.length) {
|
||
|
|
// get the cached xhr request
|
||
|
|
var xhr = requests[url];
|
||
|
|
// ensure the xhr request exists
|
||
|
|
xhr || (xhr = requests[url] = new XMLHttpRequest(), xhr.open("GET", url), xhr.send(), xhr._embeds = []),
|
||
|
|
// add the svg and id as an item to the xhr embeds list
|
||
|
|
xhr._embeds.push({
|
||
|
|
parent: parent,
|
||
|
|
svg: svg,
|
||
|
|
id: id
|
||
|
|
}),
|
||
|
|
// prepare the xhr ready state change event
|
||
|
|
loadreadystatechange(xhr, use);
|
||
|
|
} else {
|
||
|
|
// embed the local id into the svg
|
||
|
|
embed(parent, svg, document.getElementById(id), use);
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// increase the index when the previous value was not "valid"
|
||
|
|
++index, ++numberOfSvgUseElementsToBypass;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// increase the index when the previous value was not "valid"
|
||
|
|
++index;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// continue the interval
|
||
|
|
requestAnimationFrame(oninterval, 67);
|
||
|
|
}
|
||
|
|
var polyfill,
|
||
|
|
opts = Object(rawopts),
|
||
|
|
newerIEUA = /\bTrident\/[567]\b|\bMSIE (?:9|10)\.0\b/,
|
||
|
|
webkitUA = /\bAppleWebKit\/(\d+)\b/,
|
||
|
|
olderEdgeUA = /\bEdge\/12\.(\d+)\b/,
|
||
|
|
edgeUA = /\bEdge\/.(\d+)\b/,
|
||
|
|
inIframe = window.top !== window.self;
|
||
|
|
polyfill = "polyfill" in opts ? opts.polyfill : newerIEUA.test(navigator.userAgent) || (navigator.userAgent.match(olderEdgeUA) || [])[1] < 10547 || (navigator.userAgent.match(webkitUA) || [])[1] < 537 || edgeUA.test(navigator.userAgent) && inIframe;
|
||
|
|
// create xhr requests object
|
||
|
|
var requests = {},
|
||
|
|
requestAnimationFrame = window.requestAnimationFrame || setTimeout,
|
||
|
|
uses = document.getElementsByTagName("use"),
|
||
|
|
numberOfSvgUseElementsToBypass = 0;
|
||
|
|
// conditionally start the interval if the polyfill is active
|
||
|
|
polyfill && oninterval();
|
||
|
|
}
|
||
|
|
function getSVGAncestor(node) {
|
||
|
|
for (var svg = node; "svg" !== svg.nodeName.toLowerCase() && (svg = svg.parentNode);) {}
|
||
|
|
return svg;
|
||
|
|
}
|
||
|
|
return svg4everybody;
|
||
|
|
});
|
||
|
|
|
||
|
|
},{}],43:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
window.uswdsPresent = true; // GLOBAL variable to indicate that the uswds.js has loaded in the DOM.
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The 'polyfills' define key ECMAScript 5 methods that may be missing from
|
||
|
|
* older browsers, so must be loaded first.
|
||
|
|
*/
|
||
|
|
require("./polyfills");
|
||
|
|
const uswds = require("./config");
|
||
|
|
const components = require("./index");
|
||
|
|
const svg4everybody = require("./polyfills/svg4everybody");
|
||
|
|
uswds.components = components;
|
||
|
|
const initComponents = () => {
|
||
|
|
const target = document.body;
|
||
|
|
Object.keys(components).forEach(key => {
|
||
|
|
const behavior = components[key];
|
||
|
|
behavior.on(target);
|
||
|
|
});
|
||
|
|
svg4everybody();
|
||
|
|
};
|
||
|
|
if (document.readyState === "loading") {
|
||
|
|
document.addEventListener("DOMContentLoaded", initComponents, {
|
||
|
|
once: true
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
initComponents();
|
||
|
|
}
|
||
|
|
exports.default = uswds;
|
||
|
|
exports.initComponents = initComponents;
|
||
|
|
|
||
|
|
},{"./config":35,"./index":37,"./polyfills":40,"./polyfills/svg4everybody":42}],44:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
module.exports = function () {
|
||
|
|
let htmlDocument = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document;
|
||
|
|
return htmlDocument.activeElement;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],45:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const assign = require("object-assign");
|
||
|
|
const Behavior = require("receptor/behavior");
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @name sequence
|
||
|
|
* @param {...Function} seq an array of functions
|
||
|
|
* @return { closure } callHooks
|
||
|
|
*/
|
||
|
|
// We use a named function here because we want it to inherit its lexical scope
|
||
|
|
// from the behavior props object, not from the module
|
||
|
|
const sequence = function () {
|
||
|
|
for (var _len = arguments.length, seq = new Array(_len), _key = 0; _key < _len; _key++) {
|
||
|
|
seq[_key] = arguments[_key];
|
||
|
|
}
|
||
|
|
return function callHooks() {
|
||
|
|
let target = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : document.body;
|
||
|
|
seq.forEach(method => {
|
||
|
|
if (typeof this[method] === "function") {
|
||
|
|
this[method].call(this, target);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @name behavior
|
||
|
|
* @param {object} events
|
||
|
|
* @param {object?} props
|
||
|
|
* @return {receptor.behavior}
|
||
|
|
*/
|
||
|
|
module.exports = (events, props) => Behavior(events, assign({
|
||
|
|
on: sequence("init", "add"),
|
||
|
|
off: sequence("teardown", "remove")
|
||
|
|
}, props));
|
||
|
|
|
||
|
|
},{"object-assign":4,"receptor/behavior":5}],46:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Call a function every X amount of milliseconds.
|
||
|
|
*
|
||
|
|
* @param {Function} callback - A callback function to be debounced
|
||
|
|
* @param {number} delay - Milliseconds to wait before calling function
|
||
|
|
* @returns {Function} A debounced function
|
||
|
|
* @example const updateStatus = debounce((string) => console.log(string), 2000)
|
||
|
|
*/
|
||
|
|
|
||
|
|
module.exports = function debounce(callback) {
|
||
|
|
var _this = this;
|
||
|
|
let delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 500;
|
||
|
|
let timer = null;
|
||
|
|
return function () {
|
||
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
||
|
|
args[_key] = arguments[_key];
|
||
|
|
}
|
||
|
|
window.clearTimeout(timer);
|
||
|
|
timer = window.setTimeout(() => {
|
||
|
|
callback.apply(_this, args);
|
||
|
|
}, delay);
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],47:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const assign = require("object-assign");
|
||
|
|
const {
|
||
|
|
keymap
|
||
|
|
} = require("receptor");
|
||
|
|
const behavior = require("./behavior");
|
||
|
|
const select = require("./select");
|
||
|
|
const activeElement = require("./active-element");
|
||
|
|
const FOCUSABLE = 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"], [contenteditable]';
|
||
|
|
const tabHandler = context => {
|
||
|
|
const focusableElements = select(FOCUSABLE, context);
|
||
|
|
const firstTabStop = focusableElements[0];
|
||
|
|
const lastTabStop = focusableElements[focusableElements.length - 1];
|
||
|
|
|
||
|
|
// Special rules for when the user is tabbing forward from the last focusable element,
|
||
|
|
// or when tabbing backwards from the first focusable element
|
||
|
|
function tabAhead(event) {
|
||
|
|
if (activeElement() === lastTabStop) {
|
||
|
|
event.preventDefault();
|
||
|
|
firstTabStop.focus();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
function tabBack(event) {
|
||
|
|
if (activeElement() === firstTabStop) {
|
||
|
|
event.preventDefault();
|
||
|
|
lastTabStop.focus();
|
||
|
|
}
|
||
|
|
// This checks if you want to set the initial focus to a container
|
||
|
|
// instead of an element within, and the user tabs back.
|
||
|
|
// Then we set the focus to the first
|
||
|
|
else if (!focusableElements.includes(activeElement())) {
|
||
|
|
event.preventDefault();
|
||
|
|
firstTabStop.focus();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return {
|
||
|
|
firstTabStop,
|
||
|
|
lastTabStop,
|
||
|
|
tabAhead,
|
||
|
|
tabBack
|
||
|
|
};
|
||
|
|
};
|
||
|
|
module.exports = function (context) {
|
||
|
|
let additionalKeyBindings = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
||
|
|
const tabEventHandler = tabHandler(context);
|
||
|
|
const bindings = additionalKeyBindings;
|
||
|
|
const {
|
||
|
|
Esc,
|
||
|
|
Escape
|
||
|
|
} = bindings;
|
||
|
|
if (Escape && !Esc) bindings.Esc = Escape;
|
||
|
|
|
||
|
|
// TODO: In the future, loop over additional keybindings and pass an array
|
||
|
|
// of functions, if necessary, to the map keys. Then people implementing
|
||
|
|
// the focus trap could pass callbacks to fire when tabbing
|
||
|
|
const keyMappings = keymap(assign({
|
||
|
|
Tab: tabEventHandler.tabAhead,
|
||
|
|
"Shift+Tab": tabEventHandler.tabBack
|
||
|
|
}, additionalKeyBindings));
|
||
|
|
const focusTrap = behavior({
|
||
|
|
keydown: keyMappings
|
||
|
|
}, {
|
||
|
|
init() {
|
||
|
|
// TODO: is this desireable behavior? Should the trap always do this by default or should
|
||
|
|
// the component getting decorated handle this?
|
||
|
|
if (tabEventHandler.firstTabStop) {
|
||
|
|
tabEventHandler.firstTabStop.focus();
|
||
|
|
}
|
||
|
|
},
|
||
|
|
update(isActive) {
|
||
|
|
if (isActive) {
|
||
|
|
this.on();
|
||
|
|
} else {
|
||
|
|
this.off();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
});
|
||
|
|
return focusTrap;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"./active-element":44,"./behavior":45,"./select":53,"object-assign":4,"receptor":10}],48:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
// https://stackoverflow.com/a/7557433
|
||
|
|
function isElementInViewport(el) {
|
||
|
|
let win = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : window;
|
||
|
|
let docEl = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : document.documentElement;
|
||
|
|
const rect = el.getBoundingClientRect();
|
||
|
|
return rect.top >= 0 && rect.left >= 0 && rect.bottom <= (win.innerHeight || docEl.clientHeight) && rect.right <= (win.innerWidth || docEl.clientWidth);
|
||
|
|
}
|
||
|
|
module.exports = isElementInViewport;
|
||
|
|
|
||
|
|
},{}],49:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
// iOS detection from: http://stackoverflow.com/a/9039885/177710
|
||
|
|
function isIosDevice() {
|
||
|
|
return typeof navigator !== "undefined" && (navigator.userAgent.match(/(iPod|iPhone|iPad)/g) || navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1) && !window.MSStream;
|
||
|
|
}
|
||
|
|
module.exports = isIosDevice;
|
||
|
|
|
||
|
|
},{}],50:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/* eslint-disable */
|
||
|
|
/* globals define, module */
|
||
|
|
|
||
|
|
/**
|
||
|
|
* A simple library to help you escape HTML using template strings.
|
||
|
|
*
|
||
|
|
* It's the counterpart to our eslint "no-unsafe-innerhtml" plugin that helps us
|
||
|
|
* avoid unsafe coding practices.
|
||
|
|
* A full write-up of the Hows and Whys are documented
|
||
|
|
* for developers at
|
||
|
|
* https://developer.mozilla.org/en-US/Firefox_OS/Security/Security_Automation
|
||
|
|
* with additional background information and design docs at
|
||
|
|
* https://wiki.mozilla.org/User:Fbraun/Gaia/SafeinnerHTMLRoadmap
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
|
||
|
|
!function (factory) {
|
||
|
|
module.exports = factory();
|
||
|
|
}(function () {
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
var Sanitizer = {
|
||
|
|
_entity: /[&<>"'/]/g,
|
||
|
|
_entities: {
|
||
|
|
"&": "&",
|
||
|
|
"<": "<",
|
||
|
|
">": ">",
|
||
|
|
'"': """,
|
||
|
|
"'": "'",
|
||
|
|
"/": "/"
|
||
|
|
},
|
||
|
|
getEntity: function (s) {
|
||
|
|
return Sanitizer._entities[s];
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* Escapes HTML for all values in a tagged template string.
|
||
|
|
*/
|
||
|
|
escapeHTML: function (strings) {
|
||
|
|
var result = "";
|
||
|
|
for (var i = 0; i < strings.length; i++) {
|
||
|
|
result += strings[i];
|
||
|
|
if (i + 1 < arguments.length) {
|
||
|
|
var value = arguments[i + 1] || "";
|
||
|
|
result += String(value).replace(Sanitizer._entity, Sanitizer.getEntity);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* Escapes HTML and returns a wrapped object to be used during DOM insertion
|
||
|
|
*/
|
||
|
|
createSafeHTML: function (strings) {
|
||
|
|
var _len = arguments.length;
|
||
|
|
var values = new Array(_len > 1 ? _len - 1 : 0);
|
||
|
|
for (var _key = 1; _key < _len; _key++) {
|
||
|
|
values[_key - 1] = arguments[_key];
|
||
|
|
}
|
||
|
|
var escaped = Sanitizer.escapeHTML.apply(Sanitizer, [strings].concat(values));
|
||
|
|
return {
|
||
|
|
__html: escaped,
|
||
|
|
toString: function () {
|
||
|
|
return "[object WrappedHTMLObject]";
|
||
|
|
},
|
||
|
|
info: "This is a wrapped HTML object. See https://developer.mozilla.or" + "g/en-US/Firefox_OS/Security/Security_Automation for more."
|
||
|
|
};
|
||
|
|
},
|
||
|
|
/**
|
||
|
|
* Unwrap safe HTML created by createSafeHTML or a custom replacement that
|
||
|
|
* underwent security review.
|
||
|
|
*/
|
||
|
|
unwrapSafeHTML: function () {
|
||
|
|
var _len = arguments.length;
|
||
|
|
var htmlObjects = new Array(_len);
|
||
|
|
for (var _key = 0; _key < _len; _key++) {
|
||
|
|
htmlObjects[_key] = arguments[_key];
|
||
|
|
}
|
||
|
|
var markupList = htmlObjects.map(function (obj) {
|
||
|
|
return obj.__html;
|
||
|
|
});
|
||
|
|
return markupList.join("");
|
||
|
|
}
|
||
|
|
};
|
||
|
|
return Sanitizer;
|
||
|
|
});
|
||
|
|
|
||
|
|
},{}],51:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
module.exports = function getScrollbarWidth() {
|
||
|
|
// Creating invisible container
|
||
|
|
const outer = document.createElement("div");
|
||
|
|
outer.style.visibility = "hidden";
|
||
|
|
outer.style.overflow = "scroll"; // forcing scrollbar to appear
|
||
|
|
outer.style.msOverflowStyle = "scrollbar"; // needed for WinJS apps
|
||
|
|
document.body.appendChild(outer);
|
||
|
|
|
||
|
|
// Creating inner element and placing it in the container
|
||
|
|
const inner = document.createElement("div");
|
||
|
|
outer.appendChild(inner);
|
||
|
|
|
||
|
|
// Calculating difference between container's full width and the child width
|
||
|
|
const scrollbarWidth = `${outer.offsetWidth - inner.offsetWidth}px`;
|
||
|
|
|
||
|
|
// Removing temporary elements from the DOM
|
||
|
|
outer.parentNode.removeChild(outer);
|
||
|
|
return scrollbarWidth;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],52:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const select = require("./select");
|
||
|
|
/**
|
||
|
|
* @name isElement
|
||
|
|
* @desc returns whether or not the given argument is a DOM element.
|
||
|
|
* @param {any} value
|
||
|
|
* @return {boolean}
|
||
|
|
*/
|
||
|
|
const isElement = value => value && typeof value === "object" && value.nodeType === 1;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @name selectOrMatches
|
||
|
|
* @desc selects elements from the DOM by class selector or ID selector.
|
||
|
|
* @param {string} selector - The selector to traverse the DOM with.
|
||
|
|
* @param {Document|HTMLElement?} context - The context to traverse the DOM
|
||
|
|
* in. If not provided, it defaults to the document.
|
||
|
|
* @return {HTMLElement[]} - An array of DOM nodes or an empty array.
|
||
|
|
*/
|
||
|
|
module.exports = (selector, context) => {
|
||
|
|
const selection = select(selector, context);
|
||
|
|
if (typeof selector !== "string") {
|
||
|
|
return selection;
|
||
|
|
}
|
||
|
|
if (isElement(context) && context.matches(selector)) {
|
||
|
|
selection.push(context);
|
||
|
|
}
|
||
|
|
return selection;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"./select":53}],53:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @name isElement
|
||
|
|
* @desc returns whether or not the given argument is a DOM element.
|
||
|
|
* @param {any} value
|
||
|
|
* @return {boolean}
|
||
|
|
*/
|
||
|
|
const isElement = value => value && typeof value === "object" && value.nodeType === 1;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @name select
|
||
|
|
* @desc selects elements from the DOM by class selector or ID selector.
|
||
|
|
* @param {string} selector - The selector to traverse the DOM with.
|
||
|
|
* @param {Document|HTMLElement?} context - The context to traverse the DOM
|
||
|
|
* in. If not provided, it defaults to the document.
|
||
|
|
* @return {HTMLElement[]} - An array of DOM nodes or an empty array.
|
||
|
|
*/
|
||
|
|
module.exports = (selector, context) => {
|
||
|
|
if (typeof selector !== "string") {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
if (!context || !isElement(context)) {
|
||
|
|
context = window.document; // eslint-disable-line no-param-reassign
|
||
|
|
}
|
||
|
|
|
||
|
|
const selection = context.querySelectorAll(selector);
|
||
|
|
return Array.prototype.slice.call(selection);
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],54:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Flips given INPUT elements between masked (hiding the field value) and unmasked
|
||
|
|
* @param {Array.HTMLElement} fields - An array of INPUT elements
|
||
|
|
* @param {Boolean} mask - Whether the mask should be applied, hiding the field value
|
||
|
|
*/
|
||
|
|
module.exports = (field, mask) => {
|
||
|
|
field.setAttribute("autocapitalize", "off");
|
||
|
|
field.setAttribute("autocorrect", "off");
|
||
|
|
field.setAttribute("type", mask ? "password" : "text");
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],55:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const resolveIdRefs = require("resolve-id-refs");
|
||
|
|
const toggleFieldMask = require("./toggle-field-mask");
|
||
|
|
const CONTROLS = "aria-controls";
|
||
|
|
const PRESSED = "aria-pressed";
|
||
|
|
const SHOW_ATTR = "data-show-text";
|
||
|
|
const HIDE_ATTR = "data-hide-text";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Replace the word "Show" (or "show") with "Hide" (or "hide") in a string.
|
||
|
|
* @param {string} showText
|
||
|
|
* @return {strong} hideText
|
||
|
|
*/
|
||
|
|
const getHideText = showText => showText.replace(/\bShow\b/i, show => `${show[0] === "S" ? "H" : "h"}ide`);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Component that decorates an HTML element with the ability to toggle the
|
||
|
|
* masked state of an input field (like a password) when clicked.
|
||
|
|
* The ids of the fields to be masked will be pulled directly from the button's
|
||
|
|
* `aria-controls` attribute.
|
||
|
|
*
|
||
|
|
* @param {HTMLElement} el Parent element containing the fields to be masked
|
||
|
|
* @return {boolean}
|
||
|
|
*/
|
||
|
|
module.exports = el => {
|
||
|
|
// this is the *target* state:
|
||
|
|
// * if the element has the attr and it's !== "true", pressed is true
|
||
|
|
// * otherwise, pressed is false
|
||
|
|
const pressed = el.hasAttribute(PRESSED) && el.getAttribute(PRESSED) !== "true";
|
||
|
|
const fields = resolveIdRefs(el.getAttribute(CONTROLS));
|
||
|
|
fields.forEach(field => toggleFieldMask(field, pressed));
|
||
|
|
if (!el.hasAttribute(SHOW_ATTR)) {
|
||
|
|
el.setAttribute(SHOW_ATTR, el.textContent);
|
||
|
|
}
|
||
|
|
const showText = el.getAttribute(SHOW_ATTR);
|
||
|
|
const hideText = el.getAttribute(HIDE_ATTR) || getHideText(showText);
|
||
|
|
el.textContent = pressed ? showText : hideText; // eslint-disable-line no-param-reassign
|
||
|
|
el.setAttribute(PRESSED, pressed);
|
||
|
|
return pressed;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"./toggle-field-mask":54,"resolve-id-refs":13}],56:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const EXPANDED = "aria-expanded";
|
||
|
|
const CONTROLS = "aria-controls";
|
||
|
|
const HIDDEN = "hidden";
|
||
|
|
module.exports = (button, expanded) => {
|
||
|
|
let safeExpanded = expanded;
|
||
|
|
if (typeof safeExpanded !== "boolean") {
|
||
|
|
safeExpanded = button.getAttribute(EXPANDED) === "false";
|
||
|
|
}
|
||
|
|
button.setAttribute(EXPANDED, safeExpanded);
|
||
|
|
const id = button.getAttribute(CONTROLS);
|
||
|
|
const controls = document.getElementById(id);
|
||
|
|
if (!controls) {
|
||
|
|
throw new Error(`No toggle target found with id: "${id}"`);
|
||
|
|
}
|
||
|
|
if (safeExpanded) {
|
||
|
|
controls.removeAttribute(HIDDEN);
|
||
|
|
} else {
|
||
|
|
controls.setAttribute(HIDDEN, "");
|
||
|
|
}
|
||
|
|
return safeExpanded;
|
||
|
|
};
|
||
|
|
|
||
|
|
},{}],57:[function(require,module,exports){
|
||
|
|
"use strict";
|
||
|
|
|
||
|
|
const debounce = require("./debounce");
|
||
|
|
const {
|
||
|
|
prefix: PREFIX
|
||
|
|
} = require("../config");
|
||
|
|
const CHECKED_CLASS = `${PREFIX}-checklist__item--checked`;
|
||
|
|
module.exports = function validate(el) {
|
||
|
|
const id = el.dataset.validationElement;
|
||
|
|
const checkList = id.charAt(0) === "#" ? document.querySelector(id) : document.getElementById(id);
|
||
|
|
if (!checkList) {
|
||
|
|
throw new Error(`No validation element found with id: "${id}"`);
|
||
|
|
}
|
||
|
|
let statusSummary = "";
|
||
|
|
Object.entries(el.dataset).forEach(_ref => {
|
||
|
|
let [key, value] = _ref;
|
||
|
|
if (key.startsWith("validate")) {
|
||
|
|
const validatorName = key.substr("validate".length).toLowerCase();
|
||
|
|
const validatorPattern = new RegExp(value);
|
||
|
|
const validatorSelector = `[data-validator="${validatorName}"]`;
|
||
|
|
const validatorCheckbox = checkList.querySelector(validatorSelector);
|
||
|
|
const validatorParent = el.parentNode;
|
||
|
|
const statusSummaryContainer = validatorParent.querySelector(`[data-validation-status]`);
|
||
|
|
const checked = validatorPattern.test(el.value);
|
||
|
|
validatorCheckbox.classList.toggle(CHECKED_CLASS, checked);
|
||
|
|
if (!validatorCheckbox) {
|
||
|
|
throw new Error(`No validator checkbox found for: "${validatorName}"`);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Create status reports for checklist items
|
||
|
|
const statusComplete = el.dataset.validationComplete || "status complete";
|
||
|
|
const statusIncomplete = el.dataset.validationIncomplete || "status incomplete";
|
||
|
|
let checkboxContent = `${validatorCheckbox.textContent} `;
|
||
|
|
if (validatorCheckbox.classList.contains(CHECKED_CLASS)) {
|
||
|
|
checkboxContent += statusComplete;
|
||
|
|
} else {
|
||
|
|
checkboxContent += statusIncomplete;
|
||
|
|
}
|
||
|
|
|
||
|
|
// move status updates to aria-label on checklist item
|
||
|
|
validatorCheckbox.setAttribute("aria-label", checkboxContent);
|
||
|
|
|
||
|
|
// Create a summary of status for all checklist items
|
||
|
|
statusSummary += `${checkboxContent}. `;
|
||
|
|
|
||
|
|
// Add summary to screen reader summary container, after a delay
|
||
|
|
const srUpdateStatus = debounce(() => {
|
||
|
|
statusSummaryContainer.textContent = statusSummary;
|
||
|
|
}, 1000);
|
||
|
|
srUpdateStatus();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
},{"../config":35,"./debounce":46}]},{},[43])
|
||
|
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJub2RlX21vZHVsZXMvY2xhc3NsaXN0LXBvbHlmaWxsL3NyYy9pbmRleC5qcyIsIm5vZGVfbW9kdWxlcy9lbGVtZW50LWNsb3Nlc3QvZWxlbWVudC1jbG9zZXN0LmpzIiwibm9kZV9tb2R1bGVzL2tleWJvYXJkZXZlbnQta2V5LXBvbHlmaWxsL2luZGV4LmpzIiwibm9kZV9tb2R1bGVzL29iamVjdC1hc3NpZ24vaW5kZXguanMiLCJub2RlX21vZHVsZXMvcmVjZXB0b3IvYmVoYXZpb3IvaW5kZXguanMiLCJub2RlX21vZHVsZXMvcmVjZXB0b3IvY29tcG9zZS9pbmRleC5qcyIsIm5vZGVfbW9kdWxlcy9yZWNlcHRvci9kZWxlZ2F0ZS9pbmRleC5qcyIsIm5vZGVfbW9kdWxlcy9yZWNlcHRvci9kZWxlZ2F0ZUFsbC9pbmRleC5qcyIsIm5vZGVfbW9kdWxlcy9yZWNlcHRvci9pZ25vcmUvaW5kZXguanMiLCJub2RlX21vZHVsZXMvcmVjZXB0b3IvaW5kZXguanMiLCJub2RlX21vZHVsZXMvcmVjZXB0b3Iva2V5bWFwL2luZGV4LmpzIiwibm9kZV9tb2R1bGVzL3JlY2VwdG9yL29uY2UvaW5kZXguanMiLCJub2RlX21vZHVsZXMvcmVzb2x2ZS1pZC1yZWZzL2luZGV4LmpzIiwicGFja2FnZXMvX3VzYS1wYXNzd29yZC9zcmMvaW5kZXguanMiLCJwYWNrYWdlcy91c2EtYWNjb3JkaW9uL3NyYy9pbmRleC5qcyIsInBhY2thZ2VzL3VzYS1iYW5uZXIvc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXNhLWNoYXJhY3Rlci1jb3VudC9zcmMvaW5kZXguanMiLCJwYWNrYWdlcy91c2EtY29tYm8tYm94L3NyYy9pbmRleC5qcyIsInBhY2thZ2VzL3VzYS1kYXRlLXBpY2tlci9zcmMvaW5kZXguanMiLCJwYWNrYWdlcy91c2EtZGF0ZS1yYW5nZS1waWNrZXIvc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXNhLWZpbGUtaW5wdXQvc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXNhLWZvb3Rlci9zcmMvaW5kZXguanMiLCJwYWNrYWdlcy91c2EtaGVhZGVyL3NyYy9pbmRleC5qcyIsInBhY2thZ2VzL3VzYS1pbi1wYWdlLW5hdmlnYXRpb24vc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXNhLWlucHV0LW1hc2svc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXNhLWlucHV0LXByZWZpeC1zdWZmaXgvc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXNhLWxhbmd1YWdlLXNlbGVjdG9yL3NyYy9pbmRleC5qcyIsInBhY2thZ2VzL3VzYS1tb2RhbC9zcmMvaW5kZXguanMiLCJwYWNrYWdlcy91c2Etc2VhcmNoL3NyYy9pbmRleC5qcyIsInBhY2thZ2VzL3VzYS1za2lwbmF2L3NyYy9pbmRleC5qcyIsInBhY2thZ2VzL3VzYS10YWJsZS9zcmMvaW5kZXguanMiLCJwYWNrYWdlcy91c2EtdGltZS1waWNrZXIvc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXNhLXRvb2x0aXAvc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXNhLXZhbGlkYXRpb24vc3JjL2luZGV4LmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvY29uZmlnLmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvZXZlbnRzLmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvaW5kZXguanMiLCJwYWNrYWdlcy91c3dkcy1jb3JlL3NyYy9qcy9wb2x5ZmlsbHMvY3VzdG9tLWV2ZW50LmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvcG9seWZpbGxzL2VsZW1lbnQtaGlkZGVuLmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvcG9seWZpbGxzL2luZGV4LmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvcG9seWZpbGxzL251bWJlci1pcy1uYW4uanMiLCJwYWNrYWdlcy91c3dkcy1jb3JlL3NyYy9qcy9wb2x5ZmlsbHMvc3ZnNGV2ZXJ5Ym9keS5qcyIsInBhY2thZ2VzL3Vzd2RzLWNvcmUvc3JjL2pzL3N0YXJ0LmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvdXRpbHMvYWN0aXZlLWVsZW1lbnQuanMiLCJwYWNrYWdlcy91c3dkcy1jb3JlL3NyYy9qcy91dGlscy9iZWhhdmlvci5qcyIsInBhY2thZ2VzL3Vzd2RzLWNvcmUvc3JjL2pzL3V0aWxzL2RlYm91bmNlLmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvdXRpbHMvZm9jdXMtdHJhcC5qcyIsInBhY2thZ2VzL3Vzd2RzLWNvcmUvc3JjL2pzL3V0aWxzL2lzLWluLXZpZXdwb3J0LmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvdXRpbHMvaXMtaW9zLWRldmljZS5qcyIsInBhY2thZ2VzL3Vzd2RzLWNvcmUvc3JjL2pzL3V0aWxzL3Nhbml0aXplci5qcyIsInBhY2thZ2VzL3Vzd2RzLWNvcmUvc3JjL2pzL3V0aWxzL3Njcm9sbGJhci13aWR0aC5qcyIsInBhY2thZ2VzL3Vzd2RzLWNvcmUvc3JjL2pzL3V0aWxzL3NlbGVjdC1vci1tYXRjaGVzLmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvdXRpbHMvc2VsZWN0LmpzIiwicGFja2FnZXMvdXN3ZHMtY29yZS9zcmMvanMvdXRpbHMvdG9nZ2xlLWZpZWxkLW1hc2suanMiLCJwYWNrYWdlcy91c3dkcy1jb3JlL3NyYy9qcy91dGlscy90b2dnbGUtZm9ybS1pbnB1dC5qcyIsInBhY2thZ2VzL3Vzd2RzLWNvcmUvc3JjL2pzL3V0aWxzL3RvZ2dsZS5qcyIsInBhY2thZ2VzL3Vzd2RzLWNvcmUvc3JjL2pzL3V0aWxzL3ZhbGlkYXRlLWlucHV0LmpzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7QUNBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBOztBQUVBOztBQUVBOztBQUVBO0FBQ0E7QUFDQTs7QUFFQSxJQUFJLFVBQVUsSUFBSSxNQUFNLENBQUMsSUFBSSxFQUFFO0VBRTdCO0VBQ0E7RUFDQSxJQUFJLEVBQUUsV0FBVyxJQUFJLFFBQVEsQ0FBQyxhQUFhLENBQUMsR0FBRyxDQUFDLENBQUMsSUFDNUMsUUFBUSxDQUFDLGVBQWUsSUFBSSxFQUFFLFdBQVcsSUFBSSxRQUFRLENBQUMsZUFBZSxDQUFDLDRCQUE0QixFQUFDLEdBQUcsQ0FBQyxDQUFDLEVBQUU7SUFFOUcsV0FBVSxJQUFJLEVBQUU7TUFFZixZQUFZOztNQUVaLElBQUksRUFBRSxTQUFTLElBQUksSUFBSSxDQUFDLEVBQUU7TUFFMUIsSUFDSSxhQUFhLEdBQUcsV0FBVztRQUMzQixTQUFTLEdBQUcsV0FBVztRQUN2QixZQUFZLEdBQUcsSUFBSSxDQUFDLE9
|