UNPKG

@pnp/sp

Version:

pnp - provides a fluent api for working with SharePoint REST

227 lines • 7.21 kB
import { __decorate } from "tslib"; import { isArray } from "@pnp/core"; import { defaultPath } from "../decorators.js"; import { _SPInstance, spInvokableFactory, _SPCollection } from "../spqueryable.js"; import { escapeQueryStrValue } from "../utils/escape-query-str.js"; /** * Describes a collection of Form objects * */ let _TermStore = class _TermStore extends _SPInstance { /** * Gets the term groups associated with this tenant */ get groups() { return TermGroups(this); } /** * Gets the term sets associated with this tenant */ get sets() { return TermSets(this); } /** * Allows you to locate terms within the termStore * * @param params Search parameters used to locate the terms, label is required * @returns Array of terms including set information for each term */ async searchTerm(params) { const query = Reflect.ownKeys(params).reduce((c, prop) => { c.push(`${prop}='${escapeQueryStrValue(params[prop])}'`); return c; }, []).join(","); return TermStore(this, `searchTerm(${query})`).expand("set")(); } }; _TermStore = __decorate([ defaultPath("_api/v2.1/termstore") ], _TermStore); export { _TermStore }; export const TermStore = spInvokableFactory(_TermStore); let _TermGroups = class _TermGroups extends _SPCollection { /** * Gets a term group by id * * @param id Id of the term group to access */ getById(id) { return TermGroup(this, id); } }; _TermGroups = __decorate([ defaultPath("groups") ], _TermGroups); export { _TermGroups }; export const TermGroups = spInvokableFactory(_TermGroups); export class _TermGroup extends _SPInstance { /** * Gets the term sets associated with this tenant */ get sets() { return TermSets(this, "sets"); } } export const TermGroup = spInvokableFactory(_TermGroup); let _TermSets = class _TermSets extends _SPCollection { /** * Gets a term group by id * * @param id Id of the term group to access */ getById(id) { return TermSet(this, id); } }; _TermSets = __decorate([ defaultPath("sets") ], _TermSets); export { _TermSets }; export const TermSets = spInvokableFactory(_TermSets); export class _TermSet extends _SPInstance { /** * Gets all the terms in this set */ get terms() { return Terms(this); } get parentGroup() { return TermGroup(this, "parentGroup"); } get children() { return Children(this); } get relations() { return Relations(this); } getTermById(id) { return Term(this, `terms/${id}`); } /** * Gets all the terms in this termset in an ordered tree using the appropriate sort ordering * ** This is an expensive operation and you should strongly consider caching the results ** * * @param props Optional set of properties controlling how the tree is retrieved. */ async getAllChildrenAsOrderedTree(props = {}) { const selects = ["*", "customSortOrder"]; if (props.retrieveProperties) { selects.push("properties", "localProperties"); } const setInfo = await this.select(...selects)(); const tree = []; const ensureOrder = (terms, sorts, setSorts) => { // handle no custom sort information present if (!isArray(sorts) && !isArray(setSorts)) { return terms; } let ordering = null; if (sorts === null && setSorts.length > 0) { ordering = [...setSorts]; } else { const index = sorts.findIndex(v => v.setId === setInfo.id); if (index >= 0) { ordering = [...sorts[index].order]; } } if (ordering !== null) { const orderedChildren = []; ordering.forEach(o => { const found = terms.find(ch => o === ch.id); if (found) { orderedChildren.push(found); } }); // we have a case where if a set is ordered and a term is added to that set // AND the ordering information hasn't been updated in the UI the new term will not have // any associated ordering information. See #1547 which reported this. So here we // append any terms remaining in "terms" not in "orderedChildren" to the end of "orderedChildren" orderedChildren.push(...terms.filter(info => ordering.indexOf(info.id) < 0)); return orderedChildren; } return terms; }; const visitor = async (source, parent) => { const children = await source.children.select(...selects)(); for (let i = 0; i < children.length; i++) { const child = children[i]; const orderedTerm = { children: [], defaultLabel: child.labels.find(l => l.isDefault).name, ...child, }; if (child.childrenCount > 0) { await visitor(this.getTermById(children[i].id), orderedTerm.children); orderedTerm.children = ensureOrder(orderedTerm.children, child.customSortOrder); } parent.push(orderedTerm); } }; await visitor(this, tree); return ensureOrder(tree, null, setInfo.customSortOrder); } } export const TermSet = spInvokableFactory(_TermSet); let _Children = class _Children extends _SPCollection { }; _Children = __decorate([ defaultPath("children") ], _Children); export { _Children }; export const Children = spInvokableFactory(_Children); let _Terms = class _Terms extends _SPCollection { /** * Gets a term group by id * * @param id Id of the term group to access */ getById(id) { return Term(this, id); } }; _Terms = __decorate([ defaultPath("terms") ], _Terms); export { _Terms }; export const Terms = spInvokableFactory(_Terms); export class _Term extends _SPInstance { get children() { return Children(this); } get relations() { return Relations(this); } get set() { return TermSet(this, "set"); } } export const Term = spInvokableFactory(_Term); let _Relations = class _Relations extends _SPCollection { /** * Gets a term group by id * * @param id Id of the term group to access */ getById(id) { return Relation(this, id); } }; _Relations = __decorate([ defaultPath("relations") ], _Relations); export { _Relations }; export const Relations = spInvokableFactory(_Relations); export class _Relation extends _SPInstance { get fromTerm() { return Term(this, "fromTerm"); } get toTerm() { return Term(this, "toTerm"); } get set() { return TermSet(this, "set"); } } export const Relation = spInvokableFactory(_Relation); //# sourceMappingURL=types.js.map