Initial commit
This commit is contained in:
474
src/extensions/megaMenu/MegaMenu.module.scss
Normal file
474
src/extensions/megaMenu/MegaMenu.module.scss
Normal file
@@ -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 */
|
||||
9
src/extensions/megaMenu/MegaMenu.module.scss.ts
Normal file
9
src/extensions/megaMenu/MegaMenu.module.scss.ts
Normal file
@@ -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
|
||||
}
|
||||
158
src/extensions/megaMenu/MegaMenuApplicationCustomizer.ts
Normal file
158
src/extensions/megaMenu/MegaMenuApplicationCustomizer.ts
Normal file
@@ -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
src/extensions/megaMenu/MegaMenuRenderer.ts
Normal file
510
src/extensions/megaMenu/MegaMenuRenderer.ts
Normal 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
src/extensions/megaMenu/MegaMenuSettings.ts
Normal file
210
src/extensions/megaMenu/MegaMenuSettings.ts
Normal 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'>×</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, '&').replace(/"/g, '"').replace(/</g, '<').replace(/>/g, '>');
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
5
src/extensions/megaMenu/loc/en-us.js
Normal file
5
src/extensions/megaMenu/loc/en-us.js
Normal file
@@ -0,0 +1,5 @@
|
||||
define([], function() {
|
||||
return {
|
||||
"Title": "MegaMenuApplicationCustomizer"
|
||||
}
|
||||
});
|
||||
8
src/extensions/megaMenu/loc/myStrings.d.ts
vendored
Normal file
8
src/extensions/megaMenu/loc/myStrings.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
declare interface IMegaMenuApplicationCustomizerStrings {
|
||||
Title: string;
|
||||
}
|
||||
|
||||
declare module 'MegaMenuApplicationCustomizerStrings' {
|
||||
const myStrings: IMegaMenuApplicationCustomizerStrings;
|
||||
export = myStrings;
|
||||
}
|
||||
20
src/index.ts
Normal file
20
src/index.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
// tslint:disable:max-line-length no-any
|
||||
// A file is required to be in the root of the /src directory by the TypeScript compiler
|
||||
|
||||
// Export services for classic SharePoint usage
|
||||
export { TaxonomyNavigationService } from './services/TaxonomyNavigationService';
|
||||
export { MegaMenuRenderer } from './extensions/megaMenu/MegaMenuRenderer';
|
||||
export { UserCustomActionService } from './services/UserCustomActionService/UserCustomActionService';
|
||||
export { UserCustomActionScope } from './services/UserCustomActionService/UserCustomActionScope';
|
||||
|
||||
// Expose services globally for classic JavaScript consumption
|
||||
declare var window: any;
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.__megaMenuServices = {
|
||||
TaxonomyNavigationService: require('./services/TaxonomyNavigationService').TaxonomyNavigationService,
|
||||
MegaMenuRenderer: require('./extensions/megaMenu/MegaMenuRenderer').MegaMenuRenderer,
|
||||
UserCustomActionService: require('./services/UserCustomActionService/UserCustomActionService').UserCustomActionService,
|
||||
UserCustomActionScope: require('./services/UserCustomActionService/UserCustomActionScope').UserCustomActionScope
|
||||
};
|
||||
}
|
||||
10
src/services/IMenuItem.ts
Normal file
10
src/services/IMenuItem.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface IMenuItem {
|
||||
id: string; // Guid;
|
||||
label: string;
|
||||
icon?: string;
|
||||
hoverText: string;
|
||||
url?: string;
|
||||
pathDepth: number;
|
||||
items?: IMenuItem[];
|
||||
hasChildren: () => boolean;
|
||||
}
|
||||
9
src/services/IPickerTerm.ts
Normal file
9
src/services/IPickerTerm.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export interface IPickerTerm {
|
||||
name: string;
|
||||
key: string;
|
||||
path: string;
|
||||
termSet: string;
|
||||
termSetName?: string;
|
||||
}
|
||||
|
||||
export interface IPickerTerms extends Array<IPickerTerm> { }
|
||||
94
src/services/ISPTermStorePickerService.ts
Normal file
94
src/services/ISPTermStorePickerService.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
// tslint:disable:no-any
|
||||
/**
|
||||
* Interfaces for Term store, groups and term sets
|
||||
* This code is a copy from the library @pnp/sp-dev-fx-controls-react
|
||||
*/
|
||||
export interface ITermStore {
|
||||
_ObjectType_: string; // SP.Taxonomy.TermStore
|
||||
_ObjectIdentity_: string;
|
||||
Id: string;
|
||||
Name: string;
|
||||
Groups: IGroups;
|
||||
}
|
||||
|
||||
export interface IGroups {
|
||||
_ObjectType_: string; // SP.Taxonomy.TermGroupCollection
|
||||
_Child_Items_: IGroup[];
|
||||
}
|
||||
|
||||
export interface IGroup {
|
||||
_ObjectType_: string; // SP.Taxonomy.TermGroup
|
||||
_ObjectIdentity_: string;
|
||||
TermSets: ITermSets;
|
||||
Id: string;
|
||||
Name: string;
|
||||
IsSystemGroup: boolean;
|
||||
}
|
||||
|
||||
export interface ITermSets {
|
||||
_ObjectType_: string; // SP.Taxonomy.TermSetCollection
|
||||
_Child_Items_: ITermSet[];
|
||||
}
|
||||
|
||||
export interface ITermSet {
|
||||
_ObjectType_: string; // SP.Taxonomy.TermSet
|
||||
_ObjectIdentity_: string;
|
||||
Id: string;
|
||||
CustomSortOrder?: string;
|
||||
Name: string;
|
||||
Description: string;
|
||||
Names: ITermSetNames;
|
||||
Terms?: ITerm[];
|
||||
}
|
||||
|
||||
export interface ITermSetMinimal {
|
||||
_ObjectType_?: string; // SP.Taxonomy.TermSet
|
||||
_ObjectIdentity_?: string;
|
||||
Id: string;
|
||||
Name: string;
|
||||
}
|
||||
|
||||
export interface ITermSetNames {
|
||||
[locale: string]: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interfaces for the terms
|
||||
*/
|
||||
export interface ITerms {
|
||||
_ObjectType_: string; // SP.Taxonomy.TermCollection
|
||||
_Child_Items_: ITerm[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Term
|
||||
*/
|
||||
export interface ITerm {
|
||||
_ObjectType_: string; // SP.Taxonomy.Term
|
||||
_ObjectIdentity_: string;
|
||||
Id: string;
|
||||
Name: string;
|
||||
Description: string;
|
||||
IsDeprecated: boolean;
|
||||
IsAvailableForTagging: boolean;
|
||||
IsRoot: boolean;
|
||||
PathOfTerm: string;
|
||||
TermSet: ITermSetMinimal;
|
||||
CustomSortOrderIndex?: number;
|
||||
PathDepth?: number;
|
||||
ParentId?: string;
|
||||
TermsCount?: number;
|
||||
LocalCustomProperties?: {
|
||||
[property: string]: any
|
||||
};
|
||||
}
|
||||
|
||||
export interface ISuggestTerm {
|
||||
Id: string;
|
||||
DefaultLabel: string;
|
||||
Description: string;
|
||||
IsKeyword: boolean;
|
||||
IsSynonym: boolean;
|
||||
Paths: Array<string>;
|
||||
Synonyms: string;
|
||||
}
|
||||
7
src/services/ISPTermStorePickerServiceProps.ts
Normal file
7
src/services/ISPTermStorePickerServiceProps.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export interface ISPTermStorePickerServiceProps {
|
||||
termsetNameOrID: string;
|
||||
useSessionStorage: boolean;
|
||||
hideDeprecatedTags: boolean;
|
||||
hideTagsNotAvailableForTagging: boolean;
|
||||
anchorId: string;
|
||||
}
|
||||
5
src/services/ITaxonomyNavigationService.ts
Normal file
5
src/services/ITaxonomyNavigationService.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { IMenuItem } from './IMenuItem';
|
||||
|
||||
export interface ITaxonomyNavigationService {
|
||||
getMenuItems(): Promise<IMenuItem[]>;
|
||||
}
|
||||
9
src/services/ItemDictionary.ts
Normal file
9
src/services/ItemDictionary.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export class ItemDictionary<T> {
|
||||
public Get(key: string): T {
|
||||
return this[key];
|
||||
}
|
||||
|
||||
public Add(key: string, value: T): void {
|
||||
this[key] = value;
|
||||
}
|
||||
}
|
||||
26
src/services/MegaMenuDebug.ts
Normal file
26
src/services/MegaMenuDebug.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export function debugLog(enabled: boolean, source: string, message: string, ...args: any[]): void {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const output: any[] = ['[' + source + '] ' + message].concat(args || []);
|
||||
console.log.apply(console, output);
|
||||
}
|
||||
|
||||
export function debugWarn(enabled: boolean, source: string, message: string, ...args: any[]): void {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const output: any[] = ['[' + source + '] ' + message].concat(args || []);
|
||||
console.warn.apply(console, output);
|
||||
}
|
||||
|
||||
export function debugError(enabled: boolean, source: string, message: string, ...args: any[]): void {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const output: any[] = ['[' + source + '] ' + message].concat(args || []);
|
||||
console.error.apply(console, output);
|
||||
}
|
||||
37
src/services/MenuItem.ts
Normal file
37
src/services/MenuItem.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
// tslint:disable:no-any no-string-literal max-line-length
|
||||
|
||||
import { IMenuItem } from './IMenuItem';
|
||||
import { ITerm } from './ISPTermStorePickerService';
|
||||
|
||||
export class MenuItem implements IMenuItem {
|
||||
public id: string;
|
||||
public label: string;
|
||||
public hoverText: string;
|
||||
public pathDepth: number;
|
||||
public url?: string;
|
||||
public items?: IMenuItem[];
|
||||
|
||||
constructor(term: ITerm, public level: number, siteCollectionUrl?: string) {
|
||||
this.id = term.Id;
|
||||
this.label = term.Name;
|
||||
this.hoverText = term.LocalCustomProperties['_Sys_Nav_HoverText'];
|
||||
this.pathDepth = term.PathDepth;
|
||||
const rawUrl: string = term.LocalCustomProperties['_Sys_Nav_SimpleLinkUrl'] || term.LocalCustomProperties['_Sys_Nav_TargetUrl'];
|
||||
if (rawUrl) {
|
||||
this.url = siteCollectionUrl && rawUrl.indexOf('~sitecollection') === 0
|
||||
? siteCollectionUrl + rawUrl.substring('~sitecollection'.length)
|
||||
: rawUrl;
|
||||
}
|
||||
this.items = [];
|
||||
}
|
||||
|
||||
public hasChildren(): boolean {
|
||||
return this.items && this.items.length > 0;
|
||||
}
|
||||
|
||||
public command(): void {
|
||||
if (this.url) {
|
||||
(window as any).location.href = this.url;
|
||||
}
|
||||
}
|
||||
}
|
||||
106
src/services/MockTaxonomyNavigationService.ts
Normal file
106
src/services/MockTaxonomyNavigationService.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
import { IMenuItem } from './IMenuItem';
|
||||
import { ITaxonomyNavigationService } from './ITaxonomyNavigationService';
|
||||
import * as uuid from 'uuid';
|
||||
|
||||
export default class MockTaxonomyNavigationService implements ITaxonomyNavigationService {
|
||||
public getMenuItems(): Promise<IMenuItem[]> {
|
||||
return new Promise<IMenuItem[]>((resolve) => {
|
||||
resolve([
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Menu Item 1',
|
||||
url: 'https://www.bing.com',
|
||||
hoverText: 'Hover me!',
|
||||
pathDepth: 1,
|
||||
hasChildren: () => true,
|
||||
items: [
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Submenu Item 1',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 2,
|
||||
hasChildren: () => false
|
||||
},
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Submenu Item 2',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 2,
|
||||
hasChildren: () => false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Menu Item 2',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 1,
|
||||
hasChildren: () => false
|
||||
},
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Menu Item 3',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 1,
|
||||
hasChildren: () => true,
|
||||
items: [
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Submenu Item 1',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 2,
|
||||
hasChildren: () => false
|
||||
},
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Submenu Item 2',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 2,
|
||||
hasChildren: () => true,
|
||||
items: [
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Submenu Item 1',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 3,
|
||||
hasChildren: () => false
|
||||
},
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Submenu Item 2',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 3,
|
||||
hasChildren: () => false
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Submenu Item 3',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 2,
|
||||
hasChildren: () => false
|
||||
},
|
||||
{
|
||||
id: uuid.v4(),
|
||||
label: 'Submenu Item 4',
|
||||
hoverText: 'Huch!',
|
||||
url: 'https://www.bing.com',
|
||||
pathDepth: 2,
|
||||
hasChildren: () => false
|
||||
}
|
||||
]
|
||||
}
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
532
src/services/SPTermStorePickerService.ts
Normal file
532
src/services/SPTermStorePickerService.ts
Normal file
@@ -0,0 +1,532 @@
|
||||
/* tslint:disable:no-null-keyword max-line-length typedef no-any no-string-literal variable-name */
|
||||
/**
|
||||
* This code is a copy from the library @pnp/sp-dev-fx-controls-react
|
||||
*/
|
||||
|
||||
import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from '@microsoft/sp-http';
|
||||
import { ITermStore, ITerms, ITerm, IGroup, ITermSet, ISuggestTerm } from './ISPTermStorePickerService';
|
||||
import { findIndex } from '@microsoft/sp-lodash-subset';
|
||||
import { ISPTermStorePickerServiceProps } from './ISPTermStorePickerServiceProps';
|
||||
import { IPickerTerm } from './IPickerTerm';
|
||||
import { ApplicationCustomizerContext } from '@microsoft/sp-application-base';
|
||||
import { debugError } from './MegaMenuDebug';
|
||||
|
||||
const EmptyGuid: string = '00000000-0000-0000-0000-000000000000';
|
||||
|
||||
/**
|
||||
* Service implementation to manage term stores in SharePoint
|
||||
*/
|
||||
export default class SPTermStorePickerService {
|
||||
private clientServiceUrl: string;
|
||||
private suggestionServiceUrl: string;
|
||||
|
||||
/**
|
||||
* Service constructor
|
||||
*/
|
||||
constructor(private props: ISPTermStorePickerServiceProps, private context: ApplicationCustomizerContext, private debug: boolean = false) {
|
||||
this.clientServiceUrl = this.context.pageContext.web.absoluteUrl + '/_vti_bin/client.svc/ProcessQuery';
|
||||
this.suggestionServiceUrl = this.context.pageContext.web.absoluteUrl + '/_vti_bin/TaxonomyInternalService.json/GetSuggestions';
|
||||
}
|
||||
|
||||
public async getTermLabels(termId: string): Promise<string[]> {
|
||||
let result: string[] = null;
|
||||
try {
|
||||
const data = `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="8" ObjectPathId="7" /><ObjectIdentityQuery Id="9" ObjectPathId="7" /><ObjectPath Id="11" ObjectPathId="10" /><ObjectIdentityQuery Id="12" ObjectPathId="10" /><ObjectPath Id="14" ObjectPathId="13" /><ObjectIdentityQuery Id="15" ObjectPathId="13" /><Query Id="16" ObjectPathId="13"><Query SelectAllProperties="false"><Properties><Property Name="Labels" SelectAll="true"><Query SelectAllProperties="false"><Properties /></Query></Property></Properties></Query></Query></Actions><ObjectPaths><StaticMethod Id="7" Name="GetTaxonomySession" TypeId="{981cbc68-9edc-4f8d-872f-71146fcbb84f}" /><Method Id="10" ParentId="7" Name="GetDefaultKeywordsTermStore" /><Method Id="13" ParentId="10" Name="GetTerm"><Parameters><Parameter Type="Guid">${termId}</Parameter></Parameters></Method></ObjectPaths></Request>`;
|
||||
|
||||
const reqHeaders = new Headers();
|
||||
reqHeaders.append('accept', 'application/json');
|
||||
reqHeaders.append('content-type', 'application/xml');
|
||||
|
||||
const httpPostOptions: ISPHttpClientOptions = {
|
||||
headers: reqHeaders,
|
||||
body: data
|
||||
};
|
||||
|
||||
const callResult = await this.context.spHttpClient.post(this.clientServiceUrl, SPHttpClient.configurations.v1, httpPostOptions);
|
||||
const jsonResult = await callResult.json();
|
||||
|
||||
const node = jsonResult.find(x => x._ObjectType_ === 'SP.Taxonomy.Term');
|
||||
if (node && node.Labels && node.Labels._Child_Items_) {
|
||||
result = node.Labels._Child_Items_.map(termLabel => termLabel.Value);
|
||||
}
|
||||
} catch (error) {
|
||||
result = null;
|
||||
debugError(this.debug, 'SPTermStorePickerService', 'Error reading term labels.', error);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the collection of term stores in the current SharePoint env
|
||||
*/
|
||||
public getTermStores(): Promise<ITermStore[]> {
|
||||
// Retrieve the term store name, groups, and term sets
|
||||
const data = '<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="2" ObjectPathId="1" /><ObjectIdentityQuery Id="3" ObjectPathId="1" /><ObjectPath Id="5" ObjectPathId="4" /><ObjectIdentityQuery Id="6" ObjectPathId="4" /><Query Id="7" ObjectPathId="4"><Query SelectAllProperties="false"><Properties><Property Name="Id" ScalarProperty="true" /><Property Name="Name" ScalarProperty="true" /><Property Name="Groups"><Query SelectAllProperties="false"><Properties /></Query><ChildItemQuery SelectAllProperties="false"><Properties><Property Name="Name" ScalarProperty="true" /><Property Name="Id" ScalarProperty="true" /><Property Name="IsSystemGroup" ScalarProperty="true" /><Property Name="TermSets"><Query SelectAllProperties="false"><Properties /></Query><ChildItemQuery SelectAllProperties="false"><Properties><Property Name="Name" ScalarProperty="true" /><Property Name="Id" ScalarProperty="true" /><Property Name="Description" ScalarProperty="true" /><Property Name="Names" ScalarProperty="true" /></Properties></ChildItemQuery></Property></Properties></ChildItemQuery></Property></Properties></Query></Query></Actions><ObjectPaths><StaticMethod Id="1" Name="GetTaxonomySession" TypeId="{981cbc68-9edc-4f8d-872f-71146fcbb84f}" /><Method Id="4" ParentId="1" Name="GetDefaultSiteCollectionTermStore" /></ObjectPaths></Request>';
|
||||
|
||||
const reqHeaders = new Headers();
|
||||
reqHeaders.append('accept', 'application/json');
|
||||
reqHeaders.append('content-type', 'application/xml');
|
||||
|
||||
const httpPostOptions: ISPHttpClientOptions = {
|
||||
headers: reqHeaders,
|
||||
body: data
|
||||
};
|
||||
|
||||
return this.context.spHttpClient.post(this.clientServiceUrl, SPHttpClient.configurations.v1, httpPostOptions).then((serviceResponse: SPHttpClientResponse) => {
|
||||
return serviceResponse.json().then((serviceJSONResponse: any) => {
|
||||
// Construct results
|
||||
const termStoreResult: ITermStore[] = serviceJSONResponse.filter((r: { [x: string]: string; }) => r['_ObjectType_'] === 'SP.Taxonomy.TermStore');
|
||||
// Check if term store was retrieved
|
||||
if (termStoreResult.length > 0) {
|
||||
// Check if the termstore needs to be filtered or limited
|
||||
if (this.props.termsetNameOrID) {
|
||||
return termStoreResult.map(termstore => {
|
||||
let termGroups = termstore.Groups._Child_Items_;
|
||||
|
||||
// Check if the groups have to be limited to a specific term set
|
||||
if (this.props.termsetNameOrID) {
|
||||
const termsetNameOrId = this.props.termsetNameOrID;
|
||||
termGroups = termGroups.map((group: IGroup) => {
|
||||
group.TermSets._Child_Items_ = group.TermSets._Child_Items_.filter((termSet: ITermSet) => termSet.Name === termsetNameOrId || this.cleanGuid(termSet.Id).toLowerCase() === this.cleanGuid(termsetNameOrId).toLowerCase());
|
||||
return group;
|
||||
});
|
||||
}
|
||||
|
||||
// Filter out all systen groups
|
||||
termGroups = termGroups.filter(group => !group.IsSystemGroup);
|
||||
|
||||
// Filter out empty groups
|
||||
termGroups = termGroups.filter((group: IGroup) => group.TermSets._Child_Items_.length > 0);
|
||||
|
||||
// Map the new groups
|
||||
termstore.Groups._Child_Items_ = termGroups;
|
||||
return termstore;
|
||||
});
|
||||
}
|
||||
|
||||
// Return the term store results
|
||||
return termStoreResult;
|
||||
}
|
||||
return [];
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current term set
|
||||
*/
|
||||
public async getTermSet(): Promise<ITermSet> {
|
||||
const termStore = await this.getTermStores();
|
||||
return this.getTermSetId(termStore, this.props.termsetNameOrID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all terms for the given term set
|
||||
* @param termset
|
||||
*/
|
||||
public async getAllTerms(termset: string, hideDeprecatedTags?: boolean, hideTagsNotAvailableForTagging?: boolean, useSessionStorage: boolean = true): Promise<ITermSet> {
|
||||
let termsetId: string = termset;
|
||||
// Check if the provided term set property is a GUID or string
|
||||
if (!this.isGuid(termset)) {
|
||||
// Fetch the term store information
|
||||
const termStore = await this.getTermStores();
|
||||
// Get the ID of the provided term set name
|
||||
const crntTermSet = this.getTermSetId(termStore, termset);
|
||||
if (crntTermSet) {
|
||||
termsetId = this.cleanGuid(crntTermSet.Id);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
const childTerms = this.getTermsById(termsetId, useSessionStorage);
|
||||
|
||||
if (childTerms) {
|
||||
return childTerms;
|
||||
}
|
||||
|
||||
// Request body to retrieve all terms for the given term set
|
||||
const data = `<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="Javascript Library"><Actions><ObjectPath Id="1" ObjectPathId="0" /><ObjectIdentityQuery Id="2" ObjectPathId="0" /><ObjectPath Id="4" ObjectPathId="3" /><ObjectIdentityQuery Id="5" ObjectPathId="3" /><ObjectPath Id="7" ObjectPathId="6" /><ObjectIdentityQuery Id="8" ObjectPathId="6" /><ObjectPath Id="10" ObjectPathId="9" /><Query Id="11" ObjectPathId="6"><Query SelectAllProperties="true"><Properties /></Query></Query><Query Id="12" ObjectPathId="9"><Query SelectAllProperties="false"><Properties /></Query><ChildItemQuery SelectAllProperties="false"><Properties><Property Name="IsRoot" SelectAll="true" /><Property Name="Labels" SelectAll="true" /><Property Name="TermsCount" SelectAll="true" /><Property Name="CustomSortOrder" SelectAll="true" /><Property Name="Id" SelectAll="true" /><Property Name="Name" SelectAll="true" /><Property Name="PathOfTerm" SelectAll="true" /><Property Name="Parent" SelectAll="true" /><Property Name="LocalCustomProperties" SelectAll="true" /><Property Name="IsDeprecated" ScalarProperty="true" /><Property Name="IsAvailableForTagging" ScalarProperty="true" /></Properties></ChildItemQuery></Query></Actions><ObjectPaths><StaticMethod Id="0" Name="GetTaxonomySession" TypeId="{981cbc68-9edc-4f8d-872f-71146fcbb84f}" /><Method Id="3" ParentId="0" Name="GetDefaultKeywordsTermStore" /><Method Id="6" ParentId="3" Name="GetTermSet"><Parameters><Parameter Type="Guid">${termsetId}</Parameter></Parameters></Method><Method Id="9" ParentId="6" Name="GetAllTerms" /></ObjectPaths></Request>`;
|
||||
|
||||
const reqHeaders = new Headers();
|
||||
reqHeaders.append('accept', 'application/json');
|
||||
reqHeaders.append('content-type', 'application/xml');
|
||||
|
||||
const httpPostOptions: ISPHttpClientOptions = {
|
||||
headers: reqHeaders,
|
||||
body: data
|
||||
};
|
||||
|
||||
return this.context.spHttpClient.post(this.clientServiceUrl, SPHttpClient.configurations.v1, httpPostOptions).then((serviceResponse: SPHttpClientResponse) => {
|
||||
return serviceResponse.json().then((serviceJSONResponse: any) => {
|
||||
const termStoreResultTermSets: ITermSet[] = serviceJSONResponse.filter((r: { [x: string]: string; }) => r['_ObjectType_'] === 'SP.Taxonomy.TermSet');
|
||||
|
||||
if (termStoreResultTermSets.length > 0) {
|
||||
const termStoreResultTermSet = termStoreResultTermSets[0];
|
||||
termStoreResultTermSet.Terms = [];
|
||||
// Retrieve the term collection results
|
||||
const termStoreResultTerms: ITerms[] = serviceJSONResponse.filter((r: { [x: string]: string; }) => r['_ObjectType_'] === 'SP.Taxonomy.TermCollection');
|
||||
if (termStoreResultTerms.length > 0) {
|
||||
// Retrieve all terms
|
||||
let terms = termStoreResultTerms[0]._Child_Items_;
|
||||
|
||||
if (hideDeprecatedTags === true) {
|
||||
terms = terms.filter(d => d.IsDeprecated === false);
|
||||
}
|
||||
|
||||
if (hideTagsNotAvailableForTagging === true) {
|
||||
terms = terms.filter(d => d.IsAvailableForTagging === true);
|
||||
}
|
||||
|
||||
// Clean the term ID and specify the path depth
|
||||
terms = terms.map(term => {
|
||||
if (term.IsRoot) {
|
||||
term.CustomSortOrderIndex = (termStoreResultTermSet.CustomSortOrder) ? termStoreResultTermSet.CustomSortOrder.split(':').indexOf(this.cleanGuid(term.Id)) : -1;
|
||||
} else {
|
||||
term.CustomSortOrderIndex = (term['Parent'].CustomSortOrder) ? term['Parent'].CustomSortOrder.split(':').indexOf(this.cleanGuid(term.Id)) : -1;
|
||||
}
|
||||
term.Id = this.cleanGuid(term.Id);
|
||||
term['PathDepth'] = term.PathOfTerm.split(';').length;
|
||||
term.TermSet = { Id: this.cleanGuid(termStoreResultTermSet.Id), Name: termStoreResultTermSet.Name };
|
||||
if (term['Parent']) {
|
||||
term.ParentId = this.cleanGuid(term['Parent'].Id);
|
||||
}
|
||||
return term;
|
||||
});
|
||||
// Check if the term set was not empty
|
||||
if (terms.length > 0) {
|
||||
// Sort the terms by PathOfTerm and their depth
|
||||
terms = this.sortTerms(terms);
|
||||
termStoreResultTermSet.Terms = terms;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (useSessionStorage && window.sessionStorage) {
|
||||
window.sessionStorage.setItem(termsetId, JSON.stringify(termStoreResultTermSet));
|
||||
}
|
||||
} catch (error) {
|
||||
// Do nothing, sometimes "storage quota exceeded" error if too many items
|
||||
}
|
||||
return termStoreResultTermSet;
|
||||
}
|
||||
return null;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all terms that starts with the searchText
|
||||
* @param searchText
|
||||
*/
|
||||
public searchTermsByName(searchText: string): Promise<IPickerTerm[]> {
|
||||
return this.searchTermsByTermSet(searchText);
|
||||
}
|
||||
|
||||
public async searchTermsByTermId(searchText: string, termId: string): Promise<IPickerTerm[]> {
|
||||
const { useSessionStorage } = this.props;
|
||||
const childTerms = this.getTermsById(termId, useSessionStorage);
|
||||
if (childTerms) {
|
||||
return this.searchTermsBySearchText(childTerms, searchText);
|
||||
} else {
|
||||
const {
|
||||
termsetNameOrID,
|
||||
hideDeprecatedTags,
|
||||
hideTagsNotAvailableForTagging
|
||||
} = this.props;
|
||||
|
||||
const terms = await this.getAllTermsByAnchorId(
|
||||
termsetNameOrID,
|
||||
termId,
|
||||
hideDeprecatedTags,
|
||||
hideTagsNotAvailableForTagging,
|
||||
useSessionStorage);
|
||||
|
||||
if (terms) {
|
||||
return this.searchTermsBySearchText(terms, searchText);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all terms for the given term set and anchorId
|
||||
*/
|
||||
public async getAllTermsByAnchorId(termsetNameOrID: string, anchorId: string, hideDeprecatedTags?: boolean, hideTagsNotAvailableForTagging?: boolean, useSessionStorage: boolean = true): Promise<IPickerTerm[]> {
|
||||
|
||||
const returnTerms: IPickerTerm[] = [];
|
||||
|
||||
const childTerms = this.getTermsById(anchorId, useSessionStorage);
|
||||
if (childTerms) {
|
||||
return childTerms;
|
||||
}
|
||||
|
||||
const termSet = await this.getAllTerms(termsetNameOrID, hideDeprecatedTags, hideTagsNotAvailableForTagging);
|
||||
const terms = termSet.Terms;
|
||||
if (anchorId) {
|
||||
const anchorTerm = terms.filter(t => t.Id.toLowerCase() === anchorId.toLowerCase()).shift();
|
||||
if (anchorTerm) {
|
||||
// Append ';' separator, as a suffix to anchor term path.
|
||||
const anchorTermPath = `${anchorTerm.PathOfTerm};`;
|
||||
const anchorTerms: ITerm[] = terms.filter(t => t.PathOfTerm.substring(0, anchorTermPath.length) === anchorTermPath && t.Id !== anchorTerm.Id);
|
||||
|
||||
anchorTerms.forEach(term => {
|
||||
returnTerms.push(this.convertTermToPickerTerm(term));
|
||||
});
|
||||
|
||||
try {
|
||||
if (useSessionStorage && window.sessionStorage) {
|
||||
window.sessionStorage.setItem(anchorId, JSON.stringify(returnTerms));
|
||||
}
|
||||
} catch (error) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
} else {
|
||||
terms.forEach(term => {
|
||||
returnTerms.push(this.convertTermToPickerTerm(term));
|
||||
});
|
||||
}
|
||||
|
||||
return returnTerms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean the Guid from the Web Service response
|
||||
* @param guid
|
||||
*/
|
||||
public cleanGuid(guid: string): string {
|
||||
if (guid !== undefined) {
|
||||
return guid.replace('/Guid(', '').replace('/', '').replace(')', '');
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the term set ID by its name
|
||||
* @param termstore
|
||||
* @param termset
|
||||
*/
|
||||
private getTermSetId(termstore: ITermStore[], termsetName: string): ITermSet {
|
||||
if (termstore && termstore.length > 0 && termsetName) {
|
||||
// Get the first term store
|
||||
const ts = termstore[0];
|
||||
// Check if the term store contains groups
|
||||
if (ts.Groups && ts.Groups._Child_Items_) {
|
||||
for (const group of ts.Groups._Child_Items_) {
|
||||
// Check if the group contains term sets
|
||||
if (group.TermSets && group.TermSets._Child_Items_) {
|
||||
for (const termSet of group.TermSets._Child_Items_) {
|
||||
// Check if the term set is found
|
||||
if (termSet.Name === termsetName) {
|
||||
return termSet;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private getTermsById(termId, useSessionStorage: boolean = true) {
|
||||
try {
|
||||
if (useSessionStorage && window.sessionStorage) {
|
||||
const terms = window.sessionStorage.getItem(termId);
|
||||
if (terms) {
|
||||
return JSON.parse(terms);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private searchTermsBySearchText(terms, searchText) {
|
||||
if (terms) {
|
||||
return terms.filter((t) => { return t.name.toLowerCase().indexOf(searchText.toLowerCase()) > -1; });
|
||||
} else {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches terms for the given term set
|
||||
* @param searchText
|
||||
* @param termsetId
|
||||
*/
|
||||
private searchTermsByTermSet(searchText: string): Promise<IPickerTerm[]> {
|
||||
return new Promise<IPickerTerm[]>(resolve => {
|
||||
this.getTermStores().then(termStore => {
|
||||
let termSetId = this.props.termsetNameOrID;
|
||||
if (!this.isGuid(termSetId)) {
|
||||
// Get the ID of the provided term set name
|
||||
const crntTermSet = this.getTermSetId(termStore, termSetId);
|
||||
if (crntTermSet) {
|
||||
termSetId = this.cleanGuid(crntTermSet.Id);
|
||||
} else {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (termStore === undefined || termStore.length === 0) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
const loc: number = this.context.pageContext.cultureInfo.currentUICultureName === 'de-de' ? 1031 : 1033;
|
||||
const data: any = {
|
||||
start: searchText,
|
||||
lcid: loc !== 0 ? loc : this.context.pageContext.web.language,
|
||||
sspList: this.cleanGuid(termStore[0].Id),
|
||||
termSetList: termSetId,
|
||||
anchorId: this.props.anchorId ? this.props.anchorId : EmptyGuid,
|
||||
isSpanTermStores: false,
|
||||
isSpanTermSets: false,
|
||||
isIncludeUnavailable: this.props.hideTagsNotAvailableForTagging === true,
|
||||
isIncludeDeprecated: this.props.hideDeprecatedTags === true,
|
||||
isAddTerms: false,
|
||||
isIncludePathData: false,
|
||||
excludeKeyword: false,
|
||||
excludedTermset: EmptyGuid
|
||||
};
|
||||
|
||||
const reqHeaders: Headers = new Headers();
|
||||
reqHeaders.append('accept', 'application/json');
|
||||
reqHeaders.append('content-type', 'application/json');
|
||||
|
||||
const httpPostOptions: ISPHttpClientOptions = {
|
||||
headers: reqHeaders,
|
||||
body: JSON.stringify(data)
|
||||
};
|
||||
|
||||
return this.context.spHttpClient.post(this.suggestionServiceUrl, SPHttpClient.configurations.v1, httpPostOptions).then((serviceResponse: SPHttpClientResponse) => {
|
||||
return serviceResponse.json().then((serviceJSONResponse: any) => {
|
||||
const groups = serviceJSONResponse.d.Groups;
|
||||
if (groups && groups.length > 0) {
|
||||
// Retrieve the term collection results
|
||||
const terms: ISuggestTerm[] = groups[0].Suggestions;
|
||||
if (terms.length > 0) {
|
||||
// Retrieve all terms
|
||||
|
||||
const returnTerms: IPickerTerm[] = terms.map((term: ISuggestTerm) => this.convertSuggestTermToPickerTerm(term));
|
||||
resolve(returnTerms);
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
resolve([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private isGuid(strGuid: string): boolean {
|
||||
return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(strGuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorting terms based on their path and depth
|
||||
*
|
||||
* @param terms
|
||||
*/
|
||||
private sortTerms(terms: ITerm[]) {
|
||||
// Start sorting by depth
|
||||
let newTermsOrder: ITerm[] = [];
|
||||
let itemsToSort: boolean = true;
|
||||
let pathLevel: number = 1;
|
||||
while (itemsToSort) {
|
||||
// Get terms for the current level
|
||||
let crntTerms = terms.filter(term => term.PathDepth === pathLevel);
|
||||
if (crntTerms && crntTerms.length > 0) {
|
||||
crntTerms = crntTerms.sort(this.sortTermByPath);
|
||||
|
||||
if (pathLevel !== 1) {
|
||||
crntTerms = crntTerms.reverse();
|
||||
for (const crntTerm of crntTerms) {
|
||||
const pathElms: string[] = crntTerm.PathOfTerm.split(';');
|
||||
// Last item is not needed for parent path
|
||||
pathElms.pop();
|
||||
// Find the parent item and add the new item
|
||||
const idx: number = findIndex(newTermsOrder, term => term.PathOfTerm === pathElms.join(';'));
|
||||
if (idx !== -1) {
|
||||
newTermsOrder.splice(idx + 1, 0, crntTerm);
|
||||
} else {
|
||||
// Push the item at the end if the parent couldn't be found
|
||||
newTermsOrder.push(crntTerm);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
newTermsOrder = crntTerms;
|
||||
}
|
||||
|
||||
++pathLevel;
|
||||
} else {
|
||||
itemsToSort = false;
|
||||
}
|
||||
}
|
||||
return newTermsOrder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the terms by their path
|
||||
*
|
||||
* @param a term 2
|
||||
* @param b term 2
|
||||
*/
|
||||
private sortTermByPath(a: ITerm, b: ITerm) {
|
||||
if (a.CustomSortOrderIndex === -1) {
|
||||
if (a.PathOfTerm.toLowerCase() < b.PathOfTerm.toLowerCase()) {
|
||||
return -1;
|
||||
}
|
||||
if (a.PathOfTerm.toLowerCase() > b.PathOfTerm.toLowerCase()) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
if (a.CustomSortOrderIndex < b.CustomSortOrderIndex) {
|
||||
return -1;
|
||||
}
|
||||
if (a.CustomSortOrderIndex > b.CustomSortOrderIndex) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
private convertTermToPickerTerm(term: ITerm): IPickerTerm {
|
||||
return {
|
||||
key: this.cleanGuid(term.Id),
|
||||
name: term.Name,
|
||||
path: term.PathOfTerm,
|
||||
termSet: this.cleanGuid(term.TermSet.Id),
|
||||
termSetName: term.TermSet.Name
|
||||
};
|
||||
}
|
||||
|
||||
private convertSuggestTermToPickerTerm(term: ISuggestTerm): IPickerTerm {
|
||||
let path: string = '';
|
||||
let termSetName: string = '';
|
||||
if (term.Paths && term.Paths.length > 0) {
|
||||
const fullPath: string = term.Paths[0].replace(/^\[/, '').replace(/\]$/, '');
|
||||
const fullPathParts: string[] = fullPath.split(':');
|
||||
path = fullPathParts.join(';') + ';' + term.DefaultLabel;
|
||||
termSetName = fullPathParts[0];
|
||||
}
|
||||
return {
|
||||
key: this.cleanGuid(term.Id),
|
||||
name: term.DefaultLabel,
|
||||
path: path,
|
||||
termSet: EmptyGuid, // TermSet Guid is not given with suggestion
|
||||
termSetName: termSetName
|
||||
};
|
||||
}
|
||||
}
|
||||
83
src/services/TaxonomyNavigationService.ts
Normal file
83
src/services/TaxonomyNavigationService.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { ITaxonomyNavigationService } from './ITaxonomyNavigationService';
|
||||
import { IMenuItem } from './IMenuItem';
|
||||
import { sp } from '@pnp/sp';
|
||||
import { MenuItem } from './MenuItem';
|
||||
import { ApplicationCustomizerContext } from '@microsoft/sp-application-base';
|
||||
import SPTermStorePickerService from './SPTermStorePickerService';
|
||||
import { ITerm, ITermSet } from './ISPTermStorePickerService';
|
||||
import { ItemDictionary } from './ItemDictionary';
|
||||
import { debugWarn } from './MegaMenuDebug';
|
||||
|
||||
const LOG_SOURCE: string = 'TaxonomyNavigationService';
|
||||
|
||||
export class TaxonomyNavigationService implements ITaxonomyNavigationService {
|
||||
private _taxonomyPickerService: SPTermStorePickerService;
|
||||
private _noTerm: ITerm = {
|
||||
_ObjectType_: '',
|
||||
_ObjectIdentity_: '',
|
||||
CustomSortOrderIndex: 0,
|
||||
Description: '',
|
||||
Id: '',
|
||||
IsAvailableForTagging: false,
|
||||
IsDeprecated: false,
|
||||
IsRoot: true,
|
||||
LocalCustomProperties: {
|
||||
_Sys_Nav_HoverText: 'Es wurden keine Terms gefunden. Bitte ueberpruefen Sie Ihre Einstellungen.',
|
||||
_Sys_Nav_ExcludedProviders: undefined,
|
||||
_Sys_Nav_SimpleLinkUrl: undefined
|
||||
},
|
||||
Name: 'Es wurden keine Terms gefunden. Bitte ueberpruefen Sie Ihre Einstellungen.',
|
||||
PathOfTerm: '',
|
||||
TermSet: undefined
|
||||
};
|
||||
|
||||
constructor(
|
||||
private context: ApplicationCustomizerContext,
|
||||
private termSetName: string,
|
||||
private debug: boolean = false
|
||||
) {
|
||||
sp.setup({
|
||||
spfxContext: context
|
||||
});
|
||||
this._taxonomyPickerService = new SPTermStorePickerService(
|
||||
{
|
||||
anchorId: '',
|
||||
termsetNameOrID: termSetName,
|
||||
useSessionStorage: true,
|
||||
hideDeprecatedTags: true,
|
||||
hideTagsNotAvailableForTagging: false
|
||||
},
|
||||
this.context,
|
||||
this.debug
|
||||
);
|
||||
}
|
||||
|
||||
public async getMenuItems(): Promise<IMenuItem[]> {
|
||||
const siteCollectionUrl: string = this.context.pageContext.site.absoluteUrl;
|
||||
const termset: ITermSet = await this._taxonomyPickerService.getAllTerms(this.termSetName);
|
||||
const itemsDict: ItemDictionary<IMenuItem> = new ItemDictionary<IMenuItem>();
|
||||
const menuItems: IMenuItem[] = [];
|
||||
|
||||
if (!termset || !termset.Terms) {
|
||||
debugWarn(this.debug, LOG_SOURCE, 'No terms found in the term set.');
|
||||
return [new MenuItem(this._noTerm, 0, siteCollectionUrl)];
|
||||
}
|
||||
|
||||
termset.Terms.forEach((term: ITerm) => {
|
||||
const menuItem: IMenuItem = new MenuItem(term, 0, siteCollectionUrl);
|
||||
itemsDict.Add(term.Id, menuItem);
|
||||
if (menuItem.pathDepth === 1) {
|
||||
menuItems.push(menuItem);
|
||||
} else {
|
||||
const parentItem: IMenuItem = itemsDict.Get(term.ParentId);
|
||||
if (parentItem) {
|
||||
parentItem.items.push(menuItem);
|
||||
} else {
|
||||
debugWarn(this.debug, LOG_SOURCE, 'Item without parent:', term.PathOfTerm);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return menuItems;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
export interface IUserCustomActionProps {
|
||||
Id?: string;
|
||||
Title: string;
|
||||
Name?: string;
|
||||
Description?: string;
|
||||
Location: string;
|
||||
ScriptSrc?: string;
|
||||
ScriptBlock?: string;
|
||||
Url?: string;
|
||||
Sequence?: number;
|
||||
Group?: string;
|
||||
ImageUrl?: string;
|
||||
CommandUIExtension?: string;
|
||||
RegistrationType?: number;
|
||||
RegistrationId?: string;
|
||||
Rights?: {};
|
||||
Scope?: number;
|
||||
ClientSideComponentId?: string;
|
||||
ClientSideComponentProperties?: string;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/* tslint:disable:max-line-length */
|
||||
import { UserCustomActionScope } from './UserCustomActionScope';
|
||||
import { IUserCustomActionProps } from './IUserCustomActionProps';
|
||||
import { UserCustomActionAddResult, UserCustomActionUpdateResult } from '@pnp/sp';
|
||||
|
||||
export interface IUserCustomActionService {
|
||||
getUserCustomActions(scope: UserCustomActionScope, listId?: string): Promise<IUserCustomActionProps[]>;
|
||||
getUserCustomActionById(scope: UserCustomActionScope, id: string, listId?: string): Promise<IUserCustomActionProps>;
|
||||
addUserCustomAction(scope: UserCustomActionScope, customAction: IUserCustomActionProps, listId?: string): Promise<UserCustomActionAddResult>;
|
||||
updateUserCustomAction(scope: UserCustomActionScope, id: string, props: {}, listId?: string): Promise<UserCustomActionUpdateResult>;
|
||||
deleteUserCustomAction(scope: UserCustomActionScope, customAction: IUserCustomActionProps, listId?: string): Promise<void>;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export enum UserCustomActionScope {
|
||||
Web = 'web',
|
||||
Site = 'site',
|
||||
List = 'list'
|
||||
}
|
||||
134
src/services/UserCustomActionService/UserCustomActionService.ts
Normal file
134
src/services/UserCustomActionService/UserCustomActionService.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
// tslint:disable:max-line-length
|
||||
// tslint:disable:export-name
|
||||
import { UserCustomActionAddResult, UserCustomActions, UserCustomActionUpdateResult } from '@pnp/sp/src/usercustomactions';
|
||||
import { IUserCustomActionService } from './IUserCustomActionService';
|
||||
import { sp } from '@pnp/sp';
|
||||
import { UserCustomActionScope } from './UserCustomActionScope';
|
||||
import { IUserCustomActionProps } from './IUserCustomActionProps';
|
||||
import { ApplicationCustomizerContext } from '@microsoft/sp-application-base';
|
||||
import { debugError } from '../MegaMenuDebug';
|
||||
|
||||
const LOG_SOURCE: string = 'UserCustomActionService';
|
||||
|
||||
export class UserCustomActionService implements IUserCustomActionService {
|
||||
constructor(context: ApplicationCustomizerContext, private debug: boolean = false) {
|
||||
sp.setup({
|
||||
spfxContext: context
|
||||
});
|
||||
}
|
||||
|
||||
public async getUserCustomActions(scope: UserCustomActionScope, listId?: string): Promise<IUserCustomActionProps[]> {
|
||||
try {
|
||||
let actions: UserCustomActions | IUserCustomActionProps[];
|
||||
switch (scope) {
|
||||
case UserCustomActionScope.Web:
|
||||
actions = await sp.web.userCustomActions.get();
|
||||
break;
|
||||
case UserCustomActionScope.Site:
|
||||
actions = await sp.site.userCustomActions.get();
|
||||
break;
|
||||
case UserCustomActionScope.List:
|
||||
if (!listId) {
|
||||
throw new Error('List ID is required for List scope');
|
||||
}
|
||||
actions = await sp.web.lists.getById(listId).userCustomActions.get();
|
||||
break;
|
||||
default:
|
||||
throw new Error('Invalid scope');
|
||||
}
|
||||
return actions as IUserCustomActionProps[];
|
||||
} catch (error) {
|
||||
debugError(this.debug, LOG_SOURCE, 'Error getting user custom actions.', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async getUserCustomActionById(scope: UserCustomActionScope, id: string, listId?: string): Promise<IUserCustomActionProps> {
|
||||
try {
|
||||
switch (scope) {
|
||||
case UserCustomActionScope.Web:
|
||||
return sp.web.userCustomActions.getById(id) as {} as IUserCustomActionProps;
|
||||
case UserCustomActionScope.Site:
|
||||
return sp.site.userCustomActions.getById(id) as {} as IUserCustomActionProps;
|
||||
case UserCustomActionScope.List:
|
||||
if (!listId) {
|
||||
throw new Error('List ID is required for List scope');
|
||||
}
|
||||
return sp.web.lists.getById(listId).userCustomActions.getById(id) as {} as IUserCustomActionProps;
|
||||
default:
|
||||
throw new Error('Invalid scope');
|
||||
}
|
||||
} catch (error) {
|
||||
debugError(this.debug, LOG_SOURCE, 'Error getting user custom action by ID.', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async addUserCustomAction(scope: UserCustomActionScope, customAction: IUserCustomActionProps, listId?: string): Promise<UserCustomActionAddResult> {
|
||||
try {
|
||||
switch (scope) {
|
||||
case UserCustomActionScope.Web:
|
||||
return sp.web.userCustomActions.add(customAction);
|
||||
case UserCustomActionScope.Site:
|
||||
return sp.site.userCustomActions.add(customAction);
|
||||
case UserCustomActionScope.List:
|
||||
if (!listId) {
|
||||
throw new Error('List ID is required for List scope');
|
||||
}
|
||||
return sp.web.lists.getById(listId).userCustomActions.add(customAction);
|
||||
default:
|
||||
throw new Error('Invalid scope');
|
||||
}
|
||||
} catch (error) {
|
||||
debugError(this.debug, LOG_SOURCE, 'Error adding user custom action.', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async updateUserCustomAction(scope: UserCustomActionScope, id: string, props: {}, listId?: string): Promise<UserCustomActionUpdateResult> {
|
||||
try {
|
||||
let result: UserCustomActionUpdateResult;
|
||||
switch (scope) {
|
||||
case UserCustomActionScope.Web:
|
||||
result = await sp.web.userCustomActions.getById(id).update(props);
|
||||
break;
|
||||
case UserCustomActionScope.Site:
|
||||
result = await sp.site.userCustomActions.getById(id).update(props);
|
||||
break;
|
||||
case UserCustomActionScope.List:
|
||||
if (!listId) {
|
||||
throw new Error('List ID is required for List scope');
|
||||
}
|
||||
result = await sp.web.lists.getById(listId).userCustomActions.getById(id).update(props);
|
||||
break;
|
||||
default:
|
||||
throw new Error('Invalid scope');
|
||||
}
|
||||
return result;
|
||||
} catch (error) {
|
||||
debugError(this.debug, LOG_SOURCE, 'Error updating user custom action.', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
public async deleteUserCustomAction(scope: UserCustomActionScope, customAction: IUserCustomActionProps, listId?: string): Promise<void> {
|
||||
try {
|
||||
switch (scope) {
|
||||
case UserCustomActionScope.Web:
|
||||
return sp.web.userCustomActions.getById(customAction.Id).delete();
|
||||
case UserCustomActionScope.Site:
|
||||
return sp.site.userCustomActions.getById(customAction.Id).delete();
|
||||
case UserCustomActionScope.List:
|
||||
if (!listId) {
|
||||
throw new Error('List ID is required for List scope');
|
||||
}
|
||||
return sp.web.lists.getById(listId).userCustomActions.getById(customAction.Id).delete();
|
||||
default:
|
||||
throw new Error('Invalid scope');
|
||||
}
|
||||
} catch (error) {
|
||||
debugError(this.debug, LOG_SOURCE, 'Error deleting user custom action.', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user