Skip to content

Commit 484d846

Browse files
committed
no promise purely manual
1 parent adad692 commit 484d846

File tree

1 file changed

+34
-49
lines changed

1 file changed

+34
-49
lines changed

addons/html_builder/static/src/plugins/carousel_option_plugin.js

+34-49
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,11 @@ export class CarouselOptionPlugin extends Plugin {
2424
getActions() {
2525
return {
2626
addSlide: {
27-
apply: ({ editingElement }) =>
28-
new Promise((resolve) => {
29-
resolve(this.addSlide(editingElement));
30-
}),
27+
apply: ({ editingElement }) => this.addSlide(editingElement),
3128
},
3229
slideCarousel: {
33-
apply: async ({ editingElement, direction }) => {
34-
await this.slide(direction, editingElement);
30+
apply: ({ editingElement, direction }) => {
31+
this.slide(direction, editingElement);
3532
},
3633
},
3734
};
@@ -40,8 +37,6 @@ export class CarouselOptionPlugin extends Plugin {
4037
addSlide(editingElement) {
4138
const activeCarouselItem = editingElement.querySelector(".carousel-item.active");
4239
this.dependencies.clone.cloneElement(activeCarouselItem);
43-
44-
return this.slide("next", editingElement);
4540
}
4641

4742
/**
@@ -52,50 +47,38 @@ export class CarouselOptionPlugin extends Plugin {
5247
* - "next": the next slide;
5348
* - number: a slide number.
5449
* @param {Element} editingElement the carousel element.
55-
* @returns {Promise}
5650
*/
5751
slide(direction, editingElement) {
58-
// this.trigger_up("disable_loading_effect");
59-
editingElement.addEventListener("slide.bs.carousel", () =>
60-
// TODO: overlay should be already handled by optionContainer, remove this
61-
// setTimeout(() => this.trigger_up("hide_overlay"));
62-
{
63-
this.slideTimestamp = window.performance.now();
64-
}
65-
);
66-
67-
return new Promise((resolve) => {
68-
editingElement.addEventListener("slid.bs.carousel", () => {
69-
// slid.bs.carousel is most of the time fired too soon by bootstrap
70-
// since it emulates the transitionEnd with a setTimeout. We wait
71-
// here an extra 20% of the time before retargeting edition, which
72-
// should be enough...
73-
const slideDuration = window.performance.now() - this.slideTimestamp;
74-
setTimeout(() => {
75-
// Setting the active indicator manually, as Bootstrap could
76-
// not do it because the `data-bs-slide-to` attribute is not
77-
// here in edit mode anymore.
78-
const activeSlide = editingElement.querySelector(".carousel-item.active");
79-
const indicatorsEl = editingElement.querySelector(".carousel-indicators");
80-
const activeIndex = [...activeSlide.parentElement.children].indexOf(
81-
activeSlide
82-
);
83-
const activeIndicatorEl = [...indicatorsEl.children][activeIndex];
84-
activeIndicatorEl.classList.add("active");
85-
activeIndicatorEl.setAttribute("aria-current", "true");
86-
87-
resolve();
88-
}, 0.2 * slideDuration);
89-
});
52+
const activeCurrentSlide = editingElement.querySelector(".carousel-item.active");
53+
const carouselItemsEl = activeCurrentSlide.parentElement;
54+
const activeIndex = [...carouselItemsEl.children].indexOf(activeCurrentSlide);
55+
const carouselItemsCount = carouselItemsEl.children.length;
56+
57+
// compute the new active slide index
58+
let newActiveSlideIndex;
59+
if (typeof direction === "number") {
60+
newActiveSlideIndex = direction;
61+
} else if (direction === "next") {
62+
newActiveSlideIndex = (activeIndex + 1) % carouselItemsCount;
63+
} else if (direction === "prev") {
64+
newActiveSlideIndex = (activeIndex - 1 + carouselItemsCount) % carouselItemsCount;
65+
}
9066

91-
// rewrite the jquery interface in carousel.js
92-
const carouselInstance = window.Carousel.getOrCreateInstance(editingElement);
93-
if (typeof direction === "number") {
94-
carouselInstance.to(direction);
95-
} else {
96-
carouselInstance[direction]();
97-
}
98-
});
67+
// change the active slide manually
68+
activeCurrentSlide.classList.remove("active");
69+
const newActiveSlide = carouselItemsEl.children[newActiveSlideIndex];
70+
newActiveSlide.classList.add("active");
71+
72+
// change the active indicator manually
73+
const indicatorsEl = editingElement.querySelector(".carousel-indicators");
74+
const currentActiveIndicatorEl = indicatorsEl.querySelector(".active");
75+
if (currentActiveIndicatorEl) {
76+
currentActiveIndicatorEl.classList.remove("active");
77+
currentActiveIndicatorEl.removeAttribute("aria-current");
78+
}
79+
const activeIndicatorEl = [...indicatorsEl.children][newActiveSlideIndex];
80+
activeIndicatorEl.classList.add("active");
81+
activeIndicatorEl.setAttribute("aria-current", "true");
9982
}
10083

10184
onWillClone({ originalEl }) {
@@ -136,6 +119,8 @@ export class CarouselOptionPlugin extends Plugin {
136119
) {
137120
// Need to remove editor data from the clone so it gets its own.
138121
cloneEl.classList.remove("active");
122+
const editingCarousel = cloneEl.closest(".carousel");
123+
this.slide("next", editingCarousel);
139124
}
140125
}
141126

0 commit comments

Comments
 (0)