Skip to content

Commit 82a9d7c

Browse files
committed
enhancement(Timepicker) coding standards eslint fixes; refactor duplicate code fragments #506
1 parent 7f72601 commit 82a9d7c

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

src/timepicker.ts

+39-35
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ export class Timepicker extends Component<TimepickerOptions> {
134134
id: string;
135135
modal: Modal;
136136
modalEl: HTMLElement;
137-
plate: any;
138-
digitalClock: any;
137+
plate: HTMLElement;
138+
digitalClock: HTMLElement;
139139
inputHours: HTMLInputElement;
140140
inputMinutes: HTMLInputElement;
141141
x0: number;
@@ -148,24 +148,24 @@ export class Timepicker extends Component<TimepickerOptions> {
148148
* @default 'hours'
149149
*/
150150
currentView: Views;
151-
hand: any;
151+
hand: SVGElement;
152152
minutesView: HTMLElement;
153-
hours: any;
154-
minutes: any;
153+
hours: number;
154+
minutes: number;
155155
/** The selected time. */
156156
time: string;
157157
/**
158158
* If the time is AM or PM on twelve-hour clock.
159159
* @default 'PM'
160160
*/
161161
amOrPm: "AM" | "PM";
162-
static _template: any;
162+
static _template: string;
163163
/** If the picker is open. */
164164
isOpen: boolean;
165165
/** Vibrate device when dragging clock hand. */
166166
vibrate: "vibrate" | "webkitVibrate" | null;
167167
_canvas: HTMLElement;
168-
hoursView: any;
168+
hoursView: HTMLElement;
169169
spanAmPm: HTMLSpanElement;
170170
footer: HTMLElement;
171171
private _amBtn: HTMLElement;
@@ -174,8 +174,7 @@ export class Timepicker extends Component<TimepickerOptions> {
174174
bearing: Element;
175175
g: Element;
176176
toggleViewTimer: string | number | NodeJS.Timeout;
177-
canvas: any;
178-
vibrateTimer: any;
177+
vibrateTimer: NodeJS.Timeout | number;
179178

180179
constructor(el: HTMLInputElement, options: Partial<TimepickerOptions>) {
181180
super(el, options, Timepicker);
@@ -433,7 +432,7 @@ export class Timepicker extends Component<TimepickerOptions> {
433432

434433
const doneButton = this._createButton(this.options.i18n.done, '');
435434
doneButton.classList.add('timepicker-close');
436-
doneButton.addEventListener('click', this.done);
435+
doneButton.addEventListener('click', this._finishSelection);
437436
confirmationBtnsContainer.appendChild(doneButton);
438437
}
439438

@@ -491,36 +490,37 @@ export class Timepicker extends Component<TimepickerOptions> {
491490
}
492491

493492
_buildHoursView() {
494-
const $tick = document.createElement('div');
495-
$tick.classList.add('timepicker-tick');
493+
// const $tick = document.createElement('div');
494+
// $tick.classList.add('timepicker-tick');
496495
// Hours view
497496
if (this.options.twelveHour) {
498497
for (let i = 1; i < 13; i += 1) {
499-
const tick = <HTMLElement>$tick.cloneNode(true);
498+
// const tick = <HTMLElement>$tick.cloneNode(true);
500499
const radian = (i / 6) * Math.PI;
501500
const radius = this.options.outerRadius;
502-
tick.style.left = this.options.dialRadius + Math.sin(radian) * radius - this.options.tickRadius + 'px';
503-
tick.style.top = this.options.dialRadius - Math.cos(radian) * radius - this.options.tickRadius + 'px';
504-
tick.innerHTML = i === 0 ? '00' : i.toString();
505-
this.hoursView.appendChild(tick);
506-
// tick.on(mousedownEvent, mousedown);
501+
this._buildHoursTick(i, radian, radius);
507502
}
508503
}
509504
else {
510505
for (let i = 0; i < 24; i += 1) {
511-
const tick = <HTMLElement>$tick.cloneNode(true);
506+
// const tick = <HTMLElement>$tick.cloneNode(true);
512507
const radian = (i / 6) * Math.PI;
513508
const inner = i > 0 && i < 13;
514509
const radius = inner ? this.options.innerRadius : this.options.outerRadius;
515-
tick.style.left = this.options.dialRadius + Math.sin(radian) * radius - this.options.tickRadius + 'px';
516-
tick.style.top = this.options.dialRadius - Math.cos(radian) * radius - this.options.tickRadius + 'px';
517-
tick.innerHTML = i === 0 ? '00' : i.toString();
518-
this.hoursView.appendChild(tick);
519-
// tick.on(mousedownEvent, mousedown);
510+
this._buildHoursTick(i, radian, radius);
520511
}
521512
}
522513
}
523514

515+
_buildHoursTick(i: number, radian: number, radius: number) {
516+
const tick = document.createElement('div');
517+
tick.classList.add('timepicker-tick');
518+
tick.style.left = this.options.dialRadius + Math.sin(radian) * radius - this.options.tickRadius + 'px';
519+
tick.style.top = this.options.dialRadius - Math.cos(radian) * radius - this.options.tickRadius + 'px';
520+
tick.innerHTML = i === 0 ? '00' : i.toString();
521+
this.hoursView.appendChild(tick);
522+
}
523+
524524
_buildMinutesView() {
525525
const _tick = document.createElement('div');
526526
_tick.classList.add('timepicker-tick');
@@ -591,6 +591,7 @@ export class Timepicker extends Component<TimepickerOptions> {
591591
/**
592592
* Show hours or minutes view on timepicker.
593593
* @param view The name of the view you want to switch to, 'hours' or 'minutes'.
594+
* @param delay
594595
*/
595596
showView = (view: Views, delay: number = null) => {
596597
if (view === 'minutes' && getComputedStyle(this.hoursView).visibility === 'visible') {
@@ -634,14 +635,13 @@ export class Timepicker extends Component<TimepickerOptions> {
634635
radius =
635636
isHours && value > 0 && value < 13 ? this.options.innerRadius : this.options.outerRadius,
636637
x = Math.sin(radian) * radius,
637-
y = -Math.cos(radian) * radius,
638-
self = this;
638+
y = -Math.cos(radian) * radius;
639639

640640
if (delay) {
641-
this.canvas?.classList.add('timepicker-canvas-out');
641+
this._canvas?.classList.add('timepicker-canvas-out');
642642
setTimeout(() => {
643-
self.canvas?.classList.remove('timepicker-canvas-out');
644-
self.setHand(x, y);
643+
this._canvas?.classList.remove('timepicker-canvas-out');
644+
this.setHand(x, y);
645645
}, delay);
646646
}
647647
else {
@@ -668,7 +668,7 @@ export class Timepicker extends Component<TimepickerOptions> {
668668
}
669669
else {
670670
this.minutes = new Date().getMinutes();
671-
this.inputMinutes.value = this.minutes;
671+
this.inputMinutes.value = this.minutes.toString();
672672
}
673673
this.drawClockFromTimeInput(this.minutes, isHours);
674674
}
@@ -687,11 +687,11 @@ export class Timepicker extends Component<TimepickerOptions> {
687687
}
688688

689689
setHand(x, y, roundBy5: boolean = false) {
690-
let radian = Math.atan2(x, -y),
691-
isHours = this.currentView === 'hours',
690+
const isHours = this.currentView === 'hours',
692691
unit = Math.PI / (isHours || roundBy5 ? 6 : 30),
693692
z = Math.sqrt(x * x + y * y),
694-
inner = isHours && z < (this.options.outerRadius + this.options.innerRadius) / 2,
693+
inner = isHours && z < (this.options.outerRadius + this.options.innerRadius) / 2;
694+
let radian = Math.atan2(x, -y),
695695
radius = inner ? this.options.innerRadius : this.options.outerRadius;
696696

697697
if (this.options.twelveHour) {
@@ -784,6 +784,10 @@ export class Timepicker extends Component<TimepickerOptions> {
784784
this.inputHours.value = (this.hours % (this.options.twelveHour ? 12 : 24)).toString();
785785
}
786786

787+
_finishSelection = () => {
788+
this.done();
789+
}
790+
787791
/**
788792
* Open timepicker.
789793
*/
@@ -804,7 +808,7 @@ export class Timepicker extends Component<TimepickerOptions> {
804808
this.modal.close();
805809
}
806810

807-
done = (e = null, clearValue = null) => {
811+
done = (clearValue = null) => {
808812
// Set input value
809813
const last = this.el.value;
810814
let value = clearValue
@@ -824,7 +828,7 @@ export class Timepicker extends Component<TimepickerOptions> {
824828
}
825829

826830
clear = () => {
827-
this.done(null, true);
831+
this.done(true);
828832
}
829833

830834
static {

0 commit comments

Comments
 (0)