Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,31 +61,32 @@ This method creates a calendar instance and associates it to the provided `elem`

Creating a calendar has a ton of options. These have reasonable defaults that are easy to adjust, too. The options are listed below.

Option | Description
-------------------|--------------------------------------------------------------------------------------------------
`appendTo` | DOM element where the calendar will be appended to. Takes `'parent'` as the parent element
`autoClose` | When set to `true`, the calendar is auto-closed when picking a day _(or a time if `time: true` and `date: false`). A value of `'time'` will only auto-close the calendar when a time is picked.
`autoHideOnBlur` | Hides the calendar when focusing something other than the input field
`autoHideOnClick` | Hides the calendar when clicking away
`date` | The calendar shows days and allows you to navigate between months
`dateValidator` | Function to validate that a given date is considered valid. Receives a native `Date` parameter.
`dayFormat` | Format string used to display days on the calendar
`initialValue` | Value used to initialize calendar. Takes `string`, `Date`, or `moment`
`inputFormat` | Format string used for the input field as well as the results of `rome`
`invalidate` | Ensures the date is valid when the field is blurred
`strictParse` | Compares input strictly against `inputFormat`, and partial matches are discarded
`max` | Disallow dates past `max`. Takes `string`, `Date`, or `moment`
`min` | Disallow dates before `min`. Takes `string`, `Date`, or `moment`
`monthFormat` | Format string used by the calendar to display months and their year
`monthsInCalendar` | How many months get rendered in the calendar
`required` | Is the field required or do you allow empty values?
`styles` | CSS classes applied to elements on the calendar
`time` | The calendar shows the current time and allows you to change it using a dropdown
`timeFormat` | Format string used to display the time on the calendar
`timeInterval` | Seconds between each option in the time dropdown
`timeValidator` | Function to validate that a given time is considered valid. Receives a native `Date` parameter.
`weekdayFormat` | Format used to display weekdays. Takes `min` _(Mo)_, `short` _(Mon)_, `long` _(Monday)_, or an array with seven strings of your choosing.
`weekStart` | Day considered the first of the week. Range: Sunday `0` - Saturday `6`
Option | Description
----------------------|--------------------------------------------------------------------------------------------------
`appendTo` | DOM element where the calendar will be appended to. Takes `'parent'` as the parent element
`autoClose` | When set to `true`, the calendar is auto-closed when picking a day _(or a time if `time: true` and `date: false`). A value of `'time'` will only auto-close the calendar when a time is picked.
`autoHideOnBlur` | Hides the calendar when focusing something other than the input field
`autoHideOnClick` | Hides the calendar when clicking away
`date` | The calendar shows days and allows you to navigate between months
`dateValidator` | Function to validate that a given date is considered valid. Receives a native `Date` parameter.
`dayFormat` | Format string used to display days on the calendar
`initialValue` | Value used to initialize calendar. Takes `string`, `Date`, or `moment`
`inputFormat` | Format string used for the input field as well as the results of `rome`
`invalidate` | Ensures the date is valid when the field is blurred
`strictParse` | Compares input strictly against `inputFormat`, and partial matches are discarded
`max` | Disallow dates past `max`. Takes `string`, `Date`, or `moment`
`min` | Disallow dates before `min`. Takes `string`, `Date`, or `moment`
`monthFormat` | Format string used by the calendar to display months and their year
`monthsInCalendar` | How many months get rendered in the calendar
`required` | Is the field required or do you allow empty values?
`styles` | CSS classes applied to elements on the calendar
`time` | The calendar shows the current time and allows you to change it using a dropdown
`timeFormat` | Format string used to display the time on the calendar
`timeInterval` | Seconds between each option in the time dropdown
`timeValidator` | Function to validate that a given time is considered valid. Receives a native `Date` parameter.
`weekdayFormat` | Format used to display weekdays. Takes `min` _(Mo)_, `short` _(Mon)_, `long` _(Monday)_, or an array with seven strings of your choosing.
`weekStart` | Day considered the first of the week. Range: Sunday `0` - Saturday `6`
`horizontalAlignment` | Defines the alignment of calendar with the `input` element: `left`, `center`, `right`

Note that in the case of input fields, when `initialValue` isn't provided the initial value is inferred from `elem.value` instead. In the case of inline calendars, `new Date()` will be used as a default if none is provided.

Expand Down
41 changes: 34 additions & 7 deletions src/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function calendar (calendarOptions) {
api.getDate = getDate;
api.getDateString = getDateString;
api.getMoment = getMoment;
api.getRefCal = getRefCal
api.hide = hide;
api.next = addMonth;
api.options = changeOptions;
Expand All @@ -81,6 +82,10 @@ function calendar (calendarOptions) {
api.setValue = setValue;
api.show = show;

api.changeMonth = changeMonth;
api.subtractMonth = subtractMonth;
api.addMonth = addMonth;

eventListening();
ready();

Expand Down Expand Up @@ -116,6 +121,11 @@ function calendar (calendarOptions) {
api.restore = init;
api.setValue = napi;
api.show = napi;

api.changeMonth = napi;
api.subtractMonth = napi;
api.addMonth = napi;

api.off();

if (silent !== true) {
Expand Down Expand Up @@ -167,8 +177,13 @@ function calendar (calendarOptions) {
renderMonth(i);
}

crossvent.add(back, 'click', subtractMonth);
crossvent.add(next, 'click', addMonth);
// Have to pass in functions that call the API otherwise you can't overload
crossvent.add(back, 'click', function () {
api.subtractMonth();
})
crossvent.add(next, 'click', function () {
api.addMonth();
});
crossvent.add(datewrapper, 'click', pickDay);

function renderMonth (i) {
Expand Down Expand Up @@ -314,15 +329,15 @@ function calendar (calendarOptions) {

function subtractMonth () { changeMonth('subtract'); }
function addMonth () { changeMonth('add'); }
function changeMonth (op) {
function changeMonth (op, silent) {
var bound;
var direction = op === 'add' ? -1 : 1;
var offset = o.monthsInCalendar + direction * getMonthOffset(lastDayElement);
refCal[op](offset, 'months');
bound = inRange(refCal.clone());
ref = bound || ref;
if (bound) { refCal = bound.clone(); }
update();
update(silent);
api.emit(op === 'add' ? 'next' : 'back', ref.month());
}

Expand Down Expand Up @@ -426,12 +441,18 @@ function calendar (calendarOptions) {
return api;
}

function setValue (value) {
function setValue (value, force) {
var date = parse(value, o.inputFormat);
if (date === null) {
return;
}
ref = inRange(date) || ref;

if (force) {
ref = value
} else {
ref = inRange(date) || ref;
}

refCal = ref.clone();
update(true);

Expand Down Expand Up @@ -591,7 +612,9 @@ function calendar (calendarOptions) {
if (classes.contains(target, o.styles.dayDisabled) || !classes.contains(target, o.styles.dayBodyElem)) {
return;
}
var day = parseInt(text(target), 10);
var t = text(target)
var parsed = ref.localeData().preparse(t)
var day = parseInt(parsed, 10);
var prev = classes.contains(target, o.styles.dayPrevMonth);
var next = classes.contains(target, o.styles.dayNextMonth);
var offset = getMonthOffset(target) - getMonthOffset(lastDayElement);
Expand Down Expand Up @@ -662,6 +685,10 @@ function calendar (calendarOptions) {
function getMoment () {
return ref.clone();
}

function getRefCal () {
return refCal
}
}

module.exports = calendar;
2 changes: 2 additions & 0 deletions src/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ function defaults (options, cal) {
if (o.monthFormat === no) { o.monthFormat = 'MMMM YYYY'; }
if (o.dayFormat === no) { o.dayFormat = 'DD'; }
if (o.styles === no) { o.styles = {}; }

if (o.horizontalAlignment === no) { o.horizontalAlignment == 'left'; }

o.styles._isStylesConfiguration = true;

Expand Down
2 changes: 1 addition & 1 deletion src/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function inputCalendar (input, calendarOptions) {
input.value = o.initialValue.format(o.inputFormat);
}

eye = bullseye(api.container, input);
eye = bullseye(api.container, input, { horizontalAlignment: o.horizontalAlignment });
api.on('data', updateInput);
api.on('show', eye.refresh);

Expand Down