Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"@stomp/stompjs": "^7.5.0",
"dompurify": "^3.5.0",
"highlight.js": "^11.12.0",
"imagesloaded": "^4.1.4",
"jquery": "^3.7.1",
"jquery-jcrop": "^0.9.13",
"luxon": "3.7.2",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions src/pat/carousel/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import $ from "jquery";
import Base from "../../core/base";
import logging from "../../core/logging";
import Parser from "../../core/parser";
import utils from "../../core/utils";

const log = logging.getLogger("pat.carousel");

Expand All @@ -31,7 +32,6 @@ export default Base.extend({
import("slick-carousel/slick/slick.scss");
}
await import("slick-carousel");
const ImagesLoaded = (await import("imagesloaded")).default;

this.options = parser.parse(this.el, this.options);
this.settings = {
Expand All @@ -49,7 +49,19 @@ export default Base.extend({
this.settings.appendDots = this.options.appendDots;
}

ImagesLoaded(this.el, () => this.setup());
this.setup();

// Recalculate the carousel dimensions whenever an image within has
// been loaded or failed loading. The load and error events do not
// bubble, but can be captured. This also catches images which are
// added later.
const update = utils.debounce(() => {
if (this.el.classList.contains("slick-initialized")) {
$(this.el).slick("setPosition");
}
}, 100);
this.el.addEventListener("load", update, true);
this.el.addEventListener("error", update, true);
},

setup() {
Expand Down
24 changes: 24 additions & 0 deletions src/pat/carousel/carousel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,29 @@ describe("carousel-plugin", function () {
expect(options.autoplaySpeed).toBe(345);
expect(options.adaptiveHeight).toBe(true);
});
it("Recalculates the dimensions after images have been loaded.", async () => {
$("#lab").html(
"<ul class='pat-carousel'>" +
" <li><img src='a.png' /></li>" +
" <li><img src='b.png' /></li>" +
"</ul>"
);
const carousel = document.querySelector("#lab ul");
pattern.init($(carousel));
await utils.timeout(20); // wait a bit for all to settle.

// Initialized immediately, without waiting for images.
expect(carousel.classList.contains("slick-initialized")).toBe(true);

const spy_slick = jest.spyOn($.fn, "slick");
const images = carousel.querySelectorAll("img");
images[0].dispatchEvent(new Event("load"));
images[1].dispatchEvent(new Event("error"));
await utils.timeout(150); // debounce

// Debounced, only called once.
expect(spy_slick).toHaveBeenCalledTimes(1);
expect(spy_slick).toHaveBeenCalledWith("setPosition");
});
});
});
58 changes: 42 additions & 16 deletions src/pat/equaliser/equaliser.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,60 @@ var equaliser = {
name: "equaliser",
trigger: ".pat-equaliser, .pat-equalizer",

async init($el, opts) {
const ImagesLoaded = (await import("imagesloaded")).default;

init($el, opts) {
return $el.each(function () {
var $container = $(this),
options = parser.parse($container, opts);
const container = this;
const $container = $(container);
const options = parser.parse($container, opts);
$container.data("pat-equaliser", options);
$(window).on(
"resize.pat-equaliser",
null,
this,
utils.debounce(equaliser._onEvent, 100)
);
ImagesLoaded(
this,
$.proxy(function () {
equaliser._update(this);
}, this)
container,
utils.debounce(equaliser._onEvent, 100),
);
const callback = utils.debounce(equaliser._update.bind(this), 100);
const observer = new MutationObserver(callback);

const update = utils.debounce(() => {
if (container.isConnected) {
equaliser._update(container);
}
}, 100);

// Re-equalise when images within the container have been loaded.
// The load and error events do not bubble, but can be captured.
// This also catches images which are added later.
container.addEventListener("load", update, true);
container.addEventListener("error", update, true);

const observer = new MutationObserver((mutations) => {
if (!container.isConnected) {
// The container was removed from the DOM. Stop observing.
observer.disconnect();
return;
}
// Ignore the changes done by any equaliser itself on its
// children, otherwise we end up in an infinite loop.
const is_relevant = mutations.some(
(mutation) =>
!(
mutation.type === "attributes" &&
["style", "class"].includes(mutation.attributeName) &&
$(mutation.target.parentElement).data("pat-equaliser")
),
);
if (is_relevant) {
update();
}
});
const config = {
childList: true,
subtree: true,
characterData: true,
attributes: true,
};
observer.observe(document.body, config);

equaliser._update(container);
});
},

Expand Down Expand Up @@ -77,7 +103,7 @@ var equaliser = {
options.effect.easing,
function () {
$(this).addClass("equalised");
}
},
);
break;
}
Expand Down
95 changes: 95 additions & 0 deletions src/pat/equaliser/equaliser.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import $ from "jquery";
import pattern from "./equaliser";
import utils from "../../core/utils";
import { jest } from "@jest/globals";

describe("pat-equaliser", function () {
beforeEach(function () {
Expand Down Expand Up @@ -77,4 +79,97 @@ describe("pat-equaliser", function () {
expect(data.dom).toBe($container[0]);
});
});
describe("init", function () {
afterEach(function () {
jest.restoreAllMocks();
});

it("Equalises on initialization.", function () {
document.body.innerHTML = `
<div class="pat-equaliser"
data-pat-equaliser="transition: none">
<div style="height: 50px"></div>
<div style="height: 100px"></div>
</div>
`;
const container = document.querySelector(".pat-equaliser");
const spy_update = jest.spyOn(pattern, "_update");

pattern.init($(container));

expect(spy_update).toHaveBeenCalledTimes(1);
expect(spy_update).toHaveBeenCalledWith(container);
for (const child of container.children) {
expect(child.classList.contains("equalised")).toBe(true);
}
});

it("Re-equalises after an image has been loaded or failed loading.", async function () {
document.body.innerHTML = `
<div class="pat-equaliser"
data-pat-equaliser="transition: none">
<div><img src="a.png" /></div>
<div><img src="b.png" /></div>
</div>
`;
const container = document.querySelector(".pat-equaliser");
pattern.init($(container));
await utils.timeout(150); // wait for the debounced observer.

const spy_update = jest.spyOn(pattern, "_update");
const [img1, img2] = container.querySelectorAll("img");

img1.dispatchEvent(new Event("load"));
await utils.timeout(150); // debounce
expect(spy_update).toHaveBeenCalledTimes(1);
expect(spy_update).toHaveBeenCalledWith(container);

img2.dispatchEvent(new Event("error"));
await utils.timeout(150); // debounce
expect(spy_update).toHaveBeenCalledTimes(2);
});

it("Re-equalises when the DOM changes.", async function () {
document.body.innerHTML = `
<div class="pat-equaliser"
data-pat-equaliser="transition: none">
<div></div>
<div></div>
</div>
`;
const container = document.querySelector(".pat-equaliser");
pattern.init($(container));
await utils.timeout(150); // wait for the debounced observer.

const spy_update = jest.spyOn(pattern, "_update");

container.children[0].innerHTML = "<p>new content</p>";
await utils.timeout(150); // debounce
expect(spy_update).toHaveBeenCalledTimes(1);
expect(spy_update).toHaveBeenCalledWith(container);
});

it("Does not re-equalise because of its own changes.", async function () {
document.body.innerHTML = `
<div class="pat-equaliser"
data-pat-equaliser="transition: none">
<div></div>
<div></div>
</div>
<div class="pat-equaliser"
data-pat-equaliser="transition: none">
<div></div>
<div></div>
</div>
`;
const spy_update = jest.spyOn(pattern, "_update");
pattern.init($(".pat-equaliser"));
// Initial update for both.
expect(spy_update).toHaveBeenCalledTimes(2);

await utils.timeout(500);
// No further updates.
expect(spy_update).toHaveBeenCalledTimes(2);
});
});
});
11 changes: 11 additions & 0 deletions src/pat/masonry/documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ fitting stones in a wall. You’ve probably seen it in use all over the Internet

## Documentation

### Images and the `masonry-ready` class

The layout is updated whenever an image within the masonry container has been
loaded or failed loading - also for images which are added later.
When all initially present images are loaded, the class `masonry-ready` is set
on the container. The default styles hide the items until then.

Images with `loading="lazy"` are not waited for, as they might only be loaded
when scrolled into view. Set the `width` and `height` attributes on such images
to avoid layout shifts.

### Options reference

| Property | Default Value | Description |
Expand Down
59 changes: 33 additions & 26 deletions src/pat/masonry/masonry.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,24 @@ export default Base.extend({

async init($el, opts) {
Masonry = (await import("masonry-layout")).default;
const ImagesLoaded = (await import("imagesloaded")).default;

this.options = parser.parse(this.$el, opts);
// Initialize
this.initMasonry();

const imgLoad = await ImagesLoaded(this.$el);
imgLoad.on(
"progress",
function () {
if (!this.msnry) {
this.initMasonry();
}
this.quicklayout();
}.bind(this)
);
imgLoad.on(
"always",
function () {
if (!this.msnry) {
this.initMasonry();
}
this.layout();
}.bind(this)
);
// Re-layout whenever an image within has been loaded or failed
// loading. The load and error events do not bubble, but can be
// captured. This also catches images which are added later.
// When all images are loaded, do a full layout which sets the
// ``masonry-ready`` class.
this.is_images_loaded = false;
const on_image_loaded = () => {
this.quicklayout();
this.layout_when_images_loaded();
};
this.el.addEventListener("load", on_image_loaded, true);
this.el.addEventListener("error", on_image_loaded, true);
this.layout_when_images_loaded();

// Update if something gets injected inside the pat-masonry
this.$el
Expand Down Expand Up @@ -140,16 +133,30 @@ export default Base.extend({

layout: function () {
this.$el.removeClass("masonry-ready");
this.msnry.on(
"layoutComplete",
function () {
this.$el.addClass("masonry-ready");
}.bind(this)
);
this.msnry.once("layoutComplete", () => {
this.$el.addClass("masonry-ready");
});
this.msnry.layout();
this.quicklayout();
},

layout_when_images_loaded() {
// Do a full layout once all images are loaded or failed loading.
if (this.is_images_loaded) {
return;
}
// Ignore lazy loading images, which might only be loaded when
// scrolled into view.
const is_images_loaded = [...this.el.querySelectorAll("img")].every(
(img) => img.complete || img.getAttribute("loading") === "lazy"
);
if (!is_images_loaded) {
return;
}
this.is_images_loaded = true;
this.layout();
},

getTypeCastedValue: function (original) {
var val = Number(original);
return isNaN(val) ? original || 0 : val;
Expand Down
Loading