-
Notifications
You must be signed in to change notification settings - Fork 275
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 navigation component (#1894)
* sxa navigation implementation * fix shared module * fix image component bug; rename variable in navigation item component * fix typo * update changelog * remove console logs
- Loading branch information
Showing
7 changed files
with
182 additions
and
2 deletions.
There are no files selected for viewing
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
21 changes: 21 additions & 0 deletions
21
...core-jss/src/templates/angular-xmcloud/src/app/components/app-components.shared.module.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,21 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { FormsModule } from '@angular/forms'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { RouterModule } from '@angular/router'; | ||
import { JssModule } from '@sitecore-jss/sitecore-jss-angular'; | ||
import { NavigationItemComponent } from './navigation/navigation-item.component'; | ||
|
||
/* | ||
This module is imported by the generated app-components.module.ts. | ||
You can use this module to provide shared Angular components that are not | ||
JSS components, etc to the generated module. | ||
Don't want code generation? See ./.gitignore for instructions to turn it off. | ||
*/ | ||
@NgModule({ | ||
imports: [CommonModule, TranslateModule, RouterModule, JssModule, FormsModule], | ||
exports: [CommonModule, TranslateModule, RouterModule, FormsModule, NavigationItemComponent], | ||
declarations: [NavigationItemComponent], | ||
}) | ||
export class AppComponentsSharedModule {} |
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
23 changes: 23 additions & 0 deletions
23
...rc/templates/angular-xmcloud/src/app/components/navigation/navigation-item.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,23 @@ | ||
<li [class]="cssClasses" [ngClass]="{ active: isActive }" tabIndex="0"> | ||
<div [ngClass]="{ 'navigation-title': true, child: hasChildren }" (click)="isActive = !isActive"> | ||
<a *scLink="linkField" (click)="onClick($event)"> | ||
<ng-container *ngIf="navItemFields.NavigationTitle" | ||
><span *scText="navItemFields.NavigationTitle"></span | ||
></ng-container> | ||
<ng-container *ngIf="!navItemFields.NavigationTitle && navItemFields.Title"> | ||
<span *scText="navItemFields.Title"></span | ||
></ng-container> | ||
<ng-container *ngIf="!navItemFields.NavigationTitle && !navItemFields.Title">{{ | ||
navItemFields.DisplayName | ||
}}</ng-container> | ||
</a> | ||
</div> | ||
<ul *ngIf="hasChildren" class="clearfix"> | ||
<app-navigation-item | ||
*ngFor="let childNavItemFields of navItemFields.Children" | ||
[navItemFields]="childNavItemFields" | ||
[relativeLevel]="childrenRelativeLevel" | ||
(childLinkClickEvent)="onClick($event)" | ||
></app-navigation-item> | ||
</ul> | ||
</li> |
65 changes: 65 additions & 0 deletions
65
.../src/templates/angular-xmcloud/src/app/components/navigation/navigation-item.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,65 @@ | ||
import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core'; | ||
import { LinkField } from '@sitecore-jss/sitecore-jss-angular'; | ||
import { Field } from '@sitecore-jss/sitecore-jss-angular'; | ||
|
||
export interface NavItemFields { | ||
Id: string; | ||
DisplayName: string; | ||
Title: Field<string>; | ||
NavigationTitle: Field<string>; | ||
Href: string; | ||
Querystring: string; | ||
Children: Array<NavItemFields>; | ||
Styles: string[]; | ||
} | ||
|
||
@Component({ | ||
selector: 'app-navigation-item', | ||
templateUrl: './navigation-item.component.html', | ||
}) | ||
export class NavigationItemComponent implements OnInit { | ||
@Input() navItemFields: NavItemFields; | ||
@Input() relativeLevel: number; | ||
@Output() childLinkClickEvent: EventEmitter<Event> = new EventEmitter<Event>(); | ||
cssClasses = ''; | ||
isActive = false; | ||
linkField = {}; | ||
childrenRelativeLevel = 0; | ||
hasChildren = false; | ||
|
||
constructor() {} | ||
|
||
ngOnInit() { | ||
this.cssClasses = `${this.navItemFields.Styles.concat('rel-level' + this.relativeLevel).join( | ||
' ' | ||
)}`; | ||
this.linkField = this.getLinkField(this.navItemFields); | ||
this.hasChildren = this.navItemFields.Children && this.navItemFields.Children.length != 0; | ||
this.childrenRelativeLevel = this.relativeLevel + 1; | ||
} | ||
|
||
onClick(event: Event) { | ||
this.childLinkClickEvent.emit(event); | ||
} | ||
|
||
private getLinkField = (navItemFields: NavItemFields): LinkField => ({ | ||
value: { | ||
href: navItemFields.Href, | ||
title: this.getLinkTitle(navItemFields), | ||
querystring: navItemFields.Querystring, | ||
}, | ||
}); | ||
|
||
private getLinkTitle = (navItemFields: NavItemFields): string | undefined => { | ||
let title; | ||
if (navItemFields.NavigationTitle?.value) { | ||
title = navItemFields.NavigationTitle.value.toString(); | ||
} else if (navItemFields.Title?.value) { | ||
title = navItemFields.Title.value.toString(); | ||
} else { | ||
title = navItemFields.DisplayName; | ||
} | ||
|
||
return title; | ||
}; | ||
} |
26 changes: 26 additions & 0 deletions
26
...jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation.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,26 @@ | ||
<div | ||
class="component navigation {{ styles }} {{ this.rendering.params?.GridParameters }}" | ||
[attr.id]="id" | ||
> | ||
<label class="menu-mobile-navigate-wrapper"> | ||
<input | ||
type="checkbox" | ||
class="menu-mobile-navigate" | ||
[checked]="isOpenMenu" | ||
(change)="toggleMenu($event)" | ||
/> | ||
<div class="menu-humburger"></div> | ||
<div class="component-content"> | ||
<nav> | ||
<ul class="clearfix"> | ||
<app-navigation-item | ||
*ngFor="let navItemFields of rendering.fields" | ||
[navItemFields]="navItemFields" | ||
[relativeLevel]="baseLevel" | ||
(childLinkClickEvent)="toggleMenu($event, false)" | ||
></app-navigation-item> | ||
</ul> | ||
</nav> | ||
</div> | ||
</label> | ||
</div> |
44 changes: 44 additions & 0 deletions
44
...e-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation.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,44 @@ | ||
import { Component, OnInit, OnDestroy } from '@angular/core'; | ||
import { Subscription } from 'rxjs'; | ||
import { SxaComponent } from '../sxa.component'; | ||
import { JssContextService } from '../../jss-context.service'; | ||
|
||
@Component({ | ||
selector: 'app-navigation', | ||
templateUrl: './navigation.component.html', | ||
}) | ||
export class NavigationComponent extends SxaComponent implements OnInit, OnDestroy { | ||
isEditing = false; | ||
private contextSubscription: Subscription; | ||
isOpenMenu = false; | ||
baseLevel = 1; | ||
|
||
constructor(private jssContext: JssContextService) { | ||
super(); | ||
} | ||
|
||
ngOnInit() { | ||
super.ngOnInit(); | ||
this.contextSubscription = this.jssContext.state.subscribe((newState) => { | ||
this.isEditing = newState.sitecore && newState.sitecore.context.pageEditing; | ||
}); | ||
} | ||
|
||
ngOnDestroy() { | ||
if (this.contextSubscription) { | ||
this.contextSubscription.unsubscribe(); | ||
} | ||
} | ||
|
||
toggleMenu(event: Event, flag?: boolean) { | ||
if (event && this.isEditing) { | ||
event.preventDefault(); | ||
} | ||
|
||
if (flag !== undefined) { | ||
this.isOpenMenu = flag; | ||
} | ||
|
||
this.isOpenMenu = !this.isOpenMenu; | ||
} | ||
} |