Initial commit

This commit is contained in:
Torsten Brendgen
2026-04-13 10:26:01 +02:00
commit bc1258ae76
116 changed files with 30409 additions and 0 deletions
@@ -0,0 +1,474 @@
@import '~@microsoft/sp-office-ui-fabric-core/dist/sass/SPFabricCore.scss';
/*
Global override example:
:root {
--themePrimary: #00407f;
--themeLighterAlt: #f0f5fa;
--themeLighter: #c5d8eb;
--themeLight: #98b8d9;
--themeTertiary: #477db3;
--themeSecondary: #11508f;
--themeDarkAlt: #003973;
--themeDark: #003061;
--themeDarker: #002447;
--neutralLighterAlt: #faf9f8;
--neutralLighter: #f3f2f1;
--neutralLight: #edebe9;
--neutralQuaternaryAlt: #e1dfdd;
--neutralQuaternary: #d0d0d0;
--neutralTertiaryAlt: #c8c6c4;
--neutralTertiary: #a19f9d;
--neutralSecondary: #605e5c;
--neutralSecondaryAlt: #8a8886;
--neutralPrimaryAlt: #3b3a39;
--neutralPrimary: #323130;
--neutralDark: #201f1e;
--black: #000000;
--white: #ffffff;
--megaMenuNavBackground: #5f6f74;
--megaMenuNavHoverBackground: #6f8085;
--megaMenuNavOpenBackground: #728388;
--megaMenuNavTextColor: #ffffff;
--megaMenuNavDividerColor: rgba(255, 255, 255, 0.22);
--megaMenuPanelWidth: 1280px;
--megaMenuZIndex: 6000;
}
*/
$mm-white: var(--white, #{$ms-color-white});
$mm-panel-surface: var(--white, #{$ms-color-white});
$mm-panel-border: var(--neutralLight, #{$ms-color-neutralLight});
$mm-panel-divider: var(--neutralQuaternaryAlt, #{$ms-color-neutralQuaternaryAlt});
$mm-text: var(--neutralPrimary, #{$ms-color-neutralPrimary});
$mm-text-muted: var(--neutralSecondary, #{$ms-color-neutralSecondary});
$mm-nav-background: var(--megaMenuNavBackground, #5f6f74);
$mm-nav-hover-background: var(--megaMenuNavHoverBackground, #6f8085);
$mm-nav-open-background: var(--megaMenuNavOpenBackground, #728388);
$mm-nav-divider: var(--megaMenuNavDividerColor, rgba(255, 255, 255, 0.22));
$mm-nav-text: var(--megaMenuNavTextColor, #ffffff);
$mm-focus-accent: var(--themePrimary, #{$ms-color-themePrimary});
$mm-focus-soft: var(--themeLighterAlt, #{$ms-color-themeLighterAlt});
$mm-panel-shadow: var(--megaMenuPanelShadow, 0 14px 28px rgba(0, 0, 0, 0.18));
$mm-panel-width: var(--megaMenuPanelWidth, 1280px);
$mm-nav-height: var(--megaMenuTopNavHeight, 34px);
$mm-nav-inline-padding: var(--megaMenuNavInlinePadding, 8px);
$mm-item-inline-padding: var(--megaMenuItemInlinePadding, 12px);
$mm-column-padding-x: var(--megaMenuColumnPaddingX, 18px);
$mm-column-padding-y: var(--megaMenuColumnPaddingY, 16px);
$mm-link-radius: var(--megaMenuLinkRadius, 2px);
$mm-layer-index: var(--megaMenuZIndex, 6000);
:global {
:root {
--megaMenuNavBackground: #5f6f74;
--megaMenuNavHoverBackground: #6f8085;
--megaMenuNavOpenBackground: #728388;
--megaMenuNavTextColor: #ffffff;
--megaMenuNavDividerColor: rgba(255, 255, 255, 0.22);
--megaMenuTopNavHeight: 34px;
--megaMenuPanelWidth: 1280px;
--megaMenuPanelShadow: 0 14px 28px rgba(0, 0, 0, 0.18);
--megaMenuZIndex: 6000;
--megaMenuNavInlinePadding: 8px;
--megaMenuItemInlinePadding: 12px;
--megaMenuColumnPaddingX: 18px;
--megaMenuColumnPaddingY: 16px;
--megaMenuLinkRadius: 2px;
--megaMenuColumnCount: 4;
}
.skip-link {
position: absolute;
top: -40px;
left: 6px;
background: $mm-focus-accent;
color: $mm-white;
padding: 8px 12px;
text-decoration: none;
font-weight: 600;
border-radius: 4px;
z-index: 10000;
transition: top 0.2s ease;
}
.skip-link:focus {
top: 6px;
}
#CustomNavigation {
position: relative;
overflow: visible;
z-index: $mm-layer-index;
}
.mega-menu-main {
position: relative;
width: 100%;
background-color: #5f6f74;
background-color: $mm-nav-background;
border-bottom: 1px solid rgba(0, 0, 0, 0.18);
box-shadow: none;
overflow: visible;
z-index: $mm-layer-index;
}
.mega-menu-main > ul,
.mega-menu-top-level {
margin: 0 auto;
max-width: $mm-panel-width;
background-color: inherit;
width: 100%;
box-sizing: border-box;
list-style: none;
padding: 0 $mm-nav-inline-padding;
padding-inline-start: $mm-nav-inline-padding;
display: flex;
align-items: stretch;
justify-content: flex-start;
gap: 0;
}
.mega-menu-main > ul > li,
.mega-menu-top-item {
position: static;
display: flex;
align-items: stretch;
flex: 0 0 auto;
border-right: 1px solid $mm-nav-divider;
}
.mega-menu-main > ul > li:last-child,
.mega-menu-top-item:last-child {
border-right: 0;
}
.mega-menu-main > ul > li > a,
.mega-menu-main > ul > li > span[role="menuitem"],
.menu-item-link,
.menu-item-text {
position: relative;
display: flex;
align-items: center;
min-height: $mm-nav-height;
padding: 0 $mm-item-inline-padding;
color: $mm-nav-text;
font-size: 12px;
font-weight: 400;
line-height: 1;
text-decoration: none;
background: transparent;
border: 0;
transition: background-color 0.14s ease, color 0.14s ease;
outline: none;
white-space: nowrap;
}
.menu-item-text {
cursor: pointer;
}
.menu-item-has-children::after {
content: '';
width: 5px;
height: 5px;
margin-left: 8px;
border-right: 1px solid currentColor;
border-bottom: 1px solid currentColor;
transform: translateY(-1px) rotate(45deg);
transition: transform 0.14s ease;
}
.menu-item-has-children[aria-expanded="true"]::after {
transform: translateY(1px) rotate(-135deg);
}
.mega-menu-top-item.is-active > a,
.mega-menu-top-item.is-active > span,
.menu-item-link.is-active,
.menu-item-text.is-active {
background: $mm-nav-open-background;
}
.mega-menu-top-item.is-current > a,
.mega-menu-top-item.is-current > span,
.menu-item-link.is-current,
.menu-item-text.is-current {
font-weight: 600;
}
.mega-menu-main > ul > li:hover > a,
.mega-menu-main > ul > li:hover > span[role="menuitem"],
.mega-menu-main > ul > li > a[aria-expanded="true"],
.mega-menu-main > ul > li > span[aria-expanded="true"],
.menu-item-link:hover,
.menu-item-text:hover {
color: $mm-nav-text;
background: $mm-nav-hover-background;
}
.mega-menu-main > ul > li > a[aria-expanded="true"],
.mega-menu-main > ul > li > span[aria-expanded="true"] {
background: $mm-nav-open-background;
}
.mega-menu-main > ul > li > a:focus,
.mega-menu-main > ul > li > span[role="menuitem"]:focus,
.menu-item-link:focus,
.menu-item-text:focus {
color: $mm-nav-text;
background: $mm-nav-open-background;
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.65);
}
.mega-menu {
position: absolute;
left: 0;
right: 0;
top: 100%;
display: none;
visibility: hidden;
opacity: 0;
background-color: #ffffff;
background-color: $mm-panel-surface;
background-image: none;
mix-blend-mode: normal;
border: 1px solid $mm-panel-border;
border-top: 0;
box-shadow: $mm-panel-shadow;
max-height: calc(100vh - #{$mm-nav-height});
isolation: isolate;
overflow-y: auto;
overflow-x: hidden;
z-index: calc(#{$mm-layer-index} + 1);
transition: opacity 0.16s ease, visibility 0.16s ease;
}
.mega-menu-main > ul > li:hover .mega-menu,
.mega-menu.js-open {
visibility: visible;
opacity: 1;
display: block;
background-color: #ffffff;
background-color: $mm-panel-surface;
background-image: none;
}
.mega-menu-grid {
max-width: $mm-panel-width;
background-color: inherit;
margin: 0 auto;
display: grid;
grid-template-columns: repeat(var(--megaMenuColumnCount, 4), minmax(0, 1fr));
gap: 0;
align-items: start;
}
.mega-menu-category {
min-width: 0;
padding: $mm-column-padding-y $mm-column-padding-x 14px;
border-right: 1px solid $mm-panel-divider;
}
.mega-menu-grid > .mega-menu-category:last-child {
border-right: 0;
}
.mega-menu-category > h3,
.mega-menu-category-title {
margin: 0 0 10px;
padding: 0;
border: 0;
}
.mega-menu-category > h3 > span,
.mega-menu-category > h3 > a {
display: block;
color: $mm-text;
font-size: 13px;
font-weight: 600;
line-height: 18px;
text-decoration: none;
}
.mega-menu-category.is-active > h3 > span,
.mega-menu-category.is-active > h3 > a,
.mega-menu-category > h3 > span.is-active,
.mega-menu-category > h3 > a.is-active {
color: $mm-text;
}
.mega-menu-category.is-current > h3 > a,
.mega-menu-category > h3 > a.is-current {
text-decoration: underline;
}
.mega-menu-category > h3 > a:hover {
color: $mm-text;
text-decoration: underline;
}
.mega-menu-category > h3 > a:focus {
background: $mm-focus-soft;
color: $mm-text;
border-radius: 2px;
box-shadow: 0 0 0 2px $mm-focus-accent;
padding: 1px 4px;
margin: -1px -4px;
}
.mega-menu-category ul,
.mega-menu-links {
list-style: none;
margin: 0;
padding-inline-start: 0;
}
.mega-menu-category ul li,
.mega-menu-links li {
margin: 0 0 6px;
}
.mega-menu-category ul li:last-child,
.mega-menu-links li:last-child {
margin-bottom: 0;
}
.mega-menu-category ul li a,
.mega-menu-links li a {
display: block;
margin: 0;
padding: 1px 2px;
border-radius: $mm-link-radius;
color: $mm-text-muted;
font-size: 12px;
font-weight: 400;
line-height: 18px;
text-decoration: none;
transition: background-color 0.14s ease, color 0.14s ease, box-shadow 0.14s ease;
}
.mega-menu-category ul li.is-active > a,
.mega-menu-links li.is-active > a,
.mega-menu-category ul li a.is-active,
.mega-menu-links li a.is-active {
color: $mm-text;
}
.mega-menu-category ul li.is-current > a,
.mega-menu-links li.is-current > a,
.mega-menu-category ul li a.is-current,
.mega-menu-links li a.is-current {
color: $mm-text;
font-weight: 600;
text-decoration: underline;
}
.mega-menu-category ul li a:hover,
.mega-menu-links li a:hover {
background: transparent;
color: $mm-text;
text-decoration: underline;
}
.mega-menu-category ul li a:focus,
.mega-menu-links li a:focus {
background: $mm-focus-soft;
color: $mm-text;
box-shadow: 0 0 0 2px $mm-focus-accent;
outline: none;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 1024px) {
.mega-menu-grid {
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.mega-menu-category {
padding: 14px 16px 12px;
}
}
@media (max-width: 767px) {
.mega-menu-main > ul,
.mega-menu-top-level {
padding: 0 6px;
padding-inline-start: 6px;
flex-wrap: wrap;
}
.mega-menu-main > ul > li,
.mega-menu-top-item {
border-right: 0;
}
.mega-menu-main > ul > li > a,
.mega-menu-main > ul > li > span[role="menuitem"],
.menu-item-link,
.menu-item-text {
min-height: 36px;
padding: 0 10px;
font-size: 12px;
}
.mega-menu {
border-left: 0;
border-right: 0;
}
.mega-menu-grid {
grid-template-columns: 1fr;
}
.mega-menu-category {
padding: 14px 12px;
border-right: 0;
border-bottom: 1px solid $mm-panel-divider;
}
.mega-menu-grid > .mega-menu-category:last-child {
border-bottom: 0;
}
}
@media (prefers-reduced-motion: reduce) {
.skip-link,
.menu-item-has-children::after,
.mega-menu-main > ul > li > a,
.mega-menu-main > ul > li > span[role="menuitem"],
.menu-item-link,
.menu-item-text,
.mega-menu,
.mega-menu-category ul li a,
.mega-menu-links li a {
transition: none;
}
}
@media (prefers-contrast: high) {
.mega-menu-main > ul > li > a:focus,
.mega-menu-main > ul > li > span[role="menuitem"]:focus,
.menu-item-link:focus,
.menu-item-text:focus,
.mega-menu-category > h3 > a:focus,
.mega-menu-category ul li a:focus,
.mega-menu-links li a:focus {
outline: 2px solid;
outline-offset: 2px;
box-shadow: none;
}
}
} /* End :global */
@@ -0,0 +1,9 @@
/* tslint:disable */
require('./MegaMenu.module.css');
const styles = {
mmSlideIn: 'mmSlideIn_09a8e1a7',
mmFadeIn: 'mmFadeIn_09a8e1a7',
};
export default styles;
/* tslint:enable */
@@ -0,0 +1,17 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx/client-side-extension-manifest.schema.json",
"id": "abc3361f-bb2d-491f-aba3-cd51c19a299b",
"alias": "MegaMenuApplicationCustomizer",
"componentType": "Extension",
"extensionType": "ApplicationCustomizer",
// The "*" signifies that the version should be taken from the package.json
"version": "*",
"manifestVersion": 2,
// If true, the component can only be installed on sites where Custom Script is allowed.
// Components that allow authors to embed arbitrary script code should set this to true.
// https://support.office.com/en-us/article/Turn-scripting-capabilities-on-or-off-1f2c515f-5d7e-448a-9fd7-835da935584f
"requiresCustomScript": false
}
@@ -0,0 +1,158 @@
// tslint:disable:max-line-length
// tslint:disable:match-default-export-name
// tslint:disable:typedef
// tslint:disable:variable-name
import { override } from '@microsoft/decorators';
import {
BaseApplicationCustomizer,
PlaceholderContent,
PlaceholderName
} from '@microsoft/sp-application-base';
import * as strings from 'MegaMenuApplicationCustomizerStrings';
import { TaxonomyNavigationService } from '../../services/TaxonomyNavigationService';
import { MegaMenuRenderer } from './MegaMenuRenderer';
import { IMenuItem } from '../../services/IMenuItem';
import { debugError, debugLog, debugWarn } from '../../services/MegaMenuDebug';
import './MegaMenu.module.scss';
const LOG_SOURCE: string = 'MegaMenuApplicationCustomizer';
export const UserCustomActionMegaMenuId: string = 'abc3361f-bb2d-491f-aba3-cd51c19a299b';
export interface IMegaMenuApplicationCustomizerProperties {
termSetName: string;
cssUrl?: string;
debug?: boolean;
}
export default class MegaMenuApplicationCustomizer
extends BaseApplicationCustomizer<IMegaMenuApplicationCustomizerProperties> {
private _topPlaceholder: PlaceholderContent | undefined;
@override
public onInit(): Promise<void> {
debugLog(this._isDebugEnabled(), LOG_SOURCE, 'Initialized ' + strings.Title);
if (this.properties.cssUrl) {
this._loadExternalCss(this.properties.cssUrl);
}
this.context.placeholderProvider.changedEvent.add(this, this._renderPlaceHolders);
this._renderPlaceHolders();
return Promise.resolve();
}
private _renderPlaceHolders(): void {
const availablePlaceholders: string = this.context.placeholderProvider.placeholderNames
.map(name => PlaceholderName[name])
.join(', ');
debugLog(this._isDebugEnabled(), LOG_SOURCE, 'Available placeholders:', availablePlaceholders);
if (!this._topPlaceholder) {
this._topPlaceholder = this.context.placeholderProvider.tryCreateContent(
PlaceholderName.Top,
{ onDispose: this._onDispose }
);
if (!this._topPlaceholder) {
debugError(this._isDebugEnabled(), LOG_SOURCE, 'The expected placeholder (Top) was not found.');
return;
}
if (!this.properties.termSetName) {
debugWarn(this._isDebugEnabled(), LOG_SOURCE, 'No termSetName configured. Mega Menu rendering is skipped.');
return;
}
this._renderMegaMenu(this.properties.termSetName);
}
}
private async _renderMegaMenu(termSetName: string, debug: boolean = this._isDebugEnabled()): Promise<void> {
if (!this._topPlaceholder) {
return;
}
try {
const taxonomyService: TaxonomyNavigationService = new TaxonomyNavigationService(
this.context,
termSetName,
debug
);
const menuItems: IMenuItem[] = await taxonomyService.getMenuItems();
const renderer: MegaMenuRenderer = new MegaMenuRenderer(
menuItems,
debug
);
const container = this._getOrCreateContainer('CustomHeader', this._topPlaceholder);
if (container) {
renderer.render(container);
} else {
renderer.render(this._topPlaceholder.domElement);
}
debugLog(debug, LOG_SOURCE, 'MegaMenu rendered successfully with ' + menuItems.length + ' top-level items.');
} catch (error) {
debugError(debug, LOG_SOURCE, 'Error rendering MegaMenu.', error);
}
}
private _getOrCreateContainer(id: string, placeholder: PlaceholderContent): HTMLElement {
const container = document.getElementById(id);
if (container) {
const div = document.createElement('div');
container.appendChild(div);
return div;
}
return placeholder.domElement;
}
private _loadExternalCss(cssUrl?: string, debug: boolean = this._isDebugEnabled()): void {
const externalCssLinkId: string = 'mega-menu-additional-css-34FAB720';
let link: HTMLLinkElement = document.getElementById(externalCssLinkId) as HTMLLinkElement;
if (cssUrl && cssUrl.trim() !== '') {
if (!link) {
const head: HTMLHeadElement = document.getElementsByTagName('head')[0];
link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.id = externalCssLinkId;
link.onload = () => {
debugLog(debug, LOG_SOURCE, 'External CSS loaded successfully from:', cssUrl);
};
link.onerror = () => {
debugWarn(debug, LOG_SOURCE, 'Failed to load external CSS from:', cssUrl);
};
head.appendChild(link);
}
link.href = cssUrl;
} else if (link) {
link.remove();
}
}
private _onDispose = (): void => {
debugLog(this._isDebugEnabled(), LOG_SOURCE, 'Disposed custom top placeholder.');
}
private _isDebugEnabled(debugOverride?: boolean): boolean {
if (typeof debugOverride === 'boolean') {
return debugOverride;
}
return !!(this.properties && this.properties.debug === true);
}
}
+510
View File
@@ -0,0 +1,510 @@
// tslint:disable:max-line-length
// tslint:disable:match-default-export-name
// tslint:disable:typedef
// tslint:disable:variable-name
import { IMenuItem } from '../../services/IMenuItem';
import { debugLog } from '../../services/MegaMenuDebug';
import styles from './MegaMenu.module.scss'; // tslint:disable-line:no-unused-variable
const LOG_SOURCE: string = 'MegaMenuRenderer';
export class MegaMenuRenderer {
private static readonly hoverOpenDelayMs: number = 140;
private static readonly hoverCloseDelayMs: number = 180;
constructor(
private menuItems: IMenuItem[],
private debug: boolean = false
) { }
public render(container: HTMLElement) {
container.innerHTML = '';
container.id = 'CustomNavigation';
const nav = document.createElement('nav');
nav.id = 'Mega-Menu';
nav.className = 'mega-menu-main';
nav.setAttribute('role', 'navigation');
nav.setAttribute('aria-label', 'Hauptnavigation');
const topLevelUl = document.createElement('ul');
topLevelUl.className = 'mega-menu-top-level';
topLevelUl.setAttribute('role', 'menubar');
this.menuItems.forEach(topLevelItem => {
const topLevelLi = this.createTopLevelItem(topLevelItem);
topLevelUl.appendChild(topLevelLi);
});
nav.appendChild(topLevelUl);
container.appendChild(nav);
this.attachEventListeners();
this.createScreenReaderAnnouncer();
}
private createTopLevelItem(item: IMenuItem): HTMLLIElement {
const li = document.createElement('li');
const hasChildren = item.hasChildren() && item.items && item.items.length > 0;
const isActive = this.isItemActive(item);
const isCurrent = this.isCurrentUrl(item.url);
li.className = this.joinClasses(
'mega-menu-top-item',
hasChildren ? 'has-children' : undefined,
isActive ? 'is-active' : undefined,
isCurrent ? 'is-current' : undefined
);
li.setAttribute('role', 'none');
const topElement = this.createTopLevelElement(item, hasChildren, isActive, isCurrent);
li.appendChild(topElement);
if (hasChildren) {
const megaMenu = this.createMegaMenu(item);
li.appendChild(megaMenu);
}
return li;
}
private createTopLevelElement(item: IMenuItem, hasChildren: boolean, isActive: boolean, isCurrent: boolean): HTMLElement {
let element: HTMLElement;
if (item.url) {
element = document.createElement('a');
(element as HTMLAnchorElement).href = item.url;
element.className = 'menu-item-link';
} else {
element = document.createElement('span');
element.className = 'menu-item-text';
element.setAttribute('tabindex', '0');
}
element.setAttribute('role', 'menuitem');
element.textContent = item.label;
element.className = this.joinClasses(
element.className,
hasChildren ? 'menu-item-has-children' : undefined,
isActive ? 'is-active' : undefined,
isCurrent ? 'is-current' : undefined
);
if (hasChildren) {
element.setAttribute('aria-haspopup', 'true');
element.setAttribute('aria-expanded', 'false');
}
if (isCurrent && item.url) {
element.setAttribute('aria-current', 'page');
}
if (item.hoverText) {
element.title = item.hoverText;
}
return element;
}
private createMegaMenu(parentItem: IMenuItem): HTMLDivElement {
const megaMenuDiv = document.createElement('div');
megaMenuDiv.className = 'mega-menu';
megaMenuDiv.setAttribute('role', 'menu');
megaMenuDiv.setAttribute('aria-expanded', 'false');
megaMenuDiv.setAttribute('aria-label', parentItem.label + ' Unterkategorien');
const gridDiv = document.createElement('div');
gridDiv.className = 'mega-menu-grid';
gridDiv.style.setProperty('--megaMenuColumnCount', this.getColumnCount(parentItem.items));
if (parentItem.items) {
parentItem.items.forEach(secondLevelItem => {
const categoryDiv = this.createCategorySection(secondLevelItem);
gridDiv.appendChild(categoryDiv);
});
}
megaMenuDiv.appendChild(gridDiv);
return megaMenuDiv;
}
private createCategorySection(item: IMenuItem): HTMLDivElement {
const categoryDiv = document.createElement('div');
const isActive = this.isItemActive(item);
const isCurrent = this.isCurrentUrl(item.url);
categoryDiv.className = this.joinClasses(
'mega-menu-category',
isActive ? 'is-active' : undefined,
isCurrent ? 'is-current' : undefined
);
const h3 = document.createElement('h3');
h3.className = 'mega-menu-category-title';
if (item.url) {
const link = document.createElement('a');
link.href = item.url;
link.textContent = item.label;
link.className = this.joinClasses(
isActive ? 'is-active' : undefined,
isCurrent ? 'is-current' : undefined
);
if (isCurrent) {
link.setAttribute('aria-current', 'page');
}
if (item.hoverText) {
link.title = item.hoverText;
}
h3.appendChild(link);
} else {
const span = document.createElement('span');
span.textContent = item.label;
span.className = this.joinClasses(
isActive ? 'is-active' : undefined
);
if (item.hoverText) {
span.title = item.hoverText;
}
h3.appendChild(span);
}
categoryDiv.appendChild(h3);
if (item.hasChildren() && item.items && item.items.length > 0) {
const ul = document.createElement('ul');
ul.className = 'mega-menu-links';
item.items.forEach(thirdLevelItem => {
const li = document.createElement('li');
const link = document.createElement('a');
const isLinkActive = this.isItemActive(thirdLevelItem);
const isLinkCurrent = this.isCurrentUrl(thirdLevelItem.url);
li.className = this.joinClasses(
isLinkActive ? 'is-active' : undefined,
isLinkCurrent ? 'is-current' : undefined
);
link.href = thirdLevelItem.url || '#';
link.textContent = thirdLevelItem.label;
link.className = this.joinClasses(
isLinkActive ? 'is-active' : undefined,
isLinkCurrent ? 'is-current' : undefined
);
if (isLinkCurrent && thirdLevelItem.url) {
link.setAttribute('aria-current', 'page');
}
if (thirdLevelItem.hoverText) {
link.title = thirdLevelItem.hoverText;
}
li.appendChild(link);
ul.appendChild(li);
});
categoryDiv.appendChild(ul);
}
return categoryDiv;
}
private attachEventListeners(): void {
const headings = document.querySelectorAll('#Mega-Menu > ul > li > a, #Mega-Menu > ul > li > span[role="menuitem"]');
for (let i = 0; i < headings.length; i++) {
const heading = headings[i] as HTMLElement;
const megaMenu = heading.nextElementSibling as HTMLElement;
if (megaMenu && megaMenu.classList.contains('mega-menu')) {
this.attachKeyboardNavigation(heading, megaMenu);
this.attachMouseEvents(heading, megaMenu);
this.attachFocusManagement(heading, megaMenu);
}
}
this.attachGlobalKeyboardNavigation();
}
private attachKeyboardNavigation(heading: HTMLElement, megaMenu: HTMLElement): void {
heading.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.key === 'Enter') {
if (heading.tagName === 'A') {
return;
}
e.preventDefault();
this.toggleMegaMenu(heading, megaMenu);
} else if (e.key === ' ') {
e.preventDefault();
this.toggleMegaMenu(heading, megaMenu);
} else if (e.key === 'ArrowDown') {
e.preventDefault();
this.openMegaMenu(heading, megaMenu);
this.focusFirstLink(megaMenu);
} else if (e.key === 'ArrowUp') {
e.preventDefault();
this.closeMegaMenu(heading, megaMenu);
} else if (e.key === 'Escape') {
e.preventDefault();
this.closeMegaMenu(heading, megaMenu);
heading.focus();
}
});
if (heading.tagName === 'A') {
heading.addEventListener('click', () => {
debugLog(this.debug, LOG_SOURCE, 'Link clicked:', (heading as HTMLAnchorElement).href);
});
}
heading.addEventListener('focus', () => {
debugLog(this.debug, LOG_SOURCE, 'Focus moved to:', heading.textContent);
});
}
private attachMouseEvents(heading: HTMLElement, megaMenu: HTMLElement): void {
const parentLi = heading.parentElement as HTMLElement;
let openTimeout: number | undefined;
let closeTimeout: number | undefined;
const clearOpenTimeout = () => {
if (typeof openTimeout === 'number') {
window.clearTimeout(openTimeout);
openTimeout = undefined;
}
};
const clearCloseTimeout = () => {
if (typeof closeTimeout === 'number') {
window.clearTimeout(closeTimeout);
closeTimeout = undefined;
}
};
parentLi.addEventListener('mouseenter', () => {
clearCloseTimeout();
if (megaMenu.classList.contains('js-open')) {
return;
}
clearOpenTimeout();
openTimeout = window.setTimeout(() => {
this.openMegaMenu(heading, megaMenu);
openTimeout = undefined;
}, MegaMenuRenderer.hoverOpenDelayMs);
});
parentLi.addEventListener('mouseleave', () => {
clearOpenTimeout();
clearCloseTimeout();
closeTimeout = window.setTimeout(() => {
this.closeMegaMenu(heading, megaMenu);
closeTimeout = undefined;
}, MegaMenuRenderer.hoverCloseDelayMs);
});
heading.addEventListener('focus', () => {
clearCloseTimeout();
});
megaMenu.addEventListener('mouseenter', () => {
clearCloseTimeout();
});
}
private attachFocusManagement(heading: HTMLElement, megaMenu: HTMLElement): void {
megaMenu.addEventListener('focusout', () => {
setTimeout(() => {
const focusedElement = document.activeElement as HTMLElement;
const isInsideThisMenu = megaMenu.contains(focusedElement);
const isOnThisTrigger = focusedElement === heading;
const isInAnyMegaMenu = focusedElement.closest('.mega-menu');
const isOnAnyTopLevel = focusedElement.closest('#Mega-Menu > ul > li > a, #Mega-Menu > ul > li > span');
if (!isInsideThisMenu && !isOnThisTrigger && !isInAnyMegaMenu && !isOnAnyTopLevel) {
debugLog(this.debug, LOG_SOURCE, 'Closing menu because focus left the navigation.');
this.closeMegaMenu(heading, megaMenu);
}
}, 150);
});
}
private attachGlobalKeyboardNavigation(): void {
document.addEventListener('keydown', (e: KeyboardEvent) => {
const activeElement = document.activeElement as HTMLElement;
if (e.key === 'Escape') {
const openMenu = document.querySelector('.mega-menu[aria-expanded="true"]') as HTMLElement;
if (openMenu) {
const triggerLink = openMenu.previousElementSibling as HTMLElement;
this.closeMegaMenu(triggerLink, openMenu);
triggerLink.focus();
}
}
if (e.key === 'Tab') {
if (!e.shiftKey) {
const currentTopLevel = activeElement.closest('#Mega-Menu > ul > li > a, #Mega-Menu > ul > li > span') as HTMLElement;
if (currentTopLevel) {
const parentLi = currentTopLevel.closest('li') as HTMLElement;
const megaMenu = parentLi.querySelector('.mega-menu.js-open') as HTMLElement;
if (megaMenu) {
e.preventDefault();
const firstLink = megaMenu.querySelector('a') as HTMLElement;
if (firstLink) {
firstLink.focus();
}
return;
}
}
}
if (e.shiftKey) {
const megaMenu = activeElement.closest('.mega-menu') as HTMLElement;
if (megaMenu && megaMenu.classList.contains('js-open')) {
const allLinksInMenu = megaMenu.querySelectorAll('a');
const firstLinkInMenu = allLinksInMenu[0] as HTMLElement;
if (activeElement === firstLinkInMenu) {
e.preventDefault();
const triggerElement = megaMenu.previousElementSibling as HTMLElement;
triggerElement.focus();
return;
}
}
}
}
});
}
private openMegaMenu(trigger: HTMLElement, menu: HTMLElement): void {
this.closeAllMegaMenus(menu);
trigger.setAttribute('aria-expanded', 'true');
menu.setAttribute('aria-expanded', 'true');
menu.classList.add('js-open');
debugLog(this.debug, LOG_SOURCE, 'Menu opened:', trigger.textContent);
}
private closeMegaMenu(trigger: HTMLElement, menu: HTMLElement): void {
trigger.setAttribute('aria-expanded', 'false');
menu.setAttribute('aria-expanded', 'false');
menu.classList.remove('js-open');
debugLog(this.debug, LOG_SOURCE, 'Menu closed:', trigger.textContent);
}
private toggleMegaMenu(trigger: HTMLElement, menu: HTMLElement): void {
const isOpen = trigger.getAttribute('aria-expanded') === 'true';
if (isOpen) {
this.closeMegaMenu(trigger, menu);
} else {
this.openMegaMenu(trigger, menu);
}
}
private closeAllMegaMenus(exceptMenu?: HTMLElement): void {
const allTriggers = document.querySelectorAll('#Mega-Menu > ul > li > a[aria-expanded="true"], #Mega-Menu > ul > li > span[aria-expanded="true"]');
for (let i = 0; i < allTriggers.length; i++) {
const trigger = allTriggers[i] as HTMLElement;
const menu = trigger.nextElementSibling as HTMLElement;
if (menu && menu !== exceptMenu) {
this.closeMegaMenu(trigger, menu);
}
}
}
private focusFirstLink(megaMenu: HTMLElement): void {
const firstLink = megaMenu.querySelector('a') as HTMLElement;
if (firstLink) {
firstLink.focus();
}
}
private isItemActive(item: IMenuItem): boolean {
if (this.isCurrentUrl(item.url)) {
return true;
}
if (!item.items || item.items.length === 0) {
return false;
}
for (let i = 0; i < item.items.length; i++) {
if (this.isItemActive(item.items[i])) {
return true;
}
}
return false;
}
private isCurrentUrl(url?: string): boolean {
const currentPath = this.normalizePath(window.location.href);
const itemPath = this.normalizePath(url);
if (!currentPath || !itemPath) {
return false;
}
if (itemPath === '/') {
return currentPath === '/';
}
if (currentPath === itemPath) {
return true;
}
return currentPath.indexOf(itemPath + '/') === 0;
}
private normalizePath(url?: string): string {
if (!url) {
return '';
}
try {
const parsedUrl = new URL(url, window.location.origin);
const pathname = (parsedUrl.pathname || '/').replace(/\/+$/, '');
return (pathname || '/').toLowerCase();
} catch (error) {
return '';
}
}
private getColumnCount(items?: IMenuItem[]): string {
const itemCount = items ? items.length : 0;
const columnCount = Math.min(Math.max(itemCount, 1), 4);
return columnCount.toString();
}
private joinClasses(...classNames: Array<string | undefined>): string {
const filteredClassNames: string[] = [];
for (let i = 0; i < classNames.length; i++) {
if (classNames[i]) {
filteredClassNames.push(classNames[i] as string);
}
}
return filteredClassNames.join(' ');
}
private createScreenReaderAnnouncer(): void {
if (document.getElementById('mega-menu-sr-announcer')) {
return;
}
const srAnnouncer = document.createElement('div');
srAnnouncer.id = 'mega-menu-sr-announcer';
srAnnouncer.setAttribute('aria-live', 'polite');
srAnnouncer.setAttribute('aria-atomic', 'true');
srAnnouncer.className = 'sr-only';
document.body.appendChild(srAnnouncer);
debugLog(this.debug, LOG_SOURCE, 'Screen reader announcer is ready.');
}
}
+210
View File
@@ -0,0 +1,210 @@
// tslint:disable:max-line-length export-name
import { ApplicationCustomizerContext } from '@microsoft/sp-application-base';
import { IMegaMenuApplicationCustomizerProperties, UserCustomActionMegaMenuId } from './MegaMenuApplicationCustomizer';
import { UserCustomActionService } from '../../services/UserCustomActionService/UserCustomActionService';
import { UserCustomActionScope } from '../../services/UserCustomActionService/UserCustomActionScope';
import { IUserCustomActionProps } from '../../services/UserCustomActionService/IUserCustomActionProps';
import { debugError } from '../../services/MegaMenuDebug';
const LOG_SOURCE: string = 'MegaMenuSettingsPanel';
export class MegaMenuSettingsPanel {
private _service: UserCustomActionService;
private _ucaId: string;
private _panelElement: HTMLElement | undefined = undefined;
private _overlayElement: HTMLElement | undefined = undefined;
constructor(
private context: ApplicationCustomizerContext,
private dataUpdated: (data: IMegaMenuApplicationCustomizerProperties) => void,
private debug: boolean = false
) {
this._service = new UserCustomActionService(this.context, this.debug);
}
public async open(): Promise<void> {
if (this._panelElement) {
return;
}
const currentProps: IMegaMenuApplicationCustomizerProperties = await this.readApplicationCustomizerProps();
this._createPanel(currentProps);
}
public close(): void {
if (this._panelElement) {
this._panelElement.remove();
this._panelElement = undefined;
}
if (this._overlayElement) {
this._overlayElement.remove();
this._overlayElement = undefined;
}
document.body.focus();
document.body.blur();
}
private async readApplicationCustomizerProps(): Promise<IMegaMenuApplicationCustomizerProperties> {
const ucas: IUserCustomActionProps[] = await this._service.getUserCustomActions(UserCustomActionScope.Site);
const candidates: IUserCustomActionProps[] = ucas.filter(uca => uca.ClientSideComponentId === UserCustomActionMegaMenuId);
if (candidates.length) {
const uca: IUserCustomActionProps = candidates[0];
this._ucaId = uca.Id;
if (uca.ClientSideComponentProperties) {
return JSON.parse(uca.ClientSideComponentProperties) as IMegaMenuApplicationCustomizerProperties;
}
return {
termSetName: '',
cssUrl: '',
debug: false
};
}
debugError(this.debug, LOG_SOURCE, 'UserCustomAction for the MegaMenu was not found.');
return {
termSetName: '',
cssUrl: '',
debug: false
};
}
private async saveApplicationCustomizerProps(componentProps: IMegaMenuApplicationCustomizerProperties): Promise<void> {
try {
const newUserCustomActionsProperty: {} = {
ClientSideComponentProperties: JSON.stringify(componentProps)
};
await this._service.updateUserCustomAction(UserCustomActionScope.Site, this._ucaId, newUserCustomActionsProperty);
this.debug = componentProps.debug === true;
this._service = new UserCustomActionService(this.context, this.debug);
this.dataUpdated(componentProps);
} catch (e) {
debugError(this.debug, LOG_SOURCE, 'Error saving MegaMenu settings.', e);
}
}
private _createPanel(props: IMegaMenuApplicationCustomizerProperties): void {
const overlay: HTMLElement = document.createElement('div');
overlay.className = 'mm-settings-overlay';
overlay.tabIndex = -1;
overlay.onclick = () => this.close();
this._overlayElement = overlay;
const panel: HTMLElement = document.createElement('div');
panel.className = 'mm-settings-panel';
panel.setAttribute('role', 'dialog');
panel.setAttribute('aria-modal', 'true');
panel.setAttribute('aria-label', 'MegaMenu Einstellungen');
panel.innerHTML = this._getMarkup(props.termSetName, props.cssUrl || '', props.debug === true);
this._panelElement = panel;
document.body.appendChild(overlay);
document.body.appendChild(panel);
const closeBtn: HTMLButtonElement = panel.querySelector('.mm-settings-close') as HTMLButtonElement;
const cancelBtn: HTMLButtonElement = panel.querySelector('.mm-settings-cancel') as HTMLButtonElement;
const saveBtn: HTMLButtonElement = panel.querySelector('.mm-settings-save') as HTMLButtonElement;
const firstInput: HTMLInputElement = panel.querySelector('#mm-setting-termset') as HTMLInputElement;
if (closeBtn) { closeBtn.onclick = () => this.close(); }
if (cancelBtn) { cancelBtn.onclick = () => this.close(); }
if (saveBtn) { saveBtn.onclick = () => this._save(); }
panel.addEventListener('keydown', (e: KeyboardEvent) => {
if (e.key === 'Escape') {
e.preventDefault();
this.close();
} else if (e.key === 'Tab') {
this._trapFocus(e);
}
});
setTimeout(() => {
if (firstInput) {
firstInput.focus();
}
}, 0);
}
private _getMarkup(termSet: string, cssUrl: string, debug: boolean): string {
return `<div class='mm-settings-header'>
<h2 class='mm-settings-title'>Einstellungen</h2>
<button type='button' class='mm-settings-close' aria-label='Schliessen'>&times;</button>
</div>
<div class='mm-settings-body'>
<div class='mm-settings-field'>
<label for='mm-setting-termset'>Name des Navigations-Termsets</label>
<input id='mm-setting-termset' type='text' value='${this._escape(termSet)}' />
</div>
<div class='mm-settings-field'>
<label for='mm-setting-css'>Pfad zu zusaetzlicher CSS-Datei</label>
<input id='mm-setting-css' type='text' value='${this._escape(cssUrl)}' />
</div>
<div class='mm-settings-field'>
<label for='mm-setting-debug'>
<input id='mm-setting-debug' type='checkbox' ${debug ? 'checked' : ''} />
Debug-Ausgaben in der Browser-Konsole aktivieren
</label>
</div>
</div>
<div class='mm-settings-footer'>
<button type='button' class='mm-settings-save ms-Button ms-Button--primary'><span>Speichern</span></button>
<button type='button' class='mm-settings-cancel ms-Button'><span>Abbrechen</span></button>
</div>`;
}
private _save(): void {
if (!this._panelElement) {
return;
}
const termSetInput: HTMLInputElement = this._panelElement.querySelector('#mm-setting-termset') as HTMLInputElement;
const cssInput: HTMLInputElement = this._panelElement.querySelector('#mm-setting-css') as HTMLInputElement;
const debugInput: HTMLInputElement = this._panelElement.querySelector('#mm-setting-debug') as HTMLInputElement;
this.saveApplicationCustomizerProps({
termSetName: termSetInput && termSetInput.value ? termSetInput.value : '',
cssUrl: cssInput && cssInput.value ? cssInput.value : '',
debug: !!(debugInput && debugInput.checked)
});
this.close();
}
private _trapFocus(e: KeyboardEvent): void {
if (!this._panelElement) {
return;
}
const focusable: NodeListOf<Element> = this._panelElement.querySelectorAll('button, input');
if (!focusable || focusable.length === 0) {
return;
}
const first: HTMLElement = focusable[0] as HTMLElement;
const last: HTMLElement = focusable[focusable.length - 1] as HTMLElement;
const active: HTMLElement = document.activeElement as HTMLElement;
if (e.shiftKey && active === first) {
e.preventDefault();
last.focus();
} else if (!e.shiftKey && active === last) {
e.preventDefault();
first.focus();
}
}
private _escape(value: string): string {
if (value) {
return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
return '';
}
}
+5
View File
@@ -0,0 +1,5 @@
define([], function() {
return {
"Title": "MegaMenuApplicationCustomizer"
}
});
+8
View File
@@ -0,0 +1,8 @@
declare interface IMegaMenuApplicationCustomizerStrings {
Title: string;
}
declare module 'MegaMenuApplicationCustomizerStrings' {
const myStrings: IMegaMenuApplicationCustomizerStrings;
export = myStrings;
}