-
Notifications
You must be signed in to change notification settings - Fork 7
/
DynamicValueWebPart.ts
377 lines (340 loc) · 12.9 KB
/
DynamicValueWebPart.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
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version, Log } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField,
IPropertyPaneDropdownOption,
PropertyPaneDropdown,
PropertyPaneToggle,
PropertyPaneDynamicField
} from '@microsoft/sp-webpart-base';
import { IDynamicDataSource } from '@microsoft/sp-dynamic-data';
import { CalloutTriggers } from '@pnp/spfx-property-controls/lib/PropertyFieldHeader';
import { PropertyFieldTextWithCallout } from '@pnp/spfx-property-controls/lib/PropertyFieldTextWithCallout';
import * as strings from 'DynamicValueWebPartStrings';
import { DynamicValue, IDynamicValueProps } from './components/DynamicValue';
import { displayTemplateCallout } from './components/PropertyCallouts';
export interface IDynamicValueWebPartProps {
title: string;
propertyId: string;
sourceId: string;
propertyTitle: string;
labelShow: boolean;
labelText: string;
labelBold: boolean;
labelPosition: string;
displayType: string;
displayBoolTrue: string;
displayBoolFalse: string;
displayObjectProperty: string;
displayObjectPropertyManual: string;
displaySubPropertyType: string;
displayTemplate: string;
displayUndefined: string;
displayUndefinedCustom: string;
}
export default class DynamicValueWebPart extends BaseClientSideWebPart<IDynamicValueWebPartProps> {
private _lastSourceId: string;
private _lastPropertyId: string;
private _lastPropertyTitle: string;
private _sourceAttemptCount: number;
private _registrationComplete: boolean;
protected onInit(): Promise<void> {
this.render = this.render.bind(this);
this._sourceAttemptCount = 0;
this._registrationComplete = false;
return Promise.resolve();
}
public render(): void {
let value: any = undefined;
const needsConfiguration: boolean = !(this.properties.sourceId && this.properties.propertyId);
if(!needsConfiguration) {
//Get the value from our source
const source: IDynamicDataSource = this.context.dynamicDataProvider.tryGetSource(this.properties.sourceId);
if(!source) {
this._sourceAttemptCount += 1;
if(this._sourceAttemptCount <=3) {
console.log('retrying...');
window.setTimeout(this.render,100);
} else {
this.context.statusRenderer.renderError(this.domElement, `Unable to connect to the data source (${this.properties.sourceId})`);
}
return;
} else {
this._sourceAttemptCount = 0;
}
try {
value = source.getPropertyValue(this.properties.propertyId);
if(typeof value !== "undefined" && this.properties.displayType === 'object') {
value = value[this.properties.displayObjectProperty === 'manuallyspecified' ? this.properties.displayObjectPropertyManual : this.properties.displayObjectProperty];
}
}
catch(e) {
const errorMessage = `An error has occurred while retrieving the property value (${this.properties.propertyId}). Details: ${e}`;
this.context.statusRenderer.renderError(this.domElement, errorMessage);
this.logError(errorMessage, e);
return;
}
}
if (this._registrationComplete === false && !needsConfiguration) {
try {
//If the subscription props exist, but we haven't registered yet then we need to register
// otherwise, the registration will happen with the propertypane as the properties are configured
this.context.dynamicDataProvider.registerPropertyChanged(this.properties.sourceId, this.properties.propertyId, this.render);
this._lastSourceId = this.properties.sourceId;
this._lastPropertyId = this.properties.propertyId;
this._registrationComplete = true;
}
catch(e) {
const errorMessage = `An error has occurred while connecting to the data source. Details: ${e}`;
this.context.statusRenderer.renderError(this.domElement, errorMessage);
this.logError(errorMessage, e);
return;
}
}
const element: React.ReactElement<IDynamicValueProps > = React.createElement(
DynamicValue,
{
value: value,
displayMode: this.displayMode,
updateTitle: (title: string) => {
this.properties.title = title;
},
title: this.properties.title,
isConfigured: !needsConfiguration,
onConfigure: () => {
this.context.propertyPane.open();
},
labelShow: this.properties.labelShow,
labelText: this.properties.labelText,
labelBold: this.properties.labelBold,
labelPosition: this.properties.labelPosition,
displayType: this.properties.displayType === 'object' ? this.properties.displaySubPropertyType : this.properties.displayType,
displayBoolTrue: this.properties.displayBoolTrue,
displayBoolFalse: this.properties.displayBoolFalse,
displayTemplate: this.properties.displayTemplate,
displayUndefinedValue: this.properties.displayUndefined === 'blank' ? '' : (this.properties.displayUndefined === 'undefined' ? 'undefined' : this.properties.displayUndefinedCustom)
}
);
ReactDom.render(element, this.domElement);
}
private logError(message:string, e:any) {
Log.error(message,e,this.context.serviceScope);
}
protected onDispose(): void {
ReactDom.unmountComponentAtNode(this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
const sourceOptions: IPropertyPaneDropdownOption[] = this.context.dynamicDataProvider.getAvailableSources()
.map(source => {
return {
key: source.id,
text: source.metadata.title,
};
});
const selectedSource: string = this.properties.sourceId;
let propertyOptions: IPropertyPaneDropdownOption[] = [];
if(selectedSource) {
const source: IDynamicDataSource = this.context.dynamicDataProvider.tryGetSource(selectedSource);
if(source) {
propertyOptions = source.getPropertyDefinitions().map(prop => {
return {
key: prop.id,
text: prop.title,
};
});
}
}
let labelPaneFields: Array<any> = [
PropertyPaneToggle('labelShow', {
label: 'Show Label',
})
];
if(this.properties.labelShow){
labelPaneFields.push(...[
PropertyPaneTextField('labelText', {
label: 'Label Text',
}),
PropertyPaneToggle('labelBold', {
label: 'Bold',
}),
PropertyPaneDropdown('labelPosition', {
label: 'Position',
options: [
{key:'above', text:'Above'},
{key:'beside', text:'Beside'},
]
})
]);
}
//Display Type
let displayPaneFields: Array<any> = [
PropertyPaneDropdown('displayType', {
label: 'Display value as',
options: [
{key:'text', text:'Text'},
{key:'bool', text:'Yes/No'},
{key:'object', text:'Object'},
]
})
];
if(this.properties.displayType === 'object') {
let keyOptions: IPropertyPaneDropdownOption[] = [];
const source: IDynamicDataSource = this.context.dynamicDataProvider.tryGetSource(this.properties.sourceId);
if(source) {
try {
let sample: any = source.getPropertyValue(this.properties.propertyId);
keyOptions = Object.keys(sample).map(key => {
return {key:key, text:key};
});
}
catch(e) {
}
}
keyOptions.push({key:'manuallyspecified', text: 'Other'});
//If the current displayObjectProperty isn't in the list, then pick the first one
if(typeof this.properties.displayObjectProperty == "undefined" || !keyOptions.filter((opt:IPropertyPaneDropdownOption) => {
return opt.key.toString() === this.properties.displayObjectProperty;
}).length) {
this.properties.displayObjectProperty = keyOptions[0].key.toString();
this.render();
}
displayPaneFields.push(
PropertyPaneDropdown('displayObjectProperty', {
label: 'Sub property',
options: keyOptions,
selectedKey: this.properties.displayObjectProperty,
})
);
if(this.properties.displayObjectProperty === 'manuallyspecified') {
displayPaneFields.push(
PropertyPaneTextField('displayObjectPropertyManual', {
label: 'Sub Property Name'
})
);
}
displayPaneFields.push(
PropertyPaneDropdown('displaySubPropertyType', {
label: 'Display sub property as',
options: [
{key:'text', text:'Text'},
{key:'bool', text:'Yes/No'},
],
})
);
}
//Optional configuration props depending on display type
switch(this.properties.displayType === 'object' ? this.properties.displaySubPropertyType : this.properties.displayType) {
case "bool":
displayPaneFields.push(...[
PropertyPaneTextField('displayBoolTrue',{
label: 'Value when true'
}),
PropertyPaneTextField('displayBoolFalse',{
label: 'Value when false'
}),
]);
break;
}
//Advanced Display Template
displayPaneFields.push(...[
PropertyFieldTextWithCallout('displayTemplate', {
calloutTrigger: CalloutTriggers.Hover,
key: 'displayTemplate',
label: 'Template (Advanced)',
calloutWidth: 200,
value: this.properties.displayTemplate,
calloutContent: displayTemplateCallout(),
}),
PropertyPaneDropdown('displayUndefined', {
label: 'When value is undefined',
options: [
{key:'blank', text:'Show blank value'},
{key:'undefined', text:'Show "undefined"'},
{key:'custom', text:'Use custom value'}
]
})
]);
if(this.properties.displayUndefined === 'custom') {
displayPaneFields.push(
PropertyPaneTextField('displayUndefinedCustom', {
label: 'Custom text for undefined',
})
);
}
return {
pages: [
{
groups: [
{
groupName: 'Source',
groupFields: [
PropertyPaneDropdown('sourceId', {
label: "Source",
options: sourceOptions,
selectedKey: this.properties.sourceId,
}),
PropertyPaneDropdown('propertyId', {
label: "Property",
options: propertyOptions,
selectedKey: this.properties.propertyId,
}),
]
},
{
groupName: 'Label',
groupFields: labelPaneFields,
},
{
groupName: 'Display',
groupFields: displayPaneFields,
},
]
}
]
};
}
protected onPropertyPaneFieldChanged(propertyPath: string): void {
if (propertyPath === 'sourceId') {
//New source, so pick the first property (so we can subscribe)
this.properties.propertyId = this.context.dynamicDataProvider.tryGetSource(this.properties.sourceId).getPropertyDefinitions()[0].id;
}
//If either of the subscription props change, we need to unregister and then register
if (propertyPath === 'sourceId' || propertyPath === 'propertyId') {
if (this._lastSourceId && this._lastPropertyId) {
try {
//previously registered, so unregister
this.context.dynamicDataProvider.unregisterPropertyChanged(this._lastSourceId, this._lastPropertyId, this.render);
} catch (e) {
this.logError("Unable to unregister previous dynamicDataProvider propertyChanged - likely the source was removed", e);
}
}
//Get the title of the current property
const source: IDynamicDataSource = this.context.dynamicDataProvider.tryGetSource(this.properties.sourceId);
if(source) {
let props = source.getPropertyDefinitions().filter(prop => {
return prop.id == this.properties.propertyId;
});
if(props.length) {
this.properties.propertyTitle = props[0].title;
}
}
//If the label isn't set or it equals the previous property title, go ahead and change it
// This makes the label automatic but keeps any customizations
if(typeof this.properties.labelText == "undefined" || this.properties.labelText == this._lastPropertyTitle) {
this.properties.labelText = this.properties.propertyTitle;
}
//register for changes (calls render whenever there is a change)
this.context.dynamicDataProvider.registerPropertyChanged(this.properties.sourceId, this.properties.propertyId, this.render);
this._lastSourceId = this.properties.sourceId;
this._lastPropertyId = this.properties.propertyId;
this._lastPropertyTitle = this.properties.propertyTitle;
this._registrationComplete = true;
}
}
}