UNPKG

7.21 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { isArray } from "@pnp/core";
3import { defaultPath } from "../decorators.js";
4import { _SPInstance, spInvokableFactory, _SPCollection } from "../spqueryable.js";
5import { escapeQueryStrValue } from "../utils/escape-query-str.js";
6/**
7 * Describes a collection of Form objects
8 *
9 */
10let _TermStore = class _TermStore extends _SPInstance {
11 /**
12 * Gets the term groups associated with this tenant
13 */
14 get groups() {
15 return TermGroups(this);
16 }
17 /**
18 * Gets the term sets associated with this tenant
19 */
20 get sets() {
21 return TermSets(this);
22 }
23 /**
24 * Allows you to locate terms within the termStore
25 *
26 * @param params Search parameters used to locate the terms, label is required
27 * @returns Array of terms including set information for each term
28 */
29 async searchTerm(params) {
30 const query = Reflect.ownKeys(params).reduce((c, prop) => {
31 c.push(`${prop}='${escapeQueryStrValue(params[prop])}'`);
32 return c;
33 }, []).join(",");
34 return TermStore(this, `searchTerm(${query})`).expand("set")();
35 }
36};
37_TermStore = __decorate([
38 defaultPath("_api/v2.1/termstore")
39], _TermStore);
40export { _TermStore };
41export const TermStore = spInvokableFactory(_TermStore);
42let _TermGroups = class _TermGroups extends _SPCollection {
43 /**
44 * Gets a term group by id
45 *
46 * @param id Id of the term group to access
47 */
48 getById(id) {
49 return TermGroup(this, id);
50 }
51};
52_TermGroups = __decorate([
53 defaultPath("groups")
54], _TermGroups);
55export { _TermGroups };
56export const TermGroups = spInvokableFactory(_TermGroups);
57export class _TermGroup extends _SPInstance {
58 /**
59 * Gets the term sets associated with this tenant
60 */
61 get sets() {
62 return TermSets(this, "sets");
63 }
64}
65export const TermGroup = spInvokableFactory(_TermGroup);
66let _TermSets = class _TermSets extends _SPCollection {
67 /**
68 * Gets a term group by id
69 *
70 * @param id Id of the term group to access
71 */
72 getById(id) {
73 return TermSet(this, id);
74 }
75};
76_TermSets = __decorate([
77 defaultPath("sets")
78], _TermSets);
79export { _TermSets };
80export const TermSets = spInvokableFactory(_TermSets);
81export class _TermSet extends _SPInstance {
82 /**
83 * Gets all the terms in this set
84 */
85 get terms() {
86 return Terms(this);
87 }
88 get parentGroup() {
89 return TermGroup(this, "parentGroup");
90 }
91 get children() {
92 return Children(this);
93 }
94 get relations() {
95 return Relations(this);
96 }
97 getTermById(id) {
98 return Term(this, `terms/${id}`);
99 }
100 /**
101 * Gets all the terms in this termset in an ordered tree using the appropriate sort ordering
102 * ** This is an expensive operation and you should strongly consider caching the results **
103 *
104 * @param props Optional set of properties controlling how the tree is retrieved.
105 */
106 async getAllChildrenAsOrderedTree(props = {}) {
107 const selects = ["*", "customSortOrder"];
108 if (props.retrieveProperties) {
109 selects.push("properties", "localProperties");
110 }
111 const setInfo = await this.select(...selects)();
112 const tree = [];
113 const ensureOrder = (terms, sorts, setSorts) => {
114 // handle no custom sort information present
115 if (!isArray(sorts) && !isArray(setSorts)) {
116 return terms;
117 }
118 let ordering = null;
119 if (sorts === null && setSorts.length > 0) {
120 ordering = [...setSorts];
121 }
122 else {
123 const index = sorts.findIndex(v => v.setId === setInfo.id);
124 if (index >= 0) {
125 ordering = [...sorts[index].order];
126 }
127 }
128 if (ordering !== null) {
129 const orderedChildren = [];
130 ordering.forEach(o => {
131 const found = terms.find(ch => o === ch.id);
132 if (found) {
133 orderedChildren.push(found);
134 }
135 });
136 // we have a case where if a set is ordered and a term is added to that set
137 // AND the ordering information hasn't been updated in the UI the new term will not have
138 // any associated ordering information. See #1547 which reported this. So here we
139 // append any terms remaining in "terms" not in "orderedChildren" to the end of "orderedChildren"
140 orderedChildren.push(...terms.filter(info => ordering.indexOf(info.id) < 0));
141 return orderedChildren;
142 }
143 return terms;
144 };
145 const visitor = async (source, parent) => {
146 const children = await source.children.select(...selects)();
147 for (let i = 0; i < children.length; i++) {
148 const child = children[i];
149 const orderedTerm = {
150 children: [],
151 defaultLabel: child.labels.find(l => l.isDefault).name,
152 ...child,
153 };
154 if (child.childrenCount > 0) {
155 await visitor(this.getTermById(children[i].id), orderedTerm.children);
156 orderedTerm.children = ensureOrder(orderedTerm.children, child.customSortOrder);
157 }
158 parent.push(orderedTerm);
159 }
160 };
161 await visitor(this, tree);
162 return ensureOrder(tree, null, setInfo.customSortOrder);
163 }
164}
165export const TermSet = spInvokableFactory(_TermSet);
166let _Children = class _Children extends _SPCollection {
167};
168_Children = __decorate([
169 defaultPath("children")
170], _Children);
171export { _Children };
172export const Children = spInvokableFactory(_Children);
173let _Terms = class _Terms extends _SPCollection {
174 /**
175 * Gets a term group by id
176 *
177 * @param id Id of the term group to access
178 */
179 getById(id) {
180 return Term(this, id);
181 }
182};
183_Terms = __decorate([
184 defaultPath("terms")
185], _Terms);
186export { _Terms };
187export const Terms = spInvokableFactory(_Terms);
188export class _Term extends _SPInstance {
189 get children() {
190 return Children(this);
191 }
192 get relations() {
193 return Relations(this);
194 }
195 get set() {
196 return TermSet(this, "set");
197 }
198}
199export const Term = spInvokableFactory(_Term);
200let _Relations = class _Relations extends _SPCollection {
201 /**
202 * Gets a term group by id
203 *
204 * @param id Id of the term group to access
205 */
206 getById(id) {
207 return Relation(this, id);
208 }
209};
210_Relations = __decorate([
211 defaultPath("relations")
212], _Relations);
213export { _Relations };
214export const Relations = spInvokableFactory(_Relations);
215export class _Relation extends _SPInstance {
216 get fromTerm() {
217 return Term(this, "fromTerm");
218 }
219 get toTerm() {
220 return Term(this, "toTerm");
221 }
222 get set() {
223 return TermSet(this, "set");
224 }
225}
226export const Relation = spInvokableFactory(_Relation);
227//# sourceMappingURL=types.js.map
\No newline at end of file