UNPKG

9.39 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { isArray } from "@pnp/core";
3import { body } from "@pnp/queryable";
4import { defaultPath } from "../decorators.js";
5import { spDelete, spPatch, spPost } from "../operations.js";
6import { _SPInstance, spInvokableFactory, _SPCollection } from "../spqueryable.js";
7import { encodePath } from "../utils/encode-path-str.js";
8/**
9 * Describes a collection of Form objects
10 *
11 */
12let _TermStore = class _TermStore extends _SPInstance {
13 /**
14 * Gets the term groups associated with this tenant
15 */
16 get groups() {
17 return TermGroups(this);
18 }
19 /**
20 * Gets the term sets associated with this tenant
21 */
22 get sets() {
23 return TermSets(this);
24 }
25 /**
26 * Allows you to locate terms within the termStore
27 *
28 * @param params Search parameters used to locate the terms, label is required
29 * @returns Array of terms including set information for each term
30 */
31 async searchTerm(params) {
32 const query = Reflect.ownKeys(params).reduce((c, prop) => {
33 c.push(`${prop}='${encodePath(params[prop])}'`);
34 return c;
35 }, []).join(",");
36 return TermStore(this, `searchTerm(${query})`).expand("set")();
37 }
38 /**
39 * Update settings for TermStore
40 *
41 * @param props The set or properties to update
42 * @returns The updated term store information
43 */
44 update(props) {
45 return spPatch(this, body(props));
46 }
47};
48_TermStore = __decorate([
49 defaultPath("_api/v2.1/termstore")
50], _TermStore);
51export { _TermStore };
52export const TermStore = spInvokableFactory(_TermStore);
53let _TermGroups = class _TermGroups extends _SPCollection {
54 /**
55 * Gets a term group by id
56 *
57 * @param id Id of the term group to access
58 */
59 getById(id) {
60 return TermGroup(this, id);
61 }
62 /**
63 * Adds a new term group to this store
64 * @param props The set of properties
65 * @returns The information on the create group
66 */
67 add(props) {
68 return spPost(this, body(props));
69 }
70};
71_TermGroups = __decorate([
72 defaultPath("groups")
73], _TermGroups);
74export { _TermGroups };
75export const TermGroups = spInvokableFactory(_TermGroups);
76export class _TermGroup extends _SPInstance {
77 /**
78 * Gets the term sets associated with this tenant
79 */
80 get sets() {
81 return TermSets(this, "sets");
82 }
83 /**
84 * Deletes this group
85 *
86 * @returns void
87 */
88 delete() {
89 return spDelete(this);
90 }
91}
92export const TermGroup = spInvokableFactory(_TermGroup);
93let _TermSets = class _TermSets extends _SPCollection {
94 /**
95 * Gets a term group by id
96 *
97 * @param id Id of the term group to access
98 */
99 getById(id) {
100 return TermSet(this, id);
101 }
102 /**
103 * Adds a new term set to this collection
104 * @param props The set of properties
105 * @returns The information on the create group
106 */
107 add(props) {
108 return spPost(this, body(props));
109 }
110};
111_TermSets = __decorate([
112 defaultPath("sets")
113], _TermSets);
114export { _TermSets };
115export const TermSets = spInvokableFactory(_TermSets);
116export class _TermSet extends _SPInstance {
117 /**
118 * Gets all the terms in this set
119 */
120 get terms() {
121 return Terms(this);
122 }
123 get parentGroup() {
124 return TermGroup(this, "parentGroup");
125 }
126 get children() {
127 return Children(this);
128 }
129 get relations() {
130 return Relations(this);
131 }
132 getTermById(id) {
133 return Term(this, `terms/${id}`);
134 }
135 /**
136 * Update settings for TermSet
137 *
138 * @param props The set or properties to update
139 * @returns The updated term set information
140 */
141 update(props) {
142 return spPatch(this, body(props));
143 }
144 /**
145 * Deletes this group
146 *
147 * @returns void
148 */
149 delete() {
150 return spDelete(this);
151 }
152 /**
153 * Gets all the terms in this termset in an ordered tree using the appropriate sort ordering
154 * ** This is an expensive operation and you should strongly consider caching the results **
155 *
156 * @param props Optional set of properties controlling how the tree is retrieved.
157 */
158 async getAllChildrenAsOrderedTree(props = {}) {
159 const selects = ["*", "customSortOrder"];
160 if (props.retrieveProperties) {
161 selects.push("properties", "localProperties");
162 }
163 const setInfo = await this.select(...selects)();
164 const tree = [];
165 const childIds = [];
166 const ensureOrder = (terms, sorts, setSorts) => {
167 // handle no custom sort information present
168 if (!isArray(sorts) && !isArray(setSorts)) {
169 return terms;
170 }
171 let ordering = null;
172 if (sorts === null && setSorts.length > 0) {
173 ordering = [...setSorts];
174 }
175 else {
176 const index = sorts.findIndex(v => v.setId === setInfo.id);
177 if (index >= 0) {
178 ordering = [...sorts[index].order];
179 }
180 }
181 if (ordering !== null) {
182 const orderedChildren = [];
183 ordering.forEach(o => {
184 const found = terms.find(ch => o === ch.id);
185 if (found) {
186 orderedChildren.push(found);
187 }
188 });
189 // we have a case where if a set is ordered and a term is added to that set
190 // AND the ordering information hasn't been updated in the UI the new term will not have
191 // any associated ordering information. See #1547 which reported this. So here we
192 // append any terms remaining in "terms" not in "orderedChildren" to the end of "orderedChildren"
193 orderedChildren.push(...terms.filter(info => ordering.indexOf(info.id) < 0));
194 return orderedChildren;
195 }
196 return terms;
197 };
198 const visitor = async (source, parent) => {
199 const children = await source();
200 for (let i = 0; i < children.length; i++) {
201 const child = children[i];
202 childIds.push(child.id);
203 const orderedTerm = {
204 children: [],
205 defaultLabel: child.labels.find(l => l.isDefault).name,
206 ...child,
207 };
208 if (child.childrenCount > 0) {
209 await visitor(this.getTermById(children[i].id).children.select(...selects), orderedTerm.children);
210 orderedTerm.children = ensureOrder(orderedTerm.children, child.customSortOrder);
211 }
212 parent.push(orderedTerm);
213 }
214 };
215 // There is a series of issues where users expect that copied terms appear in the result of this method call. Copied terms are not "children" so we need
216 // to get all the children + all the "/terms" and filter out the children. This is expensive but this method call is already indicated to be used with caching
217 await visitor(this.children.select(...selects), tree);
218 await visitor(async () => {
219 const terms = await Terms(this).select(...selects)();
220 return terms.filter((t) => childIds.indexOf(t.id) < 0);
221 }, tree);
222 return ensureOrder(tree, null, setInfo.customSortOrder);
223 }
224}
225export const TermSet = spInvokableFactory(_TermSet);
226let _Children = class _Children extends _SPCollection {
227 /**
228 * Adds a new term set to this collection
229 * @param props The set of properties
230 * @returns The information on the create group
231 */
232 add(props) {
233 return spPost(this, body(props));
234 }
235};
236_Children = __decorate([
237 defaultPath("children")
238], _Children);
239export { _Children };
240export const Children = spInvokableFactory(_Children);
241let _Terms = class _Terms extends _SPCollection {
242 /**
243 * Gets a term group by id
244 *
245 * @param id Id of the term group to access
246 */
247 getById(id) {
248 return Term(this, id);
249 }
250};
251_Terms = __decorate([
252 defaultPath("terms")
253], _Terms);
254export { _Terms };
255export const Terms = spInvokableFactory(_Terms);
256export class _Term extends _SPInstance {
257 get children() {
258 return Children(this);
259 }
260 get relations() {
261 return Relations(this);
262 }
263 get set() {
264 return TermSet(this, "set");
265 }
266 /**
267 * Update settings for TermSet
268 *
269 * @param props The set or properties to update
270 * @returns The updated term set information
271 */
272 update(props) {
273 return spPatch(this, body(props));
274 }
275 /**
276 * Deletes this group
277 *
278 * @returns void
279 */
280 delete() {
281 return spDelete(this);
282 }
283}
284export const Term = spInvokableFactory(_Term);
285let _Relations = class _Relations extends _SPCollection {
286 /**
287 * Adds a new relation to this term
288 * @param props The set of properties
289 * @returns The information on the created relation
290 */
291 add(props) {
292 return spPost(this, body(props));
293 }
294};
295_Relations = __decorate([
296 defaultPath("relations")
297], _Relations);
298export { _Relations };
299export const Relations = spInvokableFactory(_Relations);
300//# sourceMappingURL=types.js.map
\No newline at end of file