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.fireEvent('hide', this, this.time);
421 this.setValue(this.time);
423 this.fireEvent('select', this, this.time);
428 // return false when it fails
429 parseTime : function(value)
434 if(value instanceof Date){
437 var v = Date.parseDate(value, 'H:i:s');
439 return (typeof(v) == 'undefined') ? false : v;
442 translateTime : function(time)
444 switch(this.language) {
446 return new Intl.DateTimeFormat('zh-CN', {
452 return time.format(this.format);
456 setValue: function(v)
458 var t = this.parseTime(v);
461 this.time = this.value = this.hiddenField.value = '';
463 this.inputEl().dom.value = '';
469 this.value = this.hiddenField.value = t.dateFormat('H:i:s');
471 v = this.translateTime(t);
474 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
483 setRawValue: function(v)
485 var t = this.parseTime(v);
488 this.time = this.value = this.hiddenField.value = '';
490 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
495 this.value = this.hiddenField.value = t.dateFormat('H:i:s');
497 v = this.translateTime(t);
500 this.inputEl().dom.value = (v === null || v === undefined ? '' : v);
513 getRawValue : function(){
514 return this.getValue();
517 onMousedown: function(e){
522 onIncrementHours: function()
524 Roo.log('onIncrementHours');
525 this.time = this.time.add(Date.HOUR, 1);
530 onDecrementHours: function()
532 Roo.log('onDecrementHours');
533 this.time = this.time.add(Date.HOUR, -1);
537 onIncrementMinutes: function()
539 Roo.log('onIncrementMinutes');
540 var minutesToAdd = Math.round((parseInt(this.time.format('i')) + this.minuteStep) / this.minuteStep) * this.minuteStep - parseInt(this.time.format('i'));
541 this.time = this.time.add(Date.MINUTE, minutesToAdd);
545 onDecrementMinutes: function()
547 Roo.log('onDecrementMinutes');
548 var minutesToSubtract = parseInt(this.time.format('i')) - Math.round((parseInt(this.time.format('i')) - this.minuteStep) / this.minuteStep) * this.minuteStep;
549 this.time = this.time.add(Date.MINUTE, -1 * minutesToSubtract);
553 onTogglePeriod: function()
555 Roo.log('onTogglePeriod');
556 this.time = this.time.add(Date.HOUR, 12);
562 Roo.apply(Roo.bootstrap.form.TimeField, {
576 Roo.apply(Roo.bootstrap.form.TimeField, {
579 cls: 'datepicker dropdown-menu',
583 cls: 'datepicker-time',
587 cls: 'table-condensed',
616 cls: 'btn btn-info ok',
617 html: "OK" // this is overridden on construciton