Updated from jozsika

This commit is contained in:
Trent Richardson 2010-09-01 14:48:13 -04:00
parent 62e19e68c4
commit 44a17de112

809
jquery-ui-timepicker-addon.js vendored Normal file → Executable file
View file

@ -1,405 +1,404 @@
/* /*
* jQuery timepicker addon * jQuery timepicker addon
* By: Trent Richardson [http://trentrichardson.com] * By: Trent Richardson [http://trentrichardson.com]
* Version 0.5 * Version 0.5
* Last Modified: 6/16/2010 * Last Modified: 6/16/2010
* *
* Copyright 2010 Trent Richardson * Copyright 2010 Trent Richardson
* Dual licensed under the MIT and GPL licenses. * Dual licensed under the MIT and GPL licenses.
* http://trentrichardson.com/Impromptu/GPL-LICENSE.txt * http://trentrichardson.com/Impromptu/GPL-LICENSE.txt
* http://trentrichardson.com/Impromptu/MIT-LICENSE.txt * http://trentrichardson.com/Impromptu/MIT-LICENSE.txt
* *
* HERES THE CSS: * HERES THE CSS:
* #ui-timepicker-div dl{ text-align: left; } * #ui-timepicker-div dl{ text-align: left; }
* #ui-timepicker-div dl dt{ height: 25px; } * #ui-timepicker-div dl dt{ height: 25px; }
* #ui-timepicker-div dl dd{ margin: -25px 0 10px 65px; } * #ui-timepicker-div dl dd{ margin: -25px 0 10px 65px; }
*/ */
(function($) {
function Timepicker() { } (function($) {
function Timepicker() { }
Timepicker.prototype = {
$input: null, Timepicker.prototype = {
$timeObj: null, $input: null,
inst: null, $timeObj: null,
hour_slider: null, inst: null,
minute_slider: null, hour_slider: null,
second_slider: null, minute_slider: null,
hour: 0, second_slider: null,
minute: 0, hour: 0,
second: 0, minute: 0,
ampm: '', second: 0,
formattedDate: '', ampm: '',
formattedTime: '', formattedDate: '',
formattedDateTime: '', formattedTime: '',
defaults: { formattedDateTime: '',
holdDatepickerOpen: true,
showButtonPanel: true, defaults: {
timeOnly: false, holdDatepickerOpen: true,
showHour: true, showButtonPanel: true,
showMinute: true, timeOnly: false,
showSecond: false, showHour: true,
showTime: true, showMinute: true,
//---------------------------- showSecond: false,
stepHour: .05, showTime: true,
stepMinute: .05, stepHour: 0.05,
stepSecond: .05, stepMinute: 0.05,
ampm: false, stepSecond: 0.05,
hour: 0, ampm: false,
minute: 0, hour: 0,
second: 0, minute: 0,
timeFormat: 'hh:mm tt', second: 0,
alwaysSetTime: true timeFormat: 'hh:mm tt',
//---------------------------- alwaysSetTime: true
}, },
//######################################################################## //########################################################################
// add our sliders to the calendar // add our sliders to the calendar
//######################################################################## //########################################################################
addTimePicker: function(dp_inst) { addTimePicker: function(dp_inst) {
var tp_inst = this; var tp_inst = this;
var currDT = this.$input.val(); var currDT = this.$input.val();
var regstr = this.defaults.timeFormat.toString() var regstr = this.defaults.timeFormat.toString()
.replace(/h{1,2}/ig, '(\\d?\\d)') .replace(/h{1,2}/ig, '(\\d?\\d)')
.replace(/m{1,2}/ig, '(\\d?\\d)') .replace(/m{1,2}/ig, '(\\d?\\d)')
.replace(/s{1,2}/ig, '(\\d?\\d)') .replace(/s{1,2}/ig, '(\\d?\\d)')
.replace(/t{1,2}/ig, '(am|pm|a|p)?') .replace(/t{1,2}/ig, '(am|pm|a|p)?')
.replace(/\s/g, '\\s?') + '$'; .replace(/\s/g, '\\s?') + '$';
if (!this.defaults.timeOnly) { if (!this.defaults.timeOnly) {
//the time should come after x number of characters and a space. x = at least the length of text specified by the date format //the time should come after x number of characters and a space. x = at least the length of text specified by the date format
regstr = '\\S{' + this.defaults.timeFormat.length + ',}\\s+' + regstr; regstr = '.{' + this.defaults.timeFormat.length + ',}\\s+' + regstr;
} }
//--------------------------------------
var order = this.getFormatPositions();
var order = this.getFormatPositions(); var treg = currDT.match(new RegExp(regstr, 'i'));
var treg = currDT.match(new RegExp(regstr, 'i'));
if (treg) {
if (treg) { if (order.t !== -1) {
if (order.t !== -1) this.ampm = ((treg[order.t] === undefined || treg[order.t].length === 0) ? '' : (treg[order.t].charAt(0).toUpperCase() == 'A') ? 'AM' : 'PM').toUpperCase();
this.ampm = ((treg[order.t] == undefined || treg[order.t].length == 0) ? '' : (treg[order.t].charAt(0).toUpperCase() == 'A') ? 'AM' : 'PM').toUpperCase(); }
if (order.h !== -1) { if (order.h !== -1) {
if (this.ampm == 'AM' && treg[order.h] == '12') if (this.ampm == 'AM' && treg[order.h] == '12') {
this.hour = 0; // 12am = 0 hour // 12am = 0 hour
else if (this.ampm == 'PM' && treg[order.h] != '12') this.hour = 0;
this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0); //12pm = 12 hour, any other pm = hour + 12 } else if (this.ampm == 'PM' && treg[order.h] != '12') {
else // 12pm = 12 hour, any other pm = hour + 12
this.hour = treg[order.h]; this.hour = (parseFloat(treg[order.h]) + 12).toFixed(0);
} } else {
this.hour = treg[order.h];
if (order.m !== -1) }
this.minute = treg[order.m]; }
if (order.s !== -1) if (order.m !== -1) {
this.second = treg[order.s]; this.minute = treg[order.m];
} }
tp_inst.timeDefined = (treg) ? true : false; if (order.s !== -1) {
//--------------------- this.second = treg[order.s];
}
// wait for datepicker to create itself.. 60% of the time it works every time.. }
setTimeout(function() {
tp_inst.injectTimePicker(dp_inst, tp_inst); tp_inst.timeDefined = (treg) ? true : false;
}, 10);
// wait for datepicker to create itself.. 60% of the time it works every time..
}, setTimeout(function() {
tp_inst.injectTimePicker(dp_inst, tp_inst);
//######################################################################## }, 10);
// figure out position of time elements.. cause js cant do named captures
//######################################################################## },
getFormatPositions: function() {
var finds = this.defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|t{1,2})/g); //########################################################################
var orders = { h: -1, m: -1, s: -1, t: -1 }; // figure out position of time elements.. cause js cant do named captures
//########################################################################
if (finds) { getFormatPositions: function() {
for (var i = 0; i < finds.length; i++) { var finds = this.defaults.timeFormat.toLowerCase().match(/(h{1,2}|m{1,2}|s{1,2}|t{1,2})/g);
if (orders[finds[i].toString().charAt(0)] == -1) var orders = { h: -1, m: -1, s: -1, t: -1 };
orders[finds[i].toString().charAt(0)] = i + 1;
} if (finds) {
} for (var i = 0; i < finds.length; i++) {
if (orders[finds[i].toString().charAt(0)] == -1) {
return orders; orders[finds[i].toString().charAt(0)] = i + 1;
}, }
}
//######################################################################## }
// generate and inject html for timepicker into ui datepicker
//######################################################################## return orders;
injectTimePicker: function(dp_inst, tp_inst) { },
var $dp = $('#' + $.datepicker._mainDivId);
//########################################################################
/* // generate and inject html for timepicker into ui datepicker
* Added by Peter Medeiros //########################################################################
* - Figure out what the minute max should be based on the step minute value. injectTimePicker: function(dp_inst, tp_inst) {
* - Get Remainder of 59 / Step Minute and subtract from 59 (59 being total minutes in an hour) var $dp = $('#' + $.datepicker._mainDivId);
*/ var opts = tp_inst.defaults;
var hourMax = 23 - (23 % tp_inst.defaults.stepHour);
var minMax = 59 - (59 % tp_inst.defaults.stepMinute); // Added by Peter Medeiros:
var secMax = 59 - (59 % tp_inst.defaults.stepSecond); // - Figure out what the hour/minute/second max should be based on the step values.
// - Example: if stepMinute is 15, then minMax is 45.
/* End Added by Peter Medeiros */ var hourMax = 23 - (23 % opts.stepHour);
var minMax = 59 - (59 % opts.stepMinute);
// Prevent displaying twice var secMax = 59 - (59 % opts.stepSecond);
if ($dp.find("div#ui-timepicker-div").length == 0) {
var html = '<div id="ui-timepicker-div">' + // Prevent displaying twice
'<dl>' + if ($dp.find("div#ui-timepicker-div").length === 0) {
'<dt id="ui_tpicker_time_label"' + ((tp_inst.defaults.showTime) ? '' : ' style="display:none;"') + '>Time</dt>' + var noDisplay = ' style="display:none;"';
'<dd id="ui_tpicker_time"' + ((tp_inst.defaults.showTime) ? '' : ' style="display:none;"') + '></dd>' + var html =
'<dt id="ui_tpicker_hour_label"' + ((tp_inst.defaults.showHour) ? '' : ' style="display:none;"') + '>Hour</dt>' + '<div id="ui-timepicker-div"><dl>' +
'<dd id="ui_tpicker_hour"' + ((tp_inst.defaults.showHour) ? '' : ' style="display:none;"') + '></dd>' + '<dt id="ui_tpicker_time_label"' + ((opts.showTime) ? '' : noDisplay) + '>Time</dt>' +
'<dt id="ui_tpicker_minute_label"' + ((tp_inst.defaults.showMinute) ? '' : ' style="display:none;"') + '>Minute</dt>' + '<dd id="ui_tpicker_time"' + ((opts.showTime) ? '' : noDisplay) + '></dd>' +
'<dd id="ui_tpicker_minute"' + ((tp_inst.defaults.showMinute) ? '' : ' style="display:none;"') + '></dd>' + '<dt id="ui_tpicker_hour_label"' + ((opts.showHour) ? '' : noDisplay) + '>Hour</dt>' +
'<dt id="ui_tpicker_second_label"' + ((tp_inst.defaults.showSecond) ? '' : ' style="display:none;"') + '>Second</dt>' + '<dd id="ui_tpicker_hour"' + ((opts.showHour) ? '' : noDisplay) + '></dd>' +
'<dd id="ui_tpicker_second"' + ((tp_inst.defaults.showSecond) ? '' : ' style="display:none;"') + '></dd>' + '<dt id="ui_tpicker_minute_label"' + ((opts.showMinute) ? '' : noDisplay) + '>Minute</dt>' +
'</dl>' + '<dd id="ui_tpicker_minute"' + ((opts.showMinute) ? '' : noDisplay) + '></dd>' +
'</div>'; '<dt id="ui_tpicker_second_label"' + ((opts.showSecond) ? '' : noDisplay) + '>Second</dt>' +
'<dd id="ui_tpicker_second"' + ((opts.showSecond) ? '' : noDisplay) + '></dd>' +
$tp = $(html); '</dl></div>';
$tp = $(html);
if (tp_inst.defaults.timeOnly == true) { // if we only want time picker
$tp.prepend('<div class="ui-widget-header ui-helper-clearfix ui-corner-all"><div class="ui-datepicker-title">Choose Time</div></div>'); // if we only want time picker...
$dp.find('.ui-datepicker-header, .ui-datepicker-calendar, .ui-datepicker-current').hide(); if (opts.timeOnly === true) {
} $tp.prepend(
'<div class="ui-widget-header ui-helper-clearfix ui-corner-all">' +
tp_inst.hour_slider = $tp.find('#ui_tpicker_hour').slider({ orientation: "horizontal", value: tp_inst.hour, min:0, max: hourMax, step: tp_inst.defaults.stepHour, slide: function(event, ui) { '<div class="ui-datepicker-title">Choose Time</div>' +
tp_inst.hour_slider.slider( "option", "value", ui.value ); '</div>');
tp_inst.onTimeChange(dp_inst, tp_inst); $dp.find('.ui-datepicker-header, .ui-datepicker-calendar, .ui-datepicker-current').hide();
} }); }
/*
* Updated by Peter Medeiros tp_inst.hour_slider = $tp.find('#ui_tpicker_hour').slider({
* - Pass in Event and UI instance into slide function orientation: "horizontal",
*/ value: tp_inst.hour,
tp_inst.minute_slider = $tp.find('#ui_tpicker_minute').slider({ orientation: "horizontal", value: tp_inst.minute, min:0, max: minMax, step: tp_inst.defaults.stepMinute, slide: function(event, ui) { min: 0,
tp_inst.minute_slider.slider( "option", "value", ui.value ); // update the global minute slider instance value with the current slider value max: hourMax,
tp_inst.onTimeChange(dp_inst, tp_inst); step: opts.stepHour,
} }); slide: function(event, ui) {
tp_inst.second_slider = $tp.find('#ui_tpicker_second').slider({ orientation: "horizontal", value: tp_inst.second, min:0, max: secMax, step: tp_inst.defaults.stepSecond, slide: function(event, ui) { tp_inst.hour_slider.slider( "option", "value", ui.value );
tp_inst.second_slider.slider( "option", "value", ui.value ); tp_inst.onTimeChange(dp_inst, tp_inst);
tp_inst.onTimeChange(dp_inst, tp_inst); },
} }); });
$dp.find('.ui-datepicker-calendar').after($tp); // Updated by Peter Medeiros:
tp_inst.$timeObj = $('#ui_tpicker_time'); // - Pass in Event and UI instance into slide function
tp_inst.minute_slider = $tp.find('#ui_tpicker_minute').slider({
if (dp_inst != null) { orientation: "horizontal",
value: tp_inst.minute,
var timeDefined = tp_inst.timeDefined; min: 0,
max: minMax,
tp_inst.onTimeChange(dp_inst, tp_inst); step: opts.stepMinute,
tp_inst.timeDefined = timeDefined; slide: function(event, ui) {
// update the global minute slider instance value with the current slider value
} tp_inst.minute_slider.slider( "option", "value", ui.value );
} tp_inst.onTimeChange(dp_inst, tp_inst);
}, },
});
//########################################################################
// when a slider moves.. tp_inst.second_slider = $tp.find('#ui_tpicker_second').slider({
// on time change is also called when the time is updated in the text field orientation: "horizontal",
//######################################################################## value: tp_inst.second,
onTimeChange: function(dp_inst, tp_inst) { min: 0,
var hour = tp_inst.hour_slider.slider('value'); max: secMax,
var minute = tp_inst.minute_slider.slider('value'); step: opts.stepSecond,
var second = tp_inst.second_slider.slider('value'); slide: function(event, ui) {
var ampm = (tp_inst.hour < 12) ? 'AM' : 'PM'; tp_inst.second_slider.slider( "option", "value", ui.value );
tp_inst.onTimeChange(dp_inst, tp_inst);
var hasChanged = false; },
});
// if the update was done in the input field, this field should not be updated
// if the update was done using the sliders, update the input field $dp.find('.ui-datepicker-calendar').after($tp);
if (tp_inst.hour != hour || tp_inst.minute != minute || tp_inst.second != second || (tp_inst.ampm.length > 0 && tp_inst.ampm != ampm)) tp_inst.$timeObj = $('#ui_tpicker_time');
hasChanged = true;
if (dp_inst !== null) {
tp_inst.hour = parseFloat(hour).toFixed(0); var timeDefined = tp_inst.timeDefined;
tp_inst.minute = parseFloat(minute).toFixed(0); tp_inst.onTimeChange(dp_inst, tp_inst);
tp_inst.second = parseFloat(second).toFixed(0); tp_inst.timeDefined = timeDefined;
tp_inst.ampm = ampm; }
}
tp_inst.formatTime(tp_inst); },
tp_inst.$timeObj.text(tp_inst.formattedTime); //########################################################################
// when a slider moves..
if (hasChanged) { // on time change is also called when the time is updated in the text field
tp_inst.updateDateTime(dp_inst, tp_inst); //########################################################################
tp_inst.timeDefined = true; onTimeChange: function(dp_inst, tp_inst) {
} var hour = tp_inst.hour_slider.slider('value');
}, var minute = tp_inst.minute_slider.slider('value');
var second = tp_inst.second_slider.slider('value');
//######################################################################## var ampm = (tp_inst.hour < 12) ? 'AM' : 'PM';
// format the time all pretty... var hasChanged = false;
//########################################################################
formatTime: function(inst) { // If the update was done in the input field, this field should not be updated.
var tmptime = inst.defaults.timeFormat.toString(); // If the update was done using the sliders, update the input field.
var hour12 = ((inst.ampm == 'AM') ? (inst.hour) : (inst.hour % 12)); if (tp_inst.hour != hour || tp_inst.minute != minute || tp_inst.second != second || (tp_inst.ampm.length > 0 && tp_inst.ampm != ampm)) {
hour12 = (hour12 == 0) ? 12 : hour12; hasChanged = true;
}
if (inst.defaults.ampm == true) {
tmptime = tmptime.toString() tp_inst.hour = parseFloat(hour).toFixed(0);
.replace(/hh/g, ((hour12 < 10) ? '0' : '') + hour12) tp_inst.minute = parseFloat(minute).toFixed(0);
.replace(/h/g, hour12) tp_inst.second = parseFloat(second).toFixed(0);
.replace(/mm/g, ((inst.minute < 10) ? '0' : '') + inst.minute) tp_inst.ampm = ampm;
.replace(/m/g, inst.minute)
.replace(/ss/g, ((inst.second < 10) ? '0' : '') + inst.second) tp_inst.formatTime(tp_inst);
.replace(/s/g, inst.second) tp_inst.$timeObj.text(tp_inst.formattedTime);
.replace(/TT/g, inst.ampm.toUpperCase())
.replace(/tt/g, inst.ampm.toLowerCase()) if (hasChanged) {
.replace(/T/g, inst.ampm.charAt(0).toUpperCase()) tp_inst.updateDateTime(dp_inst, tp_inst);
.replace(/t/g, inst.ampm.charAt(0).toLowerCase()); tp_inst.timeDefined = true;
} }
else { },
tmptime = tmptime.toString()
.replace(/hh/g, ((inst.hour < 10) ? '0' : '') + inst.hour) //########################################################################
.replace(/h/g, inst.hour) // format the time all pretty...
.replace(/mm/g, ((inst.minute < 10) ? '0' : '') + inst.minute) //########################################################################
.replace(/m/g, inst.minute) formatTime: function(inst) {
.replace(/ss/g, ((inst.second < 10) ? '0' : '') + inst.second) var tmptime = inst.defaults.timeFormat.toString();
.replace(/s/g, inst.second); var hour12 = ((inst.ampm == 'AM') ? (inst.hour) : (inst.hour % 12));
tmptime = $.trim(tmptime.replace(/t/gi, '')); hour12 = (hour12 === 0) ? 12 : hour12;
}
if (inst.defaults.ampm === true) {
inst.formattedTime = tmptime; tmptime = tmptime.toString()
return inst.formattedTime; .replace(/hh/g, ((hour12 < 10) ? '0' : '') + hour12)
}, .replace(/h/g, hour12)
.replace(/mm/g, ((inst.minute < 10) ? '0' : '') + inst.minute)
//######################################################################## .replace(/m/g, inst.minute)
// update our input with the new date time.. .replace(/ss/g, ((inst.second < 10) ? '0' : '') + inst.second)
//######################################################################## .replace(/s/g, inst.second)
updateDateTime: function(dp_inst, tp_inst) { .replace(/TT/g, inst.ampm.toUpperCase())
var dt = this.$input.datepicker('getDate'); .replace(/tt/g, inst.ampm.toLowerCase())
.replace(/T/g, inst.ampm.charAt(0).toUpperCase())
if (dt == null) .replace(/t/g, inst.ampm.charAt(0).toLowerCase());
this.formattedDate = $.datepicker.formatDate($.datepicker._get(dp_inst, 'dateFormat'), new Date(), $.datepicker._getFormatConfig(dp_inst));
else this.formattedDate = $.datepicker.formatDate($.datepicker._get(dp_inst, 'dateFormat'), dt, $.datepicker._getFormatConfig(dp_inst)); } else {
tmptime = tmptime.toString()
if (this.defaults.alwaysSetTime) { .replace(/hh/g, ((inst.hour < 10) ? '0' : '') + inst.hour)
this.formattedDateTime = this.formattedDate + ' ' + this.formattedTime; .replace(/h/g, inst.hour)
} .replace(/mm/g, ((inst.minute < 10) ? '0' : '') + inst.minute)
else { .replace(/m/g, inst.minute)
if (dt == null || !tp_inst.timeDefined || tp_inst.timeDefined == false) { .replace(/ss/g, ((inst.second < 10) ? '0' : '') + inst.second)
this.formattedDateTime = this.formattedDate; .replace(/s/g, inst.second);
} tmptime = $.trim(tmptime.replace(/t/gi, ''));
else { }
this.formattedDateTime = this.formattedDate + ' ' + this.formattedTime;
} inst.formattedTime = tmptime;
} return inst.formattedTime;
//----------------------------- },
if (this.defaults.timeOnly == true) //########################################################################
this.$input.val(this.formattedTime); // update our input with the new date time..
else this.$input.val(this.formattedDateTime); //########################################################################
} updateDateTime: function(dp_inst, tp_inst) {
}; var dt = this.$input.datepicker('getDate');
var dateFmt = $.datepicker._get(dp_inst, 'dateFormat');
//######################################################################## var formatCfg = $.datepicker._getFormatConfig(dp_inst);
// extend timepicker to datepicker this.formattedDate = $.datepicker.formatDate(dateFmt, (dt === null ? new Date() : dt), formatCfg);
//######################################################################## var formattedDateTime = this.formattedDate;
jQuery.fn.datetimepicker = function(o) { var timeAvailable = dt !== null && tp_inst.timeDefined;
var tp = new Timepicker();
if (this.defaults.timeOnly !== true && (this.defaults.alwaysSetTime || timeAvailable)) {
if (o == undefined) formattedDateTime += ' ' + this.formattedTime;
o = {}; }
tp.defaults = $.extend({}, tp.defaults, o); this.formattedDateTime = formattedDateTime;
this.$input.val(formattedDateTime);
tp.defaults = $.extend({}, tp.defaults, { }
beforeShow: function(input, inst) { };
tp.hour = tp.defaults.hour;
tp.minute = tp.defaults.minute; //########################################################################
tp.second = tp.defaults.second; // extend timepicker to datepicker
tp.ampm = ''; //########################################################################
tp.$input = $(input); jQuery.fn.datetimepicker = function(o) {
tp.inst = inst; var opts = (o === undefined ? {} : o);
var tp = new Timepicker();
tp.addTimePicker(inst);
var beforeShowFunc = function(input, inst) {
if ($.isFunction(o['beforeShow'])) o.beforeShow(input, inst); tp.hour = tp.defaults.hour;
}, tp.minute = tp.defaults.minute;
onChangeMonthYear: function(year, month, inst) { tp.second = tp.defaults.second;
tp.ampm = '';
// Update the time as well : this prevents the time from disappearing from the input field. tp.$input = $(input);
tp.updateDateTime(inst, tp); tp.inst = inst;
tp.addTimePicker(inst);
if ($.isFunction(o['onChangeMonthYear'])) o.onChangeMonthYear(year, month, inst); if ($.isFunction(opts.beforeShow)) {
}, opts.beforeShow(input, inst);
onClose: function(dateText, inst) { }
tp.updateDateTime(inst, tp); };
if ($.isFunction(o['onClose'])) o.onClose(dateText, inst); var onChangeMonthYearFunc = function(year, month, inst) {
} // Update the time as well : this prevents the time from disappearing from the input field.
}); tp.updateDateTime(inst, tp);
if ($.isFunction(opts.onChangeMonthYear)) {
$(this).datepicker(tp.defaults); opts.onChangeMonthYear(year, month, inst);
}; }
};
//########################################################################
// shorthand just to use timepicker.. var onCloseFunc = function(dateText, inst) {
//######################################################################## tp.updateDateTime(inst, tp);
jQuery.fn.timepicker = function(o) { if ($.isFunction(opts.onClose)) {
o = $.extend(o, { timeOnly: true }); opts.onClose(dateText, inst);
}
$(this).datetimepicker(o); };
};
tp.defaults = $.extend({}, tp.defaults, opts, {
//######################################################################## beforeShow: beforeShowFunc,
// the bad hack :/ override datepicker so it doesnt close on select onChangeMonthYear: onChangeMonthYearFunc,
//######################################################################## onClose: onCloseFunc,
$.datepicker._selectDate = function(id, dateStr) { });
var target = $(id);
var inst = this._getInst(target[0]); $(this).datepicker(tp.defaults);
var holdDatepickerOpen = (this._get(inst, 'holdDatepickerOpen') === true) ? true : false; // this line for timepicker.. };
dateStr = (dateStr != null ? dateStr : this._formatDate(inst));
//########################################################################
if (inst.input) // shorthand just to use timepicker..
inst.input.val(dateStr); //########################################################################
this._updateAlternate(inst); jQuery.fn.timepicker = function(opts) {
var onSelect = this._get(inst, 'onSelect'); opts = $.extend(opts, { timeOnly: true });
if (onSelect) $(this).datetimepicker(opts);
onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]); // trigger custom callback };
else if (inst.input)
inst.input.trigger('change'); // fire the change event //########################################################################
if (inst.inline) // the bad hack :/ override datepicker so it doesnt close on select
this._updateDatepicker(inst); // inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378
else if (holdDatepickerOpen) { } // this line for timepicker.. //########################################################################
else { $.datepicker._selectDateOverload = $.datepicker._selectDate;
this._hideDatepicker(); $.datepicker._selectDate = function (id, dateStr) {
this._lastInput = inst.input[0]; var target = $(id);
if (typeof (inst.input[0]) != 'object') var inst = this._getInst(target[0]);
inst.input.focus(); // restore focus inst.inline = true;
this._lastInput = null; $.datepicker._selectDateOverload(id, dateStr);
} inst.inline = false;
this._notifyChange(inst);
this._notifyChange(inst); this._updateDatepicker(inst);
}; };
//Change 4 - reduction of code required for override for _updateDatepicker function $.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
$.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker; //#############################################################################################
//############################################################################################# // second bad hack :/ override datepicker so it triggers an event when changing the input field
// second bad hack :/ override datepicker so it triggers an event when changing the input field //#############################################################################################
//############################################################################################# // Generate the date picker content.
/* Generate the date picker content. */ $.datepicker._updateDatepicker = function(inst) {
$.datepicker._updateDatepicker = function(inst) { this._base_updateDatepicker(inst);
this._base_updateDatepicker(inst); // Reload the time control when changing something in the input text field.
// Reload the time control when changing something in the input text field. this._beforeShow(inst.input, inst);
this._beforeShow(inst.input, inst); };
};
$.datepicker._beforeShow = function(input, inst) {
$.datepicker._beforeShow = function(input, inst) { var beforeShow = this._get(inst, 'beforeShow');
var beforeShow = this._get(inst, 'beforeShow'); if (beforeShow) {
if (beforeShow) beforeShow.apply((inst.input ? inst.input[0] : null), [inst.input, inst]);
beforeShow.apply((inst.input ? inst.input[0] : null), [inst.input, inst]); }
}; };
//End Change 4 --------------------------------------------------------------------
//#######################################################################################
//####################################################################################### // third bad hack :/ override datepicker so it allows spaces and colan in the input field
// third bad hack :/ override datepicker so it allows spaces and colan in the input field //#######################################################################################
//####################################################################################### $.datepicker._doKeyPress = function(event) {
$.datepicker._doKeyPress = function(event) { var inst = $.datepicker._getInst(event.target);
var inst = $.datepicker._getInst(event.target); if ($.datepicker._get(inst, 'constrainInput')) {
if ($.datepicker._get(inst, 'constrainInput')) { var dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat'));
var dateChars = $.datepicker._possibleChars($.datepicker._get(inst, 'dateFormat')); var chr = String.fromCharCode(event.charCode === undefined ? event.keyCode : event.charCode);
var chr = String.fromCharCode(event.charCode == undefined ? event.keyCode : event.charCode); // keyCode == 58 => ":"
// keyCode == 58 => ":" // keyCode == 32 => " "
// keyCode == 32 => " " return event.ctrlKey || (chr < ' ' || !dateChars || dateChars.indexOf(chr) > -1 || event.keyCode == 58 || event.keyCode == 32);
return event.ctrlKey || (chr < ' ' || !dateChars || dateChars.indexOf(chr) > -1 || event.keyCode == 58 || event.keyCode == 32); }
} };
};
})(jQuery);
})(jQuery);