From 367eef02b9c713ccec24972b5b90dd634dd0be7e Mon Sep 17 00:00:00 2001 From: Jon Gunderson Date: Wed, 8 Jul 2020 11:51:13 -0500 Subject: [PATCH] improving code --- examples/combobox/js/combobox-datepicker.js | 86 +++++++++------------ 1 file changed, 36 insertions(+), 50 deletions(-) diff --git a/examples/combobox/js/combobox-datepicker.js b/examples/combobox/js/combobox-datepicker.js index ab6cb9fade..ca7948cb48 100644 --- a/examples/combobox/js/combobox-datepicker.js +++ b/examples/combobox/js/combobox-datepicker.js @@ -79,12 +79,12 @@ ComboboxDatePicker.prototype.init = function () { for (var j = 0; j < 7; j++) { var cell = document.createElement('td'); - cell.setAttribute('tabindex', '-1'); + cell.tabIndex = -1; cell.addEventListener('click', this.handleDayClick.bind(this)); cell.addEventListener('keydown', this.handleDayKeyDown.bind(this)); cell.addEventListener('focus', this.handleDayFocus.bind(this)); - cell.innerHTML = '-1'; + cell.textContent = '-1'; row.appendChild(cell); this.days.push(cell); @@ -111,7 +111,7 @@ ComboboxDatePicker.prototype.updateGrid = function () { var i, flag; var fd = this.focusDay; - this.monthYearNode.innerHTML = this.monthLabels[fd.getMonth()] + ' ' + fd.getFullYear(); + this.monthYearNode.textContent = this.monthLabels[fd.getMonth()] + ' ' + fd.getFullYear(); var firstDayOfMonth = new Date(fd.getFullYear(), fd.getMonth(), 1); var dayOfWeek = firstDayOfMonth.getDay(); @@ -137,6 +137,37 @@ ComboboxDatePicker.prototype.updateGrid = function () { } }; +ComboboxDatePicker.prototype.updateDate = function (domNode, disable, day, selected) { + + var d = day.getDate().toString(); + if (day.getDate() <= 9) { + d = '0' + d; + } + + var m = day.getMonth() + 1; + if (day.getMonth() < 9) { + m = '0' + m; + } + + domNode.tabIndex = -1; + domNode.removeAttribute('aria-selected'); + domNode.setAttribute('data-date', day.getFullYear() + '-' + m + '-' + d); + + if (disable) { + domNode.classList.add('disabled'); + domNode.textContent = ''; + } + else { + domNode.classList.remove('disabled'); + domNode.textContent = day.getDate(); + if (selected) { + domNode.setAttribute('aria-selected', 'true'); + domNode.tabIndex = 0; + } + } + +}; + ComboboxDatePicker.prototype.setFocusDay = function (flag) { if (typeof flag !== 'boolean') { @@ -150,9 +181,9 @@ ComboboxDatePicker.prototype.setFocusDay = function (flag) { var d = getDayFromDataDateAttribute(domNode); - domNode.setAttribute('tabindex', '-1'); + domNode.tabIndex = -1; if (this.isSameDay(d, fd)) { - domNode.setAttribute('tabindex', '0'); + domNode.tabIndex = 0; if (flag) { domNode.focus(); } @@ -515,50 +546,6 @@ ComboboxDatePicker.prototype.getDayFromDataDateAttribute = function (domNode) { return new Date(parts[0], parseInt(parts[1])-1, parts[2]); }; -ComboboxDatePicker.prototype.updateDate = function (domNode, disable, day, selected) { - - var d = day.getDate().toString(); - if (day.getDate() <= 9) { - d = '0' + d; - } - - var m = day.getMonth() + 1; - if (day.getMonth() < 9) { - m = '0' + m; - } - - domNode.setAttribute('tabindex', '-1'); - domNode.removeAttribute('aria-selected'); - domNode.setAttribute('data-date', day.getFullYear() + '-' + m + '-' + d); - - if (disable) { - domNode.classList.add('disabled'); - domNode.innerHTML = ''; - } - else { - domNode.classList.remove('disabled'); - domNode.innerHTML = day.getDate(); - if (selected) { - domNode.setAttribute('aria-selected', 'true'); - domNode.setAttribute('tabindex', '0'); - } - } - -}; - -ComboboxDatePicker.prototype.updateSelected = function (domNode) { - for (var i = 0; i < this.days.length; i++) { - var day = this.days[i]; - if (day === domNode) { - day.setAttribute('aria-selected', 'true'); - } - else { - day.removeAttribute('aria-selected'); - } - } -}; - - ComboboxDatePicker.prototype.handleDayKeyDown = function (event) { var flag = false; @@ -570,7 +557,6 @@ ComboboxDatePicker.prototype.handleDayKeyDown = function (event) { break; case " ": - this.updateSelected(event.currentTarget); this.setComboboxDate(event.currentTarget); flag = true; break;