@kwiz/common 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. package/package.json +1 -1
  2. package/src/_dependencies.ts +0 -2
  3. package/src/helpers/Guid.ts +0 -182
  4. package/src/helpers/_dependencies.ts +0 -3
  5. package/src/helpers/base64.ts +0 -174
  6. package/src/helpers/browser.test.js +0 -9
  7. package/src/helpers/browser.ts +0 -1299
  8. package/src/helpers/browserinfo.ts +0 -293
  9. package/src/helpers/collections.base.test.js +0 -26
  10. package/src/helpers/collections.base.ts +0 -439
  11. package/src/helpers/collections.ts +0 -108
  12. package/src/helpers/color.ts +0 -55
  13. package/src/helpers/cookies.ts +0 -55
  14. package/src/helpers/date.test.js +0 -97
  15. package/src/helpers/date.ts +0 -163
  16. package/src/helpers/debug.ts +0 -187
  17. package/src/helpers/emails.ts +0 -7
  18. package/src/helpers/eval.ts +0 -5
  19. package/src/helpers/file.test.js +0 -51
  20. package/src/helpers/file.ts +0 -59
  21. package/src/helpers/flatted.ts +0 -150
  22. package/src/helpers/functions.ts +0 -17
  23. package/src/helpers/graph/calendar.types.ts +0 -11
  24. package/src/helpers/http.ts +0 -70
  25. package/src/helpers/images.ts +0 -23
  26. package/src/helpers/index.ts +0 -29
  27. package/src/helpers/json.ts +0 -39
  28. package/src/helpers/md5.ts +0 -190
  29. package/src/helpers/objects.test.js +0 -27
  30. package/src/helpers/objects.ts +0 -256
  31. package/src/helpers/promises.test.js +0 -18
  32. package/src/helpers/promises.ts +0 -102
  33. package/src/helpers/random.ts +0 -27
  34. package/src/helpers/scheduler/scheduler.test.js +0 -104
  35. package/src/helpers/scheduler/scheduler.ts +0 -132
  36. package/src/helpers/sharepoint.ts +0 -681
  37. package/src/helpers/strings.test.js +0 -43
  38. package/src/helpers/strings.ts +0 -288
  39. package/src/helpers/typecheckers.test.js +0 -35
  40. package/src/helpers/typecheckers.ts +0 -263
  41. package/src/helpers/url.test.js +0 -18
  42. package/src/helpers/url.ts +0 -202
  43. package/src/helpers/urlhelper.ts +0 -104
  44. package/src/index.ts +0 -24
  45. package/src/types/common.types.ts +0 -16
  46. package/src/types/flatted.types.ts +0 -60
  47. package/src/types/globals.types.ts +0 -7
  48. package/src/types/graph/calendar.types.ts +0 -81
  49. package/src/types/graph/index.ts +0 -1
  50. package/src/types/index.ts +0 -12
  51. package/src/types/knownscript.types.ts +0 -19
  52. package/src/types/libs/datajs.types.ts +0 -29
  53. package/src/types/libs/ics.types.ts +0 -31
  54. package/src/types/libs/index.ts +0 -4
  55. package/src/types/libs/msal.types.ts +0 -79
  56. package/src/types/locales.ts +0 -125
  57. package/src/types/localstoragecache.types.ts +0 -9
  58. package/src/types/moment.ts +0 -7
  59. package/src/types/regex.types.ts +0 -17
  60. package/src/types/rest.types.ts +0 -94
  61. package/src/types/sharepoint.types.ts +0 -1413
  62. package/src/types/sharepoint.utils.types.ts +0 -246
  63. package/src/utils/_dependencies.ts +0 -4
  64. package/src/utils/base64.ts +0 -27
  65. package/src/utils/consolelogger.ts +0 -315
  66. package/src/utils/date.ts +0 -36
  67. package/src/utils/emails.ts +0 -25
  68. package/src/utils/index.ts +0 -13
  69. package/src/utils/knownscript.ts +0 -280
  70. package/src/utils/localstoragecache.ts +0 -419
  71. package/src/utils/rest.ts +0 -465
  72. package/src/utils/script.ts +0 -168
  73. package/src/utils/sharepoint.rest/common.ts +0 -149
  74. package/src/utils/sharepoint.rest/date.ts +0 -61
  75. package/src/utils/sharepoint.rest/file.folder.ts +0 -368
  76. package/src/utils/sharepoint.rest/index.ts +0 -11
  77. package/src/utils/sharepoint.rest/item.ts +0 -456
  78. package/src/utils/sharepoint.rest/list.ts +0 -1144
  79. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +0 -750
  80. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +0 -265
  81. package/src/utils/sharepoint.rest/listutils/common.ts +0 -202
  82. package/src/utils/sharepoint.rest/user-search.ts +0 -254
  83. package/src/utils/sharepoint.rest/user.ts +0 -447
  84. package/src/utils/sharepoint.rest/web.ts +0 -1031
  85. package/src/utils/sod.ts +0 -193
  86. package/tsconfig.json +0 -21
@@ -1,681 +0,0 @@
1
- import { FieldTypeAsString, FieldTypes, IDictionary, IFieldCalculatedInfo, IFieldInfo, IFieldInfoEX, IFieldJsonSchema, IFieldTaxonomyInfo, PrincipalType, SPBasePermissionKind, ThumbnailValueType, UrlValueType, UserEntityValueType } from "./_dependencies";
2
- import { firstOrNull, forEach } from "./collections.base";
3
- import { deleteCookie, getCookie, setCookie } from "./cookies";
4
- import { isValidEmail } from "./emails";
5
- import { jsonParse } from "./json";
6
- import { hasOwnProperty } from "./objects";
7
- import { isValidDomainLogin, normalizeGuid } from "./strings";
8
- import { isNotEmptyArray, isNullOrEmptyString, isNullOrNaN, isNullOrUndefined, isNumeric, isString, isUndefined, isValidGuid } from "./typecheckers";
9
- import { normalizeUrl } from "./url";
10
-
11
- export const KWIZ_CONTROLLER_FIELD_NAME = "kwizcomcontrollerfield";
12
- const MODERN_EXPERIENCE_COOKIE_NAME = "splnu";
13
- const MODERN_EXPERIENCE_TEMP_COOKIE_NAME = `${MODERN_EXPERIENCE_COOKIE_NAME}_kwizcom_original`;
14
- const MOBILE_EXPERIENCE_COOKIE_NAME = "mobile";
15
- const MOBILE_EXPERIENCE_TEMP_COOKIE_NAME = `${MOBILE_EXPERIENCE_COOKIE_NAME}_kwizcom_original`;
16
-
17
- //const logger = ConsoleLogger.get("_modules/helpers/sharepoint");
18
- export function IsClassicPage() {
19
- //on premises has g_spribbon but no g_Workspace
20
- //can't use g_spribbon because it gets created whenever you load the init.js script
21
- if (!isUndefined(window)
22
- && window.document
23
- && document.body
24
- && document.body.childNodes.length
25
- && document.body.childNodes) {
26
- //only classic pages have the s4-workspace element, so try this first because it is the most reliable
27
- return !!document.getElementById("s4-workspace");
28
- } else if (!isUndefined((<any>window)._spWebPartComponents)) {
29
- //only classic pages have the _spWebPartComponents object created on the page inline, not in a script that
30
- //can be loaded
31
- return true;
32
- } else if (!isUndefined((<any>window)._spClientSideComponentIds)) {
33
- //only modern pages have the _spClientSideComponentIds object created on the page inline, not a in script that
34
- //can be loaded
35
- return false;
36
- } else if (!isUndefined((<any>window).g_Workspace)) {
37
- //online has g_Workspace
38
- return true;
39
- } else {
40
- return false;
41
- }
42
- }
43
-
44
- export function restoreExperience() {
45
- var splnu_original = getCookie(MODERN_EXPERIENCE_TEMP_COOKIE_NAME);
46
- deleteCookie(MODERN_EXPERIENCE_TEMP_COOKIE_NAME);
47
- deleteCookie(MODERN_EXPERIENCE_COOKIE_NAME);
48
-
49
- if (isString(splnu_original)) {
50
- setCookie(MODERN_EXPERIENCE_COOKIE_NAME, splnu_original);
51
- }
52
-
53
- var mobile_original = getCookie(MOBILE_EXPERIENCE_TEMP_COOKIE_NAME);
54
- deleteCookie(MOBILE_EXPERIENCE_TEMP_COOKIE_NAME);
55
- deleteCookie(MOBILE_EXPERIENCE_COOKIE_NAME);
56
-
57
- if (isString(mobile_original)) {
58
- setCookie(MOBILE_EXPERIENCE_COOKIE_NAME, mobile_original, null, "/");
59
- }
60
- }
61
-
62
- export function ensureClassicExperience(listId: string) {
63
- var splnu = getCookie(MODERN_EXPERIENCE_COOKIE_NAME);
64
- var mobile = getCookie(MOBILE_EXPERIENCE_COOKIE_NAME);
65
- if (isString(splnu)) {
66
- setCookie(MODERN_EXPERIENCE_TEMP_COOKIE_NAME, splnu);
67
- }
68
- if (isString(mobile)) {
69
- setCookie(MOBILE_EXPERIENCE_TEMP_COOKIE_NAME, splnu);
70
- }
71
- setCookie(MOBILE_EXPERIENCE_COOKIE_NAME, "0", null, "/");
72
- switchToClassicExperience(listId);
73
- }
74
-
75
- export function setExperienceCookie(value: string, reload?: boolean) {
76
- setCookie(MODERN_EXPERIENCE_COOKIE_NAME, value);
77
- if (reload === true) {
78
- window.location.reload();
79
- }
80
- }
81
-
82
- export function switchToClassicExperience(listId: string, reload?: boolean) {
83
- setExperienceCookie(listId ? `{${normalizeGuid(listId)}}` : "0", reload);
84
- }
85
-
86
- export function switchToModernExperience(reload?: boolean) {
87
- setExperienceCookie("1", reload);
88
- }
89
-
90
- /** Gets field schema XML and converts it into JSON object */
91
- export function SchemaXmlToJson(xml: string): IFieldJsonSchema {
92
- let result: IFieldJsonSchema = { Attributes: {}, Customizations: {} };
93
- try {
94
- if (!isNullOrEmptyString(xml !== null)) {
95
- //IE9+ supports this, we don't need to support IE8 anymore
96
- let SchemaXmlDoc: Document = new DOMParser().parseFromString(xml, "text/xml");
97
- let xField = SchemaXmlDoc.getElementsByTagName("Field")[0];
98
- for (var i = 0; i < xField.attributes.length; i++) {
99
- result.Attributes[xField.attributes[i].name] = xField.attributes[i].value;
100
- }
101
-
102
- let properties = xField.querySelectorAll("Customization>ArrayOfProperty>Property");
103
- if (properties && properties.length > 0) {
104
- properties.forEach(p => {
105
- let name = p.querySelector("Name");
106
- let value = p.querySelector("Value");
107
- if (name && value && !isNullOrEmptyString(name.textContent))
108
- result.Customizations[name.textContent] = value.textContent;
109
- });
110
- }
111
- }
112
- } catch (e) { }
113
- return result;
114
- }
115
- export function SchemaJsonToXml(json: IFieldJsonSchema): string {
116
- let doc = new Document();
117
- let fieldElm = doc.createElement("Field");
118
- forEach(json.Attributes, (name, value) => {
119
- fieldElm.setAttribute(name, value);
120
- });
121
- if (Object.keys(json.Customizations).length) {
122
- let custElm = doc.createElement("Customization");
123
- fieldElm.appendChild(custElm);
124
- let arrElm = doc.createElement("ArrayOfProperty");
125
- custElm.appendChild(arrElm);
126
- forEach(json.Customizations, (name, value) => {
127
- let propElm = doc.createElement("Property");
128
- arrElm.appendChild(propElm);
129
-
130
- let nameElm = doc.createElement("Name");
131
- propElm.appendChild(nameElm);
132
- let valElm = doc.createElement("Value");
133
- propElm.appendChild(valElm);
134
- nameElm.innerText = name;
135
- valElm.innerText = value;
136
- });
137
- }
138
- return fieldElm.outerHTML;
139
- }
140
-
141
- export function NormalizeListName(list: { EntityTypeName: string; BaseType: number; }): string {
142
- let Name = list.EntityTypeName;//get list name. if it is a list, it will be [Name]List so cut the list out.
143
- try {
144
- if (list.BaseType === 0 && Name.endsWith("List"))
145
- Name = Name.substr(0, Name.length - 4);//remove List
146
- } catch (e) { }
147
- return Name;
148
- }
149
-
150
- export class SPBasePermissions {
151
- private $High = 0;
152
- private $Low = 0;
153
- public constructor(EffectiveBasePermissions: { High: number; Low: number; }) {
154
- this.initPropertiesFromJson(EffectiveBasePermissions);
155
- }
156
- public set(perm: SPBasePermissionKind) {
157
- if (perm === SPBasePermissionKind.FullMask) {
158
- this.$Low = 65535;
159
- this.$High = 32767;
160
- return;
161
- }
162
-
163
- if (!perm) {
164
- this.$Low = 0;
165
- this.$High = 0;
166
- return;
167
- }
168
- var $v_0 = perm;
169
-
170
- $v_0 = $v_0 - 1;
171
- var $v_1 = 1;
172
-
173
- if ($v_0 >= 0 && $v_0 < 32) {
174
- $v_1 = $v_1 << $v_0;
175
- this.$Low = this.$Low | $v_1;
176
- }
177
- else if ($v_0 >= 32 && $v_0 < 64) {
178
- $v_1 = $v_1 << $v_0 - 32;
179
- this.$High = this.$High | $v_1;
180
- }
181
- }
182
- public clear(perm) {
183
- var $v_0 = perm;
184
-
185
- $v_0 = $v_0 - 1;
186
- var $v_1 = 1;
187
-
188
- if ($v_0 >= 0 && $v_0 < 32) {
189
- $v_1 = $v_1 << $v_0;
190
- $v_1 = ~$v_1;
191
- this.$Low = this.$Low & $v_1;
192
- }
193
- else if ($v_0 >= 32 && $v_0 < 64) {
194
- $v_1 = $v_1 << $v_0 - 32;
195
- $v_1 = ~$v_1;
196
- this.$High = this.$High & $v_1;
197
- }
198
- }
199
- public clearAll() {
200
- this.$High = 0;
201
- this.$Low = 0;
202
- }
203
- public has(perm: SPBasePermissionKind) {
204
- if (!perm) {
205
- return true;
206
- }
207
- if (perm === SPBasePermissionKind.FullMask) {
208
- return (this.$High & 32767) === 32767 && this.$Low === 65535;
209
- }
210
- var $v_0 = perm;
211
-
212
- $v_0 = $v_0 - 1;
213
- var $v_1 = 1;
214
-
215
- if ($v_0 >= 0 && $v_0 < 32) {
216
- $v_1 = $v_1 << $v_0;
217
- return 0 !== (this.$Low & $v_1);
218
- }
219
- else if ($v_0 >= 32 && $v_0 < 64) {
220
- $v_1 = $v_1 << $v_0 - 32;
221
- return 0 !== (this.$High & $v_1);
222
- }
223
- return false;
224
- }
225
- public hasAny(...requestedPerms: SPBasePermissionKind[]) {
226
- return (requestedPerms || []).some((t) => {
227
- return this.has(t);
228
- });
229
- }
230
- public haAll(...requestedPerms: SPBasePermissionKind[]) {
231
- return (requestedPerms || []).every((t) => {
232
- return this.has(t);
233
- });
234
- }
235
- public hasPermissions(requestedPerms: { High: number; Low: number; }) {
236
- return (this.$High & requestedPerms.High) === requestedPerms.High && (this.$Low & requestedPerms.Low) === requestedPerms.Low;
237
- }
238
- public hasAnyPermissions(...requestedPerms: { High: number; Low: number; }[]) {
239
- return (requestedPerms || []).some((t) => {
240
- return this.hasPermissions(t);
241
- });
242
- }
243
- public hasAllPermissions(...requestedPerms: { High: number; Low: number; }[]) {
244
- return (requestedPerms || []).every((t) => {
245
- return this.hasPermissions(t);
246
- });
247
- }
248
- public initPropertiesFromJson(EffectiveBasePermissions: { High: number; Low: number; }) {
249
- this.$High = 0;
250
- this.$Low = 0;
251
- if (isNullOrUndefined(EffectiveBasePermissions)) return;
252
-
253
- if (!isNullOrNaN(EffectiveBasePermissions.High)) {
254
- this.$High = EffectiveBasePermissions.High;
255
- }
256
- if (!isNullOrNaN(EffectiveBasePermissions.Low)) {
257
- this.$Low = EffectiveBasePermissions.Low;
258
- }
259
- }
260
- }
261
-
262
- export interface ISPPeoplePickerControlFormEntity {
263
- /** ie: i:0#.f|membership|user@kwizcom.com */
264
- Key: string;
265
- EntityType: "FormsRole" | "SecGroup" | "SPGroup";
266
- EntityData?: {
267
- SPGroupID?: string;
268
- PrincipalType?: "User" | "SecurityGroup" | "SharePointGroup";
269
- /** string of number "8" */
270
- SPUserID?: string;
271
- SIPAddress?: string;
272
- Email?: string;
273
- };
274
- Resolved?: boolean;
275
- }
276
-
277
- /** remove i:0#.f|membership| prefix from login */
278
- export function CleanupUserClaimsLogin(login: string) {
279
- if (login.indexOf('|membership|') >= 0)
280
- return login.slice(login.lastIndexOf('|') + 1);
281
- else return login;
282
- }
283
-
284
- export function IsSPPeoplePickerControlFormEntity(entity: any): entity is ISPPeoplePickerControlFormEntity {
285
- let asType = entity as ISPPeoplePickerControlFormEntity;
286
- return !isNullOrUndefined(entity)
287
- && !isNullOrEmptyString(asType.Key)
288
- && (!isNullOrUndefined(asType.EntityData) || !isNullOrUndefined(asType.Resolved));
289
- }
290
-
291
- export function getPrincipalTypeFromPickerEntity(entity: ISPPeoplePickerControlFormEntity): PrincipalType.SecurityGroup | PrincipalType.SharePointGroup | PrincipalType.User {
292
- if (entity.EntityType === "FormsRole"
293
- || entity.EntityType === "SecGroup"
294
- || entity.EntityData && entity.EntityData.PrincipalType === "SecurityGroup") {
295
- return PrincipalType.SecurityGroup;
296
- }
297
-
298
- if (entity.EntityType === "SPGroup"
299
- || entity.EntityData && (isNumeric(entity.EntityData.SPGroupID) || entity.EntityData.PrincipalType === "SharePointGroup")) {
300
- return PrincipalType.SharePointGroup;
301
- }
302
-
303
- if (entity.EntityType === "User" || entity.EntityType === "" && entity.EntityData && entity.EntityData.PrincipalType === "User") {
304
- if (entity.EntityData && isValidEmail(entity.EntityData.Email) || isString(entity.EntityData.SIPAddress)) {
305
- return PrincipalType.User;
306
- }
307
-
308
- var keyparts = entity.Key.split("|");
309
- if (keyparts.length === 3 && isValidEmail(keyparts[keyparts.length - 1])) {
310
- //sharepoint online key for a user is in the form xxxx|membership|email;
311
- return PrincipalType.User;
312
- } else if (keyparts.length === 2 && isValidDomainLogin(keyparts[keyparts.length - 1])) {
313
- //sharepoint onpremise key for a user is in the form xxxx|domain\\user;
314
- return PrincipalType.User;
315
- } else {
316
- //SharePoint groups on saved classic forms item are shown with EntityType = User but dont have a SIPAddress/Email
317
- //and the key does not contain a valid domain login/email
318
- return PrincipalType.SharePointGroup;
319
- }
320
- }
321
-
322
- return PrincipalType.User;
323
- }
324
-
325
- /** rest object might put array values under ".results", this will place them at the property value directly */
326
- export function NormalizeRestObject<T>(o: T): T {
327
- //extract collections such as choice field "choices"
328
- if (o) {
329
- Object.keys(o as any).forEach(key => {
330
- if (!isNullOrUndefined(o[key]) && hasOwnProperty(o[key], "results") && Array.isArray(o[key].results))
331
- o[key] = o[key].results;
332
- });
333
- }
334
- return o;
335
- }
336
-
337
- /**
338
- * Extends a field info into field info EX (adding SchemaJson)
339
- * @param field REST field data
340
- * @param allFields Optional - all list fields, used for discovering multi TaxonomyField's update hidden text field
341
- */
342
- export function extendFieldInfo(field: IFieldInfo, allFields?: IFieldInfo[]): IFieldInfoEX {
343
- let fieldEx = field as IFieldInfoEX;
344
- fieldEx.Id = normalizeGuid(field.Id);
345
-
346
- if (field.InternalName === "ContentType") {
347
- fieldEx.Required = true;
348
- }
349
- else {
350
- switch (fieldEx.TypeAsString) {
351
- case "TaxonomyFieldTypeMulti"://find the hidden rich text for updates!
352
- {
353
- let taxonomyField = (fieldEx as IFieldTaxonomyInfo);
354
- let textFieldId = normalizeGuid(taxonomyField.TextField);
355
- let related = isNotEmptyArray(allFields) ? firstOrNull(allFields, relatedField => relatedField.Id === textFieldId) : null;
356
- if (related !== null)
357
- taxonomyField.HiddenMultiValueFieldName = related.InternalName;
358
- }
359
- break;
360
- }
361
- }
362
-
363
- fieldEx.OutputTypeAsString = getFieldOutputType(fieldEx);
364
-
365
- if (isNullOrUndefined(fieldEx.SchemaJson)) {
366
- Object.defineProperty(fieldEx, 'SchemaJson', {
367
- get: function () {
368
- if (isUndefined(this._schemaJson)) {
369
- this._schemaJson = SchemaXmlToJson(this.SchemaXml);
370
- }
371
- return this._schemaJson;
372
- }
373
- });
374
- }
375
-
376
- if (field.InternalName === KWIZ_CONTROLLER_FIELD_NAME) {
377
- //not hidden by SharePoint so its shown in views/forms but as far as our products concerned - should be treated as hidden
378
- field.Hidden = true;
379
- }
380
-
381
- return fieldEx;
382
- }
383
-
384
- export function getFieldOutputType(field: IFieldInfo) {
385
- let outputType = field.TypeAsString;
386
-
387
- if (outputType === "Calculated") {
388
- switch ((field as IFieldCalculatedInfo).OutputType) {
389
- case FieldTypes.DateTime:
390
- outputType = "DateTime";
391
- break;
392
- case FieldTypes.Boolean:
393
- outputType = "Boolean";
394
- break;
395
- case FieldTypes.Currency:
396
- outputType = "Currency";
397
- break;
398
- case FieldTypes.Number:
399
- outputType = "Number";
400
- break;
401
- default:
402
- outputType = "Text";
403
- break;
404
- }
405
- }
406
-
407
- return outputType as FieldTypeAsString;
408
- }
409
-
410
- export function isDocLib(list?: { BaseType: number; }): boolean {
411
- return list && list.BaseType === 1;
412
- }
413
-
414
- export function GetOrderByFromCaml(camlQuery: string): { Name: string; IsAscending: boolean; }[] {
415
- let xmlDoc = new DOMParser().parseFromString(camlQuery, "text/xml");
416
-
417
- let orderByElm = xmlDoc.querySelector("OrderBy");
418
- let OrderBy: { Name: string; IsAscending: boolean; }[] = [];
419
- if (orderByElm) {
420
- let orderFieldsElms = orderByElm.querySelectorAll("FieldRef");
421
- orderFieldsElms.forEach(f => {
422
- let name = f.getAttribute("Name");
423
- let asc = f.getAttribute("Ascending") || "";
424
- //Issue 1019 default value is true if ommitted - https://learn.microsoft.com/en-us/sharepoint/dev/schema/fieldref-element-query
425
- let IsAscending = asc.toUpperCase() !== "FALSE";
426
- if (!isNullOrEmptyString(name))
427
- OrderBy.push({ Name: name, IsAscending: IsAscending });
428
- });
429
- }
430
-
431
- return OrderBy;
432
- }
433
-
434
- export function RemoveOrderByFromCaml(camlQuery: string): string {
435
- let xmlDoc = new DOMParser().parseFromString(camlQuery, "text/xml");
436
-
437
- let orderByElm = xmlDoc.querySelector("OrderBy");
438
- //let OrderBy: { Name: string; IsAscending: boolean; }[] = [];
439
- if (orderByElm) {
440
- orderByElm.remove();
441
- return xmlDoc.documentElement.outerHTML;
442
- }
443
-
444
- return camlQuery;
445
- }
446
-
447
- export function EnsureViewFields(camlQuery: string, fields: string[], forceCreateViewFields: boolean, removeAllOthers?: boolean) {
448
- let xmlDoc = new DOMParser().parseFromString(camlQuery, "text/xml");
449
- let viewElm = xmlDoc.querySelector("View");
450
- if (!isNullOrUndefined(viewElm)) {
451
- let viewFieldsElm = viewElm.querySelector("ViewFields");
452
-
453
- if (forceCreateViewFields && isNullOrUndefined(viewFieldsElm)) {
454
- viewFieldsElm = xmlDoc.createElement("ViewFields");
455
- viewElm.appendChild(viewFieldsElm);
456
- }
457
-
458
- if (!isNullOrUndefined(viewFieldsElm)) {
459
- let viewFieldsElms = viewFieldsElm.querySelectorAll("FieldRef");
460
-
461
- if (removeAllOthers)
462
- viewFieldsElms.forEach(e => e.remove());
463
-
464
- let viewFields = removeAllOthers ? [] : Array.from(viewFieldsElms).map(viewFieldNode => {
465
- let name = viewFieldNode.getAttribute("Name");
466
- return name.toLowerCase();
467
- });
468
-
469
- let changed = false;
470
- fields.forEach(f => {
471
- if (viewFields.indexOf(f.toLowerCase()) === -1) {
472
- let newViewFieldElm = xmlDoc.createElement("FieldRef");
473
- newViewFieldElm.setAttribute("Name", f);
474
- viewFieldsElm.appendChild(newViewFieldElm);
475
- changed = true;
476
- }
477
- });
478
-
479
- if (viewFieldsElm.querySelectorAll("FieldRef").length < 1 && !forceCreateViewFields) {
480
- //don't leave an empty object
481
- viewFieldsElm.remove();
482
- changed = true;
483
- }
484
-
485
- if (changed) return xmlDoc.documentElement.outerHTML;
486
- }
487
- }
488
-
489
- return camlQuery;
490
- }
491
-
492
- /**If it is a thumbnail field - parse and return a typed value */
493
- export function ParseThumbnalFieldValue(value?: any): ThumbnailValueType {
494
- if (!isNullOrEmptyString(value)) {
495
- try {
496
- let parsed = jsonParse<ThumbnailValueType>(value);
497
- if (parsed && !isNullOrEmptyString(parsed.serverRelativeUrl))
498
- return parsed;
499
-
500
- } catch (e) {
501
-
502
- }
503
- }
504
- return null;
505
- }
506
-
507
- export function isTitleField(fieldName: string) {
508
- return fieldName === "Title" || fieldName === "LinkTitleNoMenu" || fieldName === "LinkTitle";
509
- }
510
-
511
- /** we are on a list view page, not a web part page with possible multiple list views */
512
- export function isSingleViewPage() {
513
- return !isNullOrUndefined(_spPageContextInfo) && isValidGuid(_spPageContextInfo.viewId);
514
- }
515
-
516
- /**
517
- * Splits the ViewFields of a CAML query into separate entries based on the batch size.
518
- * @param {string} camlQuery - The CAML query string.
519
- * @param {number} batchSize - The size of each batch (number of ViewFields per entry).
520
- */
521
- export function splitViewFieldsByBatch(camlQuery: string, allListFieldsToLowerHash: IDictionary<IFieldInfoEX>, batchSize: number): string[] {
522
- let xmlDoc = new DOMParser().parseFromString(camlQuery, 'text/xml');
523
- let viewNode = xmlDoc.querySelector("View, view");
524
- let viewFieldsNode = viewNode && viewNode.querySelector("ViewFields, viewfields");
525
-
526
- if (isNullOrUndefined(viewFieldsNode)) {
527
- return [camlQuery]; // No ViewFields element found, return the original query as is
528
- }
529
-
530
- let viewFieldNodes = Array.from(viewFieldsNode.children);
531
- let numberOfEntries = Math.ceil(viewFieldNodes.length / batchSize);
532
-
533
- let splitQueries: string[] = [];
534
- for (let i = 0; i < numberOfEntries; i++) {
535
- let startIndex = i * batchSize;
536
- let endIndex = startIndex + batchSize;
537
- let slicedViewFields = viewFieldNodes.slice(startIndex, endIndex);
538
-
539
- let clonedXmlDoc = xmlDoc.cloneNode(true) as XMLDocument;
540
- let clonedViewFieldsElement = clonedXmlDoc.getElementsByTagName('ViewFields')[0];
541
-
542
- // Remove existing child nodes from cloned ViewFields
543
- while (clonedViewFieldsElement.firstChild) {
544
- clonedViewFieldsElement.removeChild(clonedViewFieldsElement.firstChild);
545
- }
546
-
547
- // Append sliced ViewFields to cloned ViewFields
548
- for (let slicedViewField of slicedViewFields) {
549
- clonedViewFieldsElement.appendChild(slicedViewField.cloneNode(true));
550
- }
551
-
552
- let splitQuery = new XMLSerializer().serializeToString(clonedXmlDoc);
553
- splitQueries.push(splitQuery);
554
- }
555
-
556
- return splitQueries;
557
- }
558
-
559
- /** Size=S = 48×48 px, M = 72×72 px, L = 300×300 px */
560
- export function UserPhoto(siteUrl: string, userName: string, size: "S" | "M" | "L" = "L") {
561
- return `${normalizeUrl(siteUrl)}/_layouts/15/userphoto.aspx?size=${size}&accountname=${encodeURIComponent(userName)}`;
562
- }
563
-
564
- export function IsFolderContentType(contentTypeId: string) {
565
- //item:0x0100
566
- //file:0x0101
567
- //folder:0x0120
568
- //item in MS Lists:0x00 Issue 7121
569
- return contentTypeId.startsWith("0x0120");
570
- }
571
-
572
- export enum PageContainerTypes {
573
- M365SPFx, M365OOBListForm,
574
- SP2019SPFx, SP2019ListForm
575
- }
576
- export function GetModernPageContainers() {
577
- let mainContent: HTMLElement = document.querySelector("section.mainContent");
578
- if (mainContent)
579
- return { mainContent, commandBar: document.querySelector(".commandBarWrapper") as HTMLElement, type: PageContainerTypes.M365SPFx };
580
-
581
- mainContent = document.querySelector("div[class^=canvasWrapper]");//document.querySelector("div.SPCanvas");
582
- if (mainContent)
583
- return { mainContent, commandBar: document.querySelector(".commandBarWrapper") as HTMLElement, type: PageContainerTypes.SP2019SPFx };
584
-
585
- mainContent = document.querySelector(".flex-mainColumn");
586
- if (mainContent)
587
- return { mainContent, commandBar: null, type: PageContainerTypes.M365OOBListForm };
588
-
589
- mainContent = document.querySelector(".Files-mainColumn");
590
- if (mainContent)
591
- return { mainContent, commandBar: null, type: PageContainerTypes.SP2019ListForm };
592
-
593
- return { mainContent: null, commandBar: null, type: PageContainerTypes.SP2019ListForm };
594
- }
595
-
596
- export function AddCamlQueryFragmentToViewQuery(viewXml: string, queryFragmentXml: string): string {
597
-
598
- const combineWithExistingConditions = (doc: XMLDocument, existingConditions: Element[], newConditionXml: string): Element => {
599
- const parser = new DOMParser();
600
- const newConditionDoc = parser.parseFromString(newConditionXml, 'text/xml');
601
- const newCondition = doc.importNode(newConditionDoc.documentElement, true);
602
-
603
- if (existingConditions.length === 0) {
604
- return newCondition;
605
- } else if (existingConditions.length === 1) {
606
- const andElement = doc.createElement("And");
607
- andElement.appendChild(existingConditions[0]);
608
- andElement.appendChild(newCondition);
609
- return andElement;
610
- } else {
611
- const lastCondition = existingConditions.pop();
612
- const andElement = doc.createElement("And");
613
- andElement.appendChild(combineWithExistingConditions(doc, existingConditions, ""));
614
- andElement.appendChild(lastCondition);
615
- return andElement;
616
- }
617
- }
618
- try {
619
- const parser = new DOMParser();
620
- const xmlDoc = parser.parseFromString(viewXml, 'text/xml');
621
- const whereClause = xmlDoc.querySelector('Where') || xmlDoc.createElement('Where');
622
- const existingConditions = Array.from(whereClause.children);
623
-
624
- const combinedCondition = combineWithExistingConditions(xmlDoc, existingConditions, queryFragmentXml);
625
- whereClause.textContent = ''; // Clear existing conditions
626
- whereClause.appendChild(combinedCondition);
627
-
628
- const query = xmlDoc.querySelector('Query') || xmlDoc.createElement('Query');
629
- query.appendChild(whereClause);
630
-
631
- const view = xmlDoc.querySelector('View') || xmlDoc.createElement('View');
632
- view.appendChild(query);
633
-
634
- const serializer = new XMLSerializer();
635
- let modifiedCamlXml = serializer.serializeToString(xmlDoc);
636
-
637
- return modifiedCamlXml;
638
- } catch (error) {
639
- return viewXml;
640
- }
641
- }
642
-
643
- export function IsUserEntityValueType(value: any): value is UserEntityValueType {
644
- if (isNullOrUndefined(value) || isString(value)) {
645
- return false;
646
- }
647
- var asUserEntityValueType = value as UserEntityValueType;
648
- var isEntityValueType =
649
- asUserEntityValueType.principalType === PrincipalType.SharePointGroup
650
- || asUserEntityValueType.principalType === PrincipalType.User
651
- || asUserEntityValueType.principalType === PrincipalType.SecurityGroup;
652
-
653
- return isEntityValueType;
654
- }
655
-
656
- export function IsMultiUserEntityValueType(value: any[]): value is UserEntityValueType[] {
657
- if (isNullOrUndefined(value) || isString(value) || !Array.isArray(value)) {
658
- return false;
659
- }
660
-
661
- return value.every((v) => {
662
- return IsUserEntityValueType(v);
663
- });
664
- }
665
-
666
- export function IsUrlValueType(value: any): value is UrlValueType {
667
- if (isNullOrUndefined(value) || isString(value)) {
668
- return false;
669
- }
670
- var asTypeUrlValue = value as UrlValueType;
671
- var isTypeUrlValue = !isNullOrUndefined(asTypeUrlValue.Url) && !isNullOrUndefined(asTypeUrlValue.Description);
672
- return isTypeUrlValue;
673
- }
674
-
675
- export function isHostedInTeams(){
676
- return window.location.pathname.toLowerCase().indexOf("teamshostedapp.aspx")>=0;
677
- }
678
- export function isClassicAppIframe() {
679
- return window.location.search.toLowerCase().indexOf("sphosturl=") >= 0 &&
680
- window.location.search.toLowerCase().indexOf("spappweburl=") >= 0;
681
- }