Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 35x 210x 210x 210x 210x 210x 210x 210x 210x 210x 210x 210x 5x 5x 5x 5x 5x 5x 5x 210x 210x 5x | /**
* @file Manages the calendar / day-picker view.
*/
import { IDatePicker, IPicker } from '../interfaces.ts';
import { now, datesEq, shiftMonth, shiftDay } from '../lib/date.ts';
import { Key } from '../lib/dom.ts';
export default {
onKeyDown: keyDown,
onClick: {
'dp-day': selectDay,
'dp-next': gotoNextMonth,
'dp-prev': gotoPrevMonth,
'dp-today': selectToday,
'dp-clear': clear,
'dp-close': close,
'dp-cal-month': showMonthPicker,
'dp-cal-year': showYearPicker,
},
render: render,
} as IPicker;
/**
* view renders the calendar (day picker) as an HTML string.
*
* @param {DatePickerContext} context the date picker being rendered
* @returns {string}
*/
function render( dp: IDatePicker ) {
const opts = dp.opts;
const lang = opts.lang;
const state = dp.state;
const dayNames = lang.days;
const dayOffset = opts.dayOffset || 0;
const selectedDate = state.selectedDate;
const highlightedDate = state.highlightedDate;
const hilightedMonth = highlightedDate!.getMonth();
const today = now().getTime();
return (
'<div tabindex="0" class="dp-cal" aria-label="' +
lang.ariaLabel.dayPicker +
'">' +
'<header class="dp-cal-header">' +
'<button tabindex="-1" type="button" aria-label="' +
lang.months[ hilightedMonth ] +
lang.ariaLabel.monthPickerButton +
'" class="dp-focusable dp-cal-month dp-cal-dropdown">' +
lang.months[ hilightedMonth ] +
'</button>' +
'<button tabindex="-1" type="button" aria-label="' +
highlightedDate!.getFullYear() +
'Year Picker. Use the space key to enter the year picker." class="dp-focusable dp-cal-year dp-cal-dropdown">' +
highlightedDate!.getFullYear() +
'</button>' +
'<button tabindex="-1" type="button" class="dp-focusable dp-prev dp-cal-nav">Previous Month</button>' +
'<button tabindex="-1" type="button" class="dp-focusable dp-next dp-cal-nav">Next Month</button>' +
'</header>' +
'<div class="dp-days">' +
dayNames
.map( function ( name: string, i: number ) {
return (
'<span class="dp-col-header">' +
dayNames[ ( i + dayOffset ) % dayNames.length ] +
'</span>'
);
} )
.join( '' ) +
mapDays( highlightedDate!, dayOffset, function ( date ) {
const isNotInMonth = date.getMonth() !== hilightedMonth;
const isDisabled = ! opts.inRange( date );
const isToday = date.getTime() === today;
let className = 'dp-day';
className += isNotInMonth ? ' dp-edge-day' : '';
className += datesEq( date, highlightedDate ) ? ' dp-current' : '';
className += datesEq( date, selectedDate ) ? ' dp-selected dp-focusable' : '';
className += isDisabled ? ' dp-day-disabled' : '';
className += isToday ? ' dp-day-today' : '';
className += ' ' + opts.dateClass( date );
return (
'<button tabindex="-1" type="button" aria-role="button" aria-label="' +
date.toDateString() +
lang.ariaLabel.dayButton +
'" class="' +
className +
'" data-date="' +
date.getTime() +
'">' +
date.getDate() +
'</button>'
);
} ) +
'</div>' +
( opts.hasFooter ? renderFooter( lang ) : '' ) +
'</div>'
);
}
function renderFooter( lang ) {
return (
'<footer class="dp-cal-footer">' +
'<button tabindex="-1" type="button" class="dp-focusable dp-today" aria-label="' +
lang.ariaLabel.todayButton +
'">' +
lang.today +
'</button>' +
'<button tabindex="-1" type="button" class="dp-focusable dp-clear" aria-label="' +
lang.ariaLabel.clearButton +
'">' +
lang.clear +
'</button>' +
'<button tabindex="-1" type="button" class="dp-focusable dp-close" aria-label="' +
lang.ariaLabel.closeButton +
'">' +
lang.close +
'</button>' +
'</footer>'
);
}
/**
* keyDown handles the key down event for the day-picker
*
* @param {Event} e
* @param {DatePickerContext} dp
*/
function keyDown( ke: KeyboardEvent, dp: IDatePicker ) {
const key = ke.code;
let shiftBy = 0;
switch ( key ) {
case Key.left:
shiftBy = -1;
break;
case Key.right:
shiftBy = 1;
break;
case Key.up:
shiftBy = -7;
break;
case Key.down:
shiftBy = 7;
break;
}
if ( key === Key.esc ) {
dp.close( true );
} else if ( shiftBy ) {
ke.preventDefault();
if ( ke.shiftKey ) {
// shift month
if ( shiftBy > 0 ) {
dp.setState( {
highlightedDate: shiftMonth( dp?.state?.highlightedDate, 1 ),
} );
} else {
dp.setState( {
highlightedDate: shiftMonth( dp?.state?.highlightedDate, -1 ),
} );
}
} else {
dp.setState( {
highlightedDate: shiftDay( dp?.state?.highlightedDate, shiftBy ),
} );
}
} else if ( key === Key.tab ) {
moveFocusToNextButton( ke, dp );
}
}
/**
* Allows the user to move focus between buttons with the tab.
*
* @param {Event} e
* @param {DatePickerContext} dp
*/
function moveFocusToNextButton( ke: KeyboardEvent, dp: IDatePicker ) {
ke.preventDefault();
const buttons = dp.el?.querySelectorAll( '.dp-focusable' ) || [];
const activeElement = document.activeElement;
const focusedIndex = activeElement ? Array.from( buttons ).indexOf( activeElement ) : -1;
if ( focusedIndex !== -1 ) {
let nextIndex = ke.shiftKey ? focusedIndex - 1 : focusedIndex + 1;
// Loop around if at the start or end
if ( nextIndex >= buttons.length ) nextIndex = 0;
if ( nextIndex < 0 ) nextIndex = buttons.length - 1;
( buttons[ nextIndex ] as HTMLElement ).focus();
} else if ( buttons.length ) {
( buttons[ 0 ] as HTMLElement ).focus();
}
}
function selectToday( e: Event, dp: IDatePicker ) {
dp.setState( {
selectedDate: now(),
} );
}
function clear( e: Event, dp: IDatePicker ) {
dp.setState( {
selectedDate: null,
} );
}
function close( e: Event, dp: IDatePicker ) {
dp.close();
}
function showMonthPicker( e: Event, dp: IDatePicker ) {
dp.setState( {
view: 'month',
} );
}
function showYearPicker( e: Event, dp: IDatePicker ) {
dp.setState( {
view: 'year',
} );
}
function focusSelector( dp: IDatePicker, selector: string ) {
const el = dp.el?.querySelector( selector );
if ( el ) {
( el as HTMLElement ).focus();
}
}
function gotoNextMonth( e: Event, dp: IDatePicker ) {
const highlightedDate = dp?.state?.highlightedDate;
dp.setState( {
highlightedDate: shiftMonth( highlightedDate, 1 ),
} );
focusSelector( dp, '.dp-next' );
}
function gotoPrevMonth( e: Event, dp: IDatePicker ) {
const highlightedDate = dp?.state?.highlightedDate;
dp.setState( {
highlightedDate: shiftMonth( highlightedDate, -1 ),
} );
focusSelector( dp, '.dp-prev' );
}
function selectDay( e: KeyboardEvent, dp: IDatePicker ) {
if ( ! e.target ) {
return;
}
const eventTarget = e.target as HTMLElement;
dp.setState( {
selectedDate: new Date( parseInt( eventTarget.getAttribute( 'data-date' ) as string ) ),
} );
}
function mapDays( currentDate: Date, dayOffset: number, fn: ( iter: Date ) => string ) {
let result = '';
const iter = new Date( currentDate );
iter.setDate( 1 );
iter.setDate( 1 - iter.getDay() + dayOffset );
// If we are showing monday as the 1st of the week,
// and the monday is the 2nd of the month, the sunday won't
// show, so we need to shift backwards
Eif ( iter.getDate() === dayOffset + 1 ) {
iter.setDate( dayOffset - 6 );
}
// We are going to have 6 weeks always displayed to keep a consistent
// calendar size
for ( let day = 0; day < 6 * 7; ++day ) {
result += fn( iter );
iter.setDate( iter.getDate() + 1 );
}
return result;
}
|