rykdesjardins / js-calendar

The lightest Javascript calendar out there, without any dependency.
MIT License
33 stars 7 forks source link

Feature requests #11

Open thewaywest opened 6 years ago

thewaywest commented 6 years ago

Thanks for this great, light calendar. I am using it with a Hugo website, and have applied a few hacks to simplify my use case. One or two of these might be useful to incorporate in a future version... :)

calendar.on('rendered', calendarRestate);
calendarRestate = function() {
    if (!desk) {
        // remove empty days
        [...document.getElementsByClassName("col-week-day-container")].forEach(function(e) {
            if (!e.querySelector(".cal-week-day-event-col")) {
                e.style.display = 'none';
                e.previousSibling.style.display = 'none';
            };
        });
    };
};

// add new events to the top of the list var events = [ { d:"2018-09-15 08:30", h:"8.5", r:"29", n:"Day of Mindfulness", c:"", l:"/pages/dayofmindfulness" }, { d:"2018-09-05 17:30", h:"2.5", r:"12,19,26", n:"Meditation", c:"", l:"/practices/practice" } ];

// js language resources var buttons = {}; buttons['en'] = { previous: "previous", today: "TODAY", next: "next" }; buttons['ca'] = { previous: "anterior", today: "AVUI", next: "pròxim" }; buttons['es'] = { previous: "anterior", today: "HOY", next: "próximo" };

var days = {}; days['en'] = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; days['ca'] = ["Diumenge", "Dilluns", "Dimarts", "Dimecres", "Dijous", "Divendres", "Disabtes"]; days['es'] = ["Domingo", "Lunes", "Martes", "Miércoles", "Jueves", "Viernes", "Sábado"];

var months = {}; months['en'] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; months['ca'] = ["Gener", "Fevrer", "Març", "Abril", "Maig", "Juny", "Juliol", "Agost", "Setembre", "Octubre", "Novembre", "Desembre"]; months['es'] = ["Enero", "Fevrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre"];

all of that is pulled into your matrix format prior to calendar rendering:
// calendar activation
var matrix = {};
events.forEach(function(e) {
    var dt = new Date(e.d),
        yr = dt.getFullYear(),
        mo = dt.getMonth(),
        dy = dt.getDate().toString(),
        hr = dt.getHours(),
        tm = dt.getTime();

    // process repeating days
    (dy + (e.r ? "," + e.r : "")).split(',').forEach(function(d) {
        // instantiate matrix elements if required
        if (!matrix[yr]) matrix[yr] = {};
        if (!matrix[yr][mo]) matrix[yr][mo] = {};
        if (!matrix[yr][mo][d]) matrix[yr][mo][d] = [];

        // add event to matrix
        var ev = { };
        if (e.n) ev.displayname = e.n;
        if (hr) ev.at = tm;
        if (e.h) ev.duration = parseInt(e.h) * 60 * 60 * 1000; // hours * minutes * seconds * milliseconds
        if (e.c) ev.color = e.c;

        matrix[yr][mo][d].push(ev);
    });
});

The last thing on my list is the ability to add more detail for flyover and linking, so those attributes are in my source arrays. I'd like to push them into the individual elements using data- strings, so that I can generate flyover dialogs using js. I bet I have to drop into your library for that one, though :)

Thanks for the work!
thewaywest commented 6 years ago

Since this is a flat site (no node), the module export fails. I have preceded that call with a quick test of "modules" to avoid errors in the build -- seems to be unobtrusive for other implementations...

lines 1207-1209:
(() => {
    if (typeof module !== 'undefined') module.exports = {JSCalendar, JSCalendarEvent}
})(JSCalendar, JSCalendarEvent)

I've hacked the source js to add three options that support my flat site needs -- a hundred characters or so, adding if () statements are listener instatin blocks, or before callog lines. If interested, let me know and I will check in:

    var calendar = new JSCalendar(calNode, {
        titleCropSize: 30,
        height: 500,
        width: (desk ? 1170 : winx - 20),
        buttonsVocab: buttons[lang],
        monthsVocab: months[lang],
        daysVocab: days[lang],
        ampm: false,
        noDrag: true,  <<< turn off draggability for read-only calendars
        noDrill: true, <<< diables drill-to-day from week or month view when not allowing switching
        noLog: true <<< disable the console log when not needed for debugging
    });