UNPKG

2.7 kBJavaScriptView Raw
1import { odataUrlFrom } from "../utils/odata-url-from.js";
2import { extractWebUrl } from "../utils/extract-web-url.js";
3import { Web } from "../webs/types.js";
4import "../lists/web.js";
5import { _Folder, Folder } from "../folders/types.js";
6_Folder.prototype.getDefaultColumnValues = async function () {
7 const folderProps = await Folder(this, "Properties").select("vti_x005f_listname")();
8 const { ServerRelativePath: serRelPath } = await this.select("ServerRelativePath")();
9 const web = Web([this, extractWebUrl(odataUrlFrom(folderProps))]);
10 const docLib = web.lists.getById(folderProps.vti_x005f_listname);
11 // and we return the defaults associated with this folder's server relative path only
12 // if you want all the defaults use list.getDefaultColumnValues()
13 return (await docLib.getDefaultColumnValues()).filter(v => v.path.toLowerCase() === serRelPath.DecodedUrl.toLowerCase());
14};
15_Folder.prototype.setDefaultColumnValues = async function (fieldDefaults, merge = true) {
16 // we start by figuring out where we are
17 const folderProps = await Folder(this, "Properties").select("vti_x005f_listname")();
18 // now we create a web, list and batch to get some info we need
19 const web = Web([this, extractWebUrl(odataUrlFrom(folderProps))]);
20 const docLib = web.lists.getById(folderProps.vti_x005f_listname);
21 // we need the proper folder path
22 const folderPath = (await this.select("ServerRelativePath")()).ServerRelativePath.DecodedUrl;
23 // at this point we should have all the defaults to update
24 // and we need to get all the defaults to update the entire doc
25 const existingDefaults = await docLib.getDefaultColumnValues();
26 // we filter all defaults to remove any associated with this folder if merge is false
27 const filteredExistingDefaults = merge ? existingDefaults : existingDefaults.filter(f => f.path !== folderPath);
28 // we update / add any new defaults from those passed to this method
29 fieldDefaults.forEach(d => {
30 const existing = filteredExistingDefaults.find(ed => ed.name === d.name && ed.path === folderPath);
31 if (existing) {
32 existing.value = d.value;
33 }
34 else {
35 filteredExistingDefaults.push({
36 name: d.name,
37 path: folderPath,
38 value: d.value,
39 });
40 }
41 });
42 // after this operation filteredExistingDefaults should contain all the value we want to write to the file
43 await docLib.setDefaultColumnValues(filteredExistingDefaults);
44};
45_Folder.prototype.clearDefaultColumnValues = async function () {
46 await this.setDefaultColumnValues([], false);
47};
48//# sourceMappingURL=folder.js.map
\No newline at end of file