SwiftCal - A Calendar and Time Picker Web Component
Default calendar: select a range by clicking/tapping on two dates, select a single data by triple clicking.
Usage
Loading the component
Load the ./clientSideJS/calendar.js in your page passing the defer attribute,
<script defer src="./clientSideJS/calendar.js"></script>
Or from a CDN:
<script defer src="https://cdn.jsdelivr.net/gh/festinalente/SwiftCal2/clientSideJS/calendar.js"></script>
Then add a tag to your markup where you want the calendar,
<swift-cal></swift-cal>
Options are passed as data-attributes. The example bellow sets the calendar to display 12 months into the future:
<swift-cal data-number-of-months-to-display='12'></swift-cal>
Retrieving data
Data is retrieved directly from the WebComponent, you can access it with a querySelector. The function dynamicData() returns the data:
document.querySelector('.calendar-container').dynamicData()
The dynamicData object
Data from the calendar is stored in an object called "dynamicData". This object can be accessed directly from the WebComponent
with the calendar.dynamicData().
The example below shows the form the data takes:
Custom Events
You can listen for an event that is fired every time a selection is made, there are two custom events:
- timeSelect - fired when a time is selected
- dateSelect - fired when a date is selected
You can listen for these as you would with a normal event:
calendar.addEventListener('timeSelect', function () { ... });
Developing and customising
To customize the calendar, follow these steps:
- git clone https://github.com/festinalente/SwiftCal2.git
- cd to the folder SwiftCal2 and run "npm install"
- then run "grunt watch
Now any changes you make will be written to and compiled (making the code backward compatible) automatically.
Appearance
To simply change appearance without breaking anything, the best thing to do is to alter the variables in variables.scss.
If you are a designer however and can make it look better, consider submitting a pull request.
Language and text
The easiest way to change language and or text is to edit the values in ./preBundlingJS/languages.js
Instantiation and Options
Instantiation via HTML
The calendar can be used as a web component by simply adding an element to the page
<swift-cal></swift-cal>
Options passed in data attributes using dash case
Various options can be passed to the calendar as data parameters. These parameters are string representations
of primitives e.g. int, boolean, string etc.
- data-number-of-months-to-display='12' - The number of months the calendar displays (whole number)
- data-display-time-chooser-modal='true' - Toggle time chooser on date select (Boolean)
- data-single-date-choice='true' - Limit date choice to a single day (Boolean)
- data-language='enGb' - Set language: 'enGb' and 'ptPt' available now
- data-selected-multiple='true' - Allow multiple dates to be selected (Boolean)
- data-preloaded-dates='["2023-10-30","2023-10-31","2023-11-01"]'- Allows dates to be added to the calendar
and selection limited to those dates
- data-preloaded-tooltip - Sets a title attribute on each preloaded date
- data-block-days-of-week='[6]' - Allows days of the week to be blocked (string representation of an array of the
indexes (Sunday, 0; Monday, 1; Tuesday 2; Wednesday, 3; Thursday, 4; Friday, 5; Saturday, 6))
- data-book-days-of-week='[6]' - Allows a weekly reoccurring booking of a day (string representation of an array of the
indexes (Sunday, 0; Monday, 1; Tuesday 2; Wednesday, 3; Thursday, 4; Friday, 5; Saturday, 6))
- data-start-date='YYYY-MM-DD' - Set a start date other than the current date
Instantiation via JavaScript
The calendar can be instantiated via JavaScript and parameters passed in an object. The big difference is the requirement to pass
a parent element to the calendar.
Example code used to instantiate the first calendar at the top of this page:
const cal = new calendar.SwiftCal();
cal.generateCalendar({
parentDiv: '.eg',
numberOfMonthsToDisplay: 3,
displayTimeChooserModal: false
});
Options passed via an object to the generateCalendar() function using camel case -these are otherwise the same as the options passed to in html data attributes:
- parentDiv: HTML querySelector // a valid querySelector,
- numberOfMonthsToDisplay: int // any whole number,
- displayTimeChooserModal: boolean // true or false,
- singleDateChoice:boolean // true or false,
- language: string // 'enGb' and 'ptPt' available now,
- selectedMultiple: boolean // true or false,
- preloadedDates: array of string // ["2023-10-30","2023-10-31","2023-11-01"],
- preloadedTooltip: string // text to display on a preloaded date on hover,
- blockDaysOfWeek: string // array with indexes to block (0-6), e.g. [6] for Saturday,
- bookDaysOfWeek: string // array with indexes to block (0-6), e.g. [6] for Saturday,
- startDate: string // Date in the form "YYYY-MM-DD" for the calendar to start at any date other than the current moment.
Examples:
TODO
- A method to pick predefined times. (e.g. to schedule an appointment),
- An option to disallow selecting end times that are before start times,
- Ability to add notes or information to a day and to specific time blocks,
- Write tests.