forked from episerver/foundation-lib-spa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpiComponent.tsx
187 lines (159 loc) · 4.98 KB
/
EpiComponent.tsx
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
import { Component, ReactText } from 'react';
import { Method } from 'axios';
import IContent from './Models/IContent';
import ActionResponse from './Models/ActionResponse';
import ContentLink from './Models/ContentLink';
import IEpiserverContext from './Core/IEpiserverContext';
import CurrentContext from './Spa';
/**
* Base properties to be applied to every Episerver component
*/
export interface ComponentProps<T extends IContent> {
/**
* The IContent data object for this component
*/
data: T
/**
* The width for this component
*/
width?: number
/**
* The height for this component
*/
height?: number
/**
* Additional classnames assigned to this component
*/
className?: string
/**
* The unique identifier of this component
*/
key?: ReactText
/**
* The link to the content item shown by this component
*/
contentLink: ContentLink
/**
* The type context to be used, typical values are null, empty string or "block"
*/
contentType?: string
/**
* The property name shown by this component
*/
propertyName?: string
/**
* The controller action name to be used
*/
actionName?: string
/**
* The controller action data to be used
*/
actionData?: any
/**
* Legacy application context, kept as argument for now. Used when provided
* resolved at runtime otherwise.
*
* @deprecated
*/
context?: IEpiserverContext
/**
* The current path being rendered
*/
path?: string
/**
* The identifier of the component, if provided
*/
id?: string
}
/**
* Type do describe a generic EpiComponent type
*/
export type EpiComponentType<T extends IContent = IContent> = new (props : ComponentProps<T>) => EpiComponent<T>;
/**
* Base abstract class to be used by components representing an Episerver IContent component (e.g. Block, Page, Media,
* Catalog, Product, etc...)
*/
export abstract class EpiComponent<T extends IContent = IContent, S = {}> extends Component<ComponentProps<T>, S, {}>
{
/**
* The component name as injected by the ComponentLoader
*/
static displayName : string;
protected currentComponentId : number;
protected currentComponentGuid : string;
public constructor (props : ComponentProps<T>)
{
super(props);
this.currentComponentId = this.props.data.contentLink.id;
this.currentComponentGuid = this.props.data.contentLink.guidValue;
if (this.componentInitialize) this.componentInitialize();
if (this.getInitialState) this.state = this.getInitialState();
}
protected getInitialState?(): S;
protected componentInitialize?() : void;
/**
* Return if debug mode is active
*/
protected isDebugActive() : boolean
{
return this.getContext().isDebugActive() === true;
}
/**
* Returns true for OPE only
*/
protected isEditable() : boolean
{
return this.getContext().isEditable();
}
/**
* Returns true for OPE & Preview
*/
protected isInEditMode() : boolean
{
return this.getContext().isInEditMode();
}
/**
* Retrieve the ContentLink for this component
*/
protected getCurrentContentLink() : ContentLink {
return this.props.data.contentLink;
}
protected getContext() : IEpiserverContext
{
const context = this.props.context || CurrentContext;
return context;
}
/**
* Invoke a method on the underlying controller for this component, using strongly typed arguments and responses.
*
* @param method The (Case sensitive) name of the method to invoke on the controller for this component
* @param verb The HTTP method to use when invoking, defaults to 'GET'
* @param args The data to send (will be converted to JSON)
*/
protected invokeTyped<TypeIn, TypeOut>(method: string, verb?: Method, args?: TypeIn) : Promise<ActionResponse<TypeOut>>
{
return this.getContext().contentDeliveryApi().invokeTypedControllerMethod<TypeOut, TypeIn>(this.getCurrentContentLink(), method, verb, args);
}
/**
* Invoke a method on the underlying controller for this component
*
* @param method The (Case sensitive) name of the method to invoke on the controller for this component
* @param verb The HTTP method to use when invoking, defaults to 'GET'
* @param args The data to send (will be converted to JSON)
*/
protected invoke(method: string, verb?: Method, args?: object) : Promise<ActionResponse<any>>
{
return this.getContext().contentDeliveryApi().invokeControllerMethod(this.getCurrentContentLink(), method, verb, args);
}
protected htmlObject(htmlValue : string) : any
{
return {
__html: htmlValue
};
}
protected navigateTo(toPage: string | ContentLink)
{
this.getContext().navigateTo(toPage);
}
}
export default EpiComponent;