1 | import { __decorate } from "tslib";
|
2 | import { isArray } from "@pnp/core";
|
3 | import { defaultPath } from "../decorators.js";
|
4 | import { _SPInstance, spInvokableFactory, _SPCollection } from "../spqueryable.js";
|
5 | import { escapeQueryStrValue } from "../utils/escape-query-str.js";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | let _TermStore = class _TermStore extends _SPInstance {
|
11 | |
12 |
|
13 |
|
14 | get groups() {
|
15 | return TermGroups(this);
|
16 | }
|
17 | |
18 |
|
19 |
|
20 | get sets() {
|
21 | return TermSets(this);
|
22 | }
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
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);
|
40 | export { _TermStore };
|
41 | export const TermStore = spInvokableFactory(_TermStore);
|
42 | let _TermGroups = class _TermGroups extends _SPCollection {
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | getById(id) {
|
49 | return TermGroup(this, id);
|
50 | }
|
51 | };
|
52 | _TermGroups = __decorate([
|
53 | defaultPath("groups")
|
54 | ], _TermGroups);
|
55 | export { _TermGroups };
|
56 | export const TermGroups = spInvokableFactory(_TermGroups);
|
57 | export class _TermGroup extends _SPInstance {
|
58 | |
59 |
|
60 |
|
61 | get sets() {
|
62 | return TermSets(this, "sets");
|
63 | }
|
64 | }
|
65 | export const TermGroup = spInvokableFactory(_TermGroup);
|
66 | let _TermSets = class _TermSets extends _SPCollection {
|
67 | |
68 |
|
69 |
|
70 |
|
71 |
|
72 | getById(id) {
|
73 | return TermSet(this, id);
|
74 | }
|
75 | };
|
76 | _TermSets = __decorate([
|
77 | defaultPath("sets")
|
78 | ], _TermSets);
|
79 | export { _TermSets };
|
80 | export const TermSets = spInvokableFactory(_TermSets);
|
81 | export class _TermSet extends _SPInstance {
|
82 | |
83 |
|
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 |
|
102 |
|
103 |
|
104 |
|
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 |
|
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 |
|
137 |
|
138 |
|
139 |
|
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 | }
|
165 | export const TermSet = spInvokableFactory(_TermSet);
|
166 | let _Children = class _Children extends _SPCollection {
|
167 | };
|
168 | _Children = __decorate([
|
169 | defaultPath("children")
|
170 | ], _Children);
|
171 | export { _Children };
|
172 | export const Children = spInvokableFactory(_Children);
|
173 | let _Terms = class _Terms extends _SPCollection {
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 | getById(id) {
|
180 | return Term(this, id);
|
181 | }
|
182 | };
|
183 | _Terms = __decorate([
|
184 | defaultPath("terms")
|
185 | ], _Terms);
|
186 | export { _Terms };
|
187 | export const Terms = spInvokableFactory(_Terms);
|
188 | export 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 | }
|
199 | export const Term = spInvokableFactory(_Term);
|
200 | let _Relations = class _Relations extends _SPCollection {
|
201 | |
202 |
|
203 |
|
204 |
|
205 |
|
206 | getById(id) {
|
207 | return Relation(this, id);
|
208 | }
|
209 | };
|
210 | _Relations = __decorate([
|
211 | defaultPath("relations")
|
212 | ], _Relations);
|
213 | export { _Relations };
|
214 | export const Relations = spInvokableFactory(_Relations);
|
215 | export 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 | }
|
226 | export const Relation = spInvokableFactory(_Relation);
|
227 |
|
\ | No newline at end of file |