forked from pnp/sp-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BannerWebPart.ts
130 lines (117 loc) · 3.73 KB
/
BannerWebPart.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
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version, DisplayMode } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField,
PropertyPaneToggle,
IWebPartPropertiesMetadata
} from '@microsoft/sp-webpart-base';
import * as strings from 'BannerWebPartStrings';
import { Banner, IBannerProps } from './components';
export interface IBannerWebPartProps {
bannerText: string;
bannerImage: string;
bannerLink: string;
bannerHeight: number;
fullWidth: boolean;
useParallax: boolean;
useParallaxInt: boolean;
}
export default class BannerWebPart extends BaseClientSideWebPart<IBannerWebPartProps> {
private propertyFieldNumber;
public render(): void {
const element: React.ReactElement<IBannerProps> = React.createElement(
Banner,
{
...this.properties,
propertyPane: this.context.propertyPane,
domElement: this.context.domElement,
useParallaxInt: this.displayMode === DisplayMode.Read && !!this.properties.bannerImage && this.properties.useParallax
}
);
ReactDom.render(element, this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
/**
* Set property metadata
*/
protected get propertiesMetadata(): IWebPartPropertiesMetadata {
return {
'bannerText': { isSearchablePlainText: true },
'bannerImage': { isImageSource: true },
'bannerLink': { isLink: true }
};
}
/**
* Field validation
*/
private _validateImageField(imgVal: string): string {
if (imgVal) {
const urlSplit = imgVal.split(".");
if (urlSplit && urlSplit.length > 0) {
const extName = urlSplit.pop().toLowerCase();
if (["jpg", "jpeg", "png", "gif"].indexOf(extName) === -1) {
return strings.BannerValidationNotImage;
}
}
}
return "";
}
//executes only before property pane is loaded.
protected async loadPropertyPaneResources(): Promise<void> {
// import additional controls/components
const { PropertyFieldNumber } = await import(
/* webpackChunkName: 'pnp-propcontrols-number' */
'@pnp/spfx-property-controls/lib/propertyFields/number'
);
this.propertyFieldNumber = PropertyFieldNumber;
}
/**
* Property pane configuration
*/
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
groups: [
{
groupName: strings.BannerConfigName,
groupFields: [
PropertyPaneTextField('bannerText', {
label: strings.BannerTextField,
multiline: true,
maxLength: 200,
value: this.properties.bannerText
}),
PropertyPaneTextField('bannerImage', {
label: strings.BannerImageUrlField,
onGetErrorMessage: this._validateImageField,
value: this.properties.bannerImage
}),
PropertyPaneTextField('bannerLink', {
label: strings.BannerLinkField,
value: this.properties.bannerLink
}),
this.propertyFieldNumber('bannerHeight', {
key: "bannerHeight",
label: strings.BannerNumberField,
value: this.properties.bannerHeight,
maxValue: 500,
minValue: 100
}),
PropertyPaneToggle('useParallax', {
label: strings.BannerParallaxField,
checked: this.properties.useParallax
})
]
}
]
}
]
};
}
}