Skip to content

Commit 984387b

Browse files
Changed timepicker Today localization to Now, now updates date and time on click
1 parent db4a4a8 commit 984387b

File tree

1 file changed

+79
-42
lines changed

1 file changed

+79
-42
lines changed

jquery-ui-timepicker-addon.js

Lines changed: 79 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
if(typeof(singleton) === 'boolean' && singleton == true) {
2121
this.regional = []; // Available regional settings, indexed by language code
2222
this.regional[''] = { // Default regional settings
23+
currentText: 'Now',
2324
ampm: false,
2425
timeFormat: 'hh:mm tt',
2526
timeOnlyTitle: 'Choose Time',
@@ -247,7 +248,7 @@
247248
var hour = tp_inst.hour_slider.slider('value');
248249
var minute = tp_inst.minute_slider.slider('value');
249250
var second = tp_inst.second_slider.slider('value');
250-
var ampm = (tp_inst.hour < 12) ? 'AM' : 'PM';
251+
var ampm = (hour < 12) ? 'AM' : 'PM';
251252
var hasChanged = false;
252253

253254
// If the update was done in the input field, this field should not be updated.
@@ -273,37 +274,37 @@
273274
//########################################################################
274275
// format the time all pretty...
275276
//########################################################################
276-
formatTime: function(inst) {
277-
var tmptime = inst.defaults.timeFormat.toString();
278-
var hour12 = ((inst.ampm == 'AM') ? (inst.hour) : (inst.hour % 12));
277+
formatTime: function(tp_inst) {
278+
var tmptime = tp_inst.defaults.timeFormat.toString();
279+
var hour12 = ((tp_inst.ampm == 'AM') ? (tp_inst.hour) : (tp_inst.hour % 12));
279280
hour12 = (hour12 === 0) ? 12 : hour12;
280281

281-
if (inst.defaults.ampm === true) {
282+
if (tp_inst.defaults.ampm === true) {
282283
tmptime = tmptime.toString()
283284
.replace(/hh/g, ((hour12 < 10) ? '0' : '') + hour12)
284285
.replace(/h/g, hour12)
285-
.replace(/mm/g, ((inst.minute < 10) ? '0' : '') + inst.minute)
286-
.replace(/m/g, inst.minute)
287-
.replace(/ss/g, ((inst.second < 10) ? '0' : '') + inst.second)
288-
.replace(/s/g, inst.second)
289-
.replace(/TT/g, inst.ampm.toUpperCase())
290-
.replace(/tt/g, inst.ampm.toLowerCase())
291-
.replace(/T/g, inst.ampm.charAt(0).toUpperCase())
292-
.replace(/t/g, inst.ampm.charAt(0).toLowerCase());
286+
.replace(/mm/g, ((tp_inst.minute < 10) ? '0' : '') + tp_inst.minute)
287+
.replace(/m/g, tp_inst.minute)
288+
.replace(/ss/g, ((tp_inst.second < 10) ? '0' : '') + tp_inst.second)
289+
.replace(/s/g, tp_inst.second)
290+
.replace(/TT/g, tp_inst.ampm.toUpperCase())
291+
.replace(/tt/g, tp_inst.ampm.toLowerCase())
292+
.replace(/T/g, tp_inst.ampm.charAt(0).toUpperCase())
293+
.replace(/t/g, tp_inst.ampm.charAt(0).toLowerCase());
293294

294295
} else {
295296
tmptime = tmptime.toString()
296-
.replace(/hh/g, ((inst.hour < 10) ? '0' : '') + inst.hour)
297-
.replace(/h/g, inst.hour)
298-
.replace(/mm/g, ((inst.minute < 10) ? '0' : '') + inst.minute)
299-
.replace(/m/g, inst.minute)
300-
.replace(/ss/g, ((inst.second < 10) ? '0' : '') + inst.second)
301-
.replace(/s/g, inst.second);
297+
.replace(/hh/g, ((tp_inst.hour < 10) ? '0' : '') + tp_inst.hour)
298+
.replace(/h/g, tp_inst.hour)
299+
.replace(/mm/g, ((tp_inst.minute < 10) ? '0' : '') + tp_inst.minute)
300+
.replace(/m/g, tp_inst.minute)
301+
.replace(/ss/g, ((tp_inst.second < 10) ? '0' : '') + tp_inst.second)
302+
.replace(/s/g, tp_inst.second);
302303
tmptime = $.trim(tmptime.replace(/t/gi, ''));
303304
}
304305

305-
inst.formattedTime = tmptime;
306-
return inst.formattedTime;
306+
tp_inst.formattedTime = tmptime;
307+
return tp_inst.formattedTime;
307308
},
308309

309310
//########################################################################
@@ -373,10 +374,12 @@
373374
tp.defaults = $.extend({}, tp.defaults, opts, {
374375
beforeShow: beforeShowFunc,
375376
onChangeMonthYear: onChangeMonthYearFunc,
376-
onClose: onCloseFunc
377+
onClose: onCloseFunc,
378+
timepicker: tp // add timepicker as a property of datepicker: $.datepicker._get(dp_inst, 'timepicker');
377379
});
378380

379381
$(this).datepicker(tp.defaults);
382+
380383
};
381384

382385
//########################################################################
@@ -391,31 +394,30 @@
391394
// the bad hack :/ override datepicker so it doesnt close on select
392395
// inspired: http://stackoverflow.com/questions/1252512/jquery-datepicker-prevent-closing-picker-when-clicking-a-date/1762378#1762378
393396
//########################################################################
394-
$.datepicker._selectDateOverload = $.datepicker._selectDate;
397+
$.datepicker._base_selectDate = $.datepicker._selectDate;
395398
$.datepicker._selectDate = function (id, dateStr) {
396399
var target = $(id);
397400
var inst = this._getInst(target[0]);
398401
inst.inline = true;
399402
inst.stay_open = true;
400-
$.datepicker._selectDateOverload(id, dateStr);
403+
$.datepicker._base_selectDate(id, dateStr);
401404
inst.stay_open = false;
402405
inst.inline = false;
403406
this._notifyChange(inst);
404407
this._updateDatepicker(inst);
405408
};
406409

407-
$.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
408410
//#############################################################################################
409411
// second bad hack :/ override datepicker so it triggers an event when changing the input field
410412
// and does not redraw the datepicker on every selectDate event
411413
//#############################################################################################
412-
// Generate the date picker content.
414+
$.datepicker._base_updateDatepicker = $.datepicker._updateDatepicker;
413415
$.datepicker._updateDatepicker = function(inst) {
414-
if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
415-
this._base_updateDatepicker(inst);
416-
// Reload the time control when changing something in the input text field.
417-
this._beforeShow(inst.input, inst);
418-
}
416+
if (typeof(inst.stay_open) !== 'boolean' || inst.stay_open === false) {
417+
this._base_updateDatepicker(inst);
418+
// Reload the time control when changing something in the input text field.
419+
this._beforeShow(inst.input, inst);
420+
}
419421
};
420422

421423
$.datepicker._beforeShow = function(input, inst) {
@@ -430,6 +432,7 @@
430432
//#######################################################################################
431433
// third bad hack :/ override datepicker so it allows spaces and colan in the input field
432434
//#######################################################################################
435+
$.datepicker._base_doKeyPress = $.datepicker._doKeyPress;
433436
$.datepicker._doKeyPress = function(event) {
434437
var inst = $.datepicker._getInst(event.target);
435438
if ($.datepicker._get(inst, 'constrainInput')) {
@@ -440,15 +443,49 @@
440443
return event.ctrlKey || (chr < ' ' || !dateChars || dateChars.indexOf(chr) > -1 || event.keyCode == 58 || event.keyCode == 32);
441444
}
442445
};
443-
444-
/* jQuery extend now ignores nulls! */
445-
function extendRemove(target, props) {
446-
$.extend(target, props);
447-
for (var name in props)
448-
if (props[name] == null || props[name] == undefined)
449-
target[name] = props[name];
450-
return target;
451-
};
452-
453-
$.timepicker = new Timepicker(true); // singleton instance
446+
447+
//#######################################################################################
448+
// override "Today" button to also grab the time.
449+
//#######################################################################################
450+
$.datepicker._base_gotoToday = $.datepicker._gotoToday;
451+
$.datepicker._gotoToday = function(id) {
452+
$.datepicker._base_gotoToday(id);
453+
454+
var target = $(id);
455+
var dp_inst = this._getInst(target[0]);
456+
var tp_inst = $.datepicker._get(dp_inst, 'timepicker');
457+
458+
if(tp_inst){
459+
var date = new Date();
460+
var hour = date.getHours();
461+
var minute = date.getMinutes();
462+
var second = date.getSeconds();
463+
464+
//check if within min/max times..
465+
if( (hour < tp_inst.defaults.hourMin || hour > tp_inst.defaults.hourMax) || (minute < tp_inst.defaults.minuteMin || minute > tp_inst.defaults.minuteMax) || (second < tp_inst.defaults.secondMin || second > tp_inst.defaults.secondMax) ){
466+
hour = tp_inst.defaults.hourMin;
467+
minute = tp_inst.defaults.minuteMin;
468+
second = tp_inst.defaults.secondMin;
469+
}
470+
471+
tp_inst.hour_slider.slider('value', hour );
472+
tp_inst.minute_slider.slider('value', minute );
473+
tp_inst.second_slider.slider('value', second );
474+
475+
tp_inst.onTimeChange(dp_inst, tp_inst);
476+
}
477+
};
478+
479+
//#######################################################################################
480+
// jQuery extend now ignores nulls!
481+
//#######################################################################################
482+
function extendRemove(target, props) {
483+
$.extend(target, props);
484+
for (var name in props)
485+
if (props[name] == null || props[name] == undefined)
486+
target[name] = props[name];
487+
return target;
488+
};
489+
490+
$.timepicker = new Timepicker(true); // singleton instance
454491
})(jQuery);

0 commit comments

Comments
 (0)