9 * @class Roo.bootstrap.form.TimeField
10 * @extends Roo.bootstrap.form.Input
11 * Bootstrap DateField class
12 * @cfg {Number} minuteStep the minutes is always the multiple of a fixed number, default 1
16 * Create a new TimeField
17 * @param {Object} config The config object
20 Roo.bootstrap.form.TimeField = function(config){
21 Roo.bootstrap.form.TimeField.superclass.constructor.call(this, config);
25 * Fires when this field show.
26 * @param {Roo.bootstrap.form.DateField} thisthis
27 * @param {Mixed} date The date value
32 * Fires when this field hide.
33 * @param {Roo.bootstrap.form.DateField} this
34 * @param {Mixed} date The date value
39 * Fires when select a date.
40 * @param {Roo.bootstrap.form.DateField} this
41 * @param {Mixed} date The date value
47 Roo.extend(Roo.bootstrap.form.TimeField, Roo.bootstrap.form.Input, {
50 * @cfg {String} format
51 * The default time format string which can be overriden for localization support. The format must be
52 * valid according to {@link Date#parseDate} (defaults to 'H:i').
58 getAutoCreate : function()
60 this.after = '<i class="fa far fa-clock"></i>';
61 return Roo.bootstrap.form.TimeField.superclass.getAutoCreate.call(this);
65 onRender: function(ct, position)
68 Roo.bootstrap.form.TimeField.superclass.onRender.call(this, ct, position);
70 this.language = this.language in Roo.bootstrap.form.TimeField.periodText ? this.language : "en";
72 this.pickerEl = Roo.get(document.body).createChild(Roo.bootstrap.form.TimeField.template);
74 this.picker().setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
76 this.pop = this.picker().select('>.datepicker-time',true).first();
77 this.pop.setVisibilityMode(Roo.Element.DISPLAY).originalDisplay = 'block';
79 this.picker().on('mousedown', this.onMousedown, this);
80 this.picker().on('click', this.onClick, this);
82 this.picker().addClass('datepicker-dropdown');
87 this.pop.select('.hours-up', true).first().on('click', this.onIncrementHours, this);
88 this.pop.select('.hours-down', true).first().on('click', this.onDecrementHours, this);
89 this.pop.select('.minutes-up', true).first().on('click', this.onIncrementMinutes, this);
90 this.pop.select('.minutes-down', true).first().on('click', this.onDecrementMinutes, this);
91 this.pop.select('button.period', true).first().on('click', this.onTogglePeriod, this);
92 this.pop.select('button.ok', true).first().on('click', this.setTime, this);
93 this.pop.select('button.ok', true).first().dom.innerHTML = Roo.bootstrap.form.TimeField.okText;
95 this.hiddenField = this.inputEl().insertSibling(
96 {tag : 'input', type : 'hidden', name : this.name},
100 this.inputEl().dom.setAttribute('name', this.name + '____hidden___');
104 fireKey: function(e){
105 if (!this.picker().isVisible()){
106 if (e.keyCode == 27) { // allow escape to hide and re-show picker
120 this.onTogglePeriod();
123 this.onIncrementMinutes();
126 this.onDecrementMinutes();
135 onClick: function(e) {
142 return this.pickerEl;
147 var time = this.pop.select('tbody', true).first();
149 time.dom.innerHTML = '';
164 cls: 'hours-up fa fas fa-chevron-up'
184 cls: 'minutes-up fa fas fa-chevron-up'
205 cls: 'timepicker-hour',
220 cls: 'timepicker-minute',
235 cls: 'btn btn-primary period',
257 cls: 'hours-down fa fas fa-chevron-down'
277 cls: 'minutes-down fa fas fa-chevron-down'
294 // default minute is a multiple of minuteStep
295 if(typeof(this.time) === 'undefined' || this.time.length == 0) {
296 this.time = new Date();
297 this.time = this.time.add(Date.MINUTE, Math.round(parseInt(this.time.format('i')) / this.minuteStep) * this.minuteStep - parseInt(this.time.format('i')));
299 this.time = (typeof(this.time) === 'undefined' || this.time.length == 0) ? new Date() : this.time;
306 var hours = this.time.getHours();
307 var minutes = this.time.getMinutes();
308 var period = Roo.bootstrap.form.TimeField.periodText[this.language]['am'];
311 period = Roo.bootstrap.form.TimeField.periodText[this.language]['pm'];
328 minutes = '0' + minutes;
331 this.pop.select('.timepicker-hour', true).first().dom.innerHTML = hours;
332 this.pop.select('.timepicker-minute', true).first().dom.innerHTML = minutes;
333 this.pop.select('button', true).first().dom.innerHTML = period;
339 this.picker().removeClass(['bottom-left', 'bottom-right', 'top-left', 'top-right']);
341 var cls = ['bottom'];
343 if((Roo.lib.Dom.getViewHeight() + Roo.get(document.body).getScroll().top) - (this.inputEl().getBottom() + this.picker().getHeight()) < 0){ // top
350 if((Roo.lib.Dom.getViewWidth() + Roo.get(document.body).getScroll().left) - (this.inputEl().getLeft() + this.picker().getWidth()) < 0){ // left
354 //this.picker().setXY(20000,20000);
355 this.picker().addClass(cls.join('-'));
359 Roo.each(cls, function(c){
364 _this.picker().alignTo(_this.inputEl(), "tr-br", [0, 10], false);
365 //_this.picker().setTop(_this.inputEl().getHeight());
369 _this.picker().alignTo(_this.inputEl(), "br-tr", [0, 10], false);
371 //_this.picker().setTop(0 - _this.picker().getHeight());
376 _this.picker().setLeft(_this.inputEl().getLeft() + _this.inputEl().getWidth() - _this.el.getLeft() - _this.picker().getWidth());
380 _this.picker().setLeft(_this.inputEl().getLeft() - _this.el.getLeft());
390 Roo.bootstrap.form.TimeField.superclass.onFocus.call(this);
396 Roo.bootstrap.form.TimeField.superclass.onBlur.call(this);
402 this.picker().show();
407 this.fireEvent('show', this, this.time);
412 this.picker().hide();
415 this.inputEl().blur();
417 this.fireEvent('hide', this, this.time);
423 this.setValue(this.time);
425 this.fireEvent('select', this, this.time);
430 // return false when it fails
431 parseTime : function(value)
436 if(value instanceof Date){
439 var v = Date.parseDate(value, 'H:i:s');
441 return (typeof(v) == 'undefined') ? false : v;
444 translateTime : function(time)
446 switch(this.language) {
448 return new Intl.DateTimeFormat('zh-CN', {
454 return time.format(this.format);
458 setValue: function(v)
460 var t = this.parseTime(v);
463 this.time = this.value = this.hiddenField.value = '';
465 this.inputEl().dom.value = '';
471 this.value = this.hiddenField.value = t.dateFormat('H:i:s');
473 v = this.translateTime(t);
476 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
485 setRawValue: function(v)
487 var t = this.parseTime(v);
490 this.time = this.value = this.hiddenField.value = '';
492 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
497 this.value = this.hiddenField.value = t.dateFormat('H:i:s');
499 v = this.translateTime(t);
502 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
515 getRawValue : function(){
516 return this.getValue();
519 onMousedown: function(e){
524 onIncrementHours: function()
526 Roo.log('onIncrementHours');
527 this.time = this.time.add(Date.HOUR, 1);
532 onDecrementHours: function()
534 Roo.log('onDecrementHours');
535 this.time = this.time.add(Date.HOUR, -1);
539 onIncrementMinutes: function()
541 Roo.log('onIncrementMinutes');
542 var minutesToAdd = Math.round((parseInt(this.time.format('i')) + this.minuteStep) / this.minuteStep) * this.minuteStep - parseInt(this.time.format('i'));
543 this.time = this.time.add(Date.MINUTE, minutesToAdd);
547 onDecrementMinutes: function()
549 Roo.log('onDecrementMinutes');
550 var minutesToSubtract = parseInt(this.time.format('i')) - Math.round((parseInt(this.time.format('i')) - this.minuteStep) / this.minuteStep) * this.minuteStep;
551 this.time = this.time.add(Date.MINUTE, -1 * minutesToSubtract);
555 onTogglePeriod: function()
557 Roo.log('onTogglePeriod');
558 this.time = this.time.add(Date.HOUR, 12);
564 Roo.apply(Roo.bootstrap.form.TimeField, {
578 Roo.apply(Roo.bootstrap.form.TimeField, {
581 cls: 'datepicker dropdown-menu',
585 cls: 'datepicker-time',
589 cls: 'table-condensed',
618 cls: 'btn btn-info ok',
619 html: "OK" // this is overridden on construciton