Aktualisiere Version auf 1.1.1, füge Unterstützung für die Aufteilung von Taxonomie-Werten hinzu und verbessere die Handhabung von Kategoriewerten im Accordion-WebPart

This commit is contained in:
Torsten Brendgen
2026-08-26 23:35:10 +02:00
parent be1701eef3
commit 3ee1247efe
8 changed files with 56 additions and 19 deletions
@@ -1,10 +1,10 @@
export function getAccordionCategoryValues(value: any): string[] { // tslint:disable-line:no-any
export function getAccordionCategoryValues(value: any, splitPlainSemicolon?: boolean): string[] { // tslint:disable-line:no-any
if (value === undefined || value === null) { return []; }
const source: any[] = value && Array.isArray(value.results) ? value.results : [value]; // tslint:disable-line:no-any
const values: string[] = [];
const keys: { [key: string]: boolean } = {};
for (let i: number = 0; i < source.length; i++) {
const current: string[] = categoryEntryValues(source[i]);
const current: string[] = categoryEntryValues(source[i], splitPlainSemicolon === true);
for (let valueIndex: number = 0; valueIndex < current.length; valueIndex++) {
const text: string = current[valueIndex].trim();
const key: string = '$' + text.toLocaleLowerCase();
@@ -14,7 +14,7 @@ export function getAccordionCategoryValues(value: any): string[] { // tslint:dis
return values;
}
function categoryEntryValues(entry: any): string[] { // tslint:disable-line:no-any
function categoryEntryValues(entry: any, splitPlainSemicolon: boolean): string[] { // tslint:disable-line:no-any
if (entry === undefined || entry === null) { return []; }
if (entry.Label) { return [String(entry.Label)]; }
if (typeof entry !== 'string') {
@@ -32,7 +32,7 @@ function categoryEntryValues(entry: any): string[] { // tslint:disable-line:no-a
const semicolonParts: string[] = candidate.split(';');
const allTaxonomyValues: boolean = semicolonParts.every((part: string): boolean =>
/\|[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(part.trim()));
if (allTaxonomyValues) { parts = semicolonParts; }
if (allTaxonomyValues || splitPlainSemicolon) { parts = semicolonParts; }
}
const values: string[] = [];
+31 -7
View File
@@ -38,7 +38,8 @@ export class AccordionDataService {
categoryField: string,
sortField: string,
sortAscending: boolean,
categoryValue: string
categoryValue: string,
categoryFieldType: string
): Promise<IAccordionItem[]> {
if (!this.isGuid(listId)) { return Promise.reject(new Error('Invalid list ID.')); }
const fields: string[] = this.uniqueFields(['Id', titleField, contentField, categoryField]);
@@ -46,15 +47,21 @@ export class AccordionDataService {
if (!this.isFieldName(fields[i])) { return Promise.reject(new Error('Invalid field name.')); }
}
const orderField: string = this.isFieldName(sortField) ? sortField : titleField;
const taxonomyCategory: boolean = this.isTaxonomyType(categoryFieldType) && !!categoryField;
const formattedSelect: string = taxonomyCategory ? ',FieldValuesAsText/' + categoryField : '';
const formattedExpand: string = taxonomyCategory ? '&$expand=FieldValuesAsText' : '';
const url: string = this.webUrl + "/_api/web/lists(guid'" + this.cleanGuid(listId) + "')/items" +
'?$select=' + fields.join(',') + '&$orderby=' + orderField + (sortAscending ? ' asc' : ' desc') + '&$top=5000';
'?$select=' + fields.join(',') + formattedSelect + formattedExpand +
'&$orderby=' + orderField + (sortAscending ? ' asc' : ' desc') + '&$top=5000';
return this.context.spHttpClient.get(url, SPHttpClient.configurations.v1, { headers: this.headers })
.then((response: SPHttpClientResponse): Promise<any> => this.ensureJson(response)) // tslint:disable-line:no-any
.then((payload: any): IAccordionItem[] => this.results(payload) // tslint:disable-line:no-any
.filter((item: any): boolean => !categoryValue || // tslint:disable-line:no-any
getAccordionCategoryValues(item[categoryField]).indexOf(categoryValue) >= 0)
this.itemCategoryValues(item, categoryField, taxonomyCategory).indexOf(categoryValue) >= 0)
.map((item: any): IAccordionItem => { // tslint:disable-line:no-any
const categories: string[] = categoryField ? getAccordionCategoryValues(item[categoryField]) : [];
const categories: string[] = categoryField
? this.itemCategoryValues(item, categoryField, taxonomyCategory)
: [];
return {
id: Number(item.Id),
title: this.toText(item[titleField]),
@@ -64,10 +71,13 @@ export class AccordionDataService {
}));
}
public getCategoryValues(listId: string, categoryField: string): Promise<string[]> {
public getCategoryValues(listId: string, categoryField: string, categoryFieldType: string): Promise<string[]> {
if (!this.isGuid(listId) || !this.isFieldName(categoryField)) { return Promise.resolve([]); }
const taxonomyCategory: boolean = this.isTaxonomyType(categoryFieldType);
const formattedSelect: string = taxonomyCategory ? ',FieldValuesAsText/' + categoryField : '';
const formattedExpand: string = taxonomyCategory ? '&$expand=FieldValuesAsText' : '';
const url: string = this.webUrl + "/_api/web/lists(guid'" + this.cleanGuid(listId) + "')/items" +
'?$select=' + categoryField + '&$top=5000';
'?$select=' + categoryField + formattedSelect + formattedExpand + '&$top=5000';
return this.context.spHttpClient.get(url, SPHttpClient.configurations.v1, { headers: this.headers })
.then((response: SPHttpClientResponse): Promise<any> => this.ensureJson(response)) // tslint:disable-line:no-any
.then((payload: any): string[] => { // tslint:disable-line:no-any
@@ -75,7 +85,7 @@ export class AccordionDataService {
const keys: { [key: string]: boolean } = {};
const items: any[] = this.results(payload); // tslint:disable-line:no-any
for (let i: number = 0; i < items.length; i++) {
const current: string[] = getAccordionCategoryValues(items[i][categoryField]);
const current: string[] = this.itemCategoryValues(items[i], categoryField, taxonomyCategory);
for (let valueIndex: number = 0; valueIndex < current.length; valueIndex++) {
const key: string = '$' + current[valueIndex].toLocaleLowerCase();
if (!keys[key]) { keys[key] = true; values.push(current[valueIndex]); }
@@ -108,6 +118,17 @@ export class AccordionDataService {
return result;
}
private itemCategoryValues(item: any, categoryField: string, taxonomyCategory: boolean): string[] { // tslint:disable-line:no-any
if (taxonomyCategory && item && item.FieldValuesAsText) {
const formatted: string[] = getAccordionCategoryValues(
item.FieldValuesAsText[categoryField],
true
);
if (formatted.length) { return formatted; }
}
return getAccordionCategoryValues(item ? item[categoryField] : undefined);
}
private toText(value: any): string { // tslint:disable-line:no-any
if (value === undefined || value === null) { return ''; }
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { return String(value); }
@@ -121,6 +142,9 @@ export class AccordionDataService {
return ['Text', 'Note', 'Choice', 'MultiChoice', 'Number', 'Integer', 'Boolean', 'DateTime', 'Computed',
'TaxonomyFieldType', 'TaxonomyFieldTypeMulti'].indexOf(String(type)) >= 0;
}
private isTaxonomyType(type: string): boolean {
return type === 'TaxonomyFieldType' || type === 'TaxonomyFieldTypeMulti';
}
private isFieldName(value: string): boolean { return /^[A-Za-z_][A-Za-z0-9_]*$/.test(String(value || '')); }
private isGuid(value: string): boolean { return /^[0-9a-f]{8}-[0-9a-f-]{27}$/i.test(this.cleanGuid(value)); }
private cleanGuid(value: string): string { return String(value || '').replace(/[{}]/g, '').toLowerCase(); }
+10 -2
View File
@@ -66,7 +66,8 @@ export default class AccordionWebPart extends BaseClientSideWebPart<IAccordionWe
this.properties.categoryField || '',
this.properties.sortField || this.properties.titleField,
this.properties.sortAscending !== false,
this.properties.selectedCategory || ''
this.properties.selectedCategory || '',
this.getFieldType(this.properties.categoryField)
).then((items: IAccordionItem[]): void => {
if (this.disposed || version !== this.requestVersion) { return; }
if (!items.length) { this.renderStatus(strings.EmptyMessage, false); return; }
@@ -238,7 +239,7 @@ export default class AccordionWebPart extends BaseClientSideWebPart<IAccordionWe
}
this.loadingCategories = true;
this.refreshPropertyPane();
return this.service.getCategoryValues(listId, categoryField).then((categories: string[]): void => {
return this.service.getCategoryValues(listId, categoryField, this.getFieldType(categoryField)).then((categories: string[]): void => {
this.categories = categories;
if (categories.indexOf(this.properties.selectedCategory) < 0) { this.properties.selectedCategory = ''; }
}).catch((error: Error): void => {
@@ -281,6 +282,13 @@ export default class AccordionWebPart extends BaseClientSideWebPart<IAccordionWe
return field.type === 'TaxonomyFieldType' || field.type === 'TaxonomyFieldTypeMulti';
}
private getFieldType(internalName: string): string {
for (let i: number = 0; i < this.fields.length; i++) {
if (this.fields[i].internalName === internalName) { return this.fields[i].type; }
}
return '';
}
private isConfigured(): boolean {
return !!(this.service && this.properties.listId && this.properties.titleField && this.properties.contentField);
}