1 | import { __decorate } from "tslib";
|
2 | import { isArray } from "@pnp/core";
|
3 | import { body } from "@pnp/queryable";
|
4 | import { defaultPath } from "../decorators.js";
|
5 | import { spDelete, spPatch, spPost } from "../operations.js";
|
6 | import { _SPInstance, spInvokableFactory, _SPCollection } from "../spqueryable.js";
|
7 | import { encodePath } from "../utils/encode-path-str.js";
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | let _TermStore = class _TermStore extends _SPInstance {
|
13 | |
14 |
|
15 |
|
16 | get groups() {
|
17 | return TermGroups(this);
|
18 | }
|
19 | |
20 |
|
21 |
|
22 | get sets() {
|
23 | return TermSets(this);
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
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 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | update(props) {
|
45 | return spPatch(this, body(props));
|
46 | }
|
47 | };
|
48 | _TermStore = __decorate([
|
49 | defaultPath("_api/v2.1/termstore")
|
50 | ], _TermStore);
|
51 | export { _TermStore };
|
52 | export const TermStore = spInvokableFactory(_TermStore);
|
53 | let _TermGroups = class _TermGroups extends _SPCollection {
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 | getById(id) {
|
60 | return TermGroup(this, id);
|
61 | }
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 | add(props) {
|
68 | return spPost(this, body(props));
|
69 | }
|
70 | };
|
71 | _TermGroups = __decorate([
|
72 | defaultPath("groups")
|
73 | ], _TermGroups);
|
74 | export { _TermGroups };
|
75 | export const TermGroups = spInvokableFactory(_TermGroups);
|
76 | export class _TermGroup extends _SPInstance {
|
77 | |
78 |
|
79 |
|
80 | get sets() {
|
81 | return TermSets(this, "sets");
|
82 | }
|
83 | |
84 |
|
85 |
|
86 |
|
87 |
|
88 | delete() {
|
89 | return spDelete(this);
|
90 | }
|
91 | }
|
92 | export const TermGroup = spInvokableFactory(_TermGroup);
|
93 | let _TermSets = class _TermSets extends _SPCollection {
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 | getById(id) {
|
100 | return TermSet(this, id);
|
101 | }
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 | add(props) {
|
108 | return spPost(this, body(props));
|
109 | }
|
110 | };
|
111 | _TermSets = __decorate([
|
112 | defaultPath("sets")
|
113 | ], _TermSets);
|
114 | export { _TermSets };
|
115 | export const TermSets = spInvokableFactory(_TermSets);
|
116 | export class _TermSet extends _SPInstance {
|
117 | |
118 |
|
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 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | update(props) {
|
142 | return spPatch(this, body(props));
|
143 | }
|
144 | |
145 |
|
146 |
|
147 |
|
148 |
|
149 | delete() {
|
150 | return spDelete(this);
|
151 | }
|
152 | |
153 |
|
154 |
|
155 |
|
156 |
|
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 |
|
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 |
|
190 |
|
191 |
|
192 |
|
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 |
|
216 |
|
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 | }
|
225 | export const TermSet = spInvokableFactory(_TermSet);
|
226 | let _Children = class _Children extends _SPCollection {
|
227 | |
228 |
|
229 |
|
230 |
|
231 |
|
232 | add(props) {
|
233 | return spPost(this, body(props));
|
234 | }
|
235 | };
|
236 | _Children = __decorate([
|
237 | defaultPath("children")
|
238 | ], _Children);
|
239 | export { _Children };
|
240 | export const Children = spInvokableFactory(_Children);
|
241 | let _Terms = class _Terms extends _SPCollection {
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 | getById(id) {
|
248 | return Term(this, id);
|
249 | }
|
250 | };
|
251 | _Terms = __decorate([
|
252 | defaultPath("terms")
|
253 | ], _Terms);
|
254 | export { _Terms };
|
255 | export const Terms = spInvokableFactory(_Terms);
|
256 | export 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 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | update(props) {
|
273 | return spPatch(this, body(props));
|
274 | }
|
275 | |
276 |
|
277 |
|
278 |
|
279 |
|
280 | delete() {
|
281 | return spDelete(this);
|
282 | }
|
283 | }
|
284 | export const Term = spInvokableFactory(_Term);
|
285 | let _Relations = class _Relations extends _SPCollection {
|
286 | |
287 |
|
288 |
|
289 |
|
290 |
|
291 | add(props) {
|
292 | return spPost(this, body(props));
|
293 | }
|
294 | };
|
295 | _Relations = __decorate([
|
296 | defaultPath("relations")
|
297 | ], _Relations);
|
298 | export { _Relations };
|
299 | export const Relations = spInvokableFactory(_Relations);
|
300 |
|
\ | No newline at end of file |