Skip to content

Commit 1acd462

Browse files
committed
enhancement(Datepicker) eslint coding standards #506
1 parent 8e59a9d commit 1acd462

File tree

1 file changed

+57
-48
lines changed

1 file changed

+57
-48
lines changed

src/datepicker.ts

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,17 @@ export interface DatepickerOptions extends BaseOptions {
151151
onDraw: (() => void) | null;
152152

153153
/** Field used for internal calculations DO NOT CHANGE IT */
154-
minYear?: any;
154+
minYear?: number;
155155
/** Field used for internal calculations DO NOT CHANGE IT */
156-
maxYear?: any;
156+
maxYear?: number;
157157
/** Field used for internal calculations DO NOT CHANGE IT */
158-
minMonth?: any;
158+
minMonth?: number;
159159
/** Field used for internal calculations DO NOT CHANGE IT */
160-
maxMonth?: any;
160+
maxMonth?: number;
161161
/** Field used for internal calculations DO NOT CHANGE IT */
162-
startRange?: any;
162+
startRange?: Date;
163163
/** Field used for internal calculations DO NOT CHANGE IT */
164-
endRange?: any;
164+
endRange?: Date;
165165
}
166166

167167
let _defaults: DatepickerOptions = {
@@ -276,10 +276,10 @@ export class Datepicker extends Component<DatepickerOptions> {
276276
/** The selected Date. */
277277
date: Date;
278278
endDate: null|Date;
279-
formats: any;
280-
calendars: any;
281-
private _y: any;
282-
private _m: any;
279+
formats: object;
280+
calendars: [{ month: number; year: number }];
281+
private _y: number;
282+
private _m: number;
283283
static _template: string;
284284

285285
constructor(el: HTMLInputElement, options: Partial<DatepickerOptions>) {
@@ -645,15 +645,14 @@ export class Datepicker extends Component<DatepickerOptions> {
645645
}
646646

647647
render(year, month, randId) {
648-
let opts = this.options,
649-
now = new Date(),
648+
const now = new Date(),
650649
days = Datepicker._getDaysInMonth(year, month),
651-
before = new Date(year, month, 1).getDay(),
652-
data = [],
650+
data = [];
651+
let before = new Date(year, month, 1).getDay(),
653652
row = [];
654653
Datepicker._setToStartOfDay(now);
655-
if (opts.firstDay > 0) {
656-
before -= opts.firstDay;
654+
if (this.options.firstDay > 0) {
655+
before -= this.options.firstDay;
657656
if (before < 0) {
658657
before += 7;
659658
}
@@ -671,23 +670,23 @@ export class Datepicker extends Component<DatepickerOptions> {
671670
cells += 7 - after;
672671
let isWeekSelected = false;
673672
for (let i = 0, r = 0; i < cells; i++) {
674-
let day = new Date(year, month, 1 + (i - before)),
673+
const day = new Date(year, month, 1 + (i - before)),
675674
isToday = Datepicker._compareDates(day, now),
676-
hasEvent = opts.events.indexOf(day.toDateString()) !== -1,
675+
hasEvent = this.options.events.indexOf(day.toDateString()) !== -1,
677676
isEmpty = i < before || i >= days + before,
678-
dayNumber = 1 + (i - before),
679-
monthNumber = month,
680-
yearNumber = year,
681-
isStartRange = opts.startRange && Datepicker._compareDates(opts.startRange, day),
682-
isEndRange = opts.endRange && Datepicker._compareDates(opts.endRange, day),
677+
isStartRange = this.options.startRange && Datepicker._compareDates(this.options.startRange, day),
678+
isEndRange = this.options.endRange && Datepicker._compareDates(this.options.endRange, day),
683679
isInRange =
684-
opts.startRange && opts.endRange && opts.startRange < day && day < opts.endRange,
680+
this.options.startRange && this.options.endRange && this.options.startRange < day && day < this.options.endRange,
685681
isDisabled =
686-
(opts.minDate && day < opts.minDate) ||
687-
(opts.maxDate && day > opts.maxDate) ||
688-
(opts.disableWeekends && Datepicker._isWeekend(day)) ||
689-
(opts.disableDayFn && opts.disableDayFn(day)),
690-
isDateRange = opts.isDateRange && Datepicker._isDate(this.endDate) && Datepicker._compareWithinRange(day, this.date, this.endDate);
682+
(this.options.minDate && day < this.options.minDate) ||
683+
(this.options.maxDate && day > this.options.maxDate) ||
684+
(this.options.disableWeekends && Datepicker._isWeekend(day)) ||
685+
(this.options.disableDayFn && this.options.disableDayFn(day)),
686+
isDateRange = this.options.isDateRange && Datepicker._isDate(this.endDate) && Datepicker._compareWithinRange(day, this.date, this.endDate);
687+
let dayNumber = 1 + (i - before),
688+
monthNumber = month,
689+
yearNumber = year;
691690

692691
let isSelected = false;
693692
if (Datepicker._isDate(this.date)) {
@@ -722,20 +721,20 @@ export class Datepicker extends Component<DatepickerOptions> {
722721
isStartRange: isStartRange,
723722
isEndRange: isEndRange,
724723
isInRange: isInRange,
725-
showDaysInNextAndPreviousMonths: opts.showDaysInNextAndPreviousMonths,
724+
showDaysInNextAndPreviousMonths: this.options.showDaysInNextAndPreviousMonths,
726725
isDateRange: isDateRange,
727726
};
728727

729728
row.push(this.renderDay(dayConfig));
730729

731730
if (++r === 7) {
732-
data.push(this.renderRow(row, opts.isRTL, isWeekSelected));
731+
data.push(this.renderRow(row, this.options.isRTL, isWeekSelected));
733732
row = [];
734733
r = 0;
735734
isWeekSelected = false;
736735
}
737736
}
738-
return this.renderTable(opts, data, randId);
737+
return this.renderTable(this.options, data, randId);
739738
}
740739

741740
renderDay(opts) {
@@ -749,29 +748,42 @@ export class Datepicker extends Component<DatepickerOptions> {
749748
return '<td class="is-empty"></td>';
750749
}
751750
}
751+
752+
// @todo wouldn't it be better defining opts class mapping and looping trough opts?
752753
if (opts.isDisabled) {
753754
arr.push('is-disabled');
754755
}
755756

756757
if (opts.isToday) {
757758
arr.push('is-today');
758759
}
760+
759761
if (opts.isSelected) {
760762
arr.push('is-selected');
761763
ariaSelected = 'true';
762764
}
765+
766+
// @todo should we create this additional css class?
763767
if (opts.hasEvent) {
764768
arr.push('has-event');
765769
}
770+
771+
// @todo should we create this additional css class?
766772
if (opts.isInRange) {
767773
arr.push('is-inrange');
768774
}
775+
776+
// @todo should we create this additional css class?
769777
if (opts.isStartRange) {
770778
arr.push('is-startrange');
771779
}
780+
781+
// @todo should we create this additional css class?
772782
if (opts.isEndRange) {
773783
arr.push('is-endrange');
774784
}
785+
786+
// @todo create additional css class
775787
if (opts.isDateRange) {
776788
arr.push('is-daterange');
777789
}
@@ -804,8 +816,8 @@ export class Datepicker extends Component<DatepickerOptions> {
804816
}
805817

806818
renderHead(opts) {
807-
let i,
808-
arr = [];
819+
const arr = [];
820+
let i
809821
for (i = 0; i < 7; i++) {
810822
arr.push(
811823
`<th scope="col"><abbr title="${this.renderDayName(opts, i)}">${this.renderDayName(
@@ -823,22 +835,20 @@ export class Datepicker extends Component<DatepickerOptions> {
823835
}
824836

825837
renderTitle(instance, c, year, month, refYear, randId) {
838+
const opts = this.options,
839+
isMinYear = year === opts.minYear,
840+
isMaxYear = year === opts.maxYear;
826841
let i,
827842
j,
828-
arr,
829-
opts = this.options,
830-
isMinYear = year === opts.minYear,
831-
isMaxYear = year === opts.maxYear,
843+
arr = [],
832844
html =
833845
'<div id="' +
834846
randId +
835847
'" class="datepicker-controls" role="heading" aria-live="assertive">',
836-
monthHtml,
837-
yearHtml,
838848
prev = true,
839849
next = true;
840850

841-
for (arr = [], i = 0; i < 12; i++) {
851+
for (i = 0; i < 12; i++) {
842852
arr.push(
843853
'<option value="' +
844854
(year === refYear ? i - c : 12 + i - c) +
@@ -853,7 +863,7 @@ export class Datepicker extends Component<DatepickerOptions> {
853863
);
854864
}
855865

856-
monthHtml = '<select class="datepicker-select orig-select-month" tabindex="-1">'+arr.join('')+'</select>';
866+
const monthHtml = '<select class="datepicker-select orig-select-month" tabindex="-1">'+arr.join('')+'</select>';
857867

858868
if (Array.isArray(opts.yearRange)) {
859869
i = opts.yearRange[0];
@@ -871,7 +881,7 @@ export class Datepicker extends Component<DatepickerOptions> {
871881
}
872882
if (opts.yearRangeReverse) arr.reverse();
873883

874-
yearHtml = `<select class="datepicker-select orig-select-year" tabindex="-1">${arr.join('')}</select>`;
884+
const yearHtml = `<select class="datepicker-select orig-select-year" tabindex="-1">${arr.join('')}</select>`;
875885

876886
let leftArrow =
877887
'<svg height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M15.41 16.09l-4.58-4.59 4.58-4.59L14 5.5l-6 6 6 6z"/><path d="M0-.5h24v24H0z" fill="none"/></svg>';
@@ -907,13 +917,12 @@ export class Datepicker extends Component<DatepickerOptions> {
907917
// refresh HTML
908918
draw(force: boolean = false) {
909919
if (!this.isOpen && !force) return;
910-
let opts = this.options,
920+
const opts = this.options,
911921
minYear = opts.minYear,
912922
maxYear = opts.maxYear,
913923
minMonth = opts.minMonth,
914-
maxMonth = opts.maxMonth,
915-
html = '',
916-
randId;
924+
maxMonth = opts.maxMonth;
925+
let html = '';
917926

918927
if (this._y <= minYear) {
919928
this._y = minYear;
@@ -928,7 +937,7 @@ export class Datepicker extends Component<DatepickerOptions> {
928937
}
929938
}
930939

931-
randId =
940+
const randId =
932941
'datepicker-title-' +
933942
Math.random()
934943
.toString(36)

0 commit comments

Comments
 (0)