2.2.4 • Published 3 years ago

@universityofwarwick/ab-datepicker v2.2.4

Weekly downloads
13
License
MIT
Repository
github
Last release
3 years ago

ab-datepicker

ab-datepicker is an accessible datepicker based on the example of the Open AJAX Alliance Accessibility Tools Task Force : http://www.oaa-accessibility.org/examplep/datepicker1/ and Inspired by http://wet-boew.github.io/wet-boew/demos/datepicker/datepicker-en.html

Table of contents

  1. Description
  2. Dependencies
  3. Demo
  4. Usage
  5. Configuration options
  6. Methods
  7. Events
  8. Theming
  9. Keyboard interaction
  10. ARIA 1.0 Markup
  11. Browser Compatibility
  12. Copyright and license

Description

This DatePicker widget is a jQuery Plugin which allows the user to select a date. It implements the WAI-ARIA Date Picker design pattern of W3C.

The calendar portion of the date picker follows a table structure where days of the week and calendar day numbers are layed out in HTML table cells where WAI-ARIA semantics for a grid are applied. This provides context so an assistive technology can render the day of the week; its corresponding numeric calendar day, and week number if necessary.

The calendar portion can be displayed in a numbers of ways, including as a popup in modal mode.

back to table of contents

Dependencies

back to table of contents

Demo

Online Demo

back to table of contents

Usage

The datepicker component must be bound to an text input field of your document:

<input class="date form-control" id="date1" type="text" placeholder="d/M/y" title="format: dd/MM/y"/>

For better accessibility, define an placeholder attribute and a title giving the input format to the screen reader.

Add the bootstrap and datepicker css to your page head

<link rel="stylesheet" href="path/to/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="path/to/datepicker.css" type="text/css" />
or
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ab-datepicker@latest/css/datepicker.css" type="text/css" />

In case you use bootstrap4 markup (see the markup option), it is necessary to include font 'Font Awesome 5':

<link rel="stylesheet" href="path/to/font-awesome5.min.css" type="text/css" />

Place the javascript files to the end of your document:

<script type="text/javascript" src="path/to/jquery.min.js"></script>
<script type="text/javascript" src="path/to/bootstrap.min.js"></script>
<script type="text/javascript" src="path/to/datepicker.min.js"></script>
or
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ab-datepicker@latest"></script>

Basic usage

Add this code to the end of your document:

    <script type="text/javascript">
    $(document).ready(function() {
    	$('.date').datepicker();
    });
    </script>

This code displays the default calendar (in english with the bootstrap 3 markup and the default theme : no colors) npm.io npm.io npm.io

AngularJS

ab-datepicker can be used with this AngularJS directive written by @dadoadk

Advanced usage including Internationalization

First, place your "locales" file before "datepicker.min.js" For example, if your language is French, add this line :

<script type="text/javascript" src="path/to/locales/fr.min.js"></script>

and give some options to the datepicker. See Configuration options below.

    <script type="text/javascript">
    $(document).ready(function() {
    	$('.date').datepicker({
			firstDayOfWeek: 1, // The first day of week is Monday
			weekDayFormat: 'narrow', // Only first letter for the weekday names
			inputFormat: 'd/M/y',
			outputFormat: 'dd/MM/y',
			titleFormat: 'EEEE d MMMM y',
			markup: 'bootstrap4',
			theme: 'blue',
			modal: false
		});
    });
    </script>

npm.io npm.io

back to table of contents

Configuration options

OptionDescriptionDefaultExample
allowSameDateIn the case of linked dates (start date and end date), this option indicates whether the two dates can be the same. This option is ignored if none of the 'next' or 'previous' option is set.trueallowSameDate: false
buttonLabelAccessibility label : 'aria-labelledby' attribute for the calendar buttonDate.dp_locales.texts.buttonLabel (*)Cliquez ou appuyez sur la touche Entrée ou la barre d'espace pour ouvrir le calendrier
buttonLeftPlace the calendar button to the left of the text fieldfalsebuttonLeft: true
buttonTitleTitle attribute for the calendar buttonDate.dp_locales.texts.buttonTitle (*)Sélectionner une date ...
changeMonthButtonLabelAccessibility label : title attribute for the calendar title when the current month is displayedDate.dp_locales.texts.changeMonthButtonLabel (*)changeMonthButtonLabel: "Cliquez ou appuyez sur la touche Entrée ou la barre d'espace pour changer le mois"
changeRangeButtonLabelAccessibility label : title attribute for the calendar title when the current years range is displayedDate.dp_locales.texts.changeRangeButtonLabel (*)changeRangeButtonLabel: "Cliquez ou appuyez sur la touche ou sur la barre Entrée pour afficher les vingt années suivantes"
changeYearButtonLabelAccessibility label : title attribute for the calendar title when the current year is displayedDate.dp_locales.texts.changeYearButtonLabel (*)changeYearButtonLabel: "Cliquez ou appuyez sur la touche Entrée ou la barre d'espace pour changer l'année"
clientValidationWhether to apply HTML client-side form validation to prevent form submission when an invalid date is typed manually.trueclientValidation: false
closeButtonLabelAccessibility label : 'aria-labelledby' attribute for the close button when the calendar is displayed in modal modeDate.dp_locales.texts.closeButtonLabel (*)closeButtonLabel: "Fermez le calendrier"
closeButtonTitleTitle attribute for the close button when the calendar is displayed in modal modeDate.dp_locales.texts.closeButtonTitle (*)closeButtonTitle: "Fermer"
datesDisabledArray of dates to disable. array items are Date object or string[]datesDisabled: new Date(2017, 12, 11), '12/12/2017'
daysOfWeekDisabledArray of the week days number to disable[]daysOfWeekDisabled: 0, 6
firstDayOfWeekDetermines the first column of the calendar grid0 = Sunday, 1 = Monday, ....Date.dp_locales.firstday_of_week (*)firstDayOfWeek: 1
fontAwesomeSet to true to use Font Awesome icons instead of Glyphicons.falsefontAwesome: true
fontAwesomeProSet to true to use Font Awesome Pro instead of Free. Requires fontAwesome:true, and an FA Pro license.falsefontAwesomePro: true
gainFocusOnConstructionIndicates if the datepicker should gain focus when it is constructedtruegainFocusOnConstruction: false
inlineDisplay the datepicker inline inside a given divfalseinline: $('#inline-div')
inputFormatdate input format by the user. Dates and times are formatted according to CLDR Date Time patternsDate.dp_locales.short_format (*)inputFormat: 'd/M/y'
isDateDisabledA function that is called for each date of the days calendar. Must return true if the date provided as argument must be disabled, false otherwise.nullfunction (date) { return date < new Date(); }
isMonthDisabledA function that is called for each date of the days calendar and each month of the months calendar. Must return true if the month provided as arguments (year and month number - 1 to 12) must be disabled, false otherwise.nullfunction (year, month) { return year < 2000(year = 2000 && month < 7); }
isYearDisabledA function that is called for each date of the days calendar, each month of the months calendar and each year of the year calendar. Must return true if the year provided as argument must be disabled, false otherwise.nullfunction (year) { return year < 2000; }
linkFieldThe ID of a hidden field that will receive valueslinkField:'startDate'
linkFormatThe format to use for setting the value on linkField.linkFormat: 'yyyy-MM-dd'
markupThe html markup to usebootstrap3markup: 'bootstrap4'
maxThe maximum/latest date that can be selectednullmax: '06/12/2016'
minThe minimum/earliest date that can be selectednullmin: '08/11/2015'
minHourThe minimum hour that is included in the time panel.0minHour:9
maxHourThe maximum hour that is included in the time panel.23minHour:18
minuteIncrementsThe size of minute steps to provide as options in the time picker. Choose 60 to only allow whole hours.30minuteIncrements:15
modalIndicates if the calendar must be opened in modal mode, that is it keeps the focus until the user selects a date or clicks the close buttonfalsemodal: true
nextLink the target input of the datepicker to the input of another datepicker that represents the end date of a rangenullnext: '#enddate' or next: $('#enddate')
nextButtonLabelAccessibility label : 'aria-labelledby' attribute for the next month buttonDate.dp_locales.texts.nextButtonLabel (*)nextButtonLabel: "Aller au mois suivant"
nextMonthButtonLabelAccessibility label : 'aria-labelledby' attribute for the next year buttonDate.dp_locales.texts.nextMonthButtonLabel (*)nextMonthButtonLabel: "Aller à l'année suivante"
nextYearButtonLabelAccessibility label : 'aria-labelledby' attribute for the next years range buttonDate.dp_locales.texts.nextYearButtonLabel (*)nextYearButtonLabel: "Allez aux vingt années suivantes"
onUpdateA function that is called when the input box has been updated by the datepickerfunction (value) {}function (value) { console.log("date updated : " + value); }
outputFormatdate output format of the datepicker. Dates and times are formatted according to CLDR Date Time patternsDate.dp_locales.short_format (*)outputFormat: 'dd/MM/y'
prevButtonLabelAccessibility label : 'aria-labelledby' attribute for the previous month buttonDate.dp_locales.texts.prevButtonLabel (*)prevButtonLabel: "Aller au mois précédent"
previousLink the target input of the datepicker to the input of another datepicker that represents the start date of a rangenullprevious: '#startdate' or previous: $('#startdate')
prevMonthButtonLabelAccessibility label : 'aria-labelledby' attribute for the previous year buttonDate.dp_locales.texts.prevMonthButtonLabel (*)prevMonthButtonLabel: "Aller à l'année précédente"
prevYearButtonLabelAccessibility label : 'aria-labelledby' attribute for the previous years range buttonDate.dp_locales.texts.prevYearButtonLabel (*)prevYearButtonLabel: "Aller aux vingt années précédentes"
startViewset the start view mode. Accepts: 'days', 'months', 'years', 0 for days, 1 for months and 2 for years0startView: 'years'
themename of the themedefaulttheme: 'green'
titleFormatDates and times are formatted according to CLDR Date Time patternsDate.dp_locales.full_format (*)titleFormat: 'EEEE d MMMM y'
timeEnabledSet to true to add a time panel to the picker. You should include a time component in your input and output formats to make this useful.falsetimeEnabled: true
timeOnlyIf true, the calendar part of the picker is greyed out. timeEnabled must also be true. Typically used to edit the time part of an existing datetime.falsetimeOnly: true
weekDayFormatDisplay format of the weekday names- values are 'short' (first letter) or 'narrow' (first two letters)shortweekDayFormat: 'narrow'

(*) Default value from the locale file

back to table of contents

Methods

datesDisabled

$('your date selector').datepicker('datesDisabled', dates);

Sets a specific date or an array of dates to be disabled. The dates are Date objects or strings.

daysOfWeekDisabled

$('your date selector').datepicker('daysOfWeekDisabled', value);

where value (type: array of number) is the list of days of the week that should be disabled.

disable

$('your date selector').datepicker('disable');

Disable the datepicker. This method has no effect on an inline datepicker.

enable

$('your date selector').datepicker('enable');

Enable the datepicker.

firstDayOfWeek

$('your date selector').datepicker('firstDayOfWeek', value);

where value (type: integer) is the day of week (0: Sunday, 1: Monday, 2: Tuesday, ...)

getDate

$('your date selector').datepicker('getDate');

Get the current Datepicker date object

hide

$('your date selector').datepicker('hide');

hide the calendar portion of the datepicker

inline

$('your date selector').datepicker('inline', value);

Display the datepicker inline inside a given div. value can be the id of a div, a jQuery object of a div or false.

inputFormat

$('your date selector').datepicker('inputFormat', formats);

where formats (type: array of string) contains the acceptable input formats according to CLDR Date Time patterns

max

$('your date selector').datepicker('max', date);

Define the biggest date the user can enter. The value of "date" (type: string) must conform to one of the formats defined with the "inputFormat" option

min

$('your date selector').datepicker('min', date);

Define the smallest date the user can enter. The value of "date" (type: string) must conform to one of the formats defined with the "inputFormat" option.

next

$('your date selector').datepicker('next', selector);

Define the date as the start date of a period. The value of "selector" (type: string or JQuery object) refers to the input that represents the end date of the period.

previous

$('your date selector').datepicker('previous', selector);

Define the date as the end date of a period. The value of "selector" (type: string or JQuery object) refers to the input that represents the start date of the period.

modal

$('your date selector').datepicker('modal', bool);

Defines the opening mode (modal or not) of the calendar portion of the datepicker. bool can be true or false.

outputFormat

$('your date selector').datepicker('outputFormat', format);

where format (type: string) contains the output format according to CLDR Date Time patterns

setDate

$('your date selector').datepicker('setDate', date);

Returns the internal date object of the datepicker

show

$('your date selector').datepicker('show');

show the calendar portion of the datepicker

startview

$('your date selector').datepicker('startview', view);

where view (type: string|int) is the value of new start view: 'days', 'months', 'years', 0 for days, 1 for months and 2 for years

theme

$('your date selector').datepicker('theme', name);

where name (type: string) contains the name of your theme

weekDayFormat

$('your date selector').datepicker('weekDayFormat', value);

where value (type: string) is one of this value : 'short' (first letter) or 'narrow' (first two letters)

back to table of contents

Events

change

This event is triggered, bound to target input box, once the date is changed by the datepicker.

ab.datepicker.opening

This event is triggered, bound for all the datepicker of the page, before the display of the calendar portion of one of the datepicker

ab.datepicker.opened

This event is triggered, bound for all the datepicker of the page, after the display of the calendar portion of one of the datepicker

ab.datepicker.closed

This event is triggered, bound for all the datepicker of the page, when the calendar portion of one of the datepicker is closed

back to table of contents

Theming

  1. Give a name to your theme;
  2. Create a CSS file with the styles below, having replaced 'default' by the name of your theme. Use the properties 'color', 'background-color', 'border-color', .....
a.datepicker-button.default {
}
a.datepicker-button.default:hover,
a.datepicker-button.default:focus {
	background-color: #DDD;
}
div.datepicker-calendar.default {
	background-color: #FFF;
}
div.datepicker-calendar.default div.datepicker-month-wrap {
}
div.datepicker-calendar.default div.datepicker-month-fast-prev.disabled,
div.datepicker-calendar.default div.datepicker-month-fast-next.disabled ,
div.datepicker-calendar.default div.datepicker-month-prev.disabled,
div.datepicker-calendar.default div.datepicker-month-next.disabled {
	color:  #999;
}
div.datepicker-calendar.default div.datepicker-month-fast-prev.enabled:hover,
div.datepicker-calendar.default div.datepicker-month-fast-prev.enabled:focus,
div.datepicker-calendar.default div.datepicker-month-fast-next.enabled:hover,
div.datepicker-calendar.default div.datepicker-month-fast-next.enabled:focus,
div.datepicker-calendar.default div.datepicker-month-prev.enabled:hover,
div.datepicker-calendar.default div.datepicker-month-prev.enabled:focus,
div.datepicker-calendar.default div.datepicker-month-next.enabled:hover,
div.datepicker-calendar.default div.datepicker-month-next.enabled:focus,
div.datepicker-calendar.default div.datepicker-month:hover,
div.datepicker-calendar.default div.datepicker-month:focus,
div.datepicker-calendar.default button.datepicker-close:hover,
div.datepicker-calendar.default button.datepicker-close:focus {
	background-color: #EEE;
	border-color:  #999;
}
div.datepicker-calendar.default table.datepicker-grid:focus {
	outline: 1px dotted #999;
}
div.datepicker-calendar.default tr.datepicker-weekdays {
	border-top:  1px solid #999;
	border-bottom:  1px solid #999;
}
div.datepicker-calendar.default table.datepicker-grid th {
	background-color: #EEE;
	border:  none;
}
div.datepicker-calendar.default table.datepicker-grid td {
	border:  none;
	color: #000;
}
div.datepicker-calendar.default table.datepicker-grid td.unselectable {
	color: #999;
}
div.datepicker-calendar.default table.datepicker-grid td.curDay,
div.datepicker-calendar.default table.datepicker-grid td.curMonth,
div.datepicker-calendar.default table.datepicker-grid td.curYear {
	background-color: #FFF0C4;
}
div.datepicker-calendar.default table.datepicker-grid td.empty {
	border:  none;
	background-color: #F9F9F9;
}
div.datepicker-calendar.default table.datepicker-grid td.selectable:hover,
div.datepicker-calendar.default table.datepicker-grid td.selectable.focus {
	background-color: #DDD;
}
div.datepicker-calendar.default table.datepicker-grid td.empty:hover {
	background-color: #F9F9F9;
}
div.datepicker-calendar.default button.datepicker-close {
	background-color: #522A3F;
	color: #FFF;
	border-color:  #EEE;
}
  1. Add the CSS file to your page
  2. call the datepicker with the 'theme' option as follows :
$('your date selector').datepicker({
	............
	theme: 'the name of your theme',
	........
});

Keyboard interaction

  • Left Move focus to the previous day. Will move to the last day of the previous month, if the current day is the first day of a month.
  • Right Move focus to the next day. Will move to the first day of the following month, if the current day is the last day of a month.
  • Up Move focus to the same day of the previous week. Will wrap to the appropriate day in the previous month.
  • Down Move focus to the same day of the following week. Will wrap to the appropriate day in the following month.
  • PgUp Move focus to the same date of the previous month. If that date does not exist, focus is placed on the last day of the month.
  • PgDn Move focus to the same date of the following month. If that date does not exist, focus is placed on the last day of the month.
  • Alt+PgUp Move focus to the same date of the previous year. If that date does not exist (e.g leap year), focus is placed on the last day of the month.
  • Alt+PgDn Move focus to the same date of the following year. If that date does not exist (e.g leap year), focus is placed on the last day of the month.
  • Home Move to the first day of the month.
  • End Move to the last day of the month
  • Tab / Shift+Tab If the datepicker is in modal mode, navigate between calander grid and close/previous/next selection buttons, otherwise move to the field following/preceding the date textbox associated with the datepicker
  • Enter / Space Fill the date textbox with the selected date then close the datepicker widget.

back to table of contents

ARIA 1.0 Markup

back to table of contents

Browser Compatibility

  • osx: Chrome 10.0+
  • osx: Firefox 3.6+
  • osx: Opera 11.0+
  • osx: Safari 5.0+
  • win: Chrome 10.0+
  • win: Firefox 3.6+
  • win: Internet Explorer 8.0+
  • win: Opera 11.0+
  • win: Safari 5.0+

back to table of contents

Copyright and license

© 2015-2019 Eureka2 - Jacques Archimède. Code released under the MIT license.

back to table of contents

2.2.4

3 years ago

2.2.3

3 years ago

2.2.1

4 years ago

2.2.2

4 years ago

2.2.0

4 years ago