Skip to content

Commit 2b07f12

Browse files
refactor: add JSDoc and optimize getDom performance
- Convert 7 block comments (/* */) to JSDoc format (/** */) - Cache Object.keys(this.config.slides).length in getDom() - Extract createPageControls() helper from getDom
1 parent 40c37ed commit 2b07f12

File tree

1 file changed

+60
-40
lines changed

1 file changed

+60
-40
lines changed

MMM-Carousel.js

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ Module.register("MMM-Carousel", {
343343
this.config.mode !== "slides" ||
344344
this.config.mode === "slides" && timer > 0
345345
) {
346-
/*
346+
/**
347347
* We set a timer to cause the page transitions
348348
* If we're in slides mode and the timer is set to 0, we only use manual transitions
349349
*/
@@ -359,7 +359,7 @@ Module.register("MMM-Carousel", {
359359
}
360360
},
361361

362-
/*
362+
/**
363363
* Calculate the next slide index based on navigation parameters
364364
* @param {object} modulesContext - The modules array context with currentIndex and slides properties
365365
* @param {object} params - Navigation parameters
@@ -420,7 +420,7 @@ Module.register("MMM-Carousel", {
420420
};
421421
},
422422

423-
/*
423+
/**
424424
* Check if a module should be shown in the current slide
425425
* @param {object} module - The MagicMirror module instance to check
426426
* @param {string|object} slideConfig - Slide configuration (module name string or config object with name and optional carouselId)
@@ -445,7 +445,7 @@ Module.register("MMM-Carousel", {
445445
return false;
446446
},
447447

448-
/*
448+
/**
449449
* Apply CSS classes and position changes to a module
450450
* @param {object} module - The MagicMirror module instance to style
451451
* @param {object} slideConfig - Slide configuration object with optional classes and position properties
@@ -477,7 +477,7 @@ Module.register("MMM-Carousel", {
477477
}
478478
},
479479

480-
/*
480+
/**
481481
* Update slide indicators (pagination dots and navigation buttons)
482482
* @param {object} modulesContext - The modules array context with currentIndex, slides, showPageIndicators, and showPageControls properties
483483
* @param {number} resetCurrentIndex - Total number of slides (for boundary checks)
@@ -704,9 +704,9 @@ Module.register("MMM-Carousel", {
704704
}
705705
},
706706

707-
/*
708-
* This is called when the module is loaded and the DOM is ready.
709-
* This is the first method called after the module has been registered.
707+
/**
708+
* Handle timeout callback to return to home slide
709+
* This is called when the transitionTimeout expires after manual navigation.
710710
*/
711711
transitionTimeoutCallback () {
712712
let goToIndex = -1;
@@ -767,7 +767,50 @@ Module.register("MMM-Carousel", {
767767
};
768768
},
769769

770-
/*
770+
/**
771+
* Creates page control elements (next/previous buttons) for the carousel.
772+
*
773+
* @param {number} slideCount - Total number of slides in the carousel.
774+
* @returns {object} Object with next and previous wrapper elements.
775+
*/
776+
createPageControls (slideCount) {
777+
const nextWrapper = document.createElement("div");
778+
nextWrapper.className = "next control";
779+
780+
const previousWrapper = document.createElement("div");
781+
previousWrapper.className = "previous control";
782+
783+
for (let slideIndex = 0; slideIndex < slideCount; slideIndex += 1) {
784+
const isNotFirstSlide = slideIndex > 0;
785+
if (isNotFirstSlide) {
786+
const nCtrlLabelWrapper = document.createElement("label");
787+
nCtrlLabelWrapper.setAttribute("for", `slider_${slideIndex}`);
788+
nCtrlLabelWrapper.id = `sliderNextBtn_${slideIndex}`;
789+
const arrow = document.createElement("span");
790+
arrow.className = "carousel-arrow carousel-arrow-right";
791+
nCtrlLabelWrapper.appendChild(arrow);
792+
nextWrapper.appendChild(nCtrlLabelWrapper);
793+
}
794+
795+
const isNotLastSlide = slideIndex < slideCount - 1;
796+
if (isNotLastSlide) {
797+
const pCtrlLabelWrapper = document.createElement("label");
798+
pCtrlLabelWrapper.setAttribute("for", `slider_${slideIndex}`);
799+
pCtrlLabelWrapper.id = `sliderPrevBtn_${slideIndex}`;
800+
const arrow = document.createElement("span");
801+
arrow.className = "carousel-arrow carousel-arrow-left";
802+
pCtrlLabelWrapper.appendChild(arrow);
803+
previousWrapper.appendChild(pCtrlLabelWrapper);
804+
}
805+
}
806+
807+
return {
808+
nextWrapper,
809+
previousWrapper
810+
};
811+
},
812+
813+
/**
771814
* Generate the DOM which needs to be displayed.
772815
* This method is called by the MagicMirror² core and needs to be subclassed
773816
* if the module wants to display info on the mirror.
@@ -786,7 +829,9 @@ Module.register("MMM-Carousel", {
786829
const paginationWrapper = document.createElement("div");
787830
paginationWrapper.className = "slider-pagination";
788831

789-
for (let slideIndex = 0; slideIndex < Object.keys(this.config.slides).length; slideIndex += 1) {
832+
const slideCount = Object.keys(this.config.slides).length;
833+
834+
for (let slideIndex = 0; slideIndex < slideCount; slideIndex += 1) {
790835
const input = document.createElement("input");
791836
input.type = "radio";
792837
input.name = "slider";
@@ -797,7 +842,7 @@ Module.register("MMM-Carousel", {
797842
}
798843

799844
if (this.config.showPageIndicators) {
800-
for (let slideIndex = 0; slideIndex < Object.keys(this.config.slides).length; slideIndex += 1) {
845+
for (let slideIndex = 0; slideIndex < slideCount; slideIndex += 1) {
801846
const label = document.createElement("label");
802847
label.setAttribute("for", `slider_${slideIndex}`);
803848
label.id = `sliderLabel_${slideIndex}`;
@@ -808,35 +853,10 @@ Module.register("MMM-Carousel", {
808853
div.appendChild(paginationWrapper);
809854

810855
if (this.config.showPageControls) {
811-
const nextWrapper = document.createElement("div");
812-
nextWrapper.className = "next control";
813-
814-
const previousWrapper = document.createElement("div");
815-
previousWrapper.className = "previous control";
816-
817-
for (let slideIndex = 0; slideIndex < Object.keys(this.config.slides).length; slideIndex += 1) {
818-
const isNotFirstSlide = slideIndex > 0;
819-
if (isNotFirstSlide) {
820-
const nCtrlLabelWrapper = document.createElement("label");
821-
nCtrlLabelWrapper.setAttribute("for", `slider_${slideIndex}`);
822-
nCtrlLabelWrapper.id = `sliderNextBtn_${slideIndex}`;
823-
const arrow = document.createElement("span");
824-
arrow.className = "carousel-arrow carousel-arrow-right";
825-
nCtrlLabelWrapper.appendChild(arrow);
826-
nextWrapper.appendChild(nCtrlLabelWrapper);
827-
}
828-
829-
const isNotLastSlide = slideIndex < Object.keys(this.config.slides).length - 1;
830-
if (isNotLastSlide) {
831-
const pCtrlLabelWrapper = document.createElement("label");
832-
pCtrlLabelWrapper.setAttribute("for", `slider_${slideIndex}`);
833-
pCtrlLabelWrapper.id = `sliderPrevBtn_${slideIndex}`;
834-
const arrow = document.createElement("span");
835-
arrow.className = "carousel-arrow carousel-arrow-left";
836-
pCtrlLabelWrapper.appendChild(arrow);
837-
previousWrapper.appendChild(pCtrlLabelWrapper);
838-
}
839-
}
856+
const {
857+
nextWrapper,
858+
previousWrapper
859+
} = this.createPageControls(slideCount);
840860

841861
div.appendChild(nextWrapper);
842862
div.appendChild(previousWrapper);

0 commit comments

Comments
 (0)