@@ -9,10 +9,11 @@ import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from '@micro
9
9
import { Environment , EnvironmentType } from '@microsoft/sp-core-library' ;
10
10
import { IWebPartContext } from '@microsoft/sp-webpart-base' ;
11
11
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' ;
14
14
import SPTermStoreMockHttpClient from './SPTermStorePickerMockService' ;
15
15
import { ApplicationCustomizerContext } from '@microsoft/sp-application-base' ;
16
+ import { findIndex } from '@microsoft/sp-lodash-subset' ;
16
17
17
18
18
19
/**
@@ -169,8 +170,8 @@ export default class SPTermStorePickerService {
169
170
} ) ;
170
171
// Check if the term set was not empty
171
172
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 ) ;
174
175
termStoreResultTermSet . Terms = terms ;
175
176
}
176
177
}
@@ -296,12 +297,56 @@ export default class SPTermStorePickerService {
296
297
return / ^ [ 0 - 9 a - f A - F ] { 8 } - [ 0 - 9 a - f A - F ] { 4 } - [ 0 - 9 a - f A - F ] { 4 } - [ 0 - 9 a - f A - F ] { 4 } - [ 0 - 9 a - f A - F ] { 12 } $ / . test ( strGuid ) ;
297
298
}
298
299
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
+
299
343
/**
300
344
* Sort the terms by their path
345
+ *
301
346
* @param a term 2
302
347
* @param b term 2
303
348
*/
304
- private _sortTerms ( a : ITerm , b : ITerm ) {
349
+ private sortTermByPath ( a : ITerm , b : ITerm ) {
305
350
if ( a . CustomSortOrderIndex === - 1 ) {
306
351
if ( a . PathOfTerm . toLowerCase ( ) < b . PathOfTerm . toLowerCase ( ) ) {
307
352
return - 1 ;
0 commit comments