http://haineault.com
MIT License (http://www.opensource.org/licenses/mit-license.php */
$.fn.timepicker = function() {
var $picker = $("#time_picker");
if($picker.length === 0) {
$picker = $._initializeTimepicker();
}
this.each(function() {
$(this).focus(function() {
var offset = $(this).offset();
var height = $(this).outerHeight();
var width = $(this).outerWidth();
var $picker = $("#time_picker");
$picker.css({
left: -1000,
height: 'auto',
width: 'auto'
}).show();
var pickerOffset = $picker.offset();
var pickerHeight = $picker.outerHeight();
var pickerWidth = $picker.outerWidth();
$picker.css({
top: offset.top + height,
left: offset.left
}).end();
$("#time_picker .time_slot").removeClass('ui-state-highlight').removeClass('ui-state-active');
$picker.data('attached_to', $(this)[0]);
var windowHeight = $(window).height();
var windowWidth = $(window).width();
var scrollTop = $.windowScrollTop();
if((offset.top + height - scrollTop + pickerHeight) > windowHeight) {
$picker.css({
top: offset.top - pickerHeight
});
}
if(offset.left + pickerWidth > windowWidth) {
$picker.css({
left: offset.left + width - pickerWidth
});
}
$("#time_picker").hide().slideDown();
}).blur(function() {
if($("#time_picker").data('attached_to') == $(this)[0]) {
$("#time_picker").data('attached_to', null);
$("#time_picker").hide()
.find(".time_slot.ui-state-highlight").removeClass('ui-state-highlight');
}
}).keycodes("esc return", function(event) {
$(this).triggerHandler('blur');
}).keycodes("ctrl+up ctrl+right ctrl+left ctrl+down", function(event) {
if($("#time_picker").data('attached_to') != $(this)[0]) {
return;
}
event.preventDefault();
var $current = $("#time_picker .time_slot.ui-state-highlight:first");
var time = $($("#time_picker").data('attached_to')).val();
var hr = 12;
var min = "00";
var ampm = "pm";
var idx;
if(time && time.length >= 7) {
hr = time.substring(0, 2);
min = time.substring(3, 5);
ampm = time.substring(5, 7);
}
if($current.length === 0) {
idx = parseInt(time, 10) - 1;
if(isNaN(idx)) { idx = 0; }
$("#time_picker .time_slot").eq(idx).triggerHandler('mouseover');
return;
}
if(event.keyString == "ctrl+up") {
var $parent = $current.parent(".widget_group");
idx = $parent.children(".time_slot").index($current);
if($parent.hasClass('ampm_group')) {
idx = min / 15;
} else if($parent.hasClass('minute_group')) {
idx = parseInt(hr, 10) - 1;
}
$parent.prev(".widget_group").find(".time_slot").eq(idx).triggerHandler('mouseover');
} else if(event.keyString == "ctrl+right") {
$current.next(".time_slot").triggerHandler('mouseover');
} else if(event.keyString == "ctrl+left") {
$current.prev(".time_slot").triggerHandler('mouseover');
} else if(event.keyString == "ctrl+down") {
$parent = $current.parent(".widget_group");
idx = $parent.children(".time_slot").index($current);
var $list = $parent.next(".widget_group").find(".time_slot");
idx = Math.min(idx, $list.length - 1);
if($parent.hasClass('hour_group')) {
idx = min / 15;
} else if($parent.hasClass('minute_group')) {
idx = (ampm == "am") ? 0 : 1;
}
$list.eq(idx).triggerHandler('mouseover');
}
});
});
return this;
};
$._initializeTimepicker = function() {
var $picker = $(document.createElement('div'));
$picker.attr('id', 'time_picker').css({
position: "absolute",
display: "none"
});
var pickerHtml = "";
pickerHtml += "";
pickerHtml += "";
$picker.html(pickerHtml);
$("body").append($picker);
$picker.find(".time_slot").mouseover(function() {
$picker.find(".time_slot.ui-state-highlight").removeClass('ui-state-highlight');
$(this).addClass('ui-state-highlight');
var $field = $($picker.data('attached_to') || "none");
var time = $field.val();
var hr = 12;
var min = "00";
var ampm = "pm";
if(time && time.length >= 7) {
hr = time.substring(0, 2);
min = time.substring(3, 5);
ampm = time.substring(5, 7);
}
var val = $(this).text();
if(val > 0 && val <= 12) {
hr = val;
} else if(val == "am" || val == "pm") {
ampm = val;
} else {
min = val;
}
$field.val(hr + ":" + min + ampm);
}).mouseout(function() {
$(this).removeClass('ui-state-highlight');
}).mousedown(function(event) {
event.preventDefault();
$(this).triggerHandler('mouseover');
$(this).removeClass('ui-state-highlight').addClass('ui-state-active');
}).mouseup(function() {
$(this).removeClass('ui-state-active');
}).click(function(event) {
event.preventDefault();
$(this).triggerHandler('mouseover');
if($picker.data('attached_to')) {
$($picker.data('attached_to')).focus();
}
$picker.stop().hide().data('attached_to', null);
});
return $picker;
};
export default $