[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/js/ -> dynCalendar.js (source)

   1  /**
   2  * Filename.......: calendar.js
   3  * Project........: Popup Calendar
   4  * Last Modified..: $Date: 02/10/15 02:49:33-00:00 $
   5  * CVS Revision...: $Revision: 1.1 $
   6  * Copyright......: 2001, 2002 Richard Heyes
   7  */
   8  
   9  /**
  10  * Global variables
  11  */
  12      dynCalendar_layers          = new Array();
  13      dynCalendar_mouseoverStatus = false;
  14      dynCalendar_mouseX          = 0;
  15      dynCalendar_mouseY          = 0;
  16  
  17  /**
  18  * The calendar constructor
  19  *
  20  * @access public
  21  * @param string objName      Name of the object that you create
  22  * @param string callbackFunc Name of the callback function
  23  * @param string OPTIONAL     Optional layer name
  24  * @param string OPTIONAL     Optional images path
  25  */
  26  	function dynCalendar(objName, callbackFunc)
  27      {
  28          /**
  29          * Properties
  30          */
  31          // Todays date
  32          this.today          = new Date();
  33          this.date           = this.today.getDate();
  34          this.month          = this.today.getMonth();
  35          this.year           = this.today.getFullYear();
  36  
  37          this.objName        = objName;
  38          this.callbackFunc   = callbackFunc;
  39          this.imagesPath     = arguments[2] ? arguments[2] : 'images/';
  40          this.layerID        = arguments[3] ? arguments[3] : 'dynCalendar_layer_' + dynCalendar_layers.length;
  41  
  42          this.offsetX        = 5;
  43          this.offsetY        = 5;
  44  
  45          this.useMonthCombo  = true;
  46          this.useYearCombo   = true;
  47          this.yearComboRange = 5;
  48  
  49          this.currentMonth   = this.month;
  50          this.currentYear    = this.year;
  51  
  52          /**
  53          * Public Methods
  54          */
  55          this.show              = dynCalendar_show;
  56          this.writeHTML         = dynCalendar_writeHTML;
  57  
  58          // Accessor methods
  59          this.setOffset         = dynCalendar_setOffset;
  60          this.setOffsetX        = dynCalendar_setOffsetX;
  61          this.setOffsetY        = dynCalendar_setOffsetY;
  62          this.setImagesPath     = dynCalendar_setImagesPath;
  63          this.setMonthCombo     = dynCalendar_setMonthCombo;
  64          this.setYearCombo      = dynCalendar_setYearCombo;
  65          this.setCurrentMonth   = dynCalendar_setCurrentMonth;
  66          this.setCurrentYear    = dynCalendar_setCurrentYear;
  67          this.setYearComboRange = dynCalendar_setYearComboRange;
  68  
  69          /**
  70          * Private methods
  71          */
  72          // Layer manipulation
  73          this._getLayer         = dynCalendar_getLayer;
  74          this._hideLayer        = dynCalendar_hideLayer;
  75          this._showLayer        = dynCalendar_showLayer;
  76          this._setLayerPosition = dynCalendar_setLayerPosition;
  77          this._setHTML          = dynCalendar_setHTML;
  78  
  79          // Miscellaneous
  80          this._getDaysInMonth   = dynCalendar_getDaysInMonth;
  81          this._mouseover        = dynCalendar_mouseover;
  82  
  83          /**
  84          * Constructor type code
  85          */
  86          dynCalendar_layers[dynCalendar_layers.length] = this;
  87          this.writeHTML();
  88      }
  89  
  90  /**
  91  * Shows the calendar, or updates the layer if
  92  * already visible.
  93  *
  94  * @access public
  95  * @param integer month Optional month number (0-11)
  96  * @param integer year  Optional year (YYYY format)
  97  */
  98  	function dynCalendar_show()
  99      {
 100          // Variable declarations to prevent globalisation
 101          var month, year, monthnames, numdays, thisMonth, firstOfMonth;
 102          var ret, row, i, cssClass, linkHTML, previousMonth, previousYear;
 103          var nextMonth, nextYear, prevImgHTML, prevLinkHTML, nextImgHTML, nextLinkHTML;
 104          var monthComboOptions, monthCombo, yearComboOptions, yearCombo, html;
 105          
 106          this.currentMonth = month = arguments[0] != null ? arguments[0] : this.currentMonth;
 107          this.currentYear  = year  = arguments[1] != null ? arguments[1] : this.currentYear;
 108  
 109          monthnames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
 110          numdays    = this._getDaysInMonth(month, year);
 111  
 112          thisMonth    = new Date(year, month, 1);
 113          firstOfMonth = thisMonth.getDay();
 114  
 115          // First few blanks up to first day
 116          ret = new Array(new Array());
 117          for(i=0; i<firstOfMonth; i++){
 118              ret[0][ret[0].length] = '<td>&nbsp;</td>';
 119          }
 120  
 121          // Main body of calendar
 122          row = 0;
 123          i   = 1;
 124          while(i <= numdays){
 125              if(ret[row].length == 7){
 126                  ret[++row] = new Array();
 127              }
 128  
 129              /**
 130              * Generate this cells' HTML
 131              */
 132              cssClass = (i == this.date && month == this.month && year == this.year) ? 'dynCalendar_today' : 'dynCalendar_day';
 133              linkHTML = sprintf('<a href="javascript: %s(%s, %s, %s); %s._hideLayer()">%s</a>',
 134                                 this.callbackFunc,
 135                                 i,
 136                                 Number(month) + 1,
 137                                 year,
 138                                 this.objName,
 139                                 i++);
 140  
 141              ret[row][ret[row].length] = sprintf('<td align="center" class="%s">%s</td>', cssClass, linkHTML);
 142          }
 143  
 144          // Format the HTML
 145          for(i=0; i<ret.length; i++){
 146              ret[i] = ret[i].join('\n') + '\n';
 147          }
 148  
 149          previousYear  = thisMonth.getFullYear();
 150          previousMonth = thisMonth.getMonth() - 1;
 151          if(previousMonth < 0){
 152              previousMonth = 11;
 153              previousYear--;
 154          }
 155          
 156          nextYear  = thisMonth.getFullYear();
 157          nextMonth = thisMonth.getMonth() + 1;
 158          if(nextMonth > 11){
 159              nextMonth = 0;
 160              nextYear++;
 161          }
 162  
 163          prevImgHTML  = sprintf('<img src="%s/prev.gif" alt="<<" border="0" />', this.imagesPath);
 164          prevLinkHTML = sprintf('<a href="javascript: %s.show(%s, %s)">%s</a>',  this.objName, previousMonth, previousYear, prevImgHTML);
 165          nextImgHTML  = sprintf('<img src="%s/next.gif" alt="<<" border="0" />', this.imagesPath);
 166          nextLinkHTML = sprintf('<a href="javascript: %s.show(%s, %s)">%s</a>',  this.objName, nextMonth, nextYear, nextImgHTML);
 167  
 168          /**
 169          * Build month combo
 170          */
 171          if (this.useMonthCombo) {
 172              monthComboOptions = '';
 173              for (i=0; i<12; i++) {
 174                  selected = (i == thisMonth.getMonth() ? 'selected="selected"' : '');
 175                  monthComboOptions += sprintf('<option value="%s" %s>%s</option>', i, selected, monthnames[i]);
 176              }
 177              monthCombo = sprintf('<select name="months" onchange="%s.show(this.options[this.selectedIndex].value, %s.currentYear)">%s</select>', this.objName, this.objName, monthComboOptions);
 178          } else {
 179              monthCombo = monthnames[thisMonth.getMonth()];
 180          }
 181          
 182          /**
 183          * Build year combo
 184          */
 185          if (this.useYearCombo) {
 186              yearComboOptions = '';
 187              for (i = thisMonth.getFullYear() - this.yearComboRange; i <= (thisMonth.getFullYear() + this.yearComboRange); i++) {
 188                  selected = (i == thisMonth.getFullYear() ? 'selected="selected"' : '');
 189                  yearComboOptions += sprintf('<option value="%s" %s>%s</option>', i, selected, i);
 190              }
 191              yearCombo = sprintf('<select style="border: 1px groove" name="years" onchange="%s.show(%s.currentMonth, this.options[this.selectedIndex].value)">%s</select>', this.objName, this.objName, yearComboOptions);
 192          } else {
 193              yearCombo = thisMonth.getFullYear();
 194          }
 195  
 196          html = '<table border="0" bgcolor="#eeeeee">';
 197          html += sprintf('<tr><td class="dynCalendar_header">%s</td><td colspan="5" align="center" class="dynCalendar_header">%s %s</td><td align="right" class="dynCalendar_header">%s</td></tr>', prevLinkHTML, monthCombo, yearCombo, nextLinkHTML);
 198          html += '<tr>';
 199          html += '<td class="dynCalendar_dayname">Sun</td>';
 200          html += '<td class="dynCalendar_dayname">Mon</td>';
 201          html += '<td class="dynCalendar_dayname">Tue</td>';
 202          html += '<td class="dynCalendar_dayname">Wed</td>';
 203          html += '<td class="dynCalendar_dayname">Thu</td>';
 204          html += '<td class="dynCalendar_dayname">Fri</td>';
 205          html += '<td class="dynCalendar_dayname">Sat</td></tr>';
 206          html += '<tr>' + ret.join('</tr>\n<tr>') + '</tr>';
 207          html += '</table>';
 208  
 209          this._setHTML(html);
 210          if (!arguments[0] && !arguments[1]) {
 211              this._showLayer();
 212              this._setLayerPosition();
 213          }
 214      }
 215  
 216  /**
 217  * Writes HTML to document for layer
 218  *
 219  * @access public
 220  */
 221  	function dynCalendar_writeHTML()
 222      {
 223          if (is_ie5up || is_nav6up || is_gecko || is_opera5up) {
 224              document.write(sprintf('<a href="javascript: %s.show()"><img src="%sdynCalendar.gif" border="0" width="16" height="16" /></a>', this.objName, this.imagesPath));
 225              document.write(sprintf('<div class="dynCalendar" id="%s" onmouseover="%s._mouseover(true)" onmouseout="%s._mouseover(false)"></div>', this.layerID, this.objName, this.objName));
 226          }
 227      }
 228  
 229  /**
 230  * Sets the offset to the mouse position
 231  * that the calendar appears at.
 232  *
 233  * @access public
 234  * @param integer Xoffset Number of pixels for vertical
 235  *                        offset from mouse position
 236  * @param integer Yoffset Number of pixels for horizontal
 237  *                        offset from mouse position
 238  */
 239  	function dynCalendar_setOffset(Xoffset, Yoffset)
 240      {
 241          this.setOffsetX(Xoffset);
 242          this.setOffsetY(Yoffset);
 243      }
 244  
 245  /**
 246  * Sets the X offset to the mouse position
 247  * that the calendar appears at.
 248  *
 249  * @access public
 250  * @param integer Xoffset Number of pixels for horizontal
 251  *                        offset from mouse position
 252  */
 253  	function dynCalendar_setOffsetX(Xoffset)
 254      {
 255          this.offsetX = Xoffset;
 256      }
 257  
 258  /**
 259  * Sets the Y offset to the mouse position
 260  * that the calendar appears at.
 261  *
 262  * @access public
 263  * @param integer Yoffset Number of pixels for vertical
 264  *                        offset from mouse position
 265  */
 266  	function dynCalendar_setOffsetY(Yoffset)
 267      {
 268          this.offsetY = Yoffset;
 269      }
 270      
 271  /**
 272  * Sets the images path
 273  *
 274  * @access public
 275  * @param string path Path to use for images
 276  */
 277  	function dynCalendar_setImagesPath(path)
 278      {
 279          this.imagesPath = path;
 280      }
 281  
 282  /**
 283  * Turns on/off the month dropdown
 284  *
 285  * @access public
 286  * @param boolean useMonthCombo Whether to use month dropdown or not
 287  */
 288  	function dynCalendar_setMonthCombo(useMonthCombo)
 289      {
 290          this.useMonthCombo = useMonthCombo;
 291      }
 292  
 293  /**
 294  * Turns on/off the year dropdown
 295  *
 296  * @access public
 297  * @param boolean useYearCombo Whether to use year dropdown or not
 298  */
 299  	function dynCalendar_setYearCombo(useYearCombo)
 300      {
 301          this.useYearCombo = useYearCombo;
 302      }
 303  
 304  /**
 305  * Sets the current month being displayed
 306  *
 307  * @access public
 308  * @param boolean month The month to set the current month to
 309  */
 310  	function dynCalendar_setCurrentMonth(month)
 311      {
 312          this.currentMonth = month;
 313      }
 314  
 315  /**
 316  * Sets the current month being displayed
 317  *
 318  * @access public
 319  * @param boolean year The year to set the current year to
 320  */
 321  	function dynCalendar_setCurrentYear(year)
 322      {
 323          this.currentYear = year;
 324      }
 325  
 326  /**
 327  * Sets the range of the year combo. Displays this number of
 328  * years either side of the year being displayed.
 329  *
 330  * @access public
 331  * @param integer range The range to set
 332  */
 333  	function dynCalendar_setYearComboRange(range)
 334      {
 335          this.yearComboRange = range;
 336      }
 337  
 338  /**
 339  * Returns the layer object
 340  *
 341  * @access private
 342  */
 343  	function dynCalendar_getLayer()
 344      {
 345          var layerID = this.layerID;
 346  
 347          if (document.getElementById(layerID)) {
 348  
 349              return document.getElementById(layerID);
 350  
 351          } else if (document.all(layerID)) {
 352              return document.all(layerID);
 353          }
 354      }
 355  
 356  /**
 357  * Hides the calendar layer
 358  *
 359  * @access private
 360  */
 361  	function dynCalendar_hideLayer()
 362      {
 363          this._getLayer().style.visibility = 'hidden';
 364      }
 365  
 366  /**
 367  * Shows the calendar layer
 368  *
 369  * @access private
 370  */
 371  	function dynCalendar_showLayer()
 372      {
 373          this._getLayer().style.visibility = 'visible';
 374      }
 375  
 376  /**
 377  * Sets the layers position
 378  *
 379  * @access private
 380  */
 381  	function dynCalendar_setLayerPosition()
 382      {
 383          this._getLayer().style.top  = (dynCalendar_mouseY + this.offsetY) + 'px';
 384          this._getLayer().style.left = (dynCalendar_mouseX + this.offsetX) + 'px';
 385      }
 386  
 387  /**
 388  * Sets the innerHTML attribute of the layer
 389  *
 390  * @access private
 391  */
 392  	function dynCalendar_setHTML(html)
 393      {
 394          this._getLayer().innerHTML = html;
 395      }
 396  
 397  /**
 398  * Returns number of days in the supplied month
 399  *
 400  * @access private
 401  * @param integer month The month to get number of days in
 402  * @param integer year  The year of the month in question
 403  */
 404  	function dynCalendar_getDaysInMonth(month, year)
 405      {
 406          monthdays = [30, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
 407          if (month != 1) {
 408              return monthdays[month];
 409          } else {
 410              return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28);
 411          }
 412      }
 413  
 414  /**
 415  * onMouse(Over|Out) event handler
 416  *
 417  * @access private
 418  * @param boolean status Whether the mouse is over the
 419  *                       calendar or not
 420  */
 421  	function dynCalendar_mouseover(status)
 422      {
 423          dynCalendar_mouseoverStatus = status;
 424          return true;
 425      }
 426  
 427  /**
 428  * onMouseMove event handler
 429  */
 430      if (!mouseMoveEventAssigned) {
 431          dynCalendar_oldOnmousemove = document.onmousemove ? document.onmousemove : new Function;
 432      
 433          document.onmousemove = function ()
 434          {
 435              if (arguments[0]) {
 436                  dynCalendar_mouseX = arguments[0].pageX;
 437                  dynCalendar_mouseY = arguments[0].pageY;
 438              } else {
 439                  dynCalendar_mouseX = event.clientX + document.body.scrollLeft;
 440                  dynCalendar_mouseY = event.clientY + document.body.scrollTop;
 441                  arguments[0] = null;
 442              }
 443      
 444              dynCalendar_oldOnmousemove(arguments[0]);
 445          }
 446          
 447          var mouseMoveEventAssigned = true;
 448      }
 449  
 450  /**
 451  * Callbacks for document.onclick
 452  */
 453      if (!clickEventAssigned) {
 454          dynCalendar_oldOnclick = document.onclick ? document.onclick : new Function;
 455      
 456          document.onclick = function ()
 457          {
 458              if(!dynCalendar_mouseoverStatus){
 459                  for(i=0; i<dynCalendar_layers.length; ++i){
 460                      dynCalendar_layers[i]._hideLayer();
 461                  }
 462              }
 463      
 464              dynCalendar_oldOnclick(arguments[0] ? arguments[0] : null);
 465          }
 466          var clickEventAssigned = true;
 467      }
 468  
 469  /**
 470  * Javascript mini implementation of sprintf()
 471  */
 472  	function sprintf(strInput)
 473      {
 474          var strOutput  = '';
 475          var currentArg = 1;
 476      
 477          for (var i=0; i<strInput.length; i++) {
 478              if (strInput.charAt(i) == '%' && i != (strInput.length - 1) && typeof(arguments[currentArg]) != 'undefined') {
 479                  switch (strInput.charAt(++i)) {
 480                      case 's':
 481                          strOutput += arguments[currentArg];
 482                          break;
 483                      case '%':
 484                          strOutput += '%';
 485                          break;
 486                  }
 487                  currentArg++;
 488              } else {
 489                  strOutput += strInput.charAt(i);
 490              }
 491          }
 492      
 493          return strOutput;
 494      } 


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7