Skip to content
Draft
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
4 changes: 2 additions & 2 deletions packages/scripts/dist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ export class App extends ENGrid {
new CustomCurrency();
// Auto Country Select
new AutoCountrySelect();
// Page Background
new PageBackground(this.options.UseBodyBannerImageAsBackground);
// Add Image Attribution
if (this.options.MediaAttribution)
new MediaAttribution();
Expand Down Expand Up @@ -230,8 +232,6 @@ export class App extends ENGrid {
new A11y();
new AddNameToMessage();
new ExpandRegionName();
// Page Background
new PageBackground();
// Url Params to Form Fields
new UrlToForm();
// Required if Visible Fields
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/dist/interfaces/options.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Options {
backgroundImage?: string | string[];
UseBodyBannerImageAsBackground?: boolean;
MediaAttribution?: boolean;
applePay?: boolean;
CapitalizeFields?: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/dist/interfaces/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ export const OptionsDefaults = {
"rightright1col",
"none",
],
UseBodyBannerImageAsBackground: false,
};
8 changes: 7 additions & 1 deletion packages/scripts/dist/page-background.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
export declare class PageBackground {
private pageBackground;
private bodyBanner;
private bodyBannerImage;
private mutationObserver;
private logger;
constructor();
constructor(useBodyBannerImage?: boolean);
private findBodyBannerImage;
/**
* Initialize background image by finding and setting CSS custom property
*/
private initializeBackgroundImage;
private getBackgroundImage;
private useBodyBannerAsBackground;
private getImageSource;
/**
* Set the background image URL as a CSS custom property
*/
Expand Down
69 changes: 55 additions & 14 deletions packages/scripts/dist/page-background.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { ENGrid, EngridLogger } from ".";
export class PageBackground {
constructor() {
constructor(useBodyBannerImage = false) {
// @TODO: Change page-backgroundImage to page-background
this.pageBackground = document.querySelector(".page-backgroundImage");
this.bodyBanner = document.querySelector(".body-banner");
this.bodyBannerImage = null;
this.mutationObserver = null;
this.logger = new EngridLogger("PageBackground", "lightblue", "darkblue", "🖼️");
if (!this.pageBackground) {
if (useBodyBannerImage) {
this.bodyBannerImage = this.findBodyBannerImage();
}
if (!this.pageBackground && !this.bodyBannerImage) {
this.logger.log("A background image set in the page was not found, any default image set in the theme on --engrid__page-backgroundImage_url will be used");
return;
}
Expand All @@ -14,28 +19,64 @@ export class PageBackground {
this.processAttributionPositioning();
this.setupMutationObserver();
}
findBodyBannerImage() {
if (!this.bodyBanner) {
return null;
}
return this.bodyBanner.querySelector(".body-banner img.preferred-image, .body-banner img");
}
/**
* Initialize background image by finding and setting CSS custom property
*/
initializeBackgroundImage() {
if (!this.pageBackground)
const backgroundImg = this.getBackgroundImage();
if (!backgroundImg) {
this.logger.log("No image found in page background and no body banner image found (or pageBackground is already occupied), any default image set in the theme on --engrid__page-backgroundImage_url will be used");
return;
const pageBackgroundImg = this.pageBackground.querySelector("img");
if (!pageBackgroundImg) {
this.logger.log("A background image set in the page was not found, any default image set in the theme on --engrid__page-backgroundImage_url will be used");
}
const imageSource = this.getImageSource(backgroundImg);
if (!imageSource) {
this.logger.log("A background image set in the page was found but without a data-src or src value, no action taken", backgroundImg);
return;
}
const dataSrc = pageBackgroundImg.getAttribute("data-src");
const src = pageBackgroundImg.src;
if (dataSrc) {
this.setBackgroundImageUrl(dataSrc, "data-src");
this.setBackgroundImageUrl(imageSource.url, imageSource.sourceType);
}
getBackgroundImage() {
if (!this.pageBackground) {
return null;
}
else if (src) {
this.setBackgroundImageUrl(src, "src");
const existingImage = this.pageBackground.querySelector("img");
if (existingImage) {
return existingImage;
}
else {
this.logger.log("A background image set in the page was found but without a data-src or src value, no action taken", pageBackgroundImg);
if (this.bodyBannerImage && this.pageBackground.children.length === 0) {
return this.useBodyBannerAsBackground();
}
return null;
}
useBodyBannerAsBackground() {
if (!this.pageBackground || !this.bodyBanner) {
return null;
}
this.logger.log("No image found in page background, using body banner image as background image instead");
const clonedBodyBanner = this.bodyBanner.cloneNode(true);
while (clonedBodyBanner.firstChild) {
this.pageBackground.appendChild(clonedBodyBanner.firstChild);
}
document.body.removeAttribute("data-engrid-no-page-backgroundImage");
ENGrid.setBodyData("use-body-banner-background", "");
return this.pageBackground.querySelector("img.preferred-image, img");
}
getImageSource(backgroundImg) {
const dataSrc = backgroundImg.getAttribute("data-src");
if (dataSrc) {
return { sourceType: "data-src", url: dataSrc };
}
const src = backgroundImg.src;
if (src) {
return { sourceType: "src", url: src };
}
return null;
}
/**
* Set the background image URL as a CSS custom property
Expand Down
5 changes: 3 additions & 2 deletions packages/scripts/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ export class App extends ENGrid {
// Auto Country Select
new AutoCountrySelect();

// Page Background
new PageBackground(this.options.UseBodyBannerImageAsBackground);

// Add Image Attribution
if (this.options.MediaAttribution) new MediaAttribution();
// Apple Pay
Expand Down Expand Up @@ -392,8 +395,6 @@ export class App extends ENGrid {
new AddNameToMessage();
new ExpandRegionName();

// Page Background
new PageBackground();

// Url Params to Form Fields
new UrlToForm();
Expand Down
2 changes: 2 additions & 0 deletions packages/scripts/src/interfaces/options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Options {
backgroundImage?: string | string[];
UseBodyBannerImageAsBackground?: boolean;
MediaAttribution?: boolean;
applePay?: boolean;
CapitalizeFields?: boolean;
Expand Down Expand Up @@ -263,4 +264,5 @@ export const OptionsDefaults: Options = {
"rightright1col",
"none",
],
UseBodyBannerImageAsBackground: false,
};
101 changes: 84 additions & 17 deletions packages/scripts/src/page-background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export class PageBackground {
private pageBackground: HTMLElement | null = document.querySelector(
".page-backgroundImage"
);
private bodyBanner: HTMLElement | null = document.querySelector(
".body-banner"
);
private bodyBannerImage: HTMLImageElement | null = null;
private mutationObserver: MutationObserver | null = null;
private logger: EngridLogger = new EngridLogger(
"PageBackground",
Expand All @@ -13,8 +17,12 @@ export class PageBackground {
"🖼️"
);

constructor() {
if (!this.pageBackground) {
constructor(useBodyBannerImage: boolean = false) {
if (useBodyBannerImage) {
this.bodyBannerImage = this.findBodyBannerImage();
}

if (!this.pageBackground && !this.bodyBannerImage) {
this.logger.log(
"A background image set in the page was not found, any default image set in the theme on --engrid__page-backgroundImage_url will be used"
);
Expand All @@ -27,36 +35,95 @@ export class PageBackground {
this.setupMutationObserver();
}

private findBodyBannerImage(): HTMLImageElement | null {
if (!this.bodyBanner) {
return null;
}

return this.bodyBanner.querySelector(
".body-banner img.preferred-image, .body-banner img"
) as HTMLImageElement | null;
}

/**
* Initialize background image by finding and setting CSS custom property
*/
private initializeBackgroundImage(): void {
if (!this.pageBackground) return;

const pageBackgroundImg = this.pageBackground.querySelector(
"img"
) as HTMLImageElement | null;
const backgroundImg = this.getBackgroundImage();

if (!pageBackgroundImg) {
if (!backgroundImg) {
this.logger.log(
"A background image set in the page was not found, any default image set in the theme on --engrid__page-backgroundImage_url will be used"
"No image found in page background and no body banner image found (or pageBackground is already occupied), any default image set in the theme on --engrid__page-backgroundImage_url will be used"
);
return;
}

const dataSrc = pageBackgroundImg.getAttribute("data-src");
const src = pageBackgroundImg.src;
const imageSource = this.getImageSource(backgroundImg);

if (dataSrc) {
this.setBackgroundImageUrl(dataSrc, "data-src");
} else if (src) {
this.setBackgroundImageUrl(src, "src");
} else {
if (!imageSource) {
this.logger.log(
"A background image set in the page was found but without a data-src or src value, no action taken",
pageBackgroundImg
backgroundImg
);
return;
}

this.setBackgroundImageUrl(imageSource.url, imageSource.sourceType);
}

private getBackgroundImage(): HTMLImageElement | null {
if (!this.pageBackground) {
return null;
}

const existingImage = this.pageBackground.querySelector("img");
if (existingImage) {
return existingImage;
}

if (this.bodyBannerImage && this.pageBackground.children.length === 0) {
return this.useBodyBannerAsBackground();
}

return null;
}

private useBodyBannerAsBackground(): HTMLImageElement | null {
if (!this.pageBackground || !this.bodyBanner) {
return null;
}

this.logger.log(
"No image found in page background, using body banner image as background image instead"
);

const clonedBodyBanner = this.bodyBanner.cloneNode(true) as HTMLElement;
while (clonedBodyBanner.firstChild) {
this.pageBackground.appendChild(clonedBodyBanner.firstChild);
}

document.body.removeAttribute("data-engrid-no-page-backgroundImage");
ENGrid.setBodyData("use-body-banner-background", "");

return this.pageBackground.querySelector(
"img.preferred-image, img"
) as HTMLImageElement | null;
}

private getImageSource(
backgroundImg: HTMLImageElement
): { sourceType: string; url: string } | null {
const dataSrc = backgroundImg.getAttribute("data-src");
if (dataSrc) {
return { sourceType: "data-src", url: dataSrc };
}

const src = backgroundImg.src;
if (src) {
return { sourceType: "src", url: src };
}

return null;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions packages/styles/dist/main.css

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

2 changes: 1 addition & 1 deletion packages/styles/dist/main.css.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions packages/styles/src/_engrid-banner-image.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
}
}

@mixin engrid_hide-body-banner-on-desktop {
&[data-engrid-use-body-banner-background]:not(#en__pagebuilder) {
.body-banner {
display: none !important;
}
}
}

@mixin engrid_mobile-banner {
// If there is content in the body-title, hide the body-bannerOverlay on mobile
body[data-engrid-has-body-title] .en__component--advrow .body-bannerOverlay {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ $centercenter1col_body-column_offset-width-remaining: $centercenter1col_content-
}

@include engrid_hide-background-on-desktop;
@include engrid_hide-body-banner-on-desktop;
}

@media screen and (max-width: $centercenter1col_content-column_min-width_minus-1px) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ $centerleft1col_body-column_offset-width-remaining: $centerleft1col_content-colu
}

@include engrid_hide-background-on-desktop;
@include engrid_hide-body-banner-on-desktop;
}

@media screen and (max-width: $centerleft1col_content-column_min-width_minus-1px) {
Expand Down
Loading
Loading