Skip to content

Commit c98a440

Browse files
committed
#229 - Fix for incorrectly positioning
1 parent 6985a86 commit c98a440

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@pnp/spfx-controls-react",
33
"description": "Reusable React controls for SharePoint Framework solutions",
4-
"version": "1.11.0",
4+
"version": "1.12.0",
55
"engines": {
66
"node": ">=0.10.0"
77
},

src/services/SPTermStorePickerService.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from '@micro
99
import { Environment, EnvironmentType } from '@microsoft/sp-core-library';
1010
import { IWebPartContext } from '@microsoft/sp-webpart-base';
1111
import { ITaxonomyPickerProps } from '../controls/taxonomyPicker/ITaxonomyPicker';
12-
import { IPickerTerms, IPickerTerm } from '../controls/taxonomyPicker/ITermPicker';
13-
import { ITermStore, ITerms, ITerm, IGroup, ITermSet, ITermSets } from './ISPTermStorePickerService';
12+
import { IPickerTerm } from '../controls/taxonomyPicker/ITermPicker';
13+
import { ITermStore, ITerms, ITerm, IGroup, ITermSet } from './ISPTermStorePickerService';
1414
import SPTermStoreMockHttpClient from './SPTermStorePickerMockService';
1515
import { ApplicationCustomizerContext } from '@microsoft/sp-application-base';
16+
import { findIndex } from '@microsoft/sp-lodash-subset';
1617

1718

1819
/**
@@ -169,8 +170,8 @@ export default class SPTermStorePickerService {
169170
});
170171
// Check if the term set was not empty
171172
if (terms.length > 0) {
172-
// Sort the terms by PathOfTerm
173-
terms = terms.sort(this._sortTerms);
173+
// Sort the terms by PathOfTerm and their depth
174+
terms = this.sortTerms(terms);
174175
termStoreResultTermSet.Terms = terms;
175176
}
176177
}
@@ -296,12 +297,56 @@ export default class SPTermStorePickerService {
296297
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);
297298
}
298299

300+
/**
301+
* Sorting terms based on their path and depth
302+
*
303+
* @param terms
304+
*/
305+
private sortTerms(terms: ITerm[]) {
306+
// Start sorting by depth
307+
let newTermsOrder: ITerm[] = [];
308+
let itemsToSort = true;
309+
let pathLevel = 1;
310+
while (itemsToSort) {
311+
// Get terms for the current level
312+
let crntTerms = terms.filter(term => term.PathDepth === pathLevel);
313+
if (crntTerms && crntTerms.length > 0) {
314+
crntTerms = crntTerms.sort(this.sortTermByPath);
315+
316+
if (pathLevel !== 1) {
317+
crntTerms = crntTerms.reverse();
318+
for (const crntTerm of crntTerms) {
319+
const pathElms = crntTerm.PathOfTerm.split(";");
320+
// Last item is not needed for parent path
321+
pathElms.pop();
322+
// Find the parent item and add the new item
323+
const idx = findIndex(newTermsOrder, term => term.PathOfTerm === pathElms.join(";"));
324+
if (idx !== -1) {
325+
newTermsOrder.splice(idx + 1, 0, crntTerm);
326+
} else {
327+
// Push the item at the end if the parent couldn't be found
328+
newTermsOrder.push(crntTerm);
329+
}
330+
}
331+
} else {
332+
newTermsOrder = crntTerms;
333+
}
334+
335+
++pathLevel;
336+
} else {
337+
itemsToSort = false;
338+
}
339+
}
340+
return newTermsOrder;
341+
}
342+
299343
/**
300344
* Sort the terms by their path
345+
*
301346
* @param a term 2
302347
* @param b term 2
303348
*/
304-
private _sortTerms(a: ITerm, b: ITerm) {
349+
private sortTermByPath(a: ITerm, b: ITerm) {
305350
if (a.CustomSortOrderIndex === -1) {
306351
if (a.PathOfTerm.toLowerCase() < b.PathOfTerm.toLowerCase()) {
307352
return -1;

0 commit comments

Comments
 (0)