|
2 | 2 | export default {
|
3 | 3 | data() {
|
4 | 4 | return {
|
| 5 | + navElements: [], |
5 | 6 | tabbableCell: null,
|
6 | 7 | }
|
7 | 8 | },
|
8 | 9 | methods: {
|
| 10 | + /** |
| 11 | + * Returns true if the calendar has been passed the given slot |
| 12 | + * @param {String} slotName The name of the slot |
| 13 | + * @return {Boolean} |
| 14 | + */ |
| 15 | + hasSlot(slotName) { |
| 16 | + return !!this.$slots[slotName] |
| 17 | + }, |
| 18 | + /** |
| 19 | + * Returns an array of all HTML elements which should be focus-trapped in the specified slot |
| 20 | + * @returns {Array} An array of HTML elements |
| 21 | + */ |
| 22 | + getElementsFromSlot(slotName) { |
| 23 | + if (!this.hasSlot(slotName)) { |
| 24 | + return [] |
| 25 | + } |
| 26 | +
|
| 27 | + if (slotName === 'beforeCalendarHeader') { |
| 28 | + return this.getFocusableElements(this.$refs.view.children[0]) |
| 29 | + } |
| 30 | +
|
| 31 | + if (slotName === 'calendarFooter') { |
| 32 | + return this.getFocusableElements(this.$refs.view.children[2]) |
| 33 | + } |
| 34 | +
|
| 35 | + const isBeforeHeader = slotName.indexOf('beforeCalendarHeader') > -1 |
| 36 | + const picker = this.$refs.picker.$el |
| 37 | + const index = isBeforeHeader ? 0 : picker.children.length - 1 |
| 38 | +
|
| 39 | + return this.getFocusableElements(picker.children[index]) |
| 40 | + }, |
| 41 | + /** |
| 42 | + * Returns an array of all HTML elements which should be focus-trapped in the header |
| 43 | + * @returns {Array} An array of HTML elements |
| 44 | + */ |
| 45 | + getElementsFromHeader() { |
| 46 | + const view = this.ucFirst(this.view) |
| 47 | + const beforeCalendarSlotName = `beforeCalendarHeader${view}` |
| 48 | + const picker = this.$refs.picker.$el |
| 49 | + const index = this.hasSlot(beforeCalendarSlotName) ? 1 : 0 |
| 50 | + const fragment = picker.children[index] |
| 51 | +
|
| 52 | + return this.showHeader ? this.getFocusableElements(fragment) : [] |
| 53 | + }, |
| 54 | + /** |
| 55 | + * Returns an array of focusable elements in a given HTML fragment |
| 56 | + * @param {Element} fragment The HTML fragment to search |
| 57 | + * @returns {Array} |
| 58 | + */ |
| 59 | + getFocusableElements(fragment) { |
| 60 | + const navNodeList = fragment.querySelectorAll( |
| 61 | + 'button:enabled, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])', |
| 62 | + ) |
| 63 | +
|
| 64 | + return [...Array.prototype.slice.call(navNodeList)] |
| 65 | + }, |
| 66 | + /** |
| 67 | + * Returns the input element (when typeable) |
| 68 | + * @returns {Element} |
| 69 | + */ |
| 70 | + getInputField() { |
| 71 | + if (!this.typeable || this.inline) { |
| 72 | + return null |
| 73 | + } |
| 74 | +
|
| 75 | + return this.$refs.dateInput.$refs[this.refName] |
| 76 | + }, |
| 77 | + /** |
| 78 | + * Determines which elements in datepicker should be focus-trapped |
| 79 | + */ |
| 80 | + setNavElements() { |
| 81 | + if (!this.view) return |
| 82 | +
|
| 83 | + const view = this.ucFirst(this.view) |
| 84 | +
|
| 85 | + this.navElements = [ |
| 86 | + this.getInputField(), |
| 87 | + this.getElementsFromSlot('beforeCalendarHeader'), |
| 88 | + this.getElementsFromSlot(`beforeCalendarHeader${view}`), |
| 89 | + this.getElementsFromHeader(), |
| 90 | + this.tabbableCell, |
| 91 | + this.getElementsFromSlot(`calendarFooter${view}`), |
| 92 | + this.getElementsFromSlot('calendarFooter'), |
| 93 | + ] |
| 94 | + .filter((item) => !!item) |
| 95 | + .reduce((acc, val) => acc.concat(val), []) |
| 96 | + }, |
9 | 97 | /**
|
10 | 98 | * Sets the focus-trapped cell in the picker
|
11 | 99 | */
|
|
0 commit comments