diff --git a/tailbone/app.py b/tailbone/app.py
index a0273acd..7ac1520b 100644
--- a/tailbone/app.py
+++ b/tailbone/app.py
@@ -137,6 +137,7 @@ def make_pyramid_config(settings):
formalchemy.FieldSet.default_renderers[sa.Boolean] = renderers.YesNoFieldRenderer
formalchemy.FieldSet.default_renderers[sa.Date] = renderers.DateFieldRenderer
formalchemy.FieldSet.default_renderers[sa.DateTime] = renderers.DateTimeFieldRenderer
+ formalchemy.FieldSet.default_renderers[sa.Time] = renderers.TimeFieldRenderer
formalchemy.FieldSet.default_renderers[GPCType] = renderers.GPCFieldRenderer
return config
diff --git a/tailbone/forms/renderers/__init__.py b/tailbone/forms/renderers/__init__.py
index 34605661..9607e5c4 100644
--- a/tailbone/forms/renderers/__init__.py
+++ b/tailbone/forms/renderers/__init__.py
@@ -30,7 +30,7 @@ from .core import CustomFieldRenderer, DateFieldRenderer
from .common import (AutocompleteFieldRenderer,
DecimalFieldRenderer, CurrencyFieldRenderer,
- DateTimeFieldRenderer, DateTimePrettyFieldRenderer,
+ DateTimeFieldRenderer, DateTimePrettyFieldRenderer, TimeFieldRenderer,
EnumFieldRenderer, YesNoFieldRenderer)
from .people import (PersonFieldRenderer, PersonFieldLinkRenderer,
diff --git a/tailbone/forms/renderers/common.py b/tailbone/forms/renderers/common.py
index c639cf21..43158ce3 100644
--- a/tailbone/forms/renderers/common.py
+++ b/tailbone/forms/renderers/common.py
@@ -26,7 +26,14 @@ Common Field Renderers
from __future__ import unicode_literals, absolute_import
+import datetime
+
+import pytz
+
+from rattail.time import localtime
+
import formalchemy
+from formalchemy import helpers
from formalchemy.fields import FieldRenderer, SelectFieldRenderer, CheckBoxFieldRenderer
from pyramid.renderers import render
@@ -102,6 +109,51 @@ class DateTimePrettyFieldRenderer(formalchemy.DateTimeFieldRenderer):
return pretty_datetime(self.request.rattail_config, value)
+class TimeFieldRenderer(formalchemy.TimeFieldRenderer):
+ """
+ Custom renderer for time fields. In edit mode, renders a simple text
+ input, which is expected to become a 'timepicker' widget in the UI.
+ However the particular magic required for that lives in 'tailbone.js'.
+ """
+ format = '%I:%M %p'
+
+ def render(self, **kwargs):
+ kwargs.setdefault('class_', 'timepicker')
+ return helpers.text_field(self.name, value=self.value, **kwargs)
+
+ def render_readonly(self, **kwargs):
+ return self.render_value(self.raw_value)
+
+ def render_value(self, value):
+ value = self.convert_value(value)
+ if isinstance(value, datetime.time):
+ return value.strftime(self.format)
+ return ''
+
+ def convert_value(self, value):
+ if isinstance(value, datetime.datetime):
+ if not value.tzinfo:
+ value = pytz.utc.localize(value)
+ return localtime(self.request.rattail_config, value).time()
+ return value
+
+ def stringify_value(self, value, as_html=False):
+ if not as_html:
+ return self.render_value(value)
+ return super(TimeFieldRenderer, self).stringify_value(value, as_html=as_html)
+
+ def _serialized_value(self):
+ return self.params.getone(self.name)
+
+ def deserialize(self):
+ value = self._serialized_value()
+ if value:
+ try:
+ return datetime.datetime.strptime(value, self.format).time()
+ except ValueError:
+ pass
+
+
class EnumFieldRenderer(SelectFieldRenderer):
"""
Renderer for simple enumeration fields.
diff --git a/tailbone/static/css/jquery.ui.timepicker.css b/tailbone/static/css/jquery.ui.timepicker.css
new file mode 100644
index 00000000..b5930fb7
--- /dev/null
+++ b/tailbone/static/css/jquery.ui.timepicker.css
@@ -0,0 +1,57 @@
+/*
+ * Timepicker stylesheet
+ * Highly inspired from datepicker
+ * FG - Nov 2010 - Web3R
+ *
+ * version 0.0.3 : Fixed some settings, more dynamic
+ * version 0.0.4 : Removed width:100% on tables
+ * version 0.1.1 : set width 0 on tables to fix an ie6 bug
+ */
+
+.ui-timepicker-inline { display: inline; }
+
+#ui-timepicker-div { padding: 0.2em; }
+.ui-timepicker-table { display: inline-table; width: 0; }
+.ui-timepicker-table table { margin:0.15em 0 0 0; border-collapse: collapse; }
+
+.ui-timepicker-hours, .ui-timepicker-minutes { padding: 0.2em; }
+
+.ui-timepicker-table .ui-timepicker-title { line-height: 1.8em; text-align: center; }
+.ui-timepicker-table td { padding: 0.1em; width: 2.2em; }
+.ui-timepicker-table th.periods { padding: 0.1em; width: 2.2em; }
+
+/* span for disabled cells */
+.ui-timepicker-table td span {
+ display:block;
+ padding:0.2em 0.3em 0.2em 0.5em;
+ width: 1.2em;
+
+ text-align:right;
+ text-decoration:none;
+}
+/* anchors for clickable cells */
+.ui-timepicker-table td a {
+ display:block;
+ padding:0.2em 0.3em 0.2em 0.5em;
+ width: 1.2em;
+ cursor: pointer;
+ text-align:right;
+ text-decoration:none;
+}
+
+
+/* buttons and button pane styling */
+.ui-timepicker .ui-timepicker-buttonpane {
+ background-image: none; margin: .7em 0 0 0; padding:0 .2em; border-left: 0; border-right: 0; border-bottom: 0;
+}
+.ui-timepicker .ui-timepicker-buttonpane button { margin: .5em .2em .4em; cursor: pointer; padding: .2em .6em .3em .6em; width:auto; overflow:visible; }
+/* The close button */
+.ui-timepicker .ui-timepicker-close { float: right }
+
+/* the now button */
+.ui-timepicker .ui-timepicker-now { float: left; }
+
+/* the deselect button */
+.ui-timepicker .ui-timepicker-deselect { float: left; }
+
+
diff --git a/tailbone/static/js/lib/jquery.ui.timepicker.js b/tailbone/static/js/lib/jquery.ui.timepicker.js
new file mode 100644
index 00000000..d8a0cfb7
--- /dev/null
+++ b/tailbone/static/js/lib/jquery.ui.timepicker.js
@@ -0,0 +1,1496 @@
+/*
+ * jQuery UI Timepicker
+ *
+ * Copyright 2010-2013, Francois Gelinas
+ * Dual licensed under the MIT or GPL Version 2 licenses.
+ * http://jquery.org/license
+ *
+ * http://fgelinas.com/code/timepicker
+ *
+ * Depends:
+ * jquery.ui.core.js
+ * jquery.ui.position.js (only if position settings are used)
+ *
+ * Change version 0.1.0 - moved the t-rex up here
+ *
+ ____
+ ___ .-~. /_"-._
+ `-._~-. / /_ "~o\ :Y
+ \ \ / : \~x. ` ')
+ ] Y / | Y< ~-.__j
+ / ! _.--~T : l l< /.-~
+ / / ____.--~ . ` l /~\ \<|Y
+ / / .-~~" /| . ',-~\ \L|
+ / / / .^ \ Y~Y \.^>/l_ "--'
+ / Y .-"( . l__ j_j l_/ /~_.-~ .
+ Y l / \ ) ~~~." / `/"~ / \.__/l_
+ | \ _.-" ~-{__ l : l._Z~-.___.--~
+ | ~---~ / ~~"---\_ ' __[>
+ l . _.^ ___ _>-y~
+ \ \ . .-~ .-~ ~>--" /
+ \ ~---" / ./ _.-'
+ "-.,_____.,_ _.--~\ _.-~
+ ~~ ( _} -Row
+ `. ~(
+ ) \
+ /,`--'~\--'~\
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ->T-Rex<-
+*/
+
+(function ($) {
+
+ $.extend($.ui, { timepicker: { version: "0.3.3"} });
+
+ var PROP_NAME = 'timepicker',
+ 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._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
+ closeButtonText: 'Done', // Text for the confirmation button (ok button)
+ nowButtonText: 'Now', // Text for the now button
+ deselectButtonText: 'Deselect' // Text for the deselect button
+ };
+ 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)
+ button: null, // 'button' element that will trigger the timepicker
+ 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
+
+ beforeShow: null, // Define a callback function executed before the timepicker is shown
+ 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 character to use to separate hours and minutes.
+ periodSeparator: ' ', // The character to use to separate the time from the time period.
+ showPeriod: false, // Define whether or not to show AM/PM with selected time
+ showPeriodLabels: true, // Show the AM/PM labels on the left of the time picker
+ 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: 'now', // Used as default time when input field is empty or for inline timePicker
+ // (set to 'now' for the current time, '' for no highlighted time)
+ myPosition: 'left top', // Position of the dialog relative to the input.
+ // see the position utility for more info : http://jqueryui.com/demos/position/
+ atPosition: 'left bottom', // Position of the input element to match
+ // Note : if the position utility is not loaded, the timepicker will attach left top to left bottom
+ //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; }
+
+ hours: {
+ starts: 0, // first displayed hour
+ ends: 23 // last displayed hour
+ },
+ minutes: {
+ starts: 0, // first displayed minute
+ ends: 55, // last displayed minute
+ interval: 5, // interval of displayed minutes
+ manual: [] // optional extra manual entries for minutes
+ },
+ rows: 4, // number of rows for the input tables, minimum 2, makes more sense if you use multiple of 2
+ // 2011-08-05 0.2.4
+ showHours: true, // display the hours section of the dialog
+ showMinutes: true, // display the minute section of the dialog
+ optionalMinutes: false, // optionally parse inputs of whole hours with minutes omitted
+
+ // buttons
+ showCloseButton: false, // shows an OK button to confirm the edit
+ showNowButton: false, // Shows the 'now' button
+ showDeselectButton: false, // Shows the deselect time button
+
+ maxTime: {
+ hour: null,
+ minute: null
+ },
+ minTime: {
+ hour: null,
+ minute: null
+ }
+
+ };
+ $.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);
+ },
+
+ _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);
+ // init inst.hours and inst.minutes from the input value
+ this._setTimeFromField(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
+ 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);
+ });
+ $.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 13:
+ $.timepicker._updateSelectedValue(inst);
+ $.timepicker._hideTimepicker();
+
+ return false; // don't submit the form
+ break; // select the value on enter
+ 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.timepicker', this._showTimepicker);
+ input.unbind('click.timepicker', this._adjustZIndex);
+
+ 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.bind("focus.timepicker", this._showTimepicker);
+ input.bind("click.timepicker", this._adjustZIndex);
+ }
+ if (showOn == 'button' || showOn == 'both') { // pop-up time picker when 'button' element is clicked
+ var button = this._get(inst, 'button');
+
+ // Add button if button element is not set
+ if(button == null) {
+ button = $('');
+ input.after(button);
+ }
+
+ $(button).bind("click.timepicker", function () {
+ if ($.timepicker._timepickerShowing && $.timepicker._lastInput == input[0]) {
+ $.timepicker._hideTimepicker();
+ } else if (!inst.input.is(':disabled')) {
+ $.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();
+ },
+
+ _adjustZIndex: function(input) {
+ input = input.target || input;
+ var inst = $.timepicker._getInst(input);
+ inst.tpDiv.css('zIndex', $.timepicker._getZIndex(input) +1);
+ },
+
+ /* 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);
+
+ // calculate default position
+ 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;
+ });
+
+ 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);
+
+
+ // position with the ui position utility, if loaded
+ if ( ( ! inst.inline ) && ( typeof $.ui.position == 'object' ) ) {
+ inst.tpDiv.position({
+ of: inst.input,
+ my: $.timepicker._get( inst, 'myPosition' ),
+ at: $.timepicker._get( inst, 'atPosition' ),
+ // offset: $( "#offset" ).val(),
+ // using: using,
+ collision: 'flip'
+ });
+ var offset = inst.tpDiv.offset();
+ $.timepicker._pos = [offset.top, offset.left];
+ }
+
+
+ // 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()
+ });
+ };
+
+ // Fixed the zIndex problem for real (I hope) - FG - v 0.2.9
+ $.timepicker._adjustZIndex(input);
+ //inst.tpDiv.css('zIndex', $.timepicker._getZIndex(input) +1);
+
+ if ($.effects && $.effects[showAnim]) {
+ inst.tpDiv.show(showAnim, $.timepicker._get(inst, 'showOptions'), duration, postProcess);
+ }
+ else {
+ inst.tpDiv.show((showAnim ? duration : null), postProcess);
+ }
+ if (!showAnim || !duration) { postProcess(); }
+ if (inst.input.is(':visible') && !inst.input.is(':disabled')) { inst.input.focus(); }
+ $.timepicker._curInst = inst;
+ }
+ },
+
+ // This is an enhanced copy of the zIndex function of UI core 1.8.?? For backward compatibility.
+ // Enhancement returns maximum zindex value discovered while traversing parent elements,
+ // rather than the first zindex value found. Ensures the timepicker popup will be in front,
+ // even in funky scenarios like non-jq dialog containers with large fixed zindex values and
+ // nested zindex-influenced elements of their own.
+ _getZIndex: function (target) {
+ var elem = $(target);
+ var maxValue = 0;
+ var position, value;
+ while (elem.length && elem[0] !== document) {
+ position = elem.css("position");
+ if (position === "absolute" || position === "relative" || position === "fixed") {
+ value = parseInt(elem.css("zIndex"), 10);
+ if (!isNaN(value) && value !== 0) {
+ if (value > maxValue) { maxValue = value; }
+ }
+ }
+ elem = elem.parent();
+ }
+
+ return maxValue;
+ },
+
+ /* Refresh the time picker
+ @param target element - The target input field or inline container element. */
+ _refreshTimepicker: function(target) {
+ var inst = this._getInst(target);
+ if (inst) {
+ this._updateTimepicker(inst);
+ }
+ },
+
+
+ /* Generate the time picker content. */
+ _updateTimepicker: function (inst) {
+ inst.tpDiv.empty().append(this._generateHTML(inst));
+ this._rebindDialogEvents(inst);
+
+ },
+
+ _rebindDialogEvents: function (inst) {
+ var borders = $.timepicker._getBorders(inst.tpDiv),
+ self = this;
+ inst.tpDiv
+ .find('iframe.ui-timepicker-cover') // IE6- only
+ .css({ left: -borders[0], top: -borders[1],
+ width: inst.tpDiv.outerWidth(), height: inst.tpDiv.outerHeight()
+ })
+ .end()
+ // after the picker html is appended bind the click & double click events (faster in IE this way
+ // then letting the browser interpret the inline events)
+ // the binding for the minute cells also exists in _updateMinuteDisplay
+ .find('.ui-timepicker-minute-cell')
+ .unbind()
+ .bind("click", { fromDoubleClick:false }, $.proxy($.timepicker.selectMinutes, this))
+ .bind("dblclick", { fromDoubleClick:true }, $.proxy($.timepicker.selectMinutes, this))
+ .end()
+ .find('.ui-timepicker-hour-cell')
+ .unbind()
+ .bind("click", { fromDoubleClick:false }, $.proxy($.timepicker.selectHours, this))
+ .bind("dblclick", { fromDoubleClick:true }, $.proxy($.timepicker.selectHours, this))
+ .end()
+ .find('.ui-timepicker td a')
+ .unbind()
+ .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()
+ .find('.ui-timepicker-now').bind("click", function(e) {
+ $.timepicker.selectNow(e);
+ }).end()
+ .find('.ui-timepicker-deselect').bind("click",function(e) {
+ $.timepicker.deselectTime(e);
+ }).end()
+ .find('.ui-timepicker-close').bind("click",function(e) {
+ $.timepicker._hideTimepicker();
+ }).end();
+ },
+
+ /* Generate the HTML for the current state of the time picker. */
+ _generateHTML: function (inst) {
+
+ var h, m, row, col, html, hoursHtml, minutesHtml = '',
+ showPeriod = (this._get(inst, 'showPeriod') == true),
+ showPeriodLabels = (this._get(inst, 'showPeriodLabels') == true),
+ showLeadingZero = (this._get(inst, 'showLeadingZero') == true),
+ showHours = (this._get(inst, 'showHours') == true),
+ showMinutes = (this._get(inst, 'showMinutes') == true),
+ amPmText = this._get(inst, 'amPmText'),
+ rows = this._get(inst, 'rows'),
+ amRows = 0,
+ pmRows = 0,
+ amItems = 0,
+ pmItems = 0,
+ amFirstRow = 0,
+ pmFirstRow = 0,
+ hours = Array(),
+ hours_options = this._get(inst, 'hours'),
+ hoursPerRow = null,
+ hourCounter = 0,
+ hourLabel = this._get(inst, 'hourText'),
+ showCloseButton = this._get(inst, 'showCloseButton'),
+ closeButtonText = this._get(inst, 'closeButtonText'),
+ showNowButton = this._get(inst, 'showNowButton'),
+ nowButtonText = this._get(inst, 'nowButtonText'),
+ showDeselectButton = this._get(inst, 'showDeselectButton'),
+ deselectButtonText = this._get(inst, 'deselectButtonText'),
+ showButtonPanel = showCloseButton || showNowButton || showDeselectButton;
+
+
+
+ // prepare all hours and minutes, makes it easier to distribute by rows
+ for (h = hours_options.starts; h <= hours_options.ends; h++) {
+ hours.push (h);
+ }
+ hoursPerRow = Math.ceil(hours.length / rows); // always round up
+
+ if (showPeriodLabels) {
+ for (hourCounter = 0; hourCounter < hours.length; hourCounter++) {
+ if (hours[hourCounter] < 12) {
+ amItems++;
+ }
+ else {
+ pmItems++;
+ }
+ }
+ hourCounter = 0;
+
+ amRows = Math.floor(amItems / hours.length * rows);
+ pmRows = Math.floor(pmItems / hours.length * rows);
+
+ // assign the extra row to the period that is more densely populated
+ if (rows != amRows + pmRows) {
+ // Make sure: AM Has Items and either PM Does Not, AM has no rows yet, or AM is more dense
+ if (amItems && (!pmItems || !amRows || (pmRows && amItems / amRows >= pmItems / pmRows))) {
+ amRows++;
+ } else {
+ pmRows++;
+ }
+ }
+ amFirstRow = Math.min(amRows, 1);
+ pmFirstRow = amRows + 1;
+
+ if (amRows == 0) {
+ hoursPerRow = Math.ceil(pmItems / pmRows);
+ } else if (pmRows == 0) {
+ hoursPerRow = Math.ceil(amItems / amRows);
+ } else {
+ hoursPerRow = Math.ceil(Math.max(amItems / amRows, pmItems / pmRows));
+ }
+ }
+
+
+ html = '
';
+
+ if (showHours) {
+
+ html += '
' +
+ '
' +
+ hourLabel +
+ '
' +
+ '
';
+
+ for (row = 1; row <= rows; row++) {
+ html += '
';
+ // AM
+ if (row == amFirstRow && showPeriodLabels) {
+ html += '
' + amPmText[0] + '
';
+ }
+ // PM
+ if (row == pmFirstRow && showPeriodLabels) {
+ html += '
' + amPmText[1] + '
';
+ }
+ for (col = 1; col <= hoursPerRow; col++) {
+ if (showPeriodLabels && row < pmFirstRow && hours[hourCounter] >= 12) {
+ html += this._generateHTMLHourCell(inst, undefined, showPeriod, showLeadingZero);
+ } else {
+ html += this._generateHTMLHourCell(inst, hours[hourCounter], showPeriod, showLeadingZero);
+ hourCounter++;
+ }
+ }
+ html += '
';
+ }
+ html += '
' + // Close the hours cells table
+ '
'; // Close the Hour td
+ }
+
+ if (showMinutes) {
+ html += '
';
+ html += this._generateHTMLMinutes(inst);
+ html += '
';
+ }
+
+ html += '
';
+
+
+ if (showButtonPanel) {
+ var buttonPanel = '
';
+ if (showNowButton) {
+ buttonPanel += '';
+ }
+ if (showDeselectButton) {
+ buttonPanel += '';
+ }
+ if (showCloseButton) {
+ buttonPanel += '';
+ }
+
+ html += buttonPanel + '
';
+ }
+ 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);
+ this._rebindDialogEvents(inst);
+ // after the picker html is appended bind the click & double click events (faster in IE this way
+ // then letting the browser interpret the inline events)
+ // yes I know, duplicate code, sorry
+/* .find('.ui-timepicker-minute-cell')
+ .bind("click", { fromDoubleClick:false }, $.proxy($.timepicker.selectMinutes, this))
+ .bind("dblclick", { fromDoubleClick:true }, $.proxy($.timepicker.selectMinutes, this));
+*/
+
+ },
+
+ /*
+ * Generate the minutes table
+ * This is separated from the _generateHTML function because is can be called separately (when hours changes)
+ */
+ _generateHTMLMinutes: function (inst) {
+
+ var m, row, html = '',
+ rows = this._get(inst, 'rows'),
+ minutes = Array(),
+ minutes_options = this._get(inst, 'minutes'),
+ minutesPerRow = null,
+ minuteCounter = 0,
+ showMinutesLeadingZero = (this._get(inst, 'showMinutesLeadingZero') == true),
+ onMinuteShow = this._get(inst, 'onMinuteShow'),
+ minuteLabel = this._get(inst, 'minuteText');
+
+ if ( ! minutes_options.starts) {
+ minutes_options.starts = 0;
+ }
+ if ( ! minutes_options.ends) {
+ minutes_options.ends = 59;
+ }
+ if ( ! minutes_options.manual) {
+ minutes_options.manual = [];
+ }
+ for (m = minutes_options.starts; m <= minutes_options.ends; m += minutes_options.interval) {
+ minutes.push(m);
+ }
+ for (i = 0; i < minutes_options.manual.length;i++) {
+ var currMin = minutes_options.manual[i];
+
+ // Validate & filter duplicates of manual minute input
+ if (typeof currMin != 'number' || currMin < 0 || currMin > 59 || $.inArray(currMin, minutes) >= 0) {
+ continue;
+ }
+ minutes.push(currMin);
+ }
+
+ // Sort to get correct order after adding manual minutes
+ // Use compare function to sort by number, instead of string (default)
+ minutes.sort(function(a, b) {
+ return a-b;
+ });
+
+ minutesPerRow = Math.round(minutes.length / rows + 0.49); // always round up
+
+ /*
+ * The minutes table
+ */
+ // if currently selected minute is not enabled, we have a problem and need to select a new minute.
+ if (onMinuteShow &&
+ (onMinuteShow.apply((inst.input ? inst.input[0] : null), [inst.hours , inst.minutes]) == false) ) {
+ // loop minutes and select first available
+ for (minuteCounter = 0; minuteCounter < minutes.length; minuteCounter += 1) {
+ m = minutes[minuteCounter];
+ if (onMinuteShow.apply((inst.input ? inst.input[0] : null), [inst.hours, m])) {
+ inst.minutes = m;
+ break;
+ }
+ }
+ }
+
+
+
+ html += '
' +
+ minuteLabel +
+ '
' +
+ '
';
+
+ minuteCounter = 0;
+ for (row = 1; row <= rows; row++) {
+ html += '
';
+ while (minuteCounter < row * minutesPerRow) {
+ var m = minutes[minuteCounter];
+ var displayText = '';
+ if (m !== undefined ) {
+ displayText = (m < 10) && showMinutesLeadingZero ? "0" + m.toString() : m.toString();
+ }
+ html += this._generateHTMLMinuteCell(inst, m, displayText);
+ minuteCounter++;
+ }
+ html += '
';
+ }
+
+ 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
+ var maxTime = this._get(inst, 'maxTime');
+ var minTime = this._get(inst, 'minTime');
+
+ if (hour == undefined) {
+ html = '
';
+ }
+ return html;
+ },
+
+ /* Generate the content of a "Hour" cell */
+ _generateHTMLMinuteCell: function (inst, minute, displayText) {
+ var html = "";
+ var enabled = true;
+ var hour = inst.hours;
+ var onMinuteShow = this._get(inst, 'onMinuteShow'); //custom callback
+ var maxTime = this._get(inst, 'maxTime');
+ var minTime = this._get(inst, 'minTime');
+
+ 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 (minute == undefined) {
+ html = '