Author: sevein
Date: Wed Jul  6 11:05:17 2011
New Revision: 9249

Log:
Improve date widget behavior, update calendar page when input value is changed 
manually

Modified:
   trunk/js/date.js

Modified: trunk/js/date.js
==============================================================================
--- trunk/js/date.js    Tue Jul  5 17:56:54 2011        (r9248)
+++ trunk/js/date.js    Wed Jul  6 11:05:17 2011        (r9249)
@@ -60,6 +60,16 @@
             return [min.join('-'), max.join('-')];
           }
 
+          function isValidDate(date)
+          {
+            if (Object.prototype.toString.call(date) !== "[object Date]")
+            {
+              return false;
+            }
+
+            return !isNaN(date.getTime());
+          }
+
           $('.date', context).each(function ()
             {
               var $start = $('[id$=startDate]', this);
@@ -84,40 +94,51 @@
               var self = this;
 
               $(self)
+                 
+                // Prepare the input field
                 .css({'float': 'left', 'width': 'auto'})
+                
+                // Add calendar button
                 .after('&nbsp;<button><img src="' + self.getAttribute('icon') 
+ '" /></button>').next()
+
+                // Bind next function to click event
                 .click(function(event)
                 {
                   event.preventDefault();
 
+                  // If already exists, use it instead of a new one
                   if (self.calendar)
                   {
                     self.calendar.show();
-
-                    return false;
                   }
+                  else
+                  {
+                    // Create container element and add to the DOM
+                    var container = $(document.createElement('div'))
+                      .css({
+                        'position': 'absolute',
+                        'left': parseInt($(self).width() + 60) + 'px',
+                        'z-index': 4})
+                      .insertAfter(self);
+
+                    self.calendar = new 
YAHOO.widget.Calendar(container.get(0), { close: true });
+
+                    self.calendar.selectEvent.subscribe(function(type, args, 
obj)
+                      {
+                        pad = function(n) { return n < 10 ? '0' + n : n; };
+                        self.value = args[0][0][0] + "-" + pad(args[0][0][1]) 
+ "-" + pad(args[0][0][2]);
+                        self.calendar.hide();
+                      })
 
-                  self.element = $(document.createElement('div'))
-                    .css({
-                      'position': 'absolute',
-                      'left': parseInt($(self).width() + 60) + 'px',
-                      'z-index': 4})
-                    .insertAfter(self);
-
-                  self.calendar = new 
YAHOO.widget.Calendar(self.element.get(0), { close: true });
-
-                  self.calendar.selectEvent.subscribe(function(type, args, obj)
-                    {
-                      var year = args[0][0][0],
-                          month = args[0][0][1],
-                          day = args[0][0][2];
-
-                      self.value = year + '-' + month + '-' + day;
-
-                      self.calendar.hide();
-                    })
+                    self.calendar.render();
+                  }
 
-                  self.calendar.render();
+                  var date = new Date(self.value);
+                  if (isValidDate(date))
+                  {
+                    self.calendar.cfg.setProperty('pagedate', (date.getMonth() 
+ 1) + "/" + date.getFullYear());
+                    self.calendar.render();
+                  }
                 })
             });
         } };

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to