diff --git a/CHANGELOG.md b/CHANGELOG.md index b829d1937e..8ba3a6628f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ Our versioning strategy is as follows: * Column-Splitter ([#1889](https://github.com/Sitecore/jss/pull/1889)) * PartialDesignDynamicPlaceholder ([#1902](https://github.com/Sitecore/jss/pull/1902)) * Promo component ([#1897](https://github.com/Sitecore/jss/pull/1897)) + * Navigation ([#1894](https://github.com/Sitecore/jss/pull/1894)) ### 🛠 Breaking Change diff --git a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/app-components.shared.module.ts b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/app-components.shared.module.ts new file mode 100644 index 0000000000..58f484d505 --- /dev/null +++ b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/app-components.shared.module.ts @@ -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 {} diff --git a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.html b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.html index f69257719b..f25621e0f4 100644 --- a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.html +++ b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/image/image.component.html @@ -2,7 +2,7 @@
-
+
@@ -28,7 +28,7 @@ -
+
Image
diff --git a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation-item.component.html b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation-item.component.html new file mode 100644 index 0000000000..a975400ac6 --- /dev/null +++ b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation-item.component.html @@ -0,0 +1,23 @@ +
  • + +
      + +
    +
  • diff --git a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation-item.component.ts b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation-item.component.ts new file mode 100644 index 0000000000..7aa7ab5005 --- /dev/null +++ b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation-item.component.ts @@ -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; + NavigationTitle: Field; + Href: string; + Querystring: string; + Children: Array; + 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 = new EventEmitter(); + 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; + }; +} diff --git a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation.component.html b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation.component.html new file mode 100644 index 0000000000..055e7c4656 --- /dev/null +++ b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation.component.html @@ -0,0 +1,26 @@ + diff --git a/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation.component.ts b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation.component.ts new file mode 100644 index 0000000000..e9e20b0e9b --- /dev/null +++ b/packages/create-sitecore-jss/src/templates/angular-xmcloud/src/app/components/navigation/navigation.component.ts @@ -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; + } +}