/
ArcTextWebPart.ts
173 lines (160 loc) · 5.27 KB
/
ArcTextWebPart.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
/**
* @file ArcTextWebPart.ts
* ArcText JQuery Plugin adaptation as a web part for the SharePoint Framework (SPfx)
*
* @copyright 2016 Olivier Carpentier
* Released under MIT licence
*/
import {
BaseClientSideWebPart,
IPropertyPaneSettings,
IWebPartContext,
PropertyPaneTextField,
PropertyPaneToggle,
PropertyPaneSlider,
PropertyPaneDropdown
} from '@microsoft/sp-webpart-base';
import * as strings from 'arcTextStrings';
import { IArcTextWebPartProps } from './IArcTextWebPartProps';
//Loads the property pane custom fields
import { PropertyFieldColorPicker } from 'sp-client-custom-fields/lib/PropertyFieldColorPicker';
import { PropertyFieldFontPicker } from 'sp-client-custom-fields/lib/PropertyFieldFontPicker';
import { PropertyFieldFontSizePicker } from 'sp-client-custom-fields/lib/PropertyFieldFontSizePicker';
//Loads JQuery & Arctext Javascript libraries
require('jquery');
require('arctext');
import * as $ from 'jquery';
/**
* @class
* ArcText Web Part
*/
export default class ArcTextWebPart extends BaseClientSideWebPart<IArcTextWebPartProps> {
private guid: string;
/**
* @function
* Web part contructor.
*/
public constructor(context: IWebPartContext) {
super(context);
//Inits the WebParts GUID
this.guid = this.getGuid();
//Hack: to invoke correctly the onPropertyChange function outside this class
//we need to bind this object on it first
this.onPropertyPaneFieldChanged = this.onPropertyPaneFieldChanged.bind(this);
}
/**
* @function
* Renders HTML code
*/
public render(): void {
//Defines the main DIV container with output HTML code
this.domElement.innerHTML = `<div style='text-align: ${this.properties.align}; font-family: ${this.properties.font}; font-size: ${this.properties.size}; color: ${this.properties.color};'><h3 class="arcText" id="${this.guid + '-arc'}">${this.properties.text}</h3></div>`;
this.renderContents();
}
/**
* @function
* Renders JavaScript JQuery calls
*/
private renderContents(): void {
if (($ as any)('#' + this.guid + '-arc') != null) {
//Calls the arctext plugin init method
($ as any)('#' + this.guid + '-arc').arctext({
radius: this.properties.radius,
rotate: this.properties.rotateLetters,
dir: this.properties.reverse === true ? -1 : 1
});
}
}
/**
* @function
* Generates a GUID
*/
private getGuid(): string {
return this.s4() + this.s4() + '-' + this.s4() + '-' + this.s4() + '-' +
this.s4() + '-' + this.s4() + this.s4() + this.s4();
}
/**
* @function
* Generates a GUID part
*/
private s4(): string {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
/**
* @function
* PropertyPanel settings definition
*/
protected get propertyPaneSettings(): IPropertyPaneSettings {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
displayGroupsAsAccordion: true,
groups: [
{
groupName: strings.EffectGroupName,
groupFields: [
PropertyPaneTextField('text', {
label: strings.TextFieldLabel,
multiline: false
}),
PropertyPaneSlider('radius', {
label: strings.RadiusFieldLabel,
min: 1,
max: 1500,
step: 1,
showValue: true
}),
PropertyPaneToggle('rotateLetters', {
label: strings.RotateLetterFieldLabel
}),
PropertyPaneToggle('reverse', {
label: strings.DirectionFieldLabel
}),
PropertyPaneDropdown('align', {
label: strings.AlignFieldLabel,
options: [
{ key: 'left', text: strings.AlignLeft },
{ key: 'center', text: strings.AlignCenter },
{ key: 'right', text: strings.AlignRight }
]
})
]
},
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyFieldFontPicker('font', {
label: strings.FontFieldLabel,
useSafeFont: true,
previewFonts: true,
initialValue: this.properties.font,
onPropertyChange: this.onPropertyPaneFieldChanged,
properties: this.properties
}),
PropertyFieldFontSizePicker('size', {
label: strings.FontSizeFieldLabel,
usePixels: true,
preview: true,
initialValue: this.properties.size,
onPropertyChange: this.onPropertyPaneFieldChanged,
properties: this.properties
}),
PropertyFieldColorPicker('color', {
label: strings.ColorFieldLabel,
initialColor: this.properties.color,
onPropertyChange: this.onPropertyPaneFieldChanged,
properties: this.properties
})
]
}
]
}
]
};
}
}