home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Java / ScriptBuilder / NOSB30_TRIAL.exe / data1.cab / Program_Files / CompLib / Calendar / Calendar.js < prev    next >
Encoding:
JavaScript  |  1998-10-05  |  15.7 KB  |  482 lines

  1. /* ======================================================================
  2. OBJECT:        com_netobjects_Calendar
  3.  
  4. DESC:            This object creates an interactive calendar.  Developers can specify
  5.                 a selected date and render a calendar for that month.  Developers write 
  6.                 custom functions to populate the calendar with data or respond to user clicks.
  7.                 Users can navigate the calendar and select a specific date.
  8.  
  9. TESTED
  10. PLATFORMS:    Netscape Navigator 4.05 
  11.                 Microsoft Internet Explorer 4.01 
  12. ====================================================================== */
  13. function com_netobjects_Calendar(params) {
  14.  
  15.    // Public Properties    
  16.     this.name                 = (params.name != null) ? params.name : "";
  17.     this.height             = (params.height != null) ? params.height : "";
  18.     this.width              = (params.width != null) ? params.width : "";
  19.  
  20.     this.selDate             = (params.selDate != null) ? params.selDate : "";
  21.     this.wdayFormat         = (params.wdayFormat != null) ? params.wdayFormat : "Abbreviated";
  22.     this.textFont             = (params.textFont != null) ? params.textFont : "Arial";
  23.     this.textSize             = (params.textSize != null) ? params.textSize : "";
  24.     this.textColor         = (params.textColor != null) ? params.textColor : "";    
  25.     this.selColor             = (params.selColor != null)  ? params.selColor : "";
  26.     this.border             = ((params.border != null) && (params.border != "")) ? params.border : "0";
  27.     this.leftButton        = (params.leftButton != null) ? params.leftButton : "leftbutton.gif";
  28.     this.rightButton        = (params.rightButton != null) ? params.rightButton : "rightbutton.gif";
  29.     this.testMode            = ((params.testMode != null) && (params.testMode != "")) ? params.testMode : "off"
  30.  
  31.     // Private Properties
  32.     this.childWindow         = null  // Handle to popup window created on open
  33.     this.RenderingDay        = null  // Day currently being rendered - used typically in onRenderDay event
  34.  
  35.     // Public Methods
  36.     this.getSelDate         = _Calendar_getSelDate;  // Returns the last selected date as a string in the form mm/dd/yy
  37.     this.render             = _Calendar_render;  // Renders selected month on the spot
  38.     this.open                 = _Calendar_open;  // Renders selected month in new window
  39.  
  40.     // Private Methods
  41.     this.renderMonth        = _Calendar_RenderMonth;
  42.     this.validateProperties= _Calendar_ValidateProperties;
  43.     //  --- date utilities
  44.     this.monthDays         = _Calendar_MonthDays;
  45.     this.getMonths         = _Calendar_GetMonths;
  46.     this.getWDays          = _Calendar_GetWDays;
  47.     this.parseDate         = _Calendar_ParseDate;
  48.     this.dateIncMonth        = _Calendar_DateIncMonth;
  49.  
  50.  
  51.     // Public Events
  52.     this.onSelDate         = _Calendar_onSelDate; // on user selection of date or change of month
  53.     this.onRender             = _Calendar_onRender; // on render of calendar
  54.     this.onRenderDay        = _Calendar_onRenderDay; // on render of each calendar day
  55.     this.onOpen                = _Calendar_onOpen;  // on open of new window & render of calendar
  56.  
  57.     // Private Events (internal event driven functionality)
  58.     this.eonSelDate         = _Calendar_eonSelDate;
  59.  
  60.     // Validate object properties
  61.     this.validateProperties();
  62.  
  63.     // Public Methods ----------------------------------------
  64.  
  65.     /* ======================================================================
  66.     
  67.    METHOD: _Calendar_render.  Renders calendar control.
  68.     
  69.     ====================================================================== */
  70.     function _Calendar_render() {
  71.         // Initialize Variables
  72.         
  73.         var theDate = "";
  74.         if (this.selDate != "")
  75.             theDate = this.parseDate(this.selDate)
  76.         else {
  77.             var today = new Date();
  78.             var todayStr = today.getMonth() + 1;
  79.             todayStr += "/" + today.getDate();
  80.             todayStr += "/" + today.getYear();
  81.             theDate = this.parseDate(todayStr);
  82.         }
  83.             
  84.         if (theDate != null) {
  85.             this.renderMonth(theDate.longYear,theDate.month-1,theDate.day);
  86.         
  87.             // Fire public events
  88.             this.onRender(); 
  89.  
  90.         } else { 
  91.             alert(this.selDate + " is not a valid date")
  92.         }
  93.                 
  94.     
  95.     } // END _Calendar_render
  96.  
  97.  
  98.     /* ======================================================================
  99.     
  100.    METHOD: _Calendar_open.  Pops up a separate window and renders calendar control.
  101.     
  102.     ====================================================================== */
  103.     function _Calendar_open() {
  104.         // Initialize Variables
  105.         var sheight = ""
  106.         var swidth = ""
  107.               
  108.         // Set Variables
  109.         if (this.height != "") sheight = ",height=" + this.height
  110.         if (this.width != "") swidth = ",width=" + this.width
  111.  
  112.         // Write HTML
  113.         if    ((this.childWindow == null) || (this.childWindow.closed)) {     
  114.             this.childWindow = window.open("", "Calendar", "resizable" + sheight + swidth)
  115.         }
  116.     
  117.         this.render();
  118.  
  119.         // Fire public events
  120.         this.onOpen();                
  121.     
  122.     } // END _Calendar_open
  123.  
  124.     /* ======================================================================
  125.     
  126.    METHOD: _Calendar_getSelDate.  Pops up a separate window and 
  127.       renders calendar control.
  128.     
  129.     ====================================================================== */
  130.     function _Calendar_getSelDate() {
  131.         return this.selDate;
  132.     } // END _Calendar_getSelDate
  133.  
  134.  
  135.     // Private Methods ----------------------------------------
  136.     
  137.     /* ======================================================================
  138.     
  139.    METHOD: _Calendar_ValidateProperties.  Validate current properties.
  140.     
  141.     ====================================================================== */
  142.     function _Calendar_ValidateProperties() {
  143.     
  144.         if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
  145.             var errors = new Array()
  146.           iIndex = 0;  
  147.          
  148.             // Check for errors
  149.             if (this.name == "") 
  150.                 errors[iIndex++] = "Name is a required property.";  
  151.  
  152.             // Display errors
  153.             for (var i=0; i<errors.length; i++)  {
  154.                 if (this.testMode.toLowerCase()    == "text") 
  155.                       document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_Calendar, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n")
  156.                 else
  157.                       alert("TEST MODE, com_netobjects_Calendar, '" + this.name + "' : " + errors[i]);
  158.  
  159.             }            
  160.         } 
  161.     } // END _Calendar_ValidateProperties
  162.  
  163.     /* ======================================================================
  164.     
  165.    METHOD: _Calendar_MonthDays.  Returns days in selected month / year.
  166.     
  167.     ====================================================================== */
  168.     function _Calendar_MonthDays(month, yr) {
  169.     var Days = new Array();
  170.  
  171.         Days[0] = 31;
  172.         Days[1] = 28;
  173.         Days[2] = 31;
  174.         Days[3] = 30;
  175.         Days[4] = 31;
  176.         Days[5] = 30;
  177.         Days[6] = 31;
  178.         Days[7] = 31;
  179.         Days[8] = 30;
  180.         Days[9] = 31;
  181.         Days[10] = 30;
  182.         Days[11] = 31;
  183.     
  184.         // Leap Year Rule: 
  185.         // If the year is evenly divisible by 4 AND NOT by 100, it's a leap year.
  186.         // If the year is evenly divisible by 4 AND 100, it MUST also be divisible by 400
  187.         // to be a leap year.  Otherwise, it is not a leap year.
  188.         var isLeap = false;
  189.         isLeap = ((yr % 400 == 0) || (( yr % 4 == 0) && (yr % 100 != 0)));
  190.  
  191.         if ((month == 1) && (isLeap)) {return Days[month] + 1} else {return Days[month]};
  192.     
  193.     } // END _Calendar_MonthDays
  194.  
  195.     /* ======================================================================
  196.     
  197.    METHOD: _Calendar_GetMonths.  Returns array of month names.
  198.     
  199.     ====================================================================== */
  200.     function _Calendar_GetMonths(){
  201.         this[0] = "January";
  202.         this[1] = "February";
  203.         this[2] = "March";
  204.         this[3] = "April";
  205.         this[4] = "May";
  206.         this[5] = "June";
  207.         this[6] = "July";
  208.         this[7] = "August";
  209.         this[8] = "September";
  210.         this[9] = "October";
  211.         this[10] = "November";
  212.         this[11] = "December";
  213.     } // END _Calendar_GetMonths
  214.     
  215.     /* ======================================================================
  216.     
  217.    METHOD: _Calendar_GetWDays.  Returns array of days in specified format.
  218.     
  219.     ====================================================================== */
  220.     function _Calendar_GetWDays(NoLtrs){
  221.         if (NoLtrs=="FirstLetter") {
  222.             this[0] = "S";
  223.             this[1] = "M";
  224.             this[2] = "T";
  225.             this[3] = "W";
  226.             this[4] = "Th";
  227.             this[5] = "F";
  228.             this[6] = "S";
  229.         } else if (NoLtrs=="FullWord") {    
  230.             this[0] = "Sunday";
  231.             this[1] = "Monday";
  232.             this[2] = "Tuesday";
  233.             this[3] = "Wednesday";
  234.             this[4] = "Thursday";
  235.             this[5] = "Friday";
  236.             this[6] = "Saturday";    
  237.         } else {  // Abbreviated    
  238.             this[0] = "Sun";
  239.             this[1] = "Mon";
  240.             this[2] = "Tues";
  241.             this[3] = "Wed";
  242.             this[4] = "Thurs";
  243.             this[5] = "Fri";
  244.             this[6] = "Sat";
  245.         }
  246.     } // END _Calendar_GetWDays
  247.     
  248.     /* ======================================================================
  249.     
  250.    METHOD: _Calendar_DateIncMonth.  Increments (or Decrements) a date by month increments.
  251.     
  252.     ====================================================================== */
  253.     function _Calendar_DateIncMonth(mm, dd, yy, incMo) {
  254.         var dateObj = new Object();
  255.     
  256.         dateObj.month = mm + incMo;
  257.        dateObj.year = yy, 10;
  258.         dateObj.day = dd;
  259.     
  260.         if (dateObj.month > 11) {
  261.             dateObj.month = 0
  262.             dateObj.year = dateObj.year + 1
  263.         } else if (dateObj.month < 0) {
  264.             dateObj.month = 11
  265.             dateObj.year = dateObj.year - 1
  266.         }
  267.         
  268.         var dayCount = this.monthDays(dateObj.month, dateObj.year); 
  269.         if (dateObj.day > dayCount) dateObj.day = dayCount;
  270.  
  271.         return dateObj
  272.     
  273.     } // END _Calendar_DateIncMonth
  274.     
  275.     /* ======================================================================
  276.     
  277.    METHOD: _Calendar_ParseDate.  Parses a date string in m/d/y or mm/dd/yy 
  278.                 or mm/dd/yyyy format    into it's month, day, year parts. Returns
  279.                  object with properties: month, day, year.
  280.     ====================================================================== */
  281.     function _Calendar_ParseDate(mdyStr) {
  282.         var isValid = true
  283.         var sep1Loc = null
  284.         var sep2Loc = null
  285.     
  286.         //Validate string and get two separator locations
  287.         if (mdyStr == null) 
  288.             isValid = false;
  289.         else {
  290.             /* loop through the string and test each character */
  291.             for (i = 0; i < mdyStr.length; i++) {
  292.                 /* the character must be a number, a forward slash or a dash */
  293.                 if (!((mdyStr.charAt(i) >= '0') && (mdyStr.charAt(i) <= '9') || 
  294.                         (mdyStr.charAt(i) == '/') || (mdyStr.charAt(i) == '-'))) {
  295.                     isValid = false;
  296.                     break;
  297.                 } else if ((mdyStr.charAt(i) == '/') || (mdyStr.charAt(i) == '-')) {
  298.                     if (sep1Loc == null) {
  299.                         sep1Loc = i
  300.                     } else {
  301.                         if (sep2Loc == null) {
  302.                             sep2Loc = i
  303.                         } else {  // Too many separators found!
  304.                             isValid = false;
  305.                             break;
  306.                          }
  307.                 }
  308.                  }
  309.             } // end for loop
  310.         }
  311.     
  312.     // Must have found 2 separators    
  313.     if (sep2Loc == null) {isValid = false}
  314.  
  315.     if (isValid) {
  316.         myObj = new Object();
  317.         myObj.month = mdyStr.substring(0, sep1Loc);
  318.         myObj.day   = mdyStr.substring(sep1Loc + 1, sep2Loc);
  319.         myObj.year  = mdyStr.substring(sep2Loc+1, mdyStr.length); 
  320.         myObj.longYear = parseInt(myObj.year, 10)
  321.         if (!isNaN(myObj.longYear)) {
  322.             if (myObj.longYear < 100) {myObj.longYear = myObj.longYear+1900}
  323.         } else {
  324.             myObj.longYear = 0
  325.         }
  326.  
  327.         return myObj
  328.     } else {
  329.         return null;
  330.     }
  331.     
  332. } // END _Calendar_ParseDate
  333.  
  334.  
  335.     /* ======================================================================
  336.     
  337.    METHOD: _Calendar_RenderMonth.  Renders a selected year month, highlighting 
  338.                 selected day if selDay = 0, does not highlight
  339.     
  340.     ====================================================================== */
  341.     function _Calendar_RenderMonth( selYear, selMonth, selDay) {
  342.  
  343.         var doc = this.childWindow.document;
  344.         var startDay = (new Date(selYear,selMonth,1)).getDay(); 
  345.         var dayCount = this.monthDays(selMonth, selYear); 
  346.         var calWeeks = Math.ceil((startDay + dayCount)/7)
  347.         var wdays = new this.getWDays(this.wdayFormat);
  348.         var cnt = 1
  349.     
  350.         // Specify height, if given
  351.         var cellHeight = ""
  352.         if (this.height != "") 
  353.             " height=" + Math.floor(this.height/10);
  354.     
  355.         // Specify text color, if given
  356.         var stextColor = ""
  357.         if (this.textColor != "") stextColor = "link=\"" + this.textColor + "\""
  358.     
  359.         // Specify font face and size, if given
  360.         var sfontTag = ""
  361.         if (this.textFont != "") sfontTag = sfontTag + " Face=\"" +this.textFont + "\""
  362.         if (this.textSize != "") sfontTag = sfontTag + " Size=\"" +this.textSize +"\""
  363.         if (sfontTag != "") sfontTag = "<Font " + sfontTag + " >"
  364.     
  365.         doc.clear();
  366.         doc.open()
  367.         doc.write("<HTML>")
  368.     
  369.         doc.write("<Body " + stextColor + ">")
  370.         doc.write("<table width='100%' Border=\"" + this.border +"\">")
  371.  
  372.         // Check sel day for existance in previous month
  373.         var prevDate = this.dateIncMonth(selMonth,selDay,selYear, -1)
  374.         var nextDate = this.dateIncMonth(selMonth,selDay,selYear, 1)
  375.  
  376.         // Write month - year title
  377.         doc.write("<tr><td colspan=7 align=center " + cellHeight + "><b>" + sfontTag)
  378.         doc.write("<TABLE WIDTH=100%><tr>");
  379.  
  380.         // Write previous month button
  381.         doc.write("<td align=left><A HREF=\"JavaScript: window.opener." + this.name + ".eonSelDate(" + prevDate.month + "," + prevDate.day  + "," + prevDate.year + ")\">")
  382.         doc.write('<IMG SRC="' + this.leftButton + '" BORDER=0 ALIGN=TOP ALT="Prev"></A>');
  383.         doc.write("</td>");
  384.         
  385.         // Write month - year title
  386.         doc.write("<td align=center><b>" + sfontTag);
  387.         doc.write((new this.getMonths())[selMonth] + " " + selYear);
  388.         doc.write("</b></font></td>")
  389.  
  390.     // Write next month button
  391.         doc.write("<td align=right>");
  392.         doc.write("<A HREF=\"JavaScript: window.opener." + this.name + ".eonSelDate(" + nextDate.month + "," + nextDate.day  + "," + nextDate.year + ")\">")
  393.         doc.write('<IMG SRC="' + this.rightButton + '" BORDER=0 ALIGN=TOP ALT="Next"></A>')
  394.         doc.write('</td>');
  395.         doc.write("</tr></TABLE></tr>");
  396.         
  397.         // Write day titles
  398.         doc.write("<tr>")
  399.         for (var k = 0; k < 7; k++)  {
  400.             doc.write("<td align=center" + cellHeight + "  bgcolor=silver >" + sfontTag)    
  401.             doc.write(wdays[k])
  402.             doc.write("</font></td>")
  403.         }
  404.         doc.write("</tr>")
  405.     
  406.         // Write days
  407.         for (var i = 0; i < calWeeks; i++)  {
  408.             doc.write("<tr>")
  409.             for (var j = 0; j < 7; j++)  {
  410.                 countDay = (((i > 0) || (j >= startDay)) && (cnt <= dayCount))
  411.     
  412.                 if ((selDay != 0) && (selDay == cnt) && (countDay)) {
  413.                     var sSelColor = ""
  414.                     if (this.selColor != "") {sSelColor = " BGCOLOR = \"" +this.selColor + "\""}
  415.                     doc.write("<td align=\"center\" " + sSelColor + cellHeight + " >")
  416.                 } else {
  417.                     doc.write("<td align=\"center\" valign=top" + cellHeight + " >")
  418.                 }
  419.     
  420.                 if (countDay) { 
  421.     
  422.                     doc.write("<A HREF=\"JavaScript: window.opener." + this.name + ".eonSelDate("  + selMonth + "," + cnt  + "," + selYear + ")\">")
  423.                     doc.write(sfontTag)
  424.                     doc.write(cnt)
  425.                     doc.write("</Font>")
  426.                     this.RenderingDay = cnt;
  427.                     doc.write("</A>")
  428.                     this.onRenderDay();
  429.                     cnt = cnt + 1;
  430.                 }
  431.                 doc.write("</td>")
  432.             }
  433.             doc.write("</tr>")
  434.         }
  435.     
  436.         doc.write("</tr><td colspan=\"7\" align=center>")
  437.     
  438.         // Check sel day for existance in previous month
  439.         var prevDate = this.dateIncMonth(selMonth,selDay,selYear, -1)
  440.         var nextDate = this.dateIncMonth(selMonth,selDay,selYear, 1)
  441.     
  442.         doc.write("</td></tr>")
  443.         doc.write("</table>")
  444.         doc.write("</Body>")
  445.         doc.write("</HTML>")
  446.         doc.close()
  447.     
  448.     } // END _Calendar_RenderMonth
  449.     
  450.     // Private Event Handlers ----------------------------------------
  451.     
  452.     /* ======================================================================
  453.     
  454.    EVENT: _Calendar_eonSelDate.  Internal event handler.  Sets selDate and 
  455.             re-renders control.
  456.     
  457.     ====================================================================== */
  458.     function _Calendar_eonSelDate(selMonth, selDay, selYear) {
  459.         //    Set the selected date
  460.         this.selDate = (selMonth +1) + "/" +  selDay + "/" +  selYear
  461.         
  462.         // Render the month
  463.         this.renderMonth(selYear,selMonth,selDay);
  464.         
  465.         // Fire public events
  466.         this.onSelDate(); 
  467.         this.onRender(); 
  468.  
  469.     } // END _Calendar_eonSelDate
  470.     
  471.     
  472.     // Public Event Handlers ----------------------------------------
  473.     function _Calendar_onRender() {
  474.     }
  475.     function _Calendar_onRenderDay() {
  476.     }  
  477.     function _Calendar_onSelDate() {
  478.     }  
  479.     function _Calendar_onOpen() {
  480.     }  
  481.  
  482. } // END CONSTRUCTOR com_netobjects_Calendar