Code Monkey home page Code Monkey logo

calendar-javascript-lib's Introduction

Calendar Javascript Library

Well, I suppose I should help you set it up.

I'd start with the HTML file. Just create the containers for each of the calendar and the organizer, in the body, ofcourse.

<link href="stylesheet.css" rel="stylesheet" />

<div id="calendarContainer"></div>
<div id="organizerContainer"></div>

<script src="js/library.js"></div>

Then, start by creating a calendar object and an organizer object.

  • calendar for
    • previewing the calendar
  • organizer for
    • handling calendar clicks
    • displaying events

The code will be similar to this

var calendar = new Calendar("calendarContainer", "small", [ "Wednesday", 3 ], [ "#e91e63", "#c2185b", "#ffffff", "#f8bbd0" ]);
var organizer = new Organizer("organizerContainer", calendar);

Ok, ok, I'll take it easy and start by explaining each parameter. For the

  • calendar
    • "calendarContainer" -> the container that will hold the calendar
    • "small" -> the size; ranges between
      • small ( width: 400px, height: 400px )
      • medium ( width: 600px, height: 600px )
      • large ( width: 800px, height: 800px )
    • [ "Wednesday", 3 ] ->
      • First is the first day of the week ( any of the 7 you want )
      • Second is the number of characters the days of the week labels should be
    • [ "#e91e63", "#c2185b", "#ffffff", "#f8bbd0" ]
  • organizer
    • "organizerContainer" -> the container that will hold the organizer
    • "calendar" -> the calendar object to associate the organizer to, so that it can have the same height and width and set up the listeners of that object

Well now, I'm gonna show you the advised format for the events, making it easier to collect them using JOIN in SQL

data = {
  years: [ {
      int: (new Date().getFullYear()), months: [ {
          int: (new Date().getMonth() + 1), days: [ {
              int: (new Date().getDate()), events: [ {
                  startTime: "6:00",
                  endTime: "7:00",
                  mTime: "am",
                  text: "This is scheduled to show today, anyday."
                }, {
                  startTime: "5:45",
                  endTime: "7:15",
                  mTime: "pm",
                  text: "WIP Library"
                }, {
                  startTime: "10:00",
                  endTime: "11:00",
                  mTime: "pm",
                  text: "Probably won't fix that (time width)"
              } ] 
          } ] 
      } ]
  } ] 
};

Woops, I did it again. I guess though this time that the events of a specific day and their formats is pretty obvious and self explanatory. Oh, again, this should be grabbed from a database or a JSON file. The library is not responsible for grabbing JSON to use. The new Date() then the methods connected to it makes these events show today, and by today I mean anyday's today.

Ofcourse, after that, we will need to display each day's events. Well, I did that by...

function showEvents() {
  theYear = -1, theMonth = -1, theDay = -1;
  for (i = 0; i < data.years.length; i++) {
    if (calendar.date.getFullYear() == data.years[i].int) {
      theYear = i;
      break;
    }
  }
  if (theYear == -1) return;
  for (i = 0; i < data.years[theYear].months.length; i++) {
    if ((calendar.date.getMonth() + 1) == data.years[theYear].months[i].int) {
      theMonth = i;
      break;
    }
  }
  if (theMonth == -1) return;
  for (i = 0; i < data.years[theYear].months[theMonth].days.length; i++) {
    if (calendar.date.getDate() == data.years[theYear].months[theMonth].days[i].int) {
      theDay = i;
      break;
    }
  }
  if (theDay == -1) return;
  theEvents = data.years[theYear].months[theMonth].days[theDay].events;  
  organizer.list(theEvents); // what's responsible for listing
}

Now as commented, the organizer.list(theEvents) is what is responsible for listing the stuff. And, oh, theEvents is the array of events of a specific day. Umm. I am having second thoughts about including that in the list method. Anyway, if there's a necessity fork this. Please. And what preceeds the listing method, is the getting the calendar's focused date, and matching it with the event's year, then month, consequently day. calendar.date is how you'll get the chosen date on the calendar of that calendar object. After deep thinking, I might've left that showing of the events for you because of terrible practices ( hinting about that data array ).

Moving away from that don't forget to call that function in order to list the events on calendar load. For me I placed it like this, directly after the function. ( also after the calendar and organizer declaration )

showEvents();

Last but not least, time to declare the listeners for the year, month, day sliders and the day blocks on the calendar.

organizer.setOnClickListener('day-slider', function () { showEvents(); console.log("Day back slider clicked"); }, function () { showEvents(); console.log("Day next slider clicked"); });
organizer.setOnClickListener('days-blocks', function () { showEvents(); console.log("Day block clicked"); }, null);
organizer.setOnClickListener('month-slider', function () { showEvents(); console.log("Month back slider clicked"); }, function () { showEvents(); console.log("Month next slider clicked"); });
organizer.setOnClickListener('year-slider', function () { showEvents(); console.log("Year back slider clicked"); }, function () { showEvents(); console.log("Year next slider clicked"); });

This way you can pass any functions in order to run after switching the day to the desired one. Oh, so the second parameter is the back callback. While the third is the next callback. Next and back are only used with sliders. So just the second parameter is needed with blocks. The only clicklisteners can be set to 'day-slider', 'month-slider', 'year-slider', 'day-blocks'. I haven't tried passing null for the parameters that I haven't shown as null here. But I doubt that you will need to pass them null since you'll have to show the events.

So the final javascript code will be

var calendar = new Calendar("calendarContainer", "small", [ "Wednesday", 3 ], [ "#e91e63", "#c2185b", "#ffffff", "#f8bbd0" ]);
var organizer = new Organizer("organizerContainer", calendar);

data = {
  years: [ {
      int: (new Date().getFullYear()), months: [ {
          int: (new Date().getMonth() + 1), days: [ {
              int: (new Date().getDate()), events: [ {
                  startTime: "6:00",
                  endTime: "7:00",
                  mTime: "am",
                  text: "This is scheduled to show today, anyday."
                }, {
                  startTime: "5:45",
                  endTime: "7:15",
                  mTime: "pm",
                  text: "WIP Library"
                }, {
                  startTime: "10:00",
                  endTime: "11:00",
                  mTime: "pm",
                  text: "Probably won't fix that (time width)"
              } ] 
          } ] 
      } ]
  } ] 
};

function showEvents() {
  theYear = -1, theMonth = -1, theDay = -1;
  for (i = 0; i < data.years.length; i++) {
    if (calendar.date.getFullYear() == data.years[i].int) {
      theYear = i;
      break;
    }
  }
  if (theYear == -1) return;
  for (i = 0; i < data.years[theYear].months.length; i++) {
    if ((calendar.date.getMonth() + 1) == data.years[theYear].months[i].int) {
      theMonth = i;
      break;
    }
  }
  if (theMonth == -1) return;
  for (i = 0; i < data.years[theYear].months[theMonth].days.length; i++) {
    if (calendar.date.getDate() == data.years[theYear].months[theMonth].days[i].int) {
      theDay = i;
      break;
    }
  }
  if (theDay == -1) return;
  theEvents = data.years[theYear].months[theMonth].days[theDay].events;  
  organizer.list(theEvents); // what's responsible for listing
}

showEvents();

organizer.setOnClickListener('day-slider', function () { showEvents(); console.log("Day back slider clicked"); }, function () { showEvents(); console.log("Day next slider clicked"); });
organizer.setOnClickListener('days-blocks', function () { showEvents(); console.log("Day block clicked"); }, null);
organizer.setOnClickListener('month-slider', function () { showEvents(); console.log("Month back slider clicked"); }, function () { showEvents(); console.log("Month next slider clicked"); });
organizer.setOnClickListener('year-slider', function () { showEvents(); console.log("Year back slider clicked"); }, function () { showEvents(); console.log("Year next slider clicked"); });

Well a good demonstration is the example.html Or you can check [CodePen] (https://codepen.io/nizarmah/pen/LkjjWV)

Have fun. Later.

calendar-javascript-lib's People

Contributors

nizarmah avatar

Watchers

James Cloos avatar Charles avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.