UNPKG

10.4 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { _SPCollection, spInvokableFactory, _SPInstance, deleteableWithETag, SPQueryable, SPInstance, } from "../spqueryable.js";
3import { hOP } from "@pnp/core";
4import { escapeQueryStrValue, extractWebUrl } from "@pnp/sp";
5import { List } from "../lists/types.js";
6import { body, headers, parseBinderWithErrorCheck, parseODataJSON } from "@pnp/queryable";
7import { defaultPath } from "../decorators.js";
8import { spPost } from "../operations.js";
9/**
10 * Describes a collection of Item objects
11 *
12 */
13let _Items = class _Items extends _SPCollection {
14 /**
15 * Gets an Item by id
16 *
17 * @param id The integer id of the item to retrieve
18 */
19 getById(id) {
20 return Item(this).concat(`(${id})`);
21 }
22 /**
23 * Gets BCS Item by string id
24 *
25 * @param stringId The string id of the BCS item to retrieve
26 */
27 getItemByStringId(stringId) {
28 // creates an item with the parent list path and append out method call
29 return Item([this, this.parentUrl], `getItemByStringId('${stringId}')`);
30 }
31 /**
32 * Skips the specified number of items (https://msdn.microsoft.com/en-us/library/office/fp142385.aspx#sectionSection6)
33 *
34 * @param skip The starting id where the page should start, use with top to specify pages
35 * @param reverse It true the PagedPrev=true parameter is added allowing backwards navigation in the collection
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 * Gets a collection designed to aid in paging through data
48 *
49 */
50 getPaged() {
51 return this.using(PagedItemParser(this))();
52 }
53 /**
54 * Adds a new item to the collection
55 *
56 * @param properties The new items's properties
57 * @param listItemEntityTypeFullName The type name of the list's entities
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);
69export { _Items };
70export const Items = spInvokableFactory(_Items);
71/**
72 * Descrines a single Item instance
73 *
74 */
75export class _Item extends _SPInstance {
76 constructor() {
77 super(...arguments);
78 this.delete = deleteableWithETag();
79 }
80 /**
81 * Gets the effective base permissions for the item
82 *
83 */
84 get effectiveBasePermissions() {
85 return SPQueryable(this, "EffectiveBasePermissions");
86 }
87 /**
88 * Gets the effective base permissions for the item in a UI context
89 *
90 */
91 get effectiveBasePermissionsForUI() {
92 return SPQueryable(this, "EffectiveBasePermissionsForUI");
93 }
94 /**
95 * Gets the field values for this list item in their HTML representation
96 *
97 */
98 get fieldValuesAsHTML() {
99 return SPInstance(this, "FieldValuesAsHTML");
100 }
101 /**
102 * Gets the field values for this list item in their text representation
103 *
104 */
105 get fieldValuesAsText() {
106 return SPInstance(this, "FieldValuesAsText");
107 }
108 /**
109 * Gets the field values for this list item for use in editing controls
110 *
111 */
112 get fieldValuesForEdit() {
113 return SPInstance(this, "FieldValuesForEdit");
114 }
115 /**
116 * Gets the collection of versions associated with this item
117 */
118 get versions() {
119 return ItemVersions(this);
120 }
121 /**
122 * this item's list
123 */
124 get list() {
125 return this.getParent(List, "", this.parentUrl.substr(0, this.parentUrl.lastIndexOf("/")));
126 }
127 /**
128 * Updates this list instance with the supplied properties
129 *
130 * @param properties A plain object hash of values to update for the list
131 * @param eTag Value used in the IF-Match header, by default "*"
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 * Moves the list item to the Recycle Bin and returns the identifier of the new Recycle Bin item.
146 */
147 recycle() {
148 return spPost(Item(this, "recycle"));
149 }
150 /**
151 * Deletes the item object with options.
152 *
153 * @param parameters Specifies the options to use when deleting a item.
154 */
155 async deleteWithParams(parameters) {
156 return spPost(Item(this, "DeleteWithParameters"), body({ parameters }));
157 }
158 /**
159 * Gets a string representation of the full URL to the WOPI frame.
160 * If there is no associated WOPI application, or no associated action, an empty string is returned.
161 *
162 * @param action Display mode: 0: view, 1: edit, 2: mobileView, 3: interactivePreview
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 * Validates and sets the values of the specified collection of fields for the list item.
171 *
172 * @param formValues The fields to change and their new values.
173 * @param bNewDocumentUpdate true if the list item is a document being updated after upload; otherwise false.
174 */
175 validateUpdateListItem(formValues, bNewDocumentUpdate = false) {
176 return spPost(Item(this, "validateupdatelistitem"), body({ formValues, bNewDocumentUpdate }));
177 }
178 /**
179 * Gets the parent information for this item's list and web
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}
227export const Item = spInvokableFactory(_Item);
228/**
229 * Describes a collection of Version objects
230 *
231 */
232let _ItemVersions = class _ItemVersions extends _SPCollection {
233 /**
234 * Gets a version by id
235 *
236 * @param versionId The id of the version to retrieve
237 */
238 getById(versionId) {
239 return ItemVersion(this).concat(`(${versionId})`);
240 }
241};
242_ItemVersions = __decorate([
243 defaultPath("versions")
244], _ItemVersions);
245export { _ItemVersions };
246export const ItemVersions = spInvokableFactory(_ItemVersions);
247/**
248 * Describes a single Version instance
249 *
250 */
251export class _ItemVersion extends _SPInstance {
252 constructor() {
253 super(...arguments);
254 this.delete = deleteableWithETag();
255 }
256}
257export const ItemVersion = spInvokableFactory(_ItemVersion);
258/**
259 * Provides paging functionality for list items
260 */
261export class PagedItemCollection {
262 constructor(parent, nextUrl, results) {
263 this.parent = parent;
264 this.nextUrl = nextUrl;
265 this.results = results;
266 }
267 /**
268 * If true there are more results available in the set, otherwise there are not
269 */
270 get hasNext() {
271 return typeof this.nextUrl === "string" && this.nextUrl.length > 0;
272 }
273 /**
274 * Gets the next set of results, or resolves to null if no results are available
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}
284function 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}
291function ItemUpdatedParser() {
292 return parseBinderWithErrorCheck(async (r) => ({
293 etag: r.headers.get("etag"),
294 }));
295}
296//# sourceMappingURL=types.js.map
\No newline at end of file