Skip to content

Commit c176fc4

Browse files
Nico RitscheNico Ritsche
authored andcommitted
added timepicker_with_days
1 parent ec1a143 commit c176fc4

File tree

1 file changed

+114
-16
lines changed

1 file changed

+114
-16
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 114 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@
1515
* .ui-timepicker-div dl dt{ height: 25px; }
1616
* .ui-timepicker-div dl dd{ margin: -25px 0 10px 65px; }
1717
* .ui-timepicker-div td { font-size: 90%; }
18+
*
19+
*
20+
* ADD THIS FOR THE timepicker_with_days:
21+
*
22+
* .ui-datepicker-buttonpane{ display: none; }
23+
*
24+
* .ui-datepicker {
25+
* width: 300px;
26+
* padding: 8px 7px 8px;
27+
* height: 55px;
28+
* margin-top: 5px;
29+
* }
30+
*
1831
*/
1932

2033
(function($) {
@@ -32,34 +45,42 @@ function Timepicker() {
3245
currentText: 'Now',
3346
closeText: 'Done',
3447
ampm: false,
35-
timeFormat: 'hh:mm tt',
48+
timeFormat: 'hh:mm tt',
3649
timeOnlyTitle: 'Choose Time',
3750
timeText: 'Time',
51+
dayText: 'Day',
3852
hourText: 'Hour',
3953
minuteText: 'Minute',
4054
secondText: 'Second'
4155
};
4256
this._defaults = { // Global defaults for all the datetime picker instances
4357
showButtonPanel: true,
4458
timeOnly: false,
59+
showTitle: true,
60+
showDay: false,
4561
showHour: true,
4662
showMinute: true,
4763
showSecond: false,
4864
showTime: true,
65+
stepDay: 0.05,
4966
stepHour: 0.05,
5067
stepMinute: 0.05,
5168
stepSecond: 0.05,
69+
day: 0,
5270
hour: 0,
5371
minute: 0,
5472
second: 0,
73+
dayMin: 0,
5574
hourMin: 0,
5675
minuteMin: 0,
5776
secondMin: 0,
77+
dayMax: 20,
5878
hourMax: 23,
5979
minuteMax: 59,
6080
secondMax: 59,
6181
minDateTime: null,
62-
maxDateTime: null,
82+
maxDateTime: null,
83+
dayGrid: 0,
6384
hourGrid: 0,
6485
minuteGrid: 0,
6586
secondGrid: 0,
@@ -76,15 +97,19 @@ $.extend(Timepicker.prototype, {
7697
$altInput: null,
7798
$timeObj: null,
7899
inst: null,
100+
day_slider: null,
79101
hour_slider: null,
80102
minute_slider: null,
81103
second_slider: null,
104+
day: 0,
82105
hour: 0,
83106
minute: 0,
84107
second: 0,
108+
dayMinOriginal: null,
85109
hourMinOriginal: null,
86110
minuteMinOriginal: null,
87111
secondMinOriginal: null,
112+
dayMaxOriginal: null,
88113
hourMaxOriginal: null,
89114
minuteMaxOriginal: null,
90115
secondMaxOriginal: null,
@@ -108,6 +133,7 @@ $.extend(Timepicker.prototype, {
108133
var tp_inst = new Timepicker(),
109134
inlineSettings = {};
110135

136+
tp_inst.day = tp_inst._defaults.day;
111137
tp_inst.hour = tp_inst._defaults.hour;
112138
tp_inst.minute = tp_inst._defaults.minute;
113139
tp_inst.second = tp_inst._defaults.second;
@@ -248,6 +274,7 @@ $.extend(Timepicker.prototype, {
248274
// Added by Peter Medeiros:
249275
// - Figure out what the hour/minute/second max should be based on the step values.
250276
// - Example: if stepMinute is 15, then minMax is 45.
277+
dayMax = (o.dayMax - (o.dayMax % o.stepDay)).toFixed(0),
251278
hourMax = (o.hourMax - (o.hourMax % o.stepHour)).toFixed(0),
252279
minMax = (o.minuteMax - (o.minuteMax % o.stepMinute)).toFixed(0),
253280
secMax = (o.secondMax - (o.secondMax % o.stepSecond)).toFixed(0),
@@ -262,13 +289,33 @@ $.extend(Timepicker.prototype, {
262289
((o.showTime) ? '' : noDisplay) + '>' + o.timeText + '</dt>' +
263290
'<dd class="ui_tpicker_time" id="ui_tpicker_time_' + dp_id + '"' +
264291
((o.showTime) ? '' : noDisplay) + '></dd>' +
265-
'<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' +
266-
((o.showHour) ? '' : noDisplay) + '>' + o.hourText + '</dt>',
292+
'<dt class="ui_tpicker_day_label" id="ui_tpicker_day_label_' + dp_id + '"' +
293+
((o.showDay) ? '' : noDisplay) + '>' + o.dayText + '</dt>',
294+
dayGridSize = 0,
267295
hourGridSize = 0,
268296
minuteGridSize = 0,
269297
secondGridSize = 0,
270298
size;
271-
299+
300+
if (o.showDay && o.dayGrid > 0) {
301+
html += '<dd class="ui_tpicker_day">' +
302+
'<div id="ui_tpicker_day_' + dp_id + '"' + ((o.showDay) ? '' : noDisplay) + '></div>' +
303+
'<div style="padding-left: 1px"><table><tr>';
304+
305+
for (var h = o.dayMin; h < hourMax; h += o.dayGrid) {
306+
dayGridSize++;
307+
html += '<td>' + h + '</td>';
308+
}
309+
310+
html += '</tr></table></div>' +
311+
'</dd>';
312+
} else html += '<dd class="ui_tpicker_day" id="ui_tpicker_day_' + dp_id + '"' +
313+
((o.showDay) ? '' : noDisplay) + '></dd>';
314+
315+
316+
html += '<dt class="ui_tpicker_hour_label" id="ui_tpicker_hour_label_' + dp_id + '"' +
317+
((o.showHour) ? '' : noDisplay) + '>' + o.hourText + '</dt>';
318+
272319
if (o.showHour && o.hourGrid > 0) {
273320
html += '<dd class="ui_tpicker_hour">' +
274321
'<div id="ui_tpicker_hour_' + dp_id + '"' + ((o.showHour) ? '' : noDisplay) + '></div>' +
@@ -334,16 +381,29 @@ $.extend(Timepicker.prototype, {
334381

335382
// if we only want time picker...
336383
if (o.timeOnly === true) {
337-
$tp.prepend(
338-
'<div class="ui-widget-header ui-helper-clearfix ui-corner-all">' +
339-
'<div class="ui-datepicker-title">' + o.timeOnlyTitle + '</div>' +
340-
'</div>');
384+
if( o.showTitle )
385+
$tp.prepend(
386+
'<div class="ui-widget-header ui-helper-clearfix ui-corner-all">' +
387+
'<div class="ui-datepicker-title">' + o.timeOnlyTitle + '</div>' +
388+
'</div>');
341389
$dp.find('.ui-datepicker-header, .ui-datepicker-calendar').hide();
342390
}
343391

392+
this.day_slider = $tp.find('#ui_tpicker_day_'+ dp_id).slider({
393+
orientation: "horizontal",
394+
value: o.day,
395+
min: o.dayMin,
396+
max: dayMax,
397+
step: o.stepDay,
398+
slide: function(event, ui) {
399+
tp_inst.day_slider.slider( "option", "value", ui.value);
400+
tp_inst._onTimeChange();
401+
}
402+
});
403+
344404
this.hour_slider = $tp.find('#ui_tpicker_hour_'+ dp_id).slider({
345405
orientation: "horizontal",
346-
value: this.hour,
406+
value: o.hour,
347407
min: o.hourMin,
348408
max: hourMax,
349409
step: o.stepHour,
@@ -357,7 +417,7 @@ $.extend(Timepicker.prototype, {
357417
// - Pass in Event and UI instance into slide function
358418
this.minute_slider = $tp.find('#ui_tpicker_minute_'+ dp_id).slider({
359419
orientation: "horizontal",
360-
value: this.minute,
420+
value: o.minute,
361421
min: o.minuteMin,
362422
max: minMax,
363423
step: o.stepMinute,
@@ -380,6 +440,25 @@ $.extend(Timepicker.prototype, {
380440
}
381441
});
382442

443+
if (o.showDay && o.dayGrid > 0) {
444+
size = 100 * dayGridSize * o.dayGrid / (minMax - o.dayMin);
445+
$tp.find(".ui_tpicker_day table").css({
446+
width: size + "%",
447+
marginLeft: (size / (-2 * dayGridSize)) + "%",
448+
borderCollapse: 'collapse'
449+
}).find("td").each(function(index) {
450+
$(this).click(function() {
451+
tp_inst.day_slider.slider("option", "value", $(this).html());
452+
tp_inst._onTimeChange();
453+
}).css({
454+
cursor: 'pointer',
455+
width: (100 / dayGridSize) + '%',
456+
textAlign: 'center',
457+
overflow: 'hidden'
458+
});
459+
});
460+
}
461+
383462
// Add grid functionality
384463
if (o.showHour && o.hourGrid > 0) {
385464
size = 100 * hourGridSize * o.hourGrid / (hourMax - o.hourMin);
@@ -467,6 +546,7 @@ $.extend(Timepicker.prototype, {
467546
var onSelectHandler = function() {
468547
onSelect.apply(inputEl, [tp_inst.formattedDateTime, tp_inst]); // trigger custom callback*/
469548
}
549+
this.day_slider.bind('slidestop',onSelectHandler);
470550
this.hour_slider.bind('slidestop',onSelectHandler);
471551
this.minute_slider.bind('slidestop',onSelectHandler);
472552
this.second_slider.bind('slidestop',onSelectHandler);
@@ -546,10 +626,12 @@ $.extend(Timepicker.prototype, {
546626
// on time change is also called when the time is updated in the text field
547627
//########################################################################
548628
_onTimeChange: function() {
549-
var hour = (this.hour_slider) ? this.hour_slider.slider('value') : false,
629+
var day = (this.day_slider) ? this.day_slider.slider('value') : false,
630+
hour = (this.hour_slider) ? this.hour_slider.slider('value') : false,
550631
minute = (this.minute_slider) ? this.minute_slider.slider('value') : false,
551632
second = (this.second_slider) ? this.second_slider.slider('value') : false;
552633

634+
if (day !== false) day = parseInt(day,10);
553635
if (hour !== false) hour = parseInt(hour,10);
554636
if (minute !== false) minute = parseInt(minute,10);
555637
if (second !== false) second = parseInt(second,10);
@@ -558,10 +640,10 @@ $.extend(Timepicker.prototype, {
558640

559641
// If the update was done in the input field, the input field should not be updated.
560642
// If the update was done using the sliders, update the input field.
561-
var hasChanged = (hour != this.hour || minute != this.minute || second != this.second || (this.ampm.length > 0 && this.ampm != ampm));
643+
var hasChanged = (day != this.day || hour != this.hour || minute != this.minute || second != this.second || (this.ampm.length > 0 && this.ampm != ampm));
562644

563645
if (hasChanged) {
564-
646+
if (day !== false)this.day = day;
565647
if (hour !== false)this.hour = hour;
566648
if (minute !== false) this.minute = minute;
567649
if (second !== false) this.second = second;
@@ -579,13 +661,14 @@ $.extend(Timepicker.prototype, {
579661
//########################################################################
580662
_formatTime: function(time, format, ampm) {
581663
if (ampm == undefined) ampm = this._defaults.ampm;
582-
time = time || { hour: this.hour, minute: this.minute, second: this.second, ampm: this.ampm };
664+
time = time || { day: this.day, hour: this.hour, minute: this.minute, second: this.second, ampm: this.ampm };
583665
var tmptime = format || this._defaults.timeFormat.toString();
584666

585667
if (ampm) {
586668
var hour12 = ((time.ampm == 'AM') ? (time.hour) : (time.hour % 12));
587669
hour12 = (Number(hour12) === 0) ? 12 : hour12;
588670
tmptime = tmptime.toString()
671+
.replace(/dd/g, time.day)
589672
.replace(/hh/g, ((hour12 < 10) ? '0' : '') + hour12)
590673
.replace(/h/g, hour12)
591674
.replace(/mm/g, ((time.minute < 10) ? '0' : '') + time.minute)
@@ -598,6 +681,7 @@ $.extend(Timepicker.prototype, {
598681
.replace(/t/g, time.ampm.charAt(0).toLowerCase());
599682
} else {
600683
tmptime = tmptime.toString()
684+
.replace(/dd/g, time.day)
601685
.replace(/hh/g, ((time.hour < 10) ? '0' : '') + time.hour)
602686
.replace(/h/g, time.hour)
603687
.replace(/mm/g, ((time.minute < 10) ? '0' : '') + time.minute)
@@ -665,6 +749,20 @@ $.fn.extend({
665749
});
666750
},
667751

752+
//########################################################################
753+
// shorthand just to use timepicker_with_hours..
754+
//########################################################################
755+
timepicker_with_days: function(o) {
756+
o = o || {};
757+
var tmp_args = arguments;
758+
759+
if (typeof o == 'object') tmp_args[0] = $.extend(o, { timeFormat: 'dd:hh:mm', timeOnly: true, showDay: true, showTime: false, showTitle: false });
760+
761+
return $(this).each(function() {
762+
$.fn.datetimepicker.apply($(this), tmp_args);
763+
});
764+
},
765+
668766
//########################################################################
669767
// extend timepicker to datepicker
670768
//########################################################################
@@ -908,4 +1006,4 @@ function extendRemove(target, props) {
9081006
$.timepicker = new Timepicker(); // singleton instance
9091007
$.timepicker.version = "0.9.3";
9101008

911-
})(jQuery);
1009+
})(jQuery);

0 commit comments

Comments
 (0)