Skip to content

Commit a61aab1

Browse files
committed
Added session storage as property
1 parent 52c6cf2 commit a61aab1

File tree

4 files changed

+29
-15
lines changed

4 files changed

+29
-15
lines changed

docs/documentation/docs/controls/TaxonomyPicker.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ The TaxonomyPicker control can be configured with the following properties:
171171
| errorMessage | string | no | Static error message displayed below the picker. Use `onGetErrorMessage` to dynamically change the error message displayed (if any) based on the current value. `errorMessage` and `onGetErrorMessage` are mutually exclusive (`errorMessage` takes precedence). |
172172
| onGetErrorMessage | (value: IPickerTerms) => string \| Promise&lt;string&gt; | no | The method is used to get the validation error message and determine whether the picker value is valid or not. Mutually exclusive with the static string `errorMessage` (it will take precedence over this).<br />When it returns string:<ul><li>If valid, it returns empty string.</li><li>If invalid, it returns the error message string to be shown below the picker.</li></ul><br />When it returns Promise&lt;string&gt;:<ul><li>The resolved value is display as error message.</li><li>The rejected, the value is thrown away.</li></ul> |
173173
| required | boolean | no | Specifies if to display an asterisk near the label. Note that error message should be specified in `onGetErrorMessage` or `errorMessage` |
174+
| useSessionStorage | boolean | no | Specify if the control uses session storage. Default is set to true for better performance. |
175+
174176

175177
Interface `IPickerTerm`
176178

src/controls/taxonomyPicker/ITaxonomyPicker.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ export interface ITaxonomyPickerProps {
120120
* Note that error message should be specified in onGetErrorMessage
121121
*/
122122
required?: boolean;
123+
124+
/**
125+
* Specifies if you want to use session storage
126+
* Default will be true
127+
*/
128+
useSessionStorage?: boolean;
123129
}
124130

125131
/**

src/controls/taxonomyPicker/TaxonomyPicker.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
112112
hideTagsNotAvailableForTagging,
113113
initialValues,
114114
validateOnLoad,
115-
termsetNameOrID
115+
termsetNameOrID,
116+
useSessionStorage
116117
} = this.props;
117118

118119
let isValidateOnLoad = validateOnLoad && initialValues && initialValues.length >= 1;
@@ -121,7 +122,7 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
121122
const notFoundTerms: string[] = [];
122123
const notFoundTermIds: string[] = [];
123124

124-
const termSet = await this.termsService.getAllTerms(termsetNameOrID, hideDeprecatedTags, hideTagsNotAvailableForTagging);
125+
const termSet = await this.termsService.getAllTerms(termsetNameOrID, hideDeprecatedTags, hideTagsNotAvailableForTagging, useSessionStorage);
125126
const allTerms = termSet.Terms;
126127

127128
for (let i = 0, len = initialValues.length; i < len; i++) {
@@ -153,7 +154,7 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
153154
// });
154155
}
155156

156-
this.termsService.getAllTerms(this.props.termsetNameOrID, this.props.hideDeprecatedTags, this.props.hideTagsNotAvailableForTagging).then((response: ITermSet) => {
157+
this.termsService.getAllTerms(this.props.termsetNameOrID, this.props.hideDeprecatedTags, this.props.hideTagsNotAvailableForTagging, this.props.useSessionStorage).then((response: ITermSet) => {
157158
// Check if a response was retrieved
158159
let termSetAndTerms = response ? response : null;
159160
this.setState({
@@ -167,7 +168,7 @@ export class TaxonomyPicker extends React.Component<ITaxonomyPickerProps, ITaxon
167168
* Force update of the taxonomy tree - required by term action in case the term has been added, deleted or moved.
168169
*/
169170
private async updateTaxonomyTree(): Promise<void> {
170-
const termSetAndTerms = await this.termsService.getAllTerms(this.props.termsetNameOrID, this.props.hideDeprecatedTags, this.props.hideTagsNotAvailableForTagging);
171+
const termSetAndTerms = await this.termsService.getAllTerms(this.props.termsetNameOrID, this.props.hideDeprecatedTags, this.props.hideTagsNotAvailableForTagging, this.props.useSessionStorage);
171172

172173
this.setState({
173174
termSetAndTerms

src/services/SPTermStorePickerService.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export default class SPTermStorePickerService {
149149
* Retrieve all terms for the given term set
150150
* @param termset
151151
*/
152-
public async getAllTerms(termset: string, hideDeprecatedTags?: boolean, hideTagsNotAvailableForTagging?: boolean): Promise<ITermSet> {
152+
public async getAllTerms(termset: string, hideDeprecatedTags?: boolean, hideTagsNotAvailableForTagging?: boolean, useSessionStorage: boolean = true): Promise<ITermSet> {
153153
if (Environment.type === EnvironmentType.Local) {
154154
// If the running environment is local, load the data from the mock
155155
return this.getAllMockTerms();
@@ -168,7 +168,7 @@ export default class SPTermStorePickerService {
168168
}
169169
}
170170

171-
let childTerms = this.getTermsById(termsetId);
171+
let childTerms = this.getTermsById(termsetId, useSessionStorage);
172172

173173
if (childTerms) {
174174
return childTerms;
@@ -231,7 +231,7 @@ export default class SPTermStorePickerService {
231231
}
232232
}
233233
try {
234-
if (window.sessionStorage) {
234+
if (useSessionStorage && window.sessionStorage) {
235235
window.sessionStorage.setItem(termsetId, JSON.stringify(termStoreResultTermSet));
236236
}
237237
} catch (error) {
@@ -287,16 +287,19 @@ export default class SPTermStorePickerService {
287287
}
288288
}
289289

290-
private getTermsById(termId) {
290+
private getTermsById(termId, useSessionStorage: boolean = true) {
291291
try {
292-
if (window.sessionStorage) {
292+
if (useSessionStorage && window.sessionStorage) {
293293
let terms = window.sessionStorage.getItem(termId);
294294
if (terms) {
295295
return JSON.parse(terms);
296296
} else {
297297
return null;
298298
}
299299
}
300+
else{
301+
return null;
302+
}
300303
} catch (error) {
301304
return null;
302305
}
@@ -315,22 +318,24 @@ export default class SPTermStorePickerService {
315318
// If the running environment is local, load the data from the mock
316319
return SPTermStoreMockHttpClient.searchTermsByName(searchText);
317320
} else {
318-
let childTerms = this.getTermsById(termId);
321+
const { useSessionStorage } = this.props;
322+
let childTerms = this.getTermsById(termId, useSessionStorage);
319323
if (childTerms) {
320324
return this.searchTermsBySearchText(childTerms, searchText);
321325
}
322326
else {
323327
const {
324328
termsetNameOrID,
325329
hideDeprecatedTags,
326-
hideTagsNotAvailableForTagging
330+
hideTagsNotAvailableForTagging,
327331
} = this.props;
328332

329333
const terms = await this.getAllTermsByAnchorId(
330334
termsetNameOrID,
331335
termId,
332336
hideDeprecatedTags,
333-
hideTagsNotAvailableForTagging);
337+
hideTagsNotAvailableForTagging,
338+
useSessionStorage);
334339

335340
if (terms) {
336341
return this.searchTermsBySearchText(terms, searchText);
@@ -344,7 +349,7 @@ export default class SPTermStorePickerService {
344349
/**
345350
* Retrieve all terms for the given term set and anchorId
346351
*/
347-
public async getAllTermsByAnchorId(termsetNameOrID: string, anchorId: string, hideDeprecatedTags?: boolean, hideTagsNotAvailableForTagging?: boolean): Promise<IPickerTerm[]> {
352+
public async getAllTermsByAnchorId(termsetNameOrID: string, anchorId: string, hideDeprecatedTags?: boolean, hideTagsNotAvailableForTagging?: boolean, useSessionStorage: boolean = true): Promise<IPickerTerm[]> {
348353

349354
let returnTerms: IPickerTerm[] = [];
350355

@@ -355,7 +360,7 @@ export default class SPTermStorePickerService {
355360
returnTerms.push(this.convertTermToPickerTerm(term));
356361
});
357362
} else {
358-
const childTerms = this.getTermsById(anchorId);
363+
const childTerms = this.getTermsById(anchorId, useSessionStorage);
359364
if (childTerms) {
360365
return childTerms;
361366
}
@@ -374,7 +379,7 @@ export default class SPTermStorePickerService {
374379
});
375380

376381
try {
377-
if (window.sessionStorage) {
382+
if (useSessionStorage && window.sessionStorage) {
378383
window.sessionStorage.setItem(anchorId, JSON.stringify(returnTerms));
379384
}
380385
}

0 commit comments

Comments
 (0)