-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[angular-xmcloud] Introduce angular SXA Image and Banner components (#…
…1886) * angular image component * banner component * some banner component fixes * minor update * update changelog * use templates for image variants * remove banner component * Update API docs [skip ci] * version v22.2.0-canary.25 [skip ci] * update yarn.lock [skip ci] * [Chore] Reverted yarn.lock, removed "yarn lock update" step from "pre-release canary publish", since exact version is already set --------- Co-authored-by: Automated Build <builds@sitecore.com> Co-authored-by: illiakovalenko <illia.kovalenko@sitecore.com>
- Loading branch information
1 parent
9f96769
commit bc24775
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
...-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
<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> |
67 changes: 67 additions & 0 deletions
67
...te-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters