forked from pnp/sp-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SiteInformationWebPart.ts
172 lines (154 loc) · 6.33 KB
/
SiteInformationWebPart.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
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IWebPartPropertiesMetadata,
IPropertyPaneConfiguration,
PropertyPaneTextField
} from '@microsoft/sp-webpart-base';
import { DisplayMode } from '@microsoft/sp-core-library';
import * as strings from 'SiteInformationWebPartStrings';
import SiteInformation from './components/SiteInformation';
import { ISiteInformationProps } from './components/ISiteInformationProps';
import { ISiteInformationWebPartProps } from './ISiteInformationWebPartProps';
export default class SiteInformationWebPart extends BaseClientSideWebPart<ISiteInformationWebPartProps> {
private propertyFieldTermPicker;
private propertyFieldPeoplePicker;
private principalType;
public onInit(): Promise<void> {
return super.onInit().then(async (_) => {
//chunk shared by all web parts
const { sp } = await import(
/* webpackChunkName: 'pnp-sp' */
"@pnp/sp");
// initialize the PnP JS library
sp.setup({
spfxContext: this.context
});
// initialize the Site Title property reading the current site title via PnP JS
if (!this.properties.siteTitle) {
sp.web.select("Title").get().then((r: any) => {
this.properties.siteTitle = r.Title;
});
}
});
}
protected get propertiesMetadata(): IWebPartPropertiesMetadata {
// makes the properties of the web part searchable with the SPO search engine
return {
'siteTitle': { isSearchablePlainText: true },
'siteContact': { isSearchablePlainText: true },
'siteOrganization': { isSearchablePlainText: true }
};
}
public render(): void {
const element: React.ReactElement<ISiteInformationProps> = React.createElement(
SiteInformation,
{
siteTitle: this.properties.siteTitle,
siteContactLogin: (this.properties.siteContact && this.properties.siteContact.length > 0) ?
this.properties.siteContact[0].login : "",
siteContactEmail: (this.properties.siteContact && this.properties.siteContact.length > 0) ?
this.properties.siteContact[0].email: null,
siteContactFullName: (this.properties.siteContact && this.properties.siteContact.length > 0) ?
this.properties.siteContact[0].fullName: null,
siteContactImageUrl: (this.properties.siteContact && this.properties.siteContact.length > 0) ?
this.properties.siteContact[0].imageUrl: null,
siteOrganization: (this.properties.siteOrganization && this.properties.siteOrganization.length > 0) ?
this.properties.siteOrganization[0].name : "",
needsConfiguration: this.needsConfiguration(),
configureHandler: () => {
this.context.propertyPane.open();
},
errorHandler: (errorMessage: string) => {
if (this.displayMode === DisplayMode.Edit) {
this.context.statusRenderer.renderError(this.domElement, errorMessage);
} else {
// nothing to do, if we are not in edit Mode
}
}
}
);
ReactDom.render(element, this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
//executes only before property pane is loaded.
protected async loadPropertyPaneResources(): Promise<void> {
// import additional controls/components
const { PropertyFieldTermPicker } = await import (
/* webpackChunkName: 'pnp-propcontrols-termpicker' */
'@pnp/spfx-property-controls/lib/PropertyFieldTermPicker'
);
const { PropertyFieldPeoplePicker, PrincipalType } = await import (
/* webpackChunkName: 'pnp-propcontrols-peoplepicker' */
'@pnp/spfx-property-controls/lib/PropertyFieldPeoplePicker'
);
this.propertyFieldTermPicker = PropertyFieldTermPicker;
this.propertyFieldPeoplePicker = PropertyFieldPeoplePicker;
this.principalType = PrincipalType;
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('siteTitle', {
label: strings.SiteTitleFieldLabel
}),
this.propertyFieldPeoplePicker('siteContact', {
label: strings.SiteContactFieldLabel,
initialData: this.properties.siteContact,
allowDuplicate: false,
multiSelect: false,
principalType: [this.principalType.Users],
onPropertyChange: this.onPropertyPaneFieldChanged,
context: this.context,
properties: this.properties,
onGetErrorMessage: null,
deferredValidationTime: 0,
key: 'siteContactId'
}),
this.propertyFieldTermPicker('siteOrganization', {
label: strings.SiteOrganizationFieldLabel,
panelTitle: strings.SiteOrganizationPanelTitle,
initialValues: this.properties.siteOrganization,
allowMultipleSelections: false,
excludeSystemGroup: true,
onPropertyChange: this.onPropertyPaneFieldChanged,
properties: this.properties,
context: this.context,
onGetErrorMessage: null,
deferredValidationTime: 0,
limitByGroupNameOrID: 'PnPTermSets',
limitByTermsetNameOrID: 'PnP-Organizations',
key: 'siteOrganizationId'
})
]
}
]
}
]
};
}
// method to refresh any error after properties configuration
protected onAfterPropertyPaneChangesApplied(): void {
this.context.statusRenderer.clearError(this.domElement);
}
// method to determine if the web part has to be configured
private needsConfiguration(): boolean {
// as long as we don't have the stock symbol, we need configuration
return ((!this.properties.siteTitle ||
this.properties.siteTitle.length === 0) ||
(!this.properties.siteContact) ||
(!this.properties.siteOrganization));
}
}