-
Notifications
You must be signed in to change notification settings - Fork 308
/
clientsidepages.ts
1015 lines (836 loc) · 29.2 KB
/
clientsidepages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Reused by courtesy of PnPJS
// original at: https://github.com/pnp/pnpjs/blob/b4336b370c9b10950a22d12c48ad69789d1382fc/packages/sp/src/clientsidepages.ts
/**
* Interface defining an object with a known property type
*/
interface TypedHash<T> {
[key: string]: T;
}
export enum CanvasSectionTemplate {
/// <summary>
/// One column
/// </summary>
OneColumn,
/// <summary>
/// One column, full browser width. This one only works for communication sites in combination with image or hero webparts
/// </summary>
OneColumnFullWidth,
/// <summary>
/// Two columns of the same size
/// </summary>
TwoColumn,
/// <summary>
/// Three columns of the same size
/// </summary>
ThreeColumn,
/// <summary>
/// Two columns, left one is 2/3, right one 1/3
/// </summary>
TwoColumnLeft,
/// <summary>
/// Two columns, left one is 1/3, right one 2/3
/// </summary>
TwoColumnRight,
/// <summary>
/// Vertical
/// </summary>
Vertical
}
/**
* Section background shading
* 0 - None
* 1 - Neutral
* 2 - Soft
* 3 - Strong
*/
export enum ZoneEmphasis {
None = 0,
Neutral = 1,
Soft = 2,
Strong = 3
}
/**
* Shorthand for Object.hasOwnProperty
*
* @param o Object to check for
* @param p Name of the property
*/
function hOP(o: any, p: string): boolean {
return Object.hasOwnProperty.call(o, p);
}
/**
* Provides functionality to extend the given object by doing a shallow copy
*
* @param target The object to which properties will be copied
* @param source The source object from which properties will be copied
* @param noOverwrite If true existing properties on the target are not overwritten from the source
*
*/
function extend(target: any, source: any, noOverwrite = false): any {
if (!objectDefinedNotNull(source)) {
return target;
}
// ensure we don't overwrite things we don't want overwritten
const check: (o: any, i: string) => Boolean = noOverwrite ? (o, i) => !(i in o) : () => true;
return Object.getOwnPropertyNames(source)
.filter((v: string) => check(target, v))
.reduce((t: any, v: string) => {
t[v] = source[v];
return t;
}, target);
}
/**
* Determines if an object is both defined and not null
* @param obj Object to test
*/
function objectDefinedNotNull(obj: any): boolean {
return typeof obj !== "undefined" && obj !== null;
}
/**
* Gets a random GUID value
*
* http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
*/
function getGUID(): string {
let d = new Date().getTime();
const guid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === "x" ? r : (r & 0x3 | 0x8)).toString(16);
});
return guid;
}
/**
* Column size factor. Max value is 12 (= one column), other options are 8,6,4 or 0
*/
type CanvasColumnFactorType = 0 | 2 | 4 | 6 | 8 | 12;
/**
* Gets the next order value 1 based for the provided collection
*
* @param collection Collection of orderable things
*/
function getNextOrder(collection: { order: number }[]): number {
if (collection.length < 1) {
return 1;
}
return Math.max.apply(null, collection.map(i => i.order)) + 1;
}
/**
* After https://stackoverflow.com/questions/273789/is-there-a-version-of-javascripts-string-indexof-that-allows-for-regular-expr/274094#274094
*
* @param this Types the called context this to a string in which the search will be conducted
* @param regex A regex or string to match
* @param startpos A starting position from which the search will begin
*/
function regexIndexOf(this: string, regex: RegExp | string, startpos = 0) {
const indexOf = this.substring(startpos).search(regex);
return (indexOf >= 0) ? (indexOf + (startpos)) : indexOf;
}
/**
* Gets an attribute value from an html string block
*
* @param html HTML to search
* @param attrName The name of the attribute to find
*/
function getAttrValueFromString(html: string, attrName: string): string | null {
const reg = new RegExp(`${attrName}="([^"]*?)"`, "i");
const match = reg.exec(html);
return match && match.length > 0 ? match[1] : null;
}
/**
* Finds bounded blocks of markup bounded by divs, ensuring to match the ending div even with nested divs in the interstitial markup
*
* @param html HTML to search
* @param boundaryStartPattern The starting pattern to find, typically a div with attribute
* @param collector A func to take the found block and provide a way to form it into a useful return that is added into the return array
*/
function getBoundedDivMarkup<T>(html: string, boundaryStartPattern: RegExp | string, collector: (s: string) => T): T[] {
const blocks: T[] = [];
if (typeof html === "undefined" || html === null) {
return blocks;
}
// remove some extra whitespace if present
const cleanedHtml = html.replace(/[\t\r\n]/g, "");
// find the first div
let startIndex = regexIndexOf.call(cleanedHtml, boundaryStartPattern);
if (startIndex < 0) {
// we found no blocks in the supplied html
return blocks;
}
// this loop finds each of the blocks
while (startIndex > -1) {
// we have one open div counting from the one found above using boundaryStartPattern so we need to ensure we find it's close
let openCounter = 1;
let searchIndex = startIndex + 1;
let nextDivOpen = -1;
let nextCloseDiv = -1;
// this loop finds the </div> tag that matches the opening of the control
while (true) {
// find both the next opening and closing div tags from our current searching index
nextDivOpen = regexIndexOf.call(cleanedHtml, /<div[^>]*>/i, searchIndex);
nextCloseDiv = regexIndexOf.call(cleanedHtml, /<\/div>/i, searchIndex);
if (nextDivOpen < 0) {
// we have no more opening divs, just set this to simplify checks below
nextDivOpen = cleanedHtml.length + 1;
}
// determine which we found first, then increment or decrement our counter
// and set the location to begin searching again
if (nextDivOpen < nextCloseDiv) {
openCounter++;
searchIndex = nextDivOpen + 1;
} else if (nextCloseDiv < nextDivOpen) {
openCounter--;
searchIndex = nextCloseDiv + 1;
}
// once we have no open divs back to the level of the opening control div
// meaning we have all of the markup we intended to find
if (openCounter === 0) {
// get the bounded markup, +6 is the size of the ending </div> tag
const markup = cleanedHtml.substring(startIndex, nextCloseDiv + 6).trim();
// save the control data we found to the array
blocks.push(collector(markup));
// get out of our while loop
break;
}
if (openCounter > 1000 || openCounter < 0) {
// this is an arbitrary cut-off but likely we will not have 1000 nested divs
// something has gone wrong above and we are probably stuck in our while loop
// let's get out of our while loop and not hang everything
throw new Error("getBoundedDivMarkup exceeded depth parameters.");
}
}
// get the start of the next control
startIndex = regexIndexOf.call(cleanedHtml, boundaryStartPattern, nextCloseDiv);
}
return blocks;
}
/**
* Normalizes the order value for all the sections, columns, and controls to be 1 based and stepped (1, 2, 3...)
*
* @param collection The collection to normalize
*/
function reindex(collection?: { order: number, columns?: { order: number }[], controls?: { order: number }[] }[]): void {
if (!collection) {
return;
}
for (let i = 0; i < collection.length; i++) {
collection[i].order = i + 1;
if (hOP(collection[i], "columns")) {
reindex(collection[i].columns);
} else if (hOP(collection[i], "controls")) {
reindex(collection[i].controls);
}
}
}
/**
* Represents the data and methods associated with client side "modern" pages
*/
export class ClientSidePage {
public sections: CanvasSection[] = [];
/**
* Converts a json object to an escaped string appropriate for use in attributes when storing client-side controls
*
* @param json The json object to encode into a string
*/
public static jsonToEscapedString(json: any): string {
return JSON.stringify(json)
.replace(/"/g, """)
.replace(/:/g, ":")
.replace(/{/g, "{")
.replace(/}/g, "}")
.replace(/\[/g, "\[")
.replace(/\]/g, "\]")
.replace(/\*/g, "\*")
.replace(/\$/g, "\$")
.replace(/\./g, "\.");
}
/**
* Converts an escaped string from a client-side control attribute to a json object
*
* @param escapedString
*/
public static escapedStringToJson<T = any>(escapedString: string | null): T {
if (!escapedString) {
return {} as any;
}
const unespace = (escaped: string): string => {
const mapDict = [
[/\\/g, "\\\\"], [/"/g, "\""], [/:/g, ":"], [/{/g, "{"], [/}/g, "}"],
[/\\\\/g, "\\"], [/\\\?/g, "?"], [/\\\./g, "."], [/\\\[/g, "["], [/\\\]/g, "]"],
[/\\\(/g, "("], [/\\\)/g, ")"], [/\\\|/g, "|"], [/\\\+/g, "+"], [/\\\*/g, "*"],
[/\\\$/g, "$"],
];
return mapDict.reduce((r, m) => r.replace(m[0], m[1] as string), escaped);
};
return JSON.parse(unespace(escapedString));
}
/**
* Converts this page's content to html markup
*/
public toHtml(): string {
// trigger reindex of the entire tree
reindex(this.sections);
const html: string[] = [];
html.push("<div>");
for (let i = 0; i < this.sections.length; i++) {
html.push(this.sections[i].toHtml());
}
html.push("</div>");
return html.join("");
}
/**
* Loads this page instance's content from the supplied html
*
* @param html html string representing the page's content
*/
public static fromHtml(html: string): ClientSidePage {
const page: ClientSidePage = new ClientSidePage();
// reset sections
page.sections = [];
// gather our controls from the supplied html
getBoundedDivMarkup(html, /<div\b[^>]*data-sp-canvascontrol[^>]*?>/i, markup => {
// get the control type
const ct = /controlType":(\d*?),/i.exec(markup);
// if no control type is present this is a column which we give type 0 to let us process it
const controlType = ct == null || ct.length < 2 ? 0 : parseInt(ct[1], 10);
let control: CanvasControl | null = null;
switch (controlType) {
case 0:
// empty canvas column
control = new CanvasColumn(null, 0);
control.fromHtml(markup);
page.mergeColumnToTree(<CanvasColumn>control);
break;
case 3:
// client side webpart
control = new ClientSideWebpart("");
control.fromHtml(markup);
page.mergePartToTree(<ClientSidePart>control);
break;
case 4:
// client side text
control = new ClientSideText();
control.fromHtml(markup);
page.mergePartToTree(<ClientSidePart>control);
break;
}
});
// refresh all the orders within the tree
reindex(page.sections);
return page;
}
/**
* Finds a control within this page's control tree using the supplied predicate
*
* @param predicate Takes a control and returns true or false, if true that control is returned by findControl
*/
public findControl<T extends ClientSidePart = ClientSidePart>(predicate: (c: ClientSidePart) => boolean): T | null {
// check all sections
for (let i = 0; i < this.sections.length; i++) {
// check all columns
for (let j = 0; j < this.sections[i].columns.length; j++) {
// check all controls
for (let k = 0; k < this.sections[i].columns[j].controls.length; k++) {
// check to see if the predicate likes this control
if (predicate(this.sections[i].columns[j].controls[k])) {
return <T>this.sections[i].columns[j].controls[k];
}
}
}
}
// we found nothing so give nothing back
return null;
}
/**
* Merges the control into the tree of sections and columns for this page
*
* @param control The control to merge
*/
private mergePartToTree(control: ClientSidePart): void {
let section: CanvasSection | null = null;
let column: CanvasColumn | null = null;
let sectionFactor: CanvasColumnFactorType = 12;
let sectionIndex = 0;
let zoneIndex = 0;
if (control.controlData) {
// handle case where we don't have position data
if (hOP(control.controlData, "position")) {
if (hOP(control.controlData.position, "zoneIndex")) {
zoneIndex = control.controlData.position.zoneIndex;
}
if (hOP(control.controlData.position, "sectionIndex")) {
sectionIndex = control.controlData.position.sectionIndex;
}
if (hOP(control.controlData.position, "sectionFactor")) {
sectionFactor = control.controlData.position.sectionFactor;
}
}
}
const sections = this.sections.filter(s => s.order === zoneIndex);
if (sections.length < 1) {
section = new CanvasSection(this, zoneIndex);
this.sections.push(section);
} else {
section = sections[0];
}
const columns = section.columns.filter(c => c.order === sectionIndex);
if (columns.length < 1) {
column = new CanvasColumn(section, sectionIndex, sectionFactor);
section.columns.push(column);
} else {
column = columns[0];
}
control.column = column;
column.addControl(control);
}
/**
* Merges the supplied column into the tree
*
* @param column Column to merge
* @param position The position data for the column
*/
private mergeColumnToTree(column: CanvasColumn): void {
const order = column.controlData && hOP(column.controlData, "position") && hOP(column.controlData.position, "zoneIndex") ? column.controlData.position.zoneIndex : 0;
let section: CanvasSection | null = null;
const sections = this.sections.filter(s => s.order === order);
if (sections.length < 1) {
section = new CanvasSection(this, order);
this.sections.push(section);
} else {
section = sections[0];
}
column.section = section;
section.columns.push(column);
}
}
export class CanvasSection {
constructor(public page: ClientSidePage, public order: number, public columns: CanvasColumn[] = []) {
}
/**
* Default column (this.columns[0]) for this section
*/
public get defaultColumn(): CanvasColumn {
if (this.columns.length < 1) {
this.addColumn(12);
}
return this.columns[0];
}
/**
* Adds a new column to this section
*/
public addColumn(factor: CanvasColumnFactorType): CanvasColumn {
const column = new CanvasColumn(this, getNextOrder(this.columns), factor);
this.columns.push(column);
return column;
}
public toHtml(): string {
const html = [];
for (let i = 0; i < this.columns.length; i++) {
html.push(this.columns[i].toHtml());
}
return html.join("");
}
}
abstract class CanvasControl {
constructor(
protected controlType: number | undefined,
public dataVersion: string | null,
public column: CanvasColumn | undefined = undefined,
public order = 1,
public id: string | undefined = getGUID(),
public controlData: ClientSideControlData | null = null,
public dynamicDataPaths: any = null,
public dynamicDataValues: any = null) { }
/**
* Value of the control's "data-sp-controldata" attribute
*/
public get jsonData(): string {
return ClientSidePage.jsonToEscapedString(this.getControlData());
}
public abstract toHtml(index: number): string;
public fromHtml(html: string): void {
this.controlData = ClientSidePage.escapedStringToJson<ClientSideControlData>(getAttrValueFromString(html, "data-sp-controldata"));
this.dataVersion = getAttrValueFromString(html, "data-sp-canvasdataversion");
this.controlType = this.controlData.controlType;
this.id = this.controlData.id;
}
protected abstract getControlData(): ClientSideControlData;
}
export class CanvasColumn extends CanvasControl {
constructor(
public section: CanvasSection | null,
public order: number,
public factor: CanvasColumnFactorType = 12,
public controls: ClientSidePart[] = [],
dataVersion = "1.0") {
super(0, dataVersion);
}
public addControl(control: ClientSidePart): this {
control.column = this;
this.controls.push(control);
return this;
}
public insertControl(control: ClientSidePart, index?: number): this {
if (typeof index === 'undefined' ||
index < 0 ||
index >= this.controls.length) {
this.addControl(control);
}
else {
control.column = this;
control.order = index;
this.controls.splice(index, 0, control);
}
return this;
}
public toHtml(): string {
const html = [];
if (this.controls.length < 1) {
html.push(`<div data-sp-canvascontrol="" data-sp-canvasdataversion="${this.dataVersion}" data-sp-controldata="${this.jsonData}"></div>`);
} else {
for (let i = 0; i < this.controls.length; i++) {
html.push(this.controls[i].toHtml(i + 1));
}
}
return html.join("");
}
public fromHtml(html: string): void {
super.fromHtml(html);
this.controlData = ClientSidePage.escapedStringToJson<ClientSideControlData>(getAttrValueFromString(html, "data-sp-controldata"));
if (hOP(this.controlData, "position")) {
if (hOP(this.controlData.position, "sectionFactor")) {
this.factor = this.controlData.position.sectionFactor;
}
if (hOP(this.controlData.position, "sectionIndex")) {
this.order = this.controlData.position.sectionIndex;
}
}
}
public getControlData(): ClientSideControlData {
return {
displayMode: 2,
position: {
sectionFactor: this.factor,
sectionIndex: this.order,
zoneIndex: this.section ? this.section.order : 0
},
};
}
/**
* Removes this column and all contained controls from the collection
*/
public remove(): void {
if (this.section) {
this.section.columns = this.section.columns.filter(column => column.id !== this.id);
if (this.column) {
reindex(this.column.controls);
}
}
}
}
/**
* Abstract class with shared functionality for parts
*/
export abstract class ClientSidePart extends CanvasControl {
/**
* Removes this column and all contained controls from the collection
*/
public remove(): void {
if (this.column) {
this.column.controls = this.column.controls.filter(control => control.id !== this.id);
reindex(this.column.controls);
}
}
}
export class ClientSideText extends ClientSidePart {
private _text: string = '';
constructor(text = "") {
super(4, "1.0");
this.text = text;
}
/**
* The text markup of this control
*/
public get text(): string {
return this._text;
}
public set text(text: string) {
if (!text.startsWith("<p>")) {
text = `<p>${text}</p>`;
}
this._text = text;
}
public getControlData(): ClientSideControlData {
return {
controlType: this.controlType,
editorType: "CKEditor",
id: this.id,
position: {
controlIndex: this.order,
sectionFactor: this.column ? this.column.factor : 0,
sectionIndex: this.column ? this.column.order : 0,
zoneIndex: this.column && this.column.section ? this.column.section.order : 0
},
};
}
public toHtml(index: number): string {
// set our order to the value passed in
this.order = index;
const html: string[] = [];
html.push(`<div data-sp-canvascontrol="" data-sp-canvasdataversion="${this.dataVersion}" data-sp-controldata="${this.jsonData}">`);
html.push("<div data-sp-rte=\"\">");
html.push(`${this.text}`);
html.push("</div>");
html.push("</div>");
return html.join("");
}
public fromHtml(html: string): void {
super.fromHtml(html);
this.text = "";
getBoundedDivMarkup(html, /<div[^>]*data-sp-rte[^>]*>/i, (s: string) => {
// now we need to grab the inner text between the divs
const match = /<div[^>]*data-sp-rte[^>]*>(.*?)<\/div>$/i.exec(s);
this.text = match && match.length > 1 ? match[1] : "";
});
}
}
export class ClientSideWebpart extends ClientSidePart {
constructor(public title: string,
public description = "",
public propertieJson: TypedHash<any> = {},
public webPartId = "",
protected htmlProperties = "",
protected serverProcessedContent: ServerProcessedContent | null = null,
protected canvasDataVersion: string | null = "1.0",
public dynamicDataPaths: any = "",
public dynamicDataValues: any = "") {
super(3, "1.0");
}
public import(component: ClientSidePageComponent): void {
this.webPartId = component.Id.replace(/^\{|\}$/g, "").toLowerCase();
const manifest: ClientSidePageComponentManifest = JSON.parse(component.Manifest);
this.title = manifest.preconfiguredEntries[0].title.default;
this.description = manifest.preconfiguredEntries[0].description.default;
this.dataVersion = "1.0";
this.propertieJson = this.parseJsonProperties(manifest.preconfiguredEntries[0].properties);
}
public setProperties<T = any>(properties: T): this {
this.propertieJson = extend(this.propertieJson, properties);
return this;
}
public toHtml(index: number): string {
// set our order to the value passed in
this.order = index;
// will form the value of the data-sp-webpartdata attribute
const data = {
dataVersion: this.dataVersion,
description: this.description,
id: this.webPartId,
instanceId: this.id,
properties: this.propertieJson,
serverProcessedContent: this.serverProcessedContent,
title: this.title
};
if (this.dynamicDataPaths) {
(data as any)['dynamicDataPaths'] = this.dynamicDataPaths;
}
if (this.dynamicDataValues) {
(data as any)['dynamicDataValues'] = this.dynamicDataValues;
}
const html: string[] = [];
html.push(`<div data-sp-canvascontrol="" data-sp-canvasdataversion="${this.canvasDataVersion}" data-sp-controldata="${this.jsonData}">`);
html.push(`<div data-sp-webpart="" data-sp-webpartdataversion="${this.dataVersion}" data-sp-webpartdata="${ClientSidePage.jsonToEscapedString(data)}">`);
html.push(`<div data-sp-componentid>`);
html.push(this.webPartId);
html.push("</div>");
html.push(`<div data-sp-htmlproperties="">`);
html.push(this.renderHtmlProperties());
html.push("</div>");
html.push("</div>");
html.push("</div>");
return html.join("");
}
public fromHtml(html: string): void {
super.fromHtml(html);
const webPartData = ClientSidePage.escapedStringToJson<ClientSideWebpartData>(getAttrValueFromString(html, "data-sp-webpartdata"));
this.title = webPartData.title;
this.description = webPartData.description;
this.webPartId = webPartData.id;
this.canvasDataVersion = (getAttrValueFromString(html, "data-sp-canvasdataversion") || '').replace(/\\\./, ".");
this.dataVersion = (getAttrValueFromString(html, "data-sp-webpartdataversion") || '').replace(/\\\./, ".");
this.setProperties(webPartData.properties);
if (typeof webPartData.serverProcessedContent !== "undefined") {
this.serverProcessedContent = webPartData.serverProcessedContent;
}
if (typeof webPartData.dynamicDataPaths !== "undefined") {
this.dynamicDataPaths = webPartData.dynamicDataPaths;
}
if (typeof webPartData.dynamicDataValues !== "undefined") {
this.dynamicDataValues = webPartData.dynamicDataValues;
}
// get our html properties
const htmlProps = getBoundedDivMarkup(html, /<div\b[^>]*data-sp-htmlproperties[^>]*?>/i, markup => {
return markup.replace(/^<div\b[^>]*data-sp-htmlproperties[^>]*?>/i, "").replace(/<\/div>$/i, "");
});
this.htmlProperties = htmlProps.length > 0 ? htmlProps[0] : "";
}
public getControlData(): ClientSideControlData {
return {
controlType: this.controlType,
id: this.id,
position: {
controlIndex: this.order,
sectionFactor: this.column ? this.column.factor : 0,
sectionIndex: this.column ? this.column.order : 0,
zoneIndex: this.column && this.column.section ? this.column.section.order : 0
},
webPartId: this.webPartId,
};
}
protected renderHtmlProperties(): string {
const html: string[] = [];
if (typeof this.serverProcessedContent === "undefined" || this.serverProcessedContent === null) {
html.push(this.htmlProperties);
} else if (typeof this.serverProcessedContent !== "undefined") {
if (typeof this.serverProcessedContent.searchablePlainTexts !== "undefined") {
const keys = Object.keys(this.serverProcessedContent.searchablePlainTexts);
for (let i = 0; i < keys.length; i++) {
html.push(`<div data-sp-prop-name="${keys[i]}" data-sp-searchableplaintext="true">`);
html.push(this.serverProcessedContent.searchablePlainTexts[keys[i]]);
html.push("</div>");
}
}
if (typeof this.serverProcessedContent.imageSources !== "undefined") {
const keys = Object.keys(this.serverProcessedContent.imageSources);
for (let i = 0; i < keys.length; i++) {
html.push(`<img data-sp-prop-name="${keys[i]}" src="${this.serverProcessedContent.imageSources[keys[i]]}" />`);
}
}
if (typeof this.serverProcessedContent.links !== "undefined") {
const keys = Object.keys(this.serverProcessedContent.links);
for (let i = 0; i < keys.length; i++) {
html.push(`<a data-sp-prop-name="${keys[i]}" href="${this.serverProcessedContent.links[keys[i]]}"></a>`);
}
}
}
return html.join("");
}
protected parseJsonProperties(props: TypedHash<any>): any {
// If the web part has the serverProcessedContent property then keep this one as it might be needed as input to render the web part HTML later on
if (typeof props.webPartData !== "undefined" && typeof props.webPartData.serverProcessedContent !== "undefined") {
this.serverProcessedContent = props.webPartData.serverProcessedContent;
} else if (typeof props.serverProcessedContent !== "undefined") {
this.serverProcessedContent = props.serverProcessedContent;
} else {
this.serverProcessedContent = null;
}
if (typeof props.webPartData !== "undefined" && typeof props.webPartData.dynamicDataPaths !== "undefined") {
this.dynamicDataPaths = props.webPartData.dynamicDataPaths;
} else if (typeof props.dynamicDataPaths !== "undefined") {
this.dynamicDataPaths = props.dynamicDataPaths;
} else {
this.dynamicDataPaths = null;
}
if (typeof props.webPartData !== "undefined" && typeof props.webPartData.dynamicDataValues !== "undefined") {
this.dynamicDataValues = props.webPartData.dynamicDataValues;
} else if (typeof props.dynamicDataValues !== "undefined") {
this.dynamicDataValues = props.dynamicDataValues;
} else {
this.dynamicDataValues = null;
}
if (typeof props.webPartData !== "undefined" && typeof props.webPartData.properties !== "undefined") {
return props.webPartData.properties;
} else if (typeof props.properties !== "undefined") {
return props.properties;
} else {
return props;
}
}
}
/**
* Client side webpart object (retrieved via the _api/web/GetClientSideWebParts REST call)
*/
export interface ClientSidePageComponent {
/**
* Component type for client side webpart object
*/
ComponentType: number;
/**
* Id for client side webpart object
*/
Id: string;
/**
* Manifest for client side webpart object
*/
Manifest: string;
/**
* Manifest type for client side webpart object
*/
ManifestType: number;
/**
* Name for client side webpart object
*/
Name: string;
/**
* Status for client side webpart object
*/
Status: number;
}
interface ClientSidePageComponentManifest {
alias: string;
componentType: "WebPart" | "" | null;
disabledOnClassicSharepoint: boolean;
hiddenFromToolbox: boolean;
id: string;
imageLinkPropertyNames: any;
isInternal: boolean;
linkPropertyNames: boolean;
loaderConfig: any;
manifestVersion: number;
preconfiguredEntries: {
description: { default: string };
group: { default: string };
groupId: string;
iconImageUrl: string;
officeFabricIconFontName: string;
properties: TypedHash<any>;
title: { default: string };
}[];
preloadComponents: any | null;
requiredCapabilities: any | null;
searchablePropertyNames: any | null;
supportsFullBleed: boolean;
version: string;
}
interface ServerProcessedContent {
searchablePlainTexts: TypedHash<string>;
imageSources: TypedHash<string>;
links: TypedHash<string>;
}
interface ClientSideControlPosition {
controlIndex?: number;
sectionFactor: CanvasColumnFactorType;
sectionIndex: number;
zoneIndex: number;
}
interface ClientSideControlData {
controlType?: number;
id?: string;
editorType?: string;
position: ClientSideControlPosition;
webPartId?: string;
displayMode?: number;
}