JavaScript
  Home arrow JavaScript arrow Page 4 - Useful Web Widgets
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Useful Web Widgets
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2006-01-18

    Table of Contents:
  • Useful Web Widgets
  • Getting Down With Father Time
  • The Function in Depth
  • A Calendar Engine
  • The Interface Code

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Useful Web Widgets - A Calendar Engine


    (Page 4 of 5 )

    The third and final script is an object engine that can be used to produce a calendar. It's just the internal engine that produces the data for a calendar over a range of years so that once the object is created, you can loop through it to produce a calendar in any user interface you wish. This, as I mentioned before, enables it to be used in any ECMA script based language that supports the code it uses. You could just as easily use this object in a Flash movie as you could in an HTML page.

    //*******************Calender Object**************************
    function calender(yearLow,yearHigh)
    {
                this.yearLow = yearLow;
                this.yearHigh = yearHigh;
                this.years = new Array();
                this.compile = compile;
                this.compile();
    }
    function compile()
    {
                if(this.yearHigh != this.yearLow)
                {
                            var range = this.yearHigh - this.yearLow;
                }
                else
                {
                            var range = 1;
                            this.yearHigh++;
                }
                for(var i = this.yearLow;i < this.yearHigh;i++)
                {
                            this.years[i] = new year(i);
                }
    }
    //**********************End Calender Object*************************

    The main object is the calendar object. A range of years can be given or the "yearHigh" and "yearLow" could be the same. Each year will be represented by a year object which is put into the "years" array.

    //**********************Year Object***************************
    function year(num)
    {
                this.number = num;
                this.monthArray = new Array("January","Febuary","March","April","May","June","July",
    "August","September","October","November","December")
                this.monthObjects = new Array();
                this.addMonths = addMonths;
                this.addMonths();
    }
    function addMonths()
    {
                for(var i = 0;i < 12;i++)
                {
                            this.monthObjects[i] = new month
    (i,this.number,this.monthArray[i]);
                }
    }
    //*************************End Year Object***********************

    The year object creates month objects for each month. Each month is sent the number of the month, the year and the text label of the month.

    //******************Month Object************************
    function month(num,year,name)
    {
                this.number = num;
                this.textName = name;
                this.year = year;
                this.days = 0;
                this.start = 0;
                this.end = 0;
                this.panes = 36;
                this.dayObjects = new Array();
                this.setDays = setDays;
                this.setStart = setStart;
                this.setEnd = setEnd;
                this.addDays = addDays;
                this.setDays();
                this.setStart();
                this.setEnd()
                this.addDays();
    }
    function setDays()
    {
                this.days = 32 - new Date(this.year, this.number,
    32).getDate();
    }
    function setStart()
    {
                this.start = new Date(this.year,this.number,1).getDay
    ();
                if((this.start == 5 && this.days == 31) ||
    (this.start > 5 && this.days > 28))
                {
                            this.panes = 43;
                }
    }
    function setEnd()
    {
                if(this.start > 0)
                {
                            this.end = (this.start) + this.days;
                }
                else
                {
                            this.end = this.days;
                }
    }
    function addDays()
    {
                var count = 1;
                var count2 = 1;
                for(var i = 0;i < this.panes;i++)
                {
                            if(i >= this.start && i < this.end)
                            {
                                        this.dayObjects[i] = new day
    (count)
                                        count++;
                            }
                            else
                            {
                                        this.dayObjects[i] = new day
    ("");
                                        count2++;
                            }
                }
    }
    //**********************End Month Object*******************

    Each month object determines the number of days it needs and creates a day object for each. Determining the number of days in the month relies on the fact that if you feed a day number to the date object that is out of range (32), it will give you a date in the next month. Subtracting the result from 32 will give you the number of days in the month.

     It also creates days for the "blank" days in the calendar. In other words, if the first day of the month starts on Tuesday, a "blank" day object will be created for each day of the week before that. The same goes for "blank" day panes at the end of the calendar. The calendar will expand from 35 panes to 42 when needed depending on the number of days in the month and the day of the first of the month.

    The final object is the day object. It simply contains the number of the day and has no methods of its own. You could store event information for each day in this object if you wished.

    function day(num)
    {
                this.displayNumber = num;
    }
    //**************End Day Object*************************

    It's easy to loop through this tree of objects to create a calendar. The input to this function could come from drop down menus for the month and year. An array of events could be written into the page using a server side language or retrieved via AJAX and fed to the function for placing event text or links for events in specific day panes.

    First we'll start with some html to put the calendar into as well as some controls to determine what year and month we need.

    <form>
        <label>Select a Year
             <select id="month">
                 <option value="0">January</option>
                 <option value="1">February</option>
                 <option value="2">March</option>
                 <option value="3">April</option>
                 <option value="4">May</option>
                 <option value="5">June</option>
                 <option value="6">July</option>
                 <option value="7">August</option>
                 <option value="8">September</option>
                 <option value="9">October</option>
                 <option value="10">November</option>
                 <option value="11">December</option>
             </select>
         </label>
         <label>Select a Year
             <select id="year">
                 <option value="2005">2005</option>
                 <option value="2006">2006</option>
                 <option value="2007">2007</option>
                 <option value="2008">2008</option>
             </select>
         </label>
         <input type="button" value="View the Calendar"
    onClick="calendarDisplay()">
    </form>
    <br /><br />
    <table id="calendarTable">
    </table>

    The button in this example calls a function called "calendarDisplay" which starts the ball rolling. The first thing that we need to do is make sure the table element for the calendar is clear to make way for a new calendar. I use the "innerHTML" property which is a Microsoft invention. There are ways to clear the contents of an element using the DOM which you can certainly use if you wish. You may need to do some method or object sniffing however, since creating a range in IE is not done the same way it is in other browsers.

    We then retrieve values from our month and year dropdowns, and call the "getCalendarMonth" function. For example purposes there is an array called "cdata" that we will use to insert events into our calendar. That array, along with the selected year and the selected month, is passed to "getCalendarMonth".

    var cdata = new Array("","","","Doctor visit","","Get Car Fixed","","","","vacation","vacation","vacation","vacation",
    "vacation","vacation","vacation","vacation","vacation",
    "vacation","vacation","","","Bob's birthday");
    function calendarDisplay()
    {
                var table = document.getElementById("calendarTable");
                table.innerHTML = "";
                var mobj = document.getElementById("month");
                var yobj = document.getElementById("year");
                var month = mobj.options[mobj.selectedIndex].value;
                var year = yobj.options[yobj.selectedIndex].value;
                getCalendarMonth(cdata,year,month);
    }

    More JavaScript Articles
    More By Chris Root


       · Thank you for reading the article. Comments are welcome.
     

    JAVASCRIPT ARTICLES

    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget
    - Ajax Hack for Entering Information Without R...
    - EXT JS 2.1 Overview
    - Using the Style Object for Zebra Tables with...
    - Binary Searching
    - An Improved Approach to Building Zebra Tables






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT