Ver Mensaje Individual
  #4 (permalink)  
Antiguo 26/01/2012, 16:12
bartman742
 
Fecha de Ingreso: enero-2012
Mensajes: 64
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: calendario jquery

Código Javascript:
Ver original
  1. display : function(eleAlignTo)
  2.             {
  3.                 if ($(this.ele).is('.dp-disabled')) return;
  4.                
  5.                 eleAlignTo = eleAlignTo || this.ele;
  6.                 var c = this;
  7.                 var $ele = $(eleAlignTo);
  8.                 var eleOffset = $ele.offset();
  9.                
  10.                 var $createIn;
  11.                 var attrs;
  12.                 var attrsCalendarHolder;
  13.                 var cssRules;
  14.                
  15.                 if (c.inline) {
  16.                     $createIn = $(this.ele);
  17.                     attrs = {
  18.                         'id'        :   'calendar-' + this.ele._dpId,
  19.                         'class' :   'dp-popup dp-popup-inline'
  20.                     };
  21.  
  22.                     $('.dp-popup', $createIn).remove();
  23.                     cssRules = {
  24.                     };
  25.                 } else {
  26.                     $createIn = $('body');
  27.                     attrs = {
  28.                         'id'        :   'dp-popup',
  29.                         'class' :   'dp-popup'
  30.                     };
  31.                     cssRules = {
  32.                         'top'   :   eleOffset.top + c.verticalOffset,
  33.                         'left'  :   eleOffset.left + c.horizontalOffset
  34.                     };
  35.                    
  36.                     var _checkMouse = function(e)
  37.                     {
  38.                         var el = e.target;
  39.                         var cal = $('#dp-popup')[0];
  40.                        
  41.                         while (true){
  42.                             if (el == cal) {
  43.                                 return true;
  44.                             } else if (el == document) {
  45.                                 c._closeCalendar();
  46.                                 return false;
  47.                             } else {
  48.                                 el = $(el).parent()[0];
  49.                             }
  50.                         }
  51.                     };
  52.                     this._checkMouse = _checkMouse;
  53.                    
  54.                     c._closeCalendar(true);
  55.                     $(document).bind(
  56.                         'keydown.datepicker',
  57.                         function(event)
  58.                         {
  59.                             if (event.keyCode == 27) {
  60.                                 c._closeCalendar();
  61.                             }
  62.                         }
  63.                     );
  64.                 }
  65.                
  66.                 if (!c.rememberViewedMonth)
  67.                 {
  68.                     var selectedDate = this.getSelected()[0];
  69.                     if (selectedDate) {
  70.                         selectedDate = new Date(selectedDate);
  71.                         this.setDisplayedMonth(selectedDate.getMonth(),
  72. selectedDate.getFullYear(), false);
  73.                     }
  74.                 }
  75.                
  76.                 $createIn
  77.                     .append(
  78.                         $('<div></div>')
  79.                             .attr(attrs)
  80.                             .css(cssRules)
  81.                             .append(
  82. //                              $('<a href="#" class="selecteee">aaa</a>'),
  83.                                 $('<h2></h2>'),
  84.                                 $('<div class="dp-nav-prev"></div>')
  85.                                     .append(
  86.                                         $('<a class="dp-nav-prev-year" href="#" title="' + $.dpText.TEXT_PREV_YEAR + '">&lt;&lt;</a>')
  87.                                             .bind(
  88.                                                 'click',
  89.                                                 function()
  90.                                                 {
  91.                                                     return c._displayNewMonth.call(c, this, 0, -1);
  92.                                                 }
  93.                                             ),
  94.                                         $('<a class="dp-nav-prev-month" href="#" title="' + $.dpText.TEXT_PREV_MONTH + '">&lt;</a>')
  95.                                             .bind(
  96.                                                 'click',
  97.                                                 function()
  98.                                                 {
  99.                                                     return c._displayNewMonth.call(c, this, -1, 0);
  100.                                                 }
  101.                                             )
  102.                                     ),
  103.                                 $('<div class="dp-nav-next"></div>')
  104.                                     .append(
  105.                                         $('<a class="dp-nav-next-year" href="#" title="' + $.dpText.TEXT_NEXT_YEAR + '">&gt;&gt;</a>')
  106.                                             .bind(
  107.                                                 'click',
  108.                                                 function()
  109.                                                 {
  110.                                                     return c._displayNewMonth.call(c, this, 0, 1);
  111.                                                 }
  112.                                             ),
  113.                                         $('<a class="dp-nav-next-month" href="#" title="' + $.dpText.TEXT_NEXT_MONTH + '">&gt;</a>')
  114.                                             .bind(
  115.                                                 'click',
  116.                                                 function()
  117.                                                 {
  118.                                                     return c._displayNewMonth.call(c, this, 1, 0);
  119.                                                 }
  120.                                             )
  121.                                     ),
  122.                                 $('<div class="dp-calendar"></div>')
  123.                             )
  124.                             .bgIframe()
  125.                         );
  126.                    
  127.                 var $pop = this.inline ? $('.dp-popup', this.context) : $('#dp-popup');
  128.                
  129.                 if (this.showYearNavigation == false) {
  130.                     $('.dp-nav-prev-year, .dp-nav-next-year', c.context).css('display', 'none');
  131.                 }
  132.                 if (this.displayClose) {
  133.                     $pop.append(
  134.                         $('<a href="#" id="dp-close">' + $.dpText.TEXT_CLOSE + '</a>')
  135.                             .bind(
  136.                                 'click',
  137.                                 function()
  138.                                 {
  139.                                     c._closeCalendar();
  140.                                     return false;
  141.                                 }
  142.                             )
  143.                     );
  144.                 }
  145.                 c._renderCalendar();
  146.  
  147.                 $(this.ele).trigger('dpDisplayed', $pop);
  148.                
  149.                 if (!c.inline) {
  150.                     if (this.verticalPosition == $.dpConst.POS_BOTTOM) {
  151.                         $pop.css('top', eleOffset.top + $ele.height() - $pop.height() + c.verticalOffset);
  152.                     }
  153.                     if (this.horizontalPosition == $.dpConst.POS_RIGHT) {
  154.                         $pop.css('left', eleOffset.left + $ele.width() - $pop.width() + c.horizontalOffset);
  155.                     }
  156. //                  $('.selectee', this.context).focus();
  157.                     $(document).bind('mousedown.datepicker', this._checkMouse);
  158.                 }
  159.                
  160.             },
  161.             setRenderCallback : function(a)
  162.             {
  163.                 if (a == null) return;
  164.                 if (a && typeof(a) == 'function') {
  165.                     a = [a];
  166.                 }
  167.                 this.renderCallback = this.renderCallback.concat(a);
  168.             },
  169.             cellRender : function ($td, thisDate, month, year) {
  170.                 var c = this.dpController;
  171.                 var d = new Date(thisDate.getTime());
  172.                
  173.                 // add our click handlers to deal with it when the days are clicked...
  174.                
  175.                 $td.bind(
  176.                     'click',
  177.                     function()
  178.                     {
  179.                         var $this = $(this);
  180.                         if (!$this.is('.disabled')) {
  181.                             c.setSelected(d, !$this.is('.selected') || !c.selectMultiple, false, true);
  182.                             if (c.closeOnSelect) {
  183.                                 // Focus the next input in the form…
  184.                                 if (c.settings.autoFocusNextInput) {
  185.                                     var ele = c.ele;
  186.                                     var found = false;
  187.                                     $(':input', ele.form).each(
  188.                                         function()
  189.                                         {
  190.                                             if (found) {
  191.                                                 $(this).focus();
  192.                                                 return false;
  193.                                             }
  194.                                             if (this == ele) {
  195.                                                 found = true;
  196.                                             }
  197.                                         }
  198.                                     );
  199.                                 } else {
  200.                                     c.ele.focus();
  201.                                 }
  202.                                 c._closeCalendar();
  203.                             }
  204.                         }
  205.                     }
  206.                 );
  207.                 if (c.isSelected(d)) {
  208.                     $td.addClass('selected');
  209.                     if (c.settings.selectWeek)
  210.                     {
  211.                         $td.parent().addClass('selectedWeek');
  212.                     }
  213.                 } else  if (c.selectMultiple && c.numSelected == c.numSelectable) {
  214.                     $td.addClass('unselectable');
  215.                 }
  216.                
  217.             },
  218.             _applyRenderCallbacks : function()
  219.             {
  220.                 var c = this;
  221.                 $('td', this.context).each(
  222.                     function()
  223.                     {
  224.                         for (var i=0; i<c.renderCallback.length; i++) {
  225.                             $td = $(this);
  226.                             c.renderCallback[i].apply(this, [$td, Date.fromString($td.data('datePickerDate')), c.displayedMonth, c.displayedYear]);
  227.                         }
  228.                     }
  229.                 );
  230.                 return;
  231.             },
  232.             // ele is the clicked button - only proceed if it doesn't have the class disabled...
  233.             // m and y are -1, 0 or 1 depending which direction we want to go in...
  234.             _displayNewMonth : function(ele, m, y)
  235.             {
  236.                 if (!$(ele).is('.disabled')) {
  237.                     this.setDisplayedMonth(this.displayedMonth + m, this.displayedYear + y, true);
  238.                 }
  239.                 ele.blur();
  240.                 return false;
  241.             },
  242.             _rerenderCalendar : function()
  243.             {
  244.                 this._clearCalendar();
  245.                 this._renderCalendar();
  246.             },
  247.             _renderCalendar : function()
  248.             {
  249.                 // set the title...
  250.                 $('h2', this.context).html((new Date(this.displayedYear, this.displayedMonth, 1)).asString($.dpText.HEADER_FORMAT));
  251.                
  252.                 // render the calendar...
  253.                 $('.dp-calendar', this.context).renderCalendar(
  254.                     $.extend(
  255.                         {},
  256.                         this.settings,
  257.                         {
  258.                             month           : this.displayedMonth,
  259.                             year            : this.displayedYear,
  260.                             renderCallback  : this.cellRender,
  261.                             dpController    : this,
  262.                             hoverClass      : this.hoverClass
  263.                         })
  264.                 );
  265.                
  266.                 // update the status of the control buttons and disable dates before startDate or after endDate...
  267.                 // TODO: When should the year buttons be disabled? When you can't go forward a whole year from where you are or is that annoying?
  268.                 if (this.displayedYear == this.startDate.getFullYear() && this.displayedMonth == this.startDate.getMonth()) {
  269.                     $('.dp-nav-prev-year', this.context).addClass('disabled');
  270.                     $('.dp-nav-prev-month', this.context).addClass('disabled');
  271.                     $('.dp-calendar td.other-month', this.context).each(
  272.                         function()
  273.                         {
  274.                             var $this = $(this);
  275.                             if (Number($this.text()) > 20) {
  276.                                 $this.addClass('disabled');
  277.                             }
  278.                         }
  279.                     );
  280.                     var d = this.startDate.getDate();
  281.                     $('.dp-calendar td.current-month', this.context).each(
  282.                         function()
  283.                         {
  284.                             var $this = $(this);
  285.                             if (Number($this.text()) < d) {
  286.                                 $this.addClass('disabled');
  287.                             }
  288.                         }
  289.                     );
  290.                 } else {
  291.                     $('.dp-nav-prev-year', this.context).removeClass('disabled');
  292.                     $('.dp-nav-prev-month', this.context).removeClass('disabled');
  293.                     var d = this.startDate.getDate();
  294.                     if (d > 20) {
  295.                         // check if the startDate is last month as we might need to add some disabled classes...
  296.                         var st = this.startDate.getTime();
  297.                         var sd = new Date(st);
  298.                         sd.addMonths(1);
  299.                         if (this.displayedYear == sd.getFullYear() && this.displayedMonth == sd.getMonth()) {
  300.                             $('.dp-calendar td.other-month', this.context).each(
  301.                                 function()
  302.                                 {
  303.                                     var $this = $(this);
  304.                                     if (Date.fromString($this.data('datePickerDate')).getTime() < st) {
  305.                                         $this.addClass('disabled');
  306.                                     }
  307.                                 }
  308.                             );
  309.                         }
  310.                     }
  311.                 }