Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[angular-xmcloud] Introduce angular SXA Image and Banner components #1886

Merged
merged 14 commits into from
Aug 19, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<ng-container [ngTemplateOutlet]="variant"></ng-container>

<ng-template #default>
<div class="component image {{ styles }}" [attr.id]="id" *ngIf="rendering.fields; else empty">
addy-pathania marked this conversation as resolved.
Show resolved Hide resolved
<div className="component-content">
<ng-container *ngIf="isEditing || !rendering.fields.TargetUrl?.value?.href; else withLink">
<img *scImage="rendering.fields.Image" />
</ng-container>
<span class="image-caption field-imagecaption" *scText="rendering.fields.ImageCaption"></span>
</div>
</div>
</ng-template>

<ng-template #banner>
<div class="component hero-banner {{ styles }} {{ classHeroBannerEmpty }}" [attr.id]="id">
<div class="component-content sc-sxa-image-hero-banner" [ngStyle]="backgroundStyle">
<ng-container *ngIf="isEditing">
<img *scImage="modifyImageProps" />
</ng-container>
</div>
</div>
</ng-template>

<ng-template #withLink>
<a *scLink="rendering.fields.TargetUrl">
<img *scImage="rendering.fields.Image" />
</a>
</ng-template>

<ng-template #empty>
<div className="component image {{ styles }}">
<div class="component-content">
<span class="is-empty-hint">Image</span>
</div>
</div>
</ng-template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Component, OnInit, OnDestroy, ViewChild, TemplateRef } from '@angular/core';
import { Subscription } from 'rxjs';
import { EditMode, ImageField } from '@sitecore-jss/sitecore-jss-angular';
import { SxaComponent } from '../sxa.component';
import { JssContextService } from '../../jss-context.service';

@Component({
selector: 'app-image',
templateUrl: './image.component.html',
})
export class ImageComponent extends SxaComponent implements OnInit, OnDestroy {
@ViewChild('default', { static: true }) defaultVariant: TemplateRef<any>;
@ViewChild('banner', { static: true }) bannerVariant: TemplateRef<any>;
classHeroBannerEmpty = '';
backgroundStyle = {};
modifyImageProps = {};
isEditing = false;
private contextSubscription: Subscription;

constructor(private jssContext: JssContextService) {
super();
}

ngOnInit() {
super.ngOnInit();

const imageField = this.rendering.fields?.Image as ImageField;
this.backgroundStyle = imageField?.value?.src && {
'background-image': `url('${imageField.value.src}')`,
};

this.contextSubscription = this.jssContext.state.subscribe((newState) => {
this.isEditing = newState.sitecore && newState.sitecore.context.pageEditing;

this.classHeroBannerEmpty =
this.isEditing && imageField?.value?.class === 'scEmptyImage' ? 'hero-banner-empty' : '';

const isMetadataMode = newState.sitecore?.context?.editMode === EditMode.Metadata;
this.modifyImageProps = !isMetadataMode
? {
...imageField,
editable: imageField?.editable
?.replace(`width="${imageField?.value?.width}"`, 'width="100%"')
.replace(`height="${imageField?.value?.height}"`, 'height="100%"'),
}
: {
...imageField,
value: {
...imageField?.value,
style: { width: '100%', height: '100%' },
},
};
});
}

ngOnDestroy() {
if (this.contextSubscription) {
this.contextSubscription.unsubscribe();
}
}

public get variant(): TemplateRef<any> {
return this.rendering.params?.FieldNames === 'Banner'
? this.bannerVariant
: this.defaultVariant;
}
}
1 change: 1 addition & 0 deletions packages/sitecore-jss-angular/src/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export {
ComponentFields,
ComponentParams,
getContentStylesheetLink,
EditMode,
} from '@sitecore-jss/sitecore-jss/layout';
export {
RetryStrategy,
Expand Down