Advertisement
Guest User

Fixed some bugs and added second slider

a guest
Sep 15th, 2010
1,581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2.  * jQuery UI Timepicker 0.2.1
  3.  *
  4.  * Copyright (c) 2009 Martin Milesich (http://milesich.com/)
  5.  *
  6.  * Some parts are
  7.  *   Copyright (c) 2009 AUTHORS.txt (http://jqueryui.com/about)
  8.  *
  9.  * $Id: timepicker.js 28 2009-08-11 20:31:23Z majlo $
  10.  *
  11.  * Depends:
  12.  *  ui.core.js
  13.  *  ui.datepicker.js
  14.  *  ui.slider.js
  15.  */
  16. (function($) {
  17.  
  18. /**
  19.  * Extending default values
  20.  */
  21. $.extend($.datepicker._defaults, {
  22.     'stepMinutes': 1, // Number of minutes to step up/down
  23.     'stepHours': 1, // Number of hours to step up/down
  24.     'stepSeconds': 1,// Number of seconds to step up/down
  25.     'time24h': false, // True if 24h time
  26.     'showTime': false, // Show timepicker with datepicker
  27.     'altTimeField': '' // Selector for an alternate field to store time into
  28. });
  29.  
  30. /**
  31.  * _hideDatepicker must be called with null
  32.  */
  33. $.datepicker._connectDatepickerOverride = $.datepicker._connectDatepicker;
  34. $.datepicker._connectDatepicker = function(target, inst) {
  35.     $.datepicker._connectDatepickerOverride(target, inst);
  36.  
  37.     // showButtonPanel is required with timepicker
  38.     if (this._get(inst, 'showTime')) {
  39.         inst.settings['showButtonPanel'] = true;
  40.     }
  41.  
  42.     var showOn = this._get(inst, 'showOn');
  43.  
  44.     if (showOn == 'button' || showOn == 'both') {
  45.         // Unbind all click events
  46.         inst.trigger.unbind('click');
  47.  
  48.         // Bind new click event
  49.         inst.trigger.click(function() {
  50.             if ($.datepicker._datepickerShowing && $.datepicker._lastInput == target)
  51.                 $.datepicker._hideDatepicker(null); // This override is all about the "null"
  52.             else
  53.                 $.datepicker._showDatepicker(target);
  54.             return false;
  55.         });
  56.     }
  57. };
  58.  
  59. /**
  60.  * Datepicker does not have an onShow event so I need to create it.
  61.  * What I actually doing here is copying original _showDatepicker
  62.  * method to _showDatepickerOverload method.
  63.  */
  64. $.datepicker._showDatepickerOverride = $.datepicker._showDatepicker;
  65. $.datepicker._showDatepicker = function (input) {
  66.     // Call the original method which will show the datepicker
  67.     $.datepicker._showDatepickerOverride(input);
  68.  
  69.     input = input.target || input;
  70.  
  71.     // find from button/image trigger
  72.     if (input.nodeName.toLowerCase() != 'input') input = $('input', input.parentNode)[0];
  73.  
  74.     // Do not show timepicker if datepicker is disabled
  75.     if ($.datepicker._isDisabledDatepicker(input)) return;
  76.  
  77.     // Get instance to datepicker
  78.     var inst = $.datepicker._getInst(input);
  79.  
  80.     var showTime = $.datepicker._get(inst, 'showTime');
  81.  
  82.     // If showTime = True show the timepicker
  83.     if (showTime) $.timepicker.show(input);
  84. };
  85.  
  86. /**
  87.  * Same as above. Here I need to extend the _checkExternalClick method
  88.  * because I don't want to close the datepicker when the sliders get focus.
  89.  */
  90. $.datepicker._checkExternalClickOverride = $.datepicker._checkExternalClick;
  91. $.datepicker._checkExternalClick = function (event) {
  92.     if (!$.datepicker._curInst) return;
  93.     var $target = $(event.target);
  94.  
  95.     if (($target.parents('#' + $.timepicker._mainDivId).length == 0)) {
  96.         $.datepicker._checkExternalClickOverride(event);
  97.     }
  98. };
  99.  
  100. /**
  101.  * Datepicker has onHide event but I just want to make it simple for you
  102.  * so I hide the timepicker when datepicker hides.
  103.  */
  104. $.datepicker._hideDatepickerOverride = $.datepicker._hideDatepicker;
  105. $.datepicker._hideDatepicker = function(input, duration) {
  106.     // Some lines from the original method
  107.     var inst = this._curInst;
  108.  
  109.     if (!inst || (input && inst != $.data(input, PROP_NAME))) return;
  110.  
  111.     // Get the value of showTime property
  112.     var showTime = this._get(inst, 'showTime');
  113.  
  114.     if (input === undefined && showTime) {
  115.         if (inst.input) {
  116.             inst.input.val(this._formatDate(inst));
  117.             inst.input.trigger('change'); // fire the change event
  118.         }
  119.  
  120.         this._updateAlternate(inst);
  121.  
  122.         if (showTime) $.timepicker.update(this._formatDate(inst));
  123.     }
  124.  
  125.     // Hide datepicker
  126.     $.datepicker._hideDatepickerOverride(input, duration);
  127.  
  128.     // Hide the timepicker if enabled
  129.     if (showTime) {
  130.         $.timepicker.hide();
  131.     }
  132. };
  133.  
  134. /**
  135.  * This is a complete replacement of the _selectDate method.
  136.  * If showed with timepicker do not close when date is selected.
  137.  */
  138. $.datepicker._selectDate = function(id, dateStr) {
  139.     var target = $(id);
  140.     var inst = this._getInst(target[0]);
  141.     var showTime = this._get(inst, 'showTime');
  142.     dateStr = (dateStr != null ? dateStr : this._formatDate(inst));
  143.     if (!showTime) {
  144.         if (inst.input)
  145.             inst.input.val(dateStr);
  146.         this._updateAlternate(inst);
  147.     }
  148.     var onSelect = this._get(inst, 'onSelect');
  149.     if (onSelect)
  150.         onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]);  // trigger custom callback
  151.     else if (inst.input && !showTime)
  152.         inst.input.trigger('change'); // fire the change event
  153.     if (inst.inline)
  154.         this._updateDatepicker(inst);
  155.     else if (!inst.stayOpen) {
  156.         if (showTime) {
  157.             this._updateDatepicker(inst);
  158.         } else {
  159.             this._hideDatepicker(null, this._get(inst, 'duration'));
  160.             this._lastInput = inst.input[0];
  161.             if (typeof(inst.input[0]) != 'object')
  162.                 inst.input[0].focus(); // restore focus
  163.             this._lastInput = null;
  164.         }
  165.     }
  166. };
  167.  
  168. /**
  169.  * We need to resize the timepicker when the datepicker has been changed.
  170.  */
  171. $.datepicker._updateDatepickerOverride = $.datepicker._updateDatepicker;
  172. $.datepicker._updateDatepicker = function(inst) {
  173.     $.datepicker._updateDatepickerOverride(inst);
  174.    // $.timepicker.resize(); //twice called therefore commented
  175. };
  176.  
  177. function Timepicker() {}
  178.  
  179. Timepicker.prototype = {
  180.     init: function()
  181.     {
  182.         this._mainDivId = 'ui-timepicker-div';
  183.         this._inputId   = null;
  184.         this._orgValue  = null;
  185.         this._orgHour   = null;
  186.         this._orgMinute = null;
  187.         this._orgSec    = null;
  188.         this._colonPos  = -1;
  189.         this._visible   = false;
  190.         this._showSec   = true;
  191.         this.tpDiv      = $('<div id="' + this._mainDivId + '" class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible" style="width: 100px; display: none; position: absolute;"></div>');
  192.         this._generateHtml();
  193.     },
  194.  
  195.     show: function (input)
  196.     {
  197.         // Get instance to datepicker
  198.         var inst = $.datepicker._getInst(input);
  199.  
  200.         this._time24h = $.datepicker._get(inst, 'time24h');
  201.         this._showTime= $.datepicker._get(inst, 'showTime');
  202.         this._showSec =(this._showTime && this._time24h);  
  203.        
  204.         if(!this._showSec){
  205.            $('#ui-timepicker-div').find('.second').hide();
  206.         }else{
  207.            $('#ui-timepicker-div').find('.second').show(); 
  208.         }
  209.        
  210.         this._altTimeField = $.datepicker._get(inst, 'altTimeField');
  211.  
  212.         var stepMinutes = parseInt($.datepicker._get(inst, 'stepMinutes'), 10) || 1;
  213.         var stepHours   = parseInt($.datepicker._get(inst, 'stepHours'), 10)   || 1;
  214.         var stepSeconds = parseInt($.datepicker._get(inst, 'stepSeconds'), 10) || 1;
  215.  
  216.         if (60 % stepMinutes != 0) { stepMinutes = 1; }
  217.         if (24 % stepHours != 0)   { stepHours   = 1; }
  218.  
  219.         $('#hourSlider').slider('option', 'max', 24 - stepHours);
  220.         $('#hourSlider').slider('option', 'step', stepHours);
  221.  
  222.         $('#minuteSlider').slider('option', 'max', 60 - stepMinutes);
  223.         $('#minuteSlider').slider('option', 'step', stepMinutes);
  224.        
  225.         if(this._showSec){
  226.             $('#secondSlider').slider('option', 'max', 60 - stepSeconds);
  227.             $('#secondSlider').slider('option', 'step', stepSeconds);
  228.         }
  229.  
  230.         this._inputId = input.id;
  231.  
  232.         if (!this._visible) {
  233.             this._parseTime();
  234.             this._orgValue = $('#' + this._inputId).val();
  235.         }
  236.  
  237.         this.resize();
  238.  
  239.         $('#' + this._mainDivId).show();
  240.  
  241.         this._visible = true;
  242.  
  243.         var dpDiv     = $('#' + $.datepicker._mainDivId);
  244.         var dpDivPos  = dpDiv.position();
  245.  
  246.         var viewWidth = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth) + $(document).scrollLeft();
  247.         var tpRight   = this.tpDiv.offset().left + this.tpDiv.outerWidth();
  248.  
  249.         if (tpRight > viewWidth) {
  250.             dpDiv.css('left', dpDivPos.left - (tpRight - viewWidth) - 5);
  251.            this.tpDiv.css('left', dpDiv.offset().left + dpDiv.outerWidth() + 'px');
  252.         }
  253.     },
  254.  
  255.     update: function (fd)
  256.     {
  257.         var curTime = $('#' + this._mainDivId + ' span.fragHours').text()
  258.                     + ':'
  259.                     + $('#' + this._mainDivId + ' span.fragMinutes').text();
  260.  
  261.         if (!this._time24h) {
  262.             curTime += ' ' + $('#' + this._mainDivId + ' span.fragAmpm').text();
  263.         }else if(this._showSec){
  264.             curTime += ':' + $('#' + this._mainDivId + ' span.fragAmpm').text();
  265.         }
  266.  
  267.         var curDate = $('#' + this._inputId).val();
  268.  
  269.         $('#' + this._inputId).val(fd + ' ' + curTime);
  270.  
  271.         if (this._altTimeField) {
  272.             $(this._altTimeField).each(function() { $(this).val(curTime); });
  273.         }
  274.     },
  275.  
  276.     hide: function ()
  277.     {
  278.         this._visible = false;
  279.         $('#' + this._mainDivId).hide();
  280.     },
  281.  
  282.     resize: function ()
  283.     {  
  284.         var dpDiv = $('#' + $.datepicker._mainDivId);
  285.         var dpDivPos = dpDiv.position();
  286.         dpDivPos.top=parseInt(dpDiv.css('top'));  //some problem with top postition alignment
  287.  
  288.         var hdrHeight = $('#' + $.datepicker._mainDivId +  ' > div.ui-datepicker-header:first-child').height();
  289.         var hdrWidth  = this._showSec ? '195px'  :  '100px'  
  290.  
  291.         $('#' + this._mainDivId + ' > div.ui-datepicker-header:first-child').css('height', hdrHeight);
  292.         $('#' + this._mainDivId ).css('width',hdrWidth);
  293.        
  294.        
  295.         this.tpDiv.css({
  296.             'height': dpDiv.height(),
  297.             'top'   : dpDivPos.top,
  298.             'left'  : dpDivPos.left + dpDiv.outerWidth() + 'px'
  299.         });
  300.    
  301.         $('#hourSlider').css('height',   this.tpDiv.height() - (3.5 * hdrHeight));
  302.         $('#minuteSlider').css('height', this.tpDiv.height() - (3.5 * hdrHeight));
  303.         if(this._showSec) $('#secondSlider').css('height', this.tpDiv.height() - (3.5 * hdrHeight));
  304.     },
  305.  
  306.     _generateHtml: function ()
  307.     {      
  308.         var html = '';
  309.  
  310.         html += '<div class="ui-datepicker-header ui-widget-header ui-helper-clearfix ui-corner-all">';
  311.         html += '<div class="ui-datepicker-title" style="margin:0">';
  312.         html += '<span class="fragHours">08</span><span class="delim">:</span><span class="fragMinutes">45</span>';
  313.         html += (this._showSec) ? '<span class="delim second">:</span>' : '';  
  314.         html += '<span class="fragAmpm"></span></div></div><table>';
  315.         html += '<tr><th>Hour</th><th>Minute</th>';
  316.         html += (this._showSec) ? '<th class="second" >Second</th>' : '';
  317.         html += '</tr>';
  318.         html += '<tr><td align="center"><div id="hourSlider" class="slider"></div></td><td align="center"><div id="minuteSlider" class="slider"></div></td>'
  319.         html += (this._showSec) ? '<td class="second" align="center"><div id="secondSlider" class="slider"></div></td>' : '';
  320.         html += '</tr>';
  321.         html += '</table>';
  322.        
  323.         this.tpDiv.empty().append(html);
  324.         $('body').append(this.tpDiv);
  325.  
  326.         var self = this;
  327.  
  328.         $('#hourSlider').slider({
  329.             orientation: "vertical",
  330.             range: 'min',
  331.             min: 0,
  332.             max: 23,
  333.             step: 1,
  334.             slide: function(event, ui) {
  335.                 self._writeTime('hour', ui.value);
  336.             },
  337.             stop: function(event, ui) {
  338.                 $('#' + self._inputId).focus();
  339.             }
  340.         });
  341.  
  342.         $('#minuteSlider').slider({
  343.             orientation: "vertical",
  344.             range: 'min',
  345.             min: 0,
  346.             max: 59,
  347.             step: 1,
  348.             slide: function(event, ui) {
  349.                 self._writeTime('minute', ui.value);
  350.             },
  351.             stop: function(event, ui) {
  352.                 $('#' + self._inputId).focus();
  353.             }
  354.         });
  355.        
  356.        $('#hourSlider > a').css('padding', 0);
  357.        $('#minuteSlider > a').css('padding', 0);
  358.        
  359.        if( this._showSec){
  360.            
  361.         $('#secondSlider').slider({
  362.             orientation: "vertical",
  363.             range: 'min',
  364.             min: 0,
  365.             max: 59,
  366.             step: 1,
  367.             slide: function(event, ui) {
  368.                 self._writeTime('second', ui.value);
  369.             },
  370.             stop: function(event, ui) {
  371.                 $('#' + self._inputId).focus();
  372.             }
  373.         });
  374.        
  375.         $('#secondSlider > a').css('padding', 0);
  376.        }
  377.                    
  378.     },
  379.  
  380.     _writeTime: function (type, value)
  381.     {
  382.         if (type == 'hour') {
  383.             if (!this._time24h) {
  384.                 if (value < 12) {
  385.                     $('#' + this._mainDivId + ' span.fragAmpm').text('am');
  386.                 } else {
  387.                     $('#' + this._mainDivId + ' span.fragAmpm').text('pm');
  388.                     value -= 12;
  389.                 }
  390.  
  391.                 if (value == 0) value = 12;
  392.             } else {
  393.               if(!this._showSec)
  394.                 $('#' + this._mainDivId + ' span.fragAmpm').text('');
  395.             }
  396.  
  397.             if (value < 10) value = '0' + value;
  398.             $('#' + this._mainDivId + ' span.fragHours').text(value);
  399.         }
  400.  
  401.         if (type == 'minute') {
  402.             if (value < 10) value = '0' + value;
  403.             $('#' + this._mainDivId + ' span.fragMinutes').text(value);
  404.         }
  405.        
  406.         if (type == 'second') {
  407.             if (value < 10) value = '0' + value;
  408.             $('#' + this._mainDivId + ' span.fragAmpm').text(value);
  409.         }
  410.     },
  411.  
  412.     _parseTime: function ()
  413.     {
  414.         var dt = $('#' + this._inputId).val();
  415.  
  416.         this._colonPos = dt.search(':');
  417.  
  418.         var m = 0, h = 0,s=0, a = '';
  419.  
  420.         if (this._colonPos != -1) {
  421.             h = parseInt(dt.substr(this._colonPos - 2, 2), 10);
  422.             m = parseInt(dt.substr(this._colonPos + 1, 2), 10);
  423.             s = parseInt(dt.substr(dt.lastIndexOf(':')+1, 2), 10);
  424.            
  425.             a = jQuery.trim(dt.substr(this._colonPos + 3, 3));
  426.         }
  427.  
  428.         a = a.toLowerCase();
  429.  
  430.         if (a != 'am' && a != 'pm') {          
  431.             a = '';            
  432.         }
  433.  
  434.         if (h < 0) h = 0;
  435.         if (m < 0) m = 0;
  436.  
  437.         if (h > 23) h = 23;
  438.         if (m > 59) m = 59;
  439.  
  440.         if (a == 'pm' && h  < 12) h += 12;
  441.         if (a == 'am' && h == 12) h  = 0;
  442.  
  443.         this._setTime('hour',   h);
  444.         this._setTime('minute', m);
  445.         this._setTime('second', s);
  446.  
  447.         this._orgHour   = h;
  448.         this._orgMinute = m;        
  449.         this._orgSec = s;
  450.     },
  451.  
  452.     _setTime: function (type, value)
  453.     {
  454.         if (isNaN(value)) value = 0;
  455.         if (value < 0)    value = 0;
  456.         if (value > 23 && type == 'hour')   value = 23;
  457.         if (value > 59 && type == 'minute') value = 59;
  458.  
  459.         if (type == 'hour') {
  460.             $('#hourSlider').slider('value', value);            
  461.         }
  462.  
  463.         if (type == 'minute') {        
  464.             $('#minuteSlider').slider('value', value);            
  465.         }
  466.        
  467.         if (type == 'second') {        
  468.             $('#secondSlider').slider('value', value);            
  469.         }
  470.  
  471.         this._writeTime(type, value);
  472.     }  
  473. };
  474.  
  475. $.timepicker = new Timepicker();
  476. $('document').ready(function () {$.timepicker.init();});
  477.  
  478. })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement