Skip to content

Commit a5f4ea5

Browse files
author
Çağatay Çivici
committed
1 parent b672f2b commit a5f4ea5

File tree

3 files changed

+76
-5
lines changed

3 files changed

+76
-5
lines changed

components/calendar/calendar.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@
180180
min-width: 1.5em;
181181
}
182182

183-
.ui-timepicker > .ui-minute-picker {
183+
.ui-timepicker > .ui-minute-picker,
184+
.ui-timepicker > .ui-second-picker {
184185
margin-left: 0;
185186
}
186187

components/calendar/calendar.ts

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ export interface LocaleSettings {
9797
<span class="fa fa-angle-down"></span>
9898
</a>
9999
</div>
100+
<div class="ui-separator" *ngIf="showSeconds">
101+
<a href="#">
102+
<span class="fa fa-angle-up"></span>
103+
</a>
104+
<span>:</span>
105+
<a href="#">
106+
<span class="fa fa-angle-down"></span>
107+
</a>
108+
</div>
109+
<div class="ui-second-picker" *ngIf="showSeconds">
110+
<a href="#" (click)="incrementSecond($event)">
111+
<span class="fa fa-angle-up"></span>
112+
</a>
113+
<span [ngStyle]="{'display': currentSecond < 10 ? 'inline': 'none'}">0</span><span>{{currentSecond}}</span>
114+
<a href="#" (click)="incrementSecond($event)">
115+
<span class="fa fa-angle-down"></span>
116+
</a>
117+
</div>
100118
<div class="ui-ampm-picker" *ngIf="hourFormat=='12'">
101119
<a href="#" (click)="toggleAMPM($event)">
102120
<span class="fa fa-angle-up"></span>
@@ -177,6 +195,10 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
177195
@Input() stepHour: number = 1;
178196

179197
@Input() stepMinute: number = 1;
198+
199+
@Input() stepSecond: number = 1;
200+
201+
@Input() showSeconds: number = 1;
180202

181203
@Input() required: boolean;
182204

@@ -217,6 +239,8 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
217239

218240
currentMinute: number;
219241

242+
currentSecond: number;
243+
220244
pm: boolean;
221245

222246
overlay: HTMLDivElement;
@@ -282,6 +306,7 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
282306
this.currentYear = date.getFullYear();
283307
if(this.showTime) {
284308
this.currentMinute = date.getMinutes();
309+
this.currentSecond = date.getSeconds();
285310
this.pm = date.getHours() > 11;
286311

287312
if(this.hourFormat == '12')
@@ -292,6 +317,7 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
292317
else if(this.timeOnly) {
293318
this.currentMinute = 0;
294319
this.currentHour = 0;
320+
this.currentSecond = 0;
295321
}
296322

297323
this.createMonth(this.currentMonth, this.currentYear);
@@ -454,6 +480,7 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
454480
this.value.setHours(this.currentHour);
455481

456482
this.value.setMinutes(this.currentMinute);
483+
this.value.setSeconds(this.currentSecond);
457484
}
458485
this.updateModel();
459486
this.onSelect.emit(this.value);
@@ -652,6 +679,24 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
652679
event.preventDefault();
653680
}
654681

682+
incrementSecond(event) {
683+
let newSecond = this.currentSecond + this.stepSecond;
684+
this.currentSecond = (newSecond > 59) ? newSecond - 60 : newSecond;
685+
686+
this.updateTime();
687+
688+
event.preventDefault();
689+
}
690+
691+
decrementSecond(event) {
692+
let newSecond = this.currentSecond - this.stepSecond;
693+
this.currentSecond = (newSecond < 0) ? 60 + newSecond : newSecond;
694+
695+
this.updateTime();
696+
697+
event.preventDefault();
698+
}
699+
655700
updateTime() {
656701
this.value = this.value||new Date();
657702
if(this.hourFormat === '12' && this.pm && this.currentHour != 12)
@@ -660,6 +705,7 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
660705
this.value.setHours(this.currentHour);
661706

662707
this.value.setMinutes(this.currentMinute);
708+
this.value.setSeconds(this.currentSecond);
663709
this.updateModel();
664710
this.onSelect.emit(this.value);
665711
this.updateInputfield();
@@ -720,6 +766,7 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
720766
}
721767

722768
value.setMinutes(time.minute);
769+
value.setSeconds(time.second);
723770
}
724771

725772
updateUI() {
@@ -744,6 +791,7 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
744791
}
745792

746793
this.currentMinute = val.getMinutes();
794+
this.currentSecond = val.getSeconds();
747795
}
748796
}
749797

@@ -874,6 +922,7 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
874922
let output = '';
875923
let hours = date.getHours();
876924
let minutes = date.getMinutes();
925+
let seconds = date.getSeconds();
877926

878927
if(this.hourFormat == '12' && this.pm && hours != 12) {
879928
hours-=12;
@@ -883,6 +932,11 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
883932
output += ':';
884933
output += (minutes < 10) ? '0' + minutes : minutes;
885934

935+
if(this.showSeconds) {
936+
output += ':';
937+
output += (seconds < 10) ? '0' + seconds : seconds;
938+
}
939+
886940
if(this.hourFormat == '12') {
887941
output += this.pm ? ' PM' : ' AM';
888942
}
@@ -892,21 +946,25 @@ export class Calendar implements AfterViewInit,OnInit,OnDestroy,ControlValueAcce
892946

893947
parseTime(value) {
894948
let tokens: string[] = value.split(':');
895-
if(tokens.length !== 2) {
949+
let validTokentLength = this.showSeconds ? 3 : 2;
950+
951+
if(tokens.length !== validTokentLength) {
896952
throw "Invalid time";
897953
}
898954

899955
let h = parseInt(tokens[0]);
900956
let m = parseInt(tokens[1]);
901-
if(isNaN(h) || isNaN(m) || h > 23 || m > 59 || (this.hourFormat == '12' && h > 12)) {
957+
let s = this.showSeconds ? parseInt(tokens[2]) : null;
958+
959+
if(isNaN(h) || isNaN(m) || h > 23 || m > 59 || (this.hourFormat == '12' && h > 12) || (this.showSeconds && (isNaN(s) || s > 59))) {
902960
throw "Invalid time";
903961
}
904962
else {
905963
if(this.hourFormat == '12' && h !== 12) {
906964
h+= 12;
907965
}
908-
909-
return {hour: parseInt(tokens[0]), minute: parseInt(tokens[1])};
966+
967+
return {hour: h, minute: m, second: s};
910968
}
911969
}
912970

showcase/demo/calendar/calendardemo.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ <h3>Attributes</h3>
354354
<td>null</td>
355355
<td>Index of the element in tabbing order.</td>
356356
</tr>
357+
<tr>
358+
<td>showSeconds</td>
359+
<td>boolean</td>
360+
<td>false</td>
361+
<td>Whether to show the seconds in time picker.</td>
362+
</tr>
357363
<tr>
358364
<td>stepHour</td>
359365
<td>number</td>
@@ -366,6 +372,12 @@ <h3>Attributes</h3>
366372
<td>1</td>
367373
<td>Minutes to change per step.</td>
368374
</tr>
375+
<tr>
376+
<td>stepSecond</td>
377+
<td>number</td>
378+
<td>1</td>
379+
<td>Seconds to change per step.</td>
380+
</tr>
369381
</tbody>
370382
</table>
371383
</div>

0 commit comments

Comments
 (0)