Skip to content

Commit 34593d0

Browse files
committed
Merge branch 'gselderslaghs-eslint-coding-standards' into v2-dev
2 parents 24daf93 + 4afb62e commit 34593d0

22 files changed

+400
-327
lines changed

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default [
1616
'@typescript-eslint/no-unused-vars': 'error',
1717
'@typescript-eslint/no-unused-expressions': 'error',
1818
'@typescript-eslint/no-this-alias': 'warn',
19-
'@typescript-eslint/no-empty-object-type': 'error',
19+
'@typescript-eslint/no-empty-object-type': ['error' , { allowWithName: 'BaseOptions$' }],
2020
'@typescript-eslint/no-require-imports': 'error',
2121
'@typescript-eslint/no-unsafe-function-type': 'error'
2222
}

src/autocomplete.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Dropdown, DropdownOptions } from "./dropdown";
33
import { Component, BaseOptions, InitElements, MElement } from "./component";
44

55
export interface AutocompleteData {
6-
/**
6+
/**
77
* A primitive value that can be converted to string.
88
* If "text" is not provided, it will also be used as "option text" as well
99
*/
@@ -66,9 +66,9 @@ export interface AutocompleteOptions extends BaseOptions {
6666
* @default {}
6767
*/
6868
dropdownOptions: Partial<DropdownOptions>;
69-
};
69+
}
7070

71-
let _defaults: AutocompleteOptions = {
71+
const _defaults: AutocompleteOptions = {
7272
data: [], // Autocomplete data set
7373
onAutocomplete: null, // Callback for when autocompleted
7474
dropdownOptions: {
@@ -82,7 +82,7 @@ let _defaults: AutocompleteOptions = {
8282
onSearch: (text: string, autocomplete: Autocomplete) => {
8383
const normSearch = text.toLocaleLowerCase();
8484
autocomplete.setMenuItems(
85-
autocomplete.options.data.filter((option) =>
85+
autocomplete.options.data.filter((option) =>
8686
option.id.toString().toLocaleLowerCase().includes(normSearch)
8787
|| option.text?.toLocaleLowerCase().includes(normSearch)
8888
)
@@ -118,7 +118,7 @@ export class Autocomplete extends Component<AutocompleteOptions> {
118118
...Autocomplete.defaults,
119119
...options
120120
};
121-
121+
122122
this.isOpen = false;
123123
this.count = 0;
124124
this.activeIndex = -1;
@@ -168,8 +168,8 @@ export class Autocomplete extends Component<AutocompleteOptions> {
168168

169169
_setupEventHandlers() {
170170
this.el.addEventListener('blur', this._handleInputBlur);
171-
this.el.addEventListener('keyup', this._handleInputKeyupAndFocus);
172-
this.el.addEventListener('focus', this._handleInputKeyupAndFocus);
171+
this.el.addEventListener('keyup', this._handleInputKeyup);
172+
this.el.addEventListener('focus', this._handleInputFocus);
173173
this.el.addEventListener('keydown', this._handleInputKeydown);
174174
this.el.addEventListener('click', this._handleInputClick);
175175
this.container.addEventListener(
@@ -188,8 +188,8 @@ export class Autocomplete extends Component<AutocompleteOptions> {
188188

189189
_removeEventHandlers() {
190190
this.el.removeEventListener('blur', this._handleInputBlur);
191-
this.el.removeEventListener('keyup', this._handleInputKeyupAndFocus);
192-
this.el.removeEventListener('focus', this._handleInputKeyupAndFocus);
191+
this.el.removeEventListener('keyup', this._handleInputKeyup);
192+
this.el.removeEventListener('focus', this._handleInputFocus);
193193
this.el.removeEventListener('keydown', this._handleInputKeydown);
194194
this.el.removeEventListener('click', this._handleInputClick);
195195
this.container.removeEventListener(
@@ -226,11 +226,12 @@ export class Autocomplete extends Component<AutocompleteOptions> {
226226
this.el.parentElement.appendChild(this.container);
227227

228228
// Initialize dropdown
229-
let dropdownOptions = {
229+
const dropdownOptions = {
230230
...Autocomplete.defaults.dropdownOptions,
231231
...this.options.dropdownOptions
232232
};
233-
let userOnItemClick = dropdownOptions.onItemClick;
233+
// @todo shouldn't we conditionally check if dropdownOptions.onItemClick is set in first place?
234+
const userOnItemClick = dropdownOptions.onItemClick;
234235
// Ensuring the select Option call when user passes custom onItemClick function to dropdown
235236
dropdownOptions.onItemClick = (li) => {
236237
if (!li) return;
@@ -270,19 +271,29 @@ export class Autocomplete extends Component<AutocompleteOptions> {
270271
}
271272
}
272273

273-
_handleInputKeyupAndFocus = (e: KeyboardEvent) => {
274+
_handleInputKeyup = (e: KeyboardEvent) => {
274275
if (e.type === 'keyup') Autocomplete._keydown = false;
275276
this.count = 0;
276277
const actualValue = this.el.value.toLocaleLowerCase();
277278
// Don't capture enter or arrow key usage.
278279
if (Utils.keys.ENTER.includes(e.key) || Utils.keys.ARROW_UP.includes(e.key) || Utils.keys.ARROW_DOWN.includes(e.key)) return;
279280
// Check if the input isn't empty
280281
// Check if focus triggered by tab
281-
if (this.oldVal !== actualValue && (Utils.tabPressed || e.type !== 'focus')) {
282+
if (this.oldVal !== actualValue && (Utils.tabPressed)) {
282283
this.open();
283284
}
285+
this._inputChangeDetection(actualValue);
286+
};
287+
288+
_handleInputFocus = () => {
289+
this.count = 0;
290+
const actualValue = this.el.value.toLocaleLowerCase();
291+
this._inputChangeDetection(actualValue);
292+
}
293+
294+
_inputChangeDetection = (value: string) => {
284295
// Value has changed!
285-
if (this.oldVal !== actualValue) {
296+
if (this.oldVal !== value) {
286297
this._setStatusLoading();
287298
this.options.onSearch(this.el.value, this);
288299
}
@@ -291,8 +302,8 @@ export class Autocomplete extends Component<AutocompleteOptions> {
291302
this.selectedValues = [];
292303
this._triggerChanged();
293304
}
294-
this.oldVal = actualValue;
295-
}
305+
this.oldVal = value;
306+
};
296307

297308
_handleInputKeydown = (e: KeyboardEvent) => {
298309
Autocomplete._keydown = true;

src/buttons.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export interface FloatingActionButtonOptions extends BaseOptions {
1818
toolbarEnabled: boolean;
1919
};
2020

21-
let _defaults: FloatingActionButtonOptions = {
21+
const _defaults: FloatingActionButtonOptions = {
2222
direction: 'top',
2323
hoverEnabled: true,
2424
toolbarEnabled: false
@@ -132,7 +132,7 @@ export class FloatingActionButton extends Component<FloatingActionButtonOptions>
132132

133133
_handleDocumentClick = (e: MouseEvent) => {
134134
const elem = e.target;
135-
if (elem !== this._menu) this.close;
135+
if (elem !== this._menu) this.close();
136136
}
137137

138138
/**
@@ -166,7 +166,7 @@ export class FloatingActionButton extends Component<FloatingActionButtonOptions>
166166
this.el.classList.add('active');
167167
const delayIncrement = 40;
168168
const duration = 275;
169-
169+
170170
this._floatingBtnsReverse.forEach((el, index) => {
171171
const delay = delayIncrement * index;
172172
el.style.transition = 'none';
@@ -198,21 +198,19 @@ export class FloatingActionButton extends Component<FloatingActionButtonOptions>
198198
}
199199

200200
_animateInToolbar() {
201-
let scaleFactor;
202-
let windowWidth = window.innerWidth;
203-
let windowHeight = window.innerHeight;
204-
let btnRect = this.el.getBoundingClientRect();
201+
const windowWidth = window.innerWidth;
202+
const windowHeight = window.innerHeight;
203+
const btnRect = this.el.getBoundingClientRect();
205204

206-
const backdrop = document.createElement('div');
205+
const backdrop = document.createElement('div'),
206+
scaleFactor = windowWidth / backdrop[0].clientWidth,
207+
fabColor = getComputedStyle(this._anchor).backgroundColor; // css('background-color');
207208
backdrop.classList.add('fab-backdrop'); // $('<div class="fab-backdrop"></div>');
208-
209-
const fabColor = getComputedStyle(this._anchor).backgroundColor; // css('background-color');
210-
209+
backdrop.style.backgroundColor = fabColor;
211210
this._anchor.append(backdrop);
212211

213212
this.offsetX = btnRect.left - windowWidth / 2 + btnRect.width / 2;
214213
this.offsetY = windowHeight - btnRect.bottom;
215-
scaleFactor = windowWidth / backdrop[0].clientWidth;
216214
this.btnBottom = btnRect.bottom;
217215
this.btnLeft = btnRect.left;
218216
this.btnWidth = btnRect.width;
@@ -229,8 +227,6 @@ export class FloatingActionButton extends Component<FloatingActionButtonOptions>
229227
this._anchor.style.transform = `translateY(${this.offsetY}px`;
230228
this._anchor.style.transition = 'none';
231229

232-
backdrop.style.backgroundColor = fabColor;
233-
234230
setTimeout(() => {
235231
this.el.style.transform = '';
236232
this.el.style.transition = 'transform .2s cubic-bezier(0.550, 0.085, 0.680, 0.530), background-color 0s linear .2s';

src/carousel.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export interface CarouselOptions extends BaseOptions{
4949
onCycleTo: (current: Element, dragged: boolean) => void;
5050
}
5151

52-
let _defaults: CarouselOptions = {
52+
const _defaults: CarouselOptions = {
5353
duration: 200, // ms
5454
dist: -100, // zoom scale TODO: make this more intuitive as an option
5555
shift: 0, // spacing for center image
@@ -72,25 +72,25 @@ export class Carousel extends Component<CarouselOptions> {
7272
offset: number;
7373
target: number;
7474
images: HTMLElement[];
75-
itemWidth: any;
76-
itemHeight: any;
75+
itemWidth: number;
76+
itemHeight: number;
7777
dim: number;
78-
_indicators: any;
78+
_indicators: HTMLUListElement;
7979
count: number;
8080
xform: string;
8181
verticalDragged: boolean;
82-
reference: any;
83-
referenceY: any;
82+
reference: number;
83+
referenceY: number;
8484
velocity: number;
8585
frame: number;
8686
timestamp: number;
8787
ticker: string | number | NodeJS.Timeout;
8888
amplitude: number;
8989
/** The index of the center carousel item. */
9090
center: number = 0;
91-
imageHeight: any;
92-
scrollingTimeout: any;
93-
oneTimeCallback: any;
91+
imageHeight: number;
92+
scrollingTimeout: number | NodeJS.Timeout;
93+
oneTimeCallback: (current: Element, dragged: boolean) => void | null;
9494

9595
constructor(el: HTMLElement, options: Partial<CarouselOptions>) {
9696
super(el, options, Carousel);
@@ -152,7 +152,7 @@ export class Carousel extends Component<CarouselOptions> {
152152
// Setup cross browser string
153153
this.xform = 'transform';
154154
['webkit', 'Moz', 'O', 'ms'].every((prefix) => {
155-
var e = prefix + 'Transform';
155+
const e = prefix + 'Transform';
156156
if (typeof document.body.style[e] !== 'undefined') {
157157
this.xform = e;
158158
return false;
@@ -446,13 +446,16 @@ export class Carousel extends Component<CarouselOptions> {
446446
}
447447

448448
_track = () => {
449-
let now: number, elapsed: number, delta: number, v: number;
450-
now = Date.now();
451-
elapsed = now - this.timestamp;
449+
const now: number = Date.now(),
450+
elapsed: number = now - this.timestamp,
451+
delta: number = this.offset - this.frame,
452+
v: number = (1000 * delta) / (1 + elapsed);
453+
// now = Date.now();
454+
// elapsed = now - this.timestamp;
452455
this.timestamp = now;
453-
delta = this.offset - this.frame;
456+
// delta = this.offset - this.frame;
454457
this.frame = this.offset;
455-
v = (1000 * delta) / (1 + elapsed);
458+
// v = (1000 * delta) / (1 + elapsed);
456459
this.velocity = 0.8 * v + 0.2 * this.velocity;
457460
}
458461

@@ -476,33 +479,33 @@ export class Carousel extends Component<CarouselOptions> {
476479
this.el.classList.add('scrolling');
477480
}
478481
if (this.scrollingTimeout != null) {
479-
window.clearTimeout(this.scrollingTimeout);
482+
clearTimeout(this.scrollingTimeout);
480483
}
481-
this.scrollingTimeout = window.setTimeout(() => {
484+
this.scrollingTimeout = setTimeout(() => {
482485
this.el.classList.remove('scrolling');
483486
}, this.options.duration);
484487

485488
// Start actual scroll
489+
this.offset = typeof x === 'number' ? x : this.offset;
490+
this.center = Math.floor((this.offset + this.dim / 2) / this.dim);
491+
492+
const half: number = this.count >> 1,
493+
delta: number = this.offset - this.center * this.dim,
494+
dir: number = delta < 0 ? 1 : -1,
495+
tween: number = (-dir * delta * 2) / this.dim;
486496
let i: number,
487-
half: number,
488-
delta: number,
489-
dir: number,
490-
tween: number,
491497
el: HTMLElement,
492498
alignment: string,
493499
zTranslation: number,
494500
tweenedOpacity: number,
495501
centerTweenedOpacity: number;
496-
let lastCenter = this.center;
497-
let numVisibleOffset = 1 / this.options.numVisible;
498-
499-
this.offset = typeof x === 'number' ? x : this.offset;
500-
this.center = Math.floor((this.offset + this.dim / 2) / this.dim);
502+
const lastCenter = this.center;
503+
const numVisibleOffset = 1 / this.options.numVisible;
501504

502-
delta = this.offset - this.center * this.dim;
503-
dir = delta < 0 ? 1 : -1;
504-
tween = (-dir * delta * 2) / this.dim;
505-
half = this.count >> 1;
505+
// delta = this.offset - this.center * this.dim;
506+
// dir = delta < 0 ? 1 : -1;
507+
// tween = (-dir * delta * 2) / this.dim;
508+
// half = this.count >> 1;
506509

507510
if (this.options.fullWidth) {
508511
alignment = 'translateX(0)';
@@ -537,7 +540,7 @@ export class Carousel extends Component<CarouselOptions> {
537540
el.classList.add('active');
538541
}
539542

540-
let transformString = `${alignment} translateX(${-delta / 2}px) translateX(${dir *
543+
const transformString = `${alignment} translateX(${-delta / 2}px) translateX(${dir *
541544
this.options.shift *
542545
tween *
543546
i}px) translateZ(${this.options.dist * tween}px)`;
@@ -556,7 +559,7 @@ export class Carousel extends Component<CarouselOptions> {
556559
// Don't show wrapped items.
557560
if (!this.noWrap || this.center + i < this.count) {
558561
el = this.images[this._wrap(this.center + i)];
559-
let transformString = `${alignment} translateX(${this.options.shift +
562+
const transformString = `${alignment} translateX(${this.options.shift +
560563
(this.dim * i - delta) / 2}px) translateZ(${zTranslation}px)`;
561564
this._updateItemStyle(el, tweenedOpacity, -i, transformString);
562565
}
@@ -571,7 +574,7 @@ export class Carousel extends Component<CarouselOptions> {
571574
// Don't show wrapped items.
572575
if (!this.noWrap || this.center - i >= 0) {
573576
el = this.images[this._wrap(this.center - i)];
574-
let transformString = `${alignment} translateX(${-this.options.shift +
577+
const transformString = `${alignment} translateX(${-this.options.shift +
575578
(-this.dim * i - delta) / 2}px) translateZ(${zTranslation}px)`;
576579
this._updateItemStyle(el, tweenedOpacity, -i, transformString);
577580
}
@@ -580,7 +583,7 @@ export class Carousel extends Component<CarouselOptions> {
580583
// Don't show wrapped items.
581584
if (!this.noWrap || (this.center >= 0 && this.center < this.count)) {
582585
el = this.images[this._wrap(this.center)];
583-
let transformString = `${alignment} translateX(${-delta / 2}px) translateX(${dir *
586+
const transformString = `${alignment} translateX(${-delta / 2}px) translateX(${dir *
584587
this.options.shift *
585588
tween}px) translateZ(${this.options.dist * tween}px)`;
586589
this._updateItemStyle(el, centerTweenedOpacity, 0, transformString);

0 commit comments

Comments
 (0)