Skip to content

Commit 6485991

Browse files
authored
Merge pull request #119 from Travelopia/fix/QE-1993-slider-js-error
QE-1993 fix undefined updateNavItems error in TP slider
2 parents 3d535a3 + 7849c4c commit 6485991

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/slider/tp-slider.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ export class TPSliderElement extends HTMLElement {
254254
// Initialize total slides variable.
255255
const totalSlides: number = this.getTotalSlides();
256256

257-
// Total Posible groups.
258-
const totalPosibleGroups: number = Math.ceil( totalSlides / this.step );
257+
// Total Possible groups.
258+
const totalPossibleGroups: number = Math.ceil( totalSlides / this.step );
259259

260260
// Initialize previous slide number.
261261
let previousSlideNumber: number = 0;
@@ -267,13 +267,13 @@ export class TPSliderElement extends HTMLElement {
267267

268268
// Check if we are in the last group or in any other.
269269
if ( this.currentSlideIndex + this.step - 1 >= totalSlides ) {
270-
currentGroup = totalPosibleGroups;
270+
currentGroup = totalPossibleGroups;
271271
} else {
272272
currentGroup = Math.ceil( this.currentSlideIndex / this.step );
273273
}
274274

275275
// Update previous slide number based on which group we are in.
276-
if ( currentGroup === totalPosibleGroups ) {
276+
if ( currentGroup === totalPossibleGroups ) {
277277
previousSlideNumber = this.currentSlideIndex - this.step + 1;
278278
} else {
279279
previousSlideNumber = this.currentSlideIndex - this.step;
@@ -397,16 +397,22 @@ export class TPSliderElement extends HTMLElement {
397397
* Update stuff when any attribute has changed.
398398
* Example: Update sub-components.
399399
*/
400-
update(): void {
400+
async update(): Promise<void> {
401401
// Get sub-components.
402402
const sliderNav: TPSliderNavElement | null = this.querySelector( 'tp-slider-nav' );
403403
const sliderCounts: NodeListOf<TPSliderCountElement> | null = this.querySelectorAll( 'tp-slider-count' );
404404
const leftArrow: TPSliderArrowElement | null = this.getArrow( 'tp-slider-arrow[direction="previous"]' );
405405
const rightArrow: TPSliderArrowElement | null = this.getArrow( 'tp-slider-arrow[direction="next"]' );
406406

407-
// Total slides variable and Total posible group.
407+
// Wait for initialization - done to avoid updateNavItems undefined error.
408+
await customElements.whenDefined( 'tp-slider-nav' );
409+
await customElements.whenDefined( 'tp-slider-nav-item' );
410+
await customElements.whenDefined( 'tp-slider-count' );
411+
await customElements.whenDefined( 'tp-slider-arrow' );
412+
413+
// Total slides variable and Total possible group.
408414
const totalSlides: number = this.getTotalSlides();
409-
const totalPosibleGroups: number = Math.ceil( totalSlides / this.step );
415+
const totalPossibleGroups: number = Math.ceil( totalSlides / this.step );
410416

411417
// Set active slide.
412418
const slides: NodeListOf<TPSliderSlideElement> | null | undefined = this.getSlideElements();
@@ -442,7 +448,7 @@ export class TPSliderElement extends HTMLElement {
442448
// Check if index and groups are equal to update active dot.
443449
if ( groupIndex === index ) {
444450
navItem.setAttribute( 'current', 'yes' );
445-
} else if ( ( index === totalPosibleGroups - 1 && this.currentSlideIndex + this.step - 1 >= totalSlides ) ) {
451+
} else if ( ( index === totalPossibleGroups - 1 && this.currentSlideIndex + this.step - 1 >= totalSlides ) ) {
446452
navItem.setAttribute( 'current', 'yes' );
447453

448454
// Remove current index from last 2nd item.

0 commit comments

Comments
 (0)