1.1.0 • Published 6 years ago

vanilla-js-calendar v1.1.0

Weekly downloads
12
License
MIT
Repository
github
Last release
6 years ago

JSCalendar

The lightest Javascript calendar out there, without any dependency (no jQuery). This is a work in progress, but it is ready to be used in production.

The development files are using es2016, so I used babel with the es2015 preset and babily to minify the whole thing. Distribution files can be found under ./dist/. There are both normal and minified files.

Sailor's honour

If other kind developers want to lend a hand, I will obviously stop pushing directly to master and switch to a dev branch ;)

EDIT : This now has a dev branch. Thanks to @jamacon36, I will probably be maintaining this library again.

Installation

Using git

git clone https://github.com/rykdesjardins/js-calendar

Using NPM

npm install --save vanilla-js-calendar

Using bower

bower install --save vanilla-js-calendar

Basic usage

var elem = document.getElementById("myCalendar");
var calendar = new JSCalendar(elem, { /* options */ }).init().render();

Imported Usage

import * as LibName from "vanilla-js-calendar"

var elem = document.getElementById("myCalendar");
var JSCalendar = LibName.JSCalendar;
var JSCalendarEvent = LibName.JSCalendarEvent;
var calendar = new JSCalendar(elem, { /* options */ }).init().render();

Latest tag

1.0.10

Simple design and fast rendering

demoscreenshot

Options

Option nameUsageDefault
viewsArray of available views"day", "week", "month"
viewsVocabObject of words used to represent the views { view : "display name" }{day : "Day", week : "Week", month : "Month" }
buttonsAvailable buttons in the top control bar"previous", "today", "next"
buttonsActionsJSCalendar function, or user-defined function object for top buttons { action : "functionName" || function }{ previous : "goBack", today : "goNow", next : "goNext" }
buttonsVocabObject of words used to represent the buttons { action : "display name" }{previous : "<", today : "Today", next : ">"}
monthsVocabArray of display names for every months"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
daysVocabArray of display names for every week day"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
emptyDayVocabDisplay phrase when no event is found for a partical day"Nothing here."
noDateWord used when an event has no time at a certain date"Somewhen"
nonameEventVocabWords used when an event does not have a name"Event without a name"
globalSelectorQuery Selector used to search for JSCalendars in the DOM".js-calendar"
eventBackgroundDefault event background color"rgb(126, 156, 193)"
titleCropSizeNumber of letter max for events in the month view20
datasourceURL used to request matrix data""
datasourceHeadersHeaders to be sent to the data source on every request{}
ampmFlag, setting it to true will use 12h system, false will use 24htrue
displaySecondsFlag, setting it to true will display the seconds for the events in the calendarfalse
dayviewGapMinutesNumber of minutes between each block in day view30
dayviewNoTimeGapSizeNumber of block used in day view for events without a time2
alwaysFetchWether to always fetch from server or notfalse
fetchReplacesWether to replace the entire matrix after fetching, false for appendfalse
dayviewGapHeightHeight of a block in day view, in pixels38
heightMaximum height for the calendar, if it goes beyond, scroll bars are added700
widthMaximum width for the calendar, if it goes beyond, the rest will be hidden. Can also be "full" for responsive full width.1024

Hooks

Hooks can be used to customize certain events and alter the way data is fetched, processed and rendered.

// Hooks on a specific instance
var calendarInstance = new JSCalendar(elem);
calendarInstance.on('eventName', (calendar, extra) => {
    // calendar is the current calendar instance
    // extra contains various data depending on the event
});

// Hooks on all instances
JSCalendar.on('eventName', (calendar, extra) => {
    // Arguments are the same as above
});
Hook nameContext
newFired when a JSCalendar object is built using the default constructor.
initFired when the initialization logic is being processed for a newly built object. Fired before the DOM structure is created.
willPushFired before adding an item to the calendar dynamically.
pushedFired after adding an item to the calendar dynamically.
didNotPushFired after failing to add an item to the calendar dynamically.
willRenderFired before rendering the calendar, also before fetching data from the data source if specified.
renderedFired after the render logic was processed.
draggingFired when an event is grabbed and ready to be moved around.
droppedFired when an event is dropped, the event cellMightMove will be triggered right after this one.
cellMightMoveFired when a cell movement is validated.
cellMovedFired after an event was moved from a cell to another.
cellDidNotMovedFired when a cell could have moved, but the validation failed or the event was dropped onto its original cell.
matrixWillSetFired when a matrix is sent to replace the current one in the calendar's state.
matrixSetFired after a matrix was set as the current matrix state.
matrixWillAppendFired when another matrix is about to be inserted into the current matrix state
matrixAppendedFired after another matrix was inserted into the current matrix state
willFetchFired before sending a fetch request to a data source.
fetchedFired after receiving data from the data source.
viewWillChangeFired before rendering a new view (month, week, day).
viewChangedFired after changing view. This will not fire if the view was set to the current one (wasn't changed).

Static functions

Those can be called using the class directly

// Will instanciate all calendars found under a certain wrapper
JSCalendar.find(document.getElementById("calendar-area"));

// Will return an instance of a calendar automatically generated using JSCalendar.find()
JSCalendar.getInstance(instanceid);

// Fires an event to all calendars from a specified instance
JSCalendar.fire(event, instance, extra);

/* 
    Returns an object : 
    { 
        numberOfDays : number,  // Total number of days in the month
        firstDay : number       // Week day of the 1st of the month
    }
*/
JSCalendar.getDaysInMonth(year, month);

Instance functions

Associated with all JSCalendar instanciated objects. Chainable functions return "this" so you can chain multiple functions on one line.

// Must be called after instanciating [chainable]
calendar.init(callback)

// Previous, today, next [chainable]
// Those are called when pressing the direction buttons on the top bar. 
// The calendar will be rendered at the end
calendar.goBack();
calendar.goNow();
calendar.goNext();

// Adding an item to the matrix [chainable]
/*
    The first parameter is an object : 
    {
        at : Date,
        event : JSCalendarEventTemplate { 
            at : Date, 
            displayname : string, 
            duration : number,  
            color : string
        }
    }
*/
calendar.push(event);

// Destroy calendar reference [chainable]
calendar.destroy();

// Switch from current view to another [chainable]
/*
    First parameter is on of the following strings : 
        month
        week
        day
*/
calendar.setView(view);

// Refresh and rebuild current view [chainable]
calendar.render();

// Merge a new matrix into the current state matrix [chainable]
/*
    A matrix is an object formatted like the following :
    matrix[year][month][day] = [JSCalendarEventTemplate];
    
    The second parameter is a flag (true|false). If true, 
    the calendar will render after setting the new matrix
*/
calendar.appendMatrix(matrix, rerender);

// Set the current state matrix, and destroy the old one if any [chainable]
calendar.setMatrix(matrix, rerender);

// If a data source is specified, fetch a new matrix to append [chainable]
/*
    This will request to the data source specified with query parameters
    like the following : 
    year = current state year
    month = current state month
    day = current state day
    
    Once finished, the provided callback will be executed
    The second parameter is a flag (true|false), if set to true, will
    fetch even is a cached version of the matrix exists.
*/
calendar.fetch(callback, force);

More screenshots

No conflicts with overlapping events

demoscreenshot

Week view

demoscreenshot