/*
*/
(function ($, undefined) {
$.extend($.ui, { timepicker: { version: "0.0.8"} });
var PROP_NAME = 'timepicker';
var tpuuid = new Date().getTime();
/* Time picker manager.
Use the singleton instance of this class, $.timepicker, to interact with the time picker.
Settings for (groups of) time pickers are maintained in an instance object,
allowing multiple different settings on the same page. */
function Timepicker() {
this.debug = true; // Change this to true to start debugging
this._curInst = null; // The current instance in use
this._isInline = false; // true if the instance is displayed inline
this._disabledInputs = []; // List of time picker inputs that have been disabled
this._timepickerShowing = false; // True if the popup picker is showing , false if not
this._inDialog = false; // True if showing within a "dialog", false if not
this._dialogClass = 'ui-timepicker-dialog'; // The name of the dialog marker class
this._mainDivId = 'ui-timepicker-div'; // The ID of the main timepicker division
this._inlineClass = 'ui-timepicker-inline'; // The name of the inline marker class
this._currentClass = 'ui-timepicker-current'; // The name of the current hour / minutes marker class
this._dayOverClass = 'ui-timepicker-days-cell-over'; // The name of the day hover marker class
this.regional = []; // Available regional settings, indexed by language code
this.regional[''] = { // Default regional settings
hourText: 'Hour', // Display text for hours section
minuteText: 'Minute', // Display text for minutes link
amPmText: ['AM', 'PM'] // Display text for AM PM
};
this._defaults = { // Global defaults for all the time picker instances
showOn: 'focus', // 'focus' for popup on focus,
// 'button' for trigger button, or 'both' for either (not yet implemented)
showAnim: 'fadeIn', // Name of jQuery animation for popup
showOptions: {}, // Options for enhanced animations
appendText: '', // Display text following the input box, e.g. showing the format
onSelect: null, // Define a callback function when a hour / minutes is selected
onClose: null, // Define a callback function when the timepicker is closed
timeSeparator: ':', // The caracter to use to separate hours and minutes.
showPeriod: false, // Define whether or not to show AM/PM with selected time
showLeadingZero: true, // Define whether or not to show a leading zero for hours < 10. [true/false]
showMinutesLeadingZero: true, // Define whether or not to show a leading zero for minutes < 10.
altField: '', // Selector for an alternate field to store selected time into
defaultTime: '', // Used as default time when input field is empty or for inline timePicker
//NEW: 2011-02-03
onHourShow: null, // callback for enabling / disabling on selectable hours ex : function(hour) { return true; }
onMinuteShow: null // callback for enabling / disabling on time selection ex : function(hour,minute) { return true; }
};
$.extend(this._defaults, this.regional['']);
this.tpDiv = $('
');
}
$.extend(Timepicker.prototype, {
/* Class name added to elements to indicate already configured with a time picker. */
markerClassName: 'hasTimepicker',
/* Debug logging (if enabled). */
log: function () {
if (this.debug)
console.log.apply('', arguments);
},
// TODO rename to "widget" when switching to widget factory
_widgetTimepicker: function () {
return this.tpDiv;
},
/* Override the default settings for all instances of the time picker.
@param settings object - the new settings to use as defaults (anonymous object)
@return the manager object */
setDefaults: function (settings) {
extendRemove(this._defaults, settings || {});
return this;
},
/* Attach the time picker to a jQuery selection.
@param target element - the target input field or division or span
@param settings object - the new settings to use for this time picker instance (anonymous) */
_attachTimepicker: function (target, settings) {
// check for settings on the control itself - in namespace 'time:'
var inlineSettings = null;
for (var attrName in this._defaults) {
var attrValue = target.getAttribute('time:' + attrName);
if (attrValue) {
inlineSettings = inlineSettings || {};
try {
inlineSettings[attrName] = eval(attrValue);
} catch (err) {
inlineSettings[attrName] = attrValue;
}
}
}
var nodeName = target.nodeName.toLowerCase();
var inline = (nodeName == 'div' || nodeName == 'span');
if (!target.id) {
this.uuid += 1;
target.id = 'tp' + this.uuid;
}
var inst = this._newInst($(target), inline);
inst.settings = $.extend({}, settings || {}, inlineSettings || {});
if (nodeName == 'input') {
this._connectTimepicker(target, inst);
} else if (inline) {
this._inlineTimepicker(target, inst);
}
},
/* Create a new instance object. */
_newInst: function (target, inline) {
var id = target[0].id.replace(/([^A-Za-z0-9_-])/g, '\\\\$1'); // escape jQuery meta chars
return { id: id, input: target, // associated target
selectedDay: 0, selectedMonth: 0, selectedYear: 0, // current selection
drawMonth: 0, drawYear: 0, // month being drawn
inline: inline, // is timepicker inline or not :
tpDiv: (!inline ? this.tpDiv : // presentation div
$(''))
};
},
/* Attach the time picker to an input field. */
_connectTimepicker: function (target, inst) {
var input = $(target);
inst.append = $([]);
inst.trigger = $([]);
if (input.hasClass(this.markerClassName)) { return; }
this._attachments(input, inst);
input.addClass(this.markerClassName).
keydown(this._doKeyDown).
keyup(this._doKeyUp).
bind("setData.timepicker", function (event, key, value) {
inst.settings[key] = value;
}).
bind("getData.timepicker", function (event, key) {
return this._get(inst, key);
});
//this._autoSize(inst);
$.data(target, PROP_NAME, inst);
},
/* Handle keystrokes. */
_doKeyDown: function (event) {
var inst = $.timepicker._getInst(event.target);
var handled = true;
inst._keyEvent = true;
if ($.timepicker._timepickerShowing) {
switch (event.keyCode) {
case 9: $.timepicker._hideTimepicker();
handled = false;
break; // hide on tab out
case 27: $.timepicker._hideTimepicker();
break; // hide on escape
default: handled = false;
}
}
else if (event.keyCode == 36 && event.ctrlKey) { // display the time picker on ctrl+home
$.timepicker._showTimepicker(this);
}
else {
handled = false;
}
if (handled) {
event.preventDefault();
event.stopPropagation();
}
},
/* Update selected time on keyUp */
/* Added verion 0.0.5 */
_doKeyUp: function (event) {
var inst = $.timepicker._getInst(event.target);
$.timepicker._setTimeFromField(inst);
$.timepicker._updateTimepicker(inst);
},
/* Make attachments based on settings. */
_attachments: function (input, inst) {
var appendText = this._get(inst, 'appendText');
var isRTL = this._get(inst, 'isRTL');
if (inst.append) { inst.append.remove(); }
if (appendText) {
inst.append = $('' + appendText + '');
input[isRTL ? 'before' : 'after'](inst.append);
}
input.unbind('focus', this._showTimepicker);
if (inst.trigger) { inst.trigger.remove(); }
var showOn = this._get(inst, 'showOn');
if (showOn == 'focus' || showOn == 'both') { // pop-up time picker when in the marked field
input.focus(this._showTimepicker);
}
if (showOn == 'button' || showOn == 'both') { // pop-up time picker when button clicked
var buttonText = this._get(inst, 'buttonText');
var buttonImage = this._get(inst, 'buttonImage');
inst.trigger = $(this._get(inst, 'buttonImageOnly') ?
$('').addClass(this._triggerClass).
attr({ src: buttonImage, alt: buttonText, title: buttonText }) :
$('').addClass(this._triggerClass).
html(buttonImage == '' ? buttonText : $('').attr(
{ src: buttonImage, alt: buttonText, title: buttonText })));
input[isRTL ? 'before' : 'after'](inst.trigger);
inst.trigger.click(function () {
if ($.timepicker._timepickerShowing && $.timepicker._lastInput == input[0]) { $.timepicker._hideTimepicker(); }
else { $.timepicker._showTimepicker(input[0]); }
return false;
});
}
},
/* Attach an inline time picker to a div. */
_inlineTimepicker: function(target, inst) {
var divSpan = $(target);
if (divSpan.hasClass(this.markerClassName))
return;
divSpan.addClass(this.markerClassName).append(inst.tpDiv).
bind("setData.timepicker", function(event, key, value){
inst.settings[key] = value;
}).bind("getData.timepicker", function(event, key){
return this._get(inst, key);
});
$.data(target, PROP_NAME, inst);
this._setTimeFromField(inst);
this._updateTimepicker(inst);
inst.tpDiv.show();
},
/* Pop-up the time picker for a given input field.
@param input element - the input field attached to the time picker or
event - if triggered by focus */
_showTimepicker: function (input) {
input = input.target || input;
if (input.nodeName.toLowerCase() != 'input') { input = $('input', input.parentNode)[0]; } // find from button/image trigger
if ($.timepicker._isDisabledTimepicker(input) || $.timepicker._lastInput == input) { return; } // already here
// fix v 0.0.8 - close current timepicker before showing another one
$.timepicker._hideTimepicker();
var inst = $.timepicker._getInst(input);
if ($.timepicker._curInst && $.timepicker._curInst != inst) {
$.timepicker._curInst.tpDiv.stop(true, true);
}
var beforeShow = $.timepicker._get(inst, 'beforeShow');
extendRemove(inst.settings, (beforeShow ? beforeShow.apply(input, [input, inst]) : {}));
inst.lastVal = null;
$.timepicker._lastInput = input;
$.timepicker._setTimeFromField(inst);
if ($.timepicker._inDialog) { input.value = ''; } // hide cursor
if (!$.timepicker._pos) { // position below input
$.timepicker._pos = $.timepicker._findPos(input);
$.timepicker._pos[1] += input.offsetHeight; // add the height
}
var isFixed = false;
$(input).parents().each(function () {
isFixed |= $(this).css('position') == 'fixed';
return !isFixed;
});
if (isFixed && $.browser.opera) { // correction for Opera when fixed and scrolled
$.timepicker._pos[0] -= document.documentElement.scrollLeft;
$.timepicker._pos[1] -= document.documentElement.scrollTop;
}
var offset = { left: $.timepicker._pos[0], top: $.timepicker._pos[1] };
$.timepicker._pos = null;
// determine sizing offscreen
inst.tpDiv.css({ position: 'absolute', display: 'block', top: '-1000px' });
$.timepicker._updateTimepicker(inst);
// reset clicked state
inst._hoursClicked = false;
inst._minutesClicked = false;
// fix width for dynamic number of time pickers
// and adjust position before showing
offset = $.timepicker._checkOffset(inst, offset, isFixed);
inst.tpDiv.css({ position: ($.timepicker._inDialog && $.blockUI ?
'static' : (isFixed ? 'fixed' : 'absolute')), display: 'none',
left: offset.left + 'px', top: offset.top + 'px'
});
if (!inst.inline) {
var showAnim = $.timepicker._get(inst, 'showAnim');
var duration = $.timepicker._get(inst, 'duration');
var postProcess = function () {
$.timepicker._timepickerShowing = true;
var borders = $.timepicker._getBorders(inst.tpDiv);
inst.tpDiv.find('iframe.ui-timepicker-cover'). // IE6- only
css({ left: -borders[0], top: -borders[1],
width: inst.tpDiv.outerWidth(), height: inst.tpDiv.outerHeight()
});
};
inst.tpDiv.zIndex($(input).zIndex() + 1);
if ($.effects && $.effects[showAnim]) {
inst.tpDiv.show(showAnim, $.timepicker._get(inst, 'showOptions'), duration, postProcess);
}
else {
inst.tpDiv[showAnim || 'show']((showAnim ? duration : null), postProcess);
}
if (!showAnim || !duration) { postProcess(); }
if (inst.input.is(':visible') && !inst.input.is(':disabled')) { inst.input.focus(); }
$.timepicker._curInst = inst;
}
},
/* Generate the time picker content. */
_updateTimepicker: function (inst) {
var self = this;
var borders = $.timepicker._getBorders(inst.tpDiv);
inst.tpDiv.empty().append(this._generateHTML(inst))
.find('iframe.ui-timepicker-cover') // IE6- only
.css({ left: -borders[0], top: -borders[1],
width: inst.tpDiv.outerWidth(), height: inst.tpDiv.outerHeight()
})
.end()
.find('.ui-timepicker td a')
.bind('mouseout', function () {
$(this).removeClass('ui-state-hover');
if (this.className.indexOf('ui-timepicker-prev') != -1) $(this).removeClass('ui-timepicker-prev-hover');
if (this.className.indexOf('ui-timepicker-next') != -1) $(this).removeClass('ui-timepicker-next-hover');
})
.bind('mouseover', function () {
if (!self._isDisabledTimepicker(inst.inline ? inst.tpDiv.parent()[0] : inst.input[0])) {
$(this).parents('.ui-timepicker-calendar').find('a').removeClass('ui-state-hover');
$(this).addClass('ui-state-hover');
if (this.className.indexOf('ui-timepicker-prev') != -1) $(this).addClass('ui-timepicker-prev-hover');
if (this.className.indexOf('ui-timepicker-next') != -1) $(this).addClass('ui-timepicker-next-hover');
}
})
.end()
.find('.' + this._dayOverClass + ' a')
.trigger('mouseover')
.end();
},
/* Generate the HTML for the current state of the date picker. */
_generateHTML: function (inst) {
var h, m, html = '';
var showPeriod = (this._get(inst, 'showPeriod') == true);
var showLeadingZero = (this._get(inst, 'showLeadingZero') == true);
var amPmText = this._get(inst, 'amPmText');
html = '
' +
'
' +
'
' +
this._get(inst, 'hourText') +
'
' +
'
';
// AM
html += '
' + amPmText[0] + '
';
for (h = 0; h <= 5; h++) {
html += this._generateHTMLHourCell(inst, h, showPeriod, showLeadingZero);
}
html += '
';
for (h = 6; h <= 11; h++) {
html += this._generateHTMLHourCell(inst, h, showPeriod, showLeadingZero);
}
// PM
html += '
' + amPmText[1] + '
';
for (h = 12; h <= 17; h++) {
html += this._generateHTMLHourCell(inst, h, showPeriod, showLeadingZero);
}
html += '
';
for (h = 18; h <= 23; h++) {
html += this._generateHTMLHourCell(inst, h, showPeriod, showLeadingZero);
}
html += '
' + // Close the hours cells table
'
' + // Close the Hour td
'
';
html += this._generateHTMLMinutes(inst);
html += '
';
return html;
},
/* Special function that update the minutes selection in currently visible timepicker
* called on hour selection when onMinuteShow is defined */
_updateMinuteDisplay: function (inst) {
var newHtml = this._generateHTMLMinutes(inst);
inst.tpDiv.find('td.ui-timepicker-minutes').html(newHtml);
},
/* Generate the minutes table */
_generateHTMLMinutes: function (inst) {
var m;
var showMinutesLeadingZero = (this._get(inst, 'showMinutesLeadingZero') == true);
var onMinuteShow = this._get(inst, 'onMinuteShow');
// if currently selected minute is not enabled, we have a problem and need to select a new minute.
if ( (onMinuteShow) ) {
if (onMinuteShow.apply((inst.input ? inst.input[0] : null), [inst.hours , inst.minutes]) == false) {
// loop minutes and select first available
for (m = 0; m < 60; m += 5) {
if (onMinuteShow.apply((inst.input ? inst.input[0] : null), [inst.hours, m])) {
inst.minutes = m;
break;
}
}
}
}
var html = '' + // open minutes td
/* Add the minutes */
'
' +
this._get(inst, 'minuteText') +
'
' +
'
' +
'
';
;
for (m = 0; m < 15; m += 5) {
html += this._generateHTMLMinuteCell(inst, m, (m < 10) && showMinutesLeadingZero ? "0" + m.toString() : m.toString());
}
html += '
';
for (m = 15; m < 30; m += 5) {
html += this._generateHTMLMinuteCell(inst, m, m.toString());
}
html += '
';
for (m = 30; m < 45; m += 5) {
html += this._generateHTMLMinuteCell(inst, m, m.toString());
}
html += '
';
for (m = 45; m < 60; m += 5) {
html += this._generateHTMLMinuteCell(inst, m, m.toString());
}
html += '
';
return html;
},
/* Generate the content of a "Hour" cell */
_generateHTMLHourCell: function (inst, hour, showPeriod, showLeadingZero) {
var displayHour = hour;
if ((hour > 12) && showPeriod) {
displayHour = hour - 12;
}
if ((displayHour == 0) && showPeriod) {
displayHour = 12;
}
if ((displayHour < 10) && showLeadingZero) {
displayHour = '0' + displayHour;
}
var html = "";
var enabled = true;
var onHourShow = this._get(inst, 'onHourShow'); //custom callback
if (onHourShow) {
enabled = onHourShow.apply((inst.input ? inst.input[0] : null), [hour]);
}
if (enabled) {
html = '
';
}
return html;
},
/* Generate the content of a "Hour" cell */
_generateHTMLMinuteCell: function (inst, minute, displayText) {
var html = "";
var enabled = true;
var onMinuteShow = this._get(inst, 'onMinuteShow'); //custom callback
if (onMinuteShow) {
//NEW: 2011-02-03 we should give the hour as a parameter as well!
enabled = onMinuteShow.apply((inst.input ? inst.input[0] : null), [inst.hours,minute]); //trigger callback
}
if (enabled) {
html = '