1 | import { __decorate } from "tslib";
|
2 | import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, SPQueryable, SPInstance, } from "../spqueryable.js";
|
3 | import { hOP } from "@pnp/core";
|
4 | import { escapeQueryStrValue, extractWebUrl } from "@pnp/sp";
|
5 | import { List } from "../lists/types.js";
|
6 | import { body, headers, parseBinderWithErrorCheck, parseODataJSON } from "@pnp/queryable";
|
7 | import { defaultPath } from "../decorators.js";
|
8 | import { spPost } from "../operations.js";
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | let _Items = class _Items extends _SPCollection {
|
14 | |
15 |
|
16 |
|
17 |
|
18 |
|
19 | getById(id) {
|
20 | return Item(this).concat(`(${id})`);
|
21 | }
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 | getItemByStringId(stringId) {
|
28 |
|
29 | return Item(this.parentUrl, `getItemByStringId('${stringId}')`);
|
30 | }
|
31 | |
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | skip(skip, reverse = false) {
|
38 | if (reverse) {
|
39 | this.query.set("$skiptoken", encodeURIComponent(`Paged=TRUE&PagedPrev=TRUE&p_ID=${skip}`));
|
40 | }
|
41 | else {
|
42 | this.query.set("$skiptoken", encodeURIComponent(`Paged=TRUE&p_ID=${skip}`));
|
43 | }
|
44 | return this;
|
45 | }
|
46 | |
47 |
|
48 |
|
49 |
|
50 | getPaged() {
|
51 | return this.using(PagedItemParser(this))();
|
52 | }
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | async add(properties = {}) {
|
60 | return spPost(this, body(properties)).then((data) => ({
|
61 | data: data,
|
62 | item: this.getById(data.Id),
|
63 | }));
|
64 | }
|
65 | };
|
66 | _Items = __decorate([
|
67 | defaultPath("items")
|
68 | ], _Items);
|
69 | export { _Items };
|
70 | export const Items = spInvokableFactory(_Items);
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | export class _Item extends _SPInstance {
|
76 | constructor() {
|
77 | super(...arguments);
|
78 | this.delete = deleteableWithETag();
|
79 | }
|
80 | |
81 |
|
82 |
|
83 |
|
84 | get effectiveBasePermissions() {
|
85 | return SPQueryable(this, "EffectiveBasePermissions");
|
86 | }
|
87 | |
88 |
|
89 |
|
90 |
|
91 | get effectiveBasePermissionsForUI() {
|
92 | return SPQueryable(this, "EffectiveBasePermissionsForUI");
|
93 | }
|
94 | |
95 |
|
96 |
|
97 |
|
98 | get fieldValuesAsHTML() {
|
99 | return SPInstance(this, "FieldValuesAsHTML");
|
100 | }
|
101 | |
102 |
|
103 |
|
104 |
|
105 | get fieldValuesAsText() {
|
106 | return SPInstance(this, "FieldValuesAsText");
|
107 | }
|
108 | |
109 |
|
110 |
|
111 |
|
112 | get fieldValuesForEdit() {
|
113 | return SPInstance(this, "FieldValuesForEdit");
|
114 | }
|
115 | |
116 |
|
117 |
|
118 | get versions() {
|
119 | return ItemVersions(this);
|
120 | }
|
121 | |
122 |
|
123 |
|
124 | get list() {
|
125 | return this.getParent(List, "", this.parentUrl.substr(0, this.parentUrl.lastIndexOf("/")));
|
126 | }
|
127 | |
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | async update(properties, eTag = "*") {
|
134 | const postBody = body(properties, headers({
|
135 | "IF-Match": eTag,
|
136 | "X-HTTP-Method": "MERGE",
|
137 | }));
|
138 | const data = await spPost(Item(this).using(ItemUpdatedParser()), postBody);
|
139 | return {
|
140 | data,
|
141 | item: this,
|
142 | };
|
143 | }
|
144 | |
145 |
|
146 |
|
147 | recycle() {
|
148 | return spPost(Item(this, "recycle"));
|
149 | }
|
150 | |
151 |
|
152 |
|
153 |
|
154 |
|
155 | async deleteWithParams(parameters) {
|
156 | return spPost(Item(this, "DeleteWithParameters"), body({ parameters }));
|
157 | }
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | async getWopiFrameUrl(action = 0) {
|
165 | const i = Item(this, "getWOPIFrameUrl(@action)");
|
166 | i.query.set("@action", action);
|
167 | return spPost(i);
|
168 | }
|
169 | |
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | validateUpdateListItem(formValues, bNewDocumentUpdate = false) {
|
176 | return spPost(Item(this, "validateupdatelistitem"), body({ formValues, bNewDocumentUpdate }));
|
177 | }
|
178 | |
179 |
|
180 |
|
181 | async getParentInfos() {
|
182 | const urlInfo = await this.select("Id", "ParentList/Id", "ParentList/Title", "ParentList/RootFolder/UniqueId", "ParentList/RootFolder/ServerRelativeUrl", "ParentList/RootFolder/ServerRelativePath", "ParentList/ParentWeb/Id", "ParentList/ParentWeb/Url", "ParentList/ParentWeb/ServerRelativeUrl", "ParentList/ParentWeb/ServerRelativePath").expand("ParentList", "ParentList/RootFolder", "ParentList/ParentWeb")();
|
183 | return {
|
184 | Item: {
|
185 | Id: urlInfo.Id,
|
186 | },
|
187 | ParentList: {
|
188 | Id: urlInfo.ParentList.Id,
|
189 | Title: urlInfo.ParentList.Title,
|
190 | RootFolderServerRelativePath: urlInfo.ParentList.RootFolder.ServerRelativePath,
|
191 | RootFolderServerRelativeUrl: urlInfo.ParentList.RootFolder.ServerRelativeUrl,
|
192 | RootFolderUniqueId: urlInfo.ParentList.RootFolder.UniqueId,
|
193 | },
|
194 | ParentWeb: {
|
195 | Id: urlInfo.ParentList.ParentWeb.Id,
|
196 | ServerRelativePath: urlInfo.ParentList.ParentWeb.ServerRelativePath,
|
197 | ServerRelativeUrl: urlInfo.ParentList.ParentWeb.ServerRelativeUrl,
|
198 | Url: urlInfo.ParentList.ParentWeb.Url,
|
199 | },
|
200 | };
|
201 | }
|
202 | async setImageField(fieldName, imageName, imageContent) {
|
203 | const contextInfo = await this.getParentInfos();
|
204 | const webUrl = extractWebUrl(this.toUrl());
|
205 | const q = SPQueryable(webUrl, "/_api/web/UploadImage");
|
206 | q.concat("(listTitle=@a1,imageName=@a2,listId=@a3,itemId=@a4)");
|
207 | q.query.set("@a1", `'${escapeQueryStrValue(contextInfo.ParentList.Title)}'`);
|
208 | q.query.set("@a2", `'${escapeQueryStrValue(imageName)}'`);
|
209 | q.query.set("@a3", `'${escapeQueryStrValue(contextInfo.ParentList.Id)}'`);
|
210 | q.query.set("@a4", contextInfo.Item.Id);
|
211 | const result = await spPost(q, { body: imageContent });
|
212 | const itemInfo = {
|
213 | "type": "thumbnail",
|
214 | "fileName": result.Name,
|
215 | "nativeFile": {},
|
216 | "fieldName": fieldName,
|
217 | "serverUrl": contextInfo.ParentWeb.Url.replace(contextInfo.ParentWeb.ServerRelativeUrl, ""),
|
218 | "serverRelativeUrl": result.ServerRelativeUrl,
|
219 | "id": result.UniqueId,
|
220 | };
|
221 | return this.validateUpdateListItem([{
|
222 | FieldName: fieldName,
|
223 | FieldValue: JSON.stringify(itemInfo),
|
224 | }]);
|
225 | }
|
226 | }
|
227 | export const Item = spInvokableFactory(_Item);
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | let _ItemVersions = class _ItemVersions extends _SPCollection {
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 | getById(versionId) {
|
239 | return ItemVersion(this).concat(`(${versionId})`);
|
240 | }
|
241 | };
|
242 | _ItemVersions = __decorate([
|
243 | defaultPath("versions")
|
244 | ], _ItemVersions);
|
245 | export { _ItemVersions };
|
246 | export const ItemVersions = spInvokableFactory(_ItemVersions);
|
247 |
|
248 |
|
249 |
|
250 |
|
251 | export class _ItemVersion extends _SPInstance {
|
252 | constructor() {
|
253 | super(...arguments);
|
254 | this.delete = deleteableWithETag();
|
255 | }
|
256 | }
|
257 | export const ItemVersion = spInvokableFactory(_ItemVersion);
|
258 |
|
259 |
|
260 |
|
261 | export class PagedItemCollection {
|
262 | constructor(parent, nextUrl, results) {
|
263 | this.parent = parent;
|
264 | this.nextUrl = nextUrl;
|
265 | this.results = results;
|
266 | }
|
267 | |
268 |
|
269 |
|
270 | get hasNext() {
|
271 | return typeof this.nextUrl === "string" && this.nextUrl.length > 0;
|
272 | }
|
273 | |
274 |
|
275 |
|
276 | async getNext() {
|
277 | if (this.hasNext) {
|
278 | const items = Items([this.parent, this.nextUrl], "");
|
279 | return items.getPaged();
|
280 | }
|
281 | return null;
|
282 | }
|
283 | }
|
284 | function PagedItemParser(parent) {
|
285 | return parseBinderWithErrorCheck(async (r) => {
|
286 | const json = await r.json();
|
287 | const nextUrl = hOP(json, "d") && hOP(json.d, "__next") ? json.d.__next : json["odata.nextLink"];
|
288 | return new PagedItemCollection(parent, nextUrl, parseODataJSON(json));
|
289 | });
|
290 | }
|
291 | function ItemUpdatedParser() {
|
292 | return parseBinderWithErrorCheck(async (r) => ({
|
293 | etag: r.headers.get("etag"),
|
294 | }));
|
295 | }
|
296 |
|
\ | No newline at end of file |