-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathmainController.tsx
More file actions
382 lines (336 loc) · 13.9 KB
/
mainController.tsx
File metadata and controls
382 lines (336 loc) · 13.9 KB
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
378
379
380
381
382
declare var Velocity: jquery.velocity.VelocityStatic;
import {SmartValue} from "../communicator/smartValue";
import {Constants} from "../constants";
import {Localization} from "../localization/localization";
import * as Log from "../logging/log";
import {AnimationHelper} from "./animations/animationHelper";
import {AnimationState} from "./animations/animationState";
import {AnimationStrategy} from "./animations/animationStrategy";
import {ExpandFromRightAnimationStrategy} from "./animations/expandFromRightAnimationStrategy";
import {FadeInAnimationStrategy} from "./animations/fadeInAnimationStrategy";
import {SlidingHeightAnimationStrategy} from "./animations/slidingHeightAnimationStrategy";
import {ClipMode} from "./clipMode";
import {ClipperStateProp} from "./clipperState";
import {ClipperStateUtilities} from "./clipperStateUtilities";
import {ComponentBase} from "./componentBase";
import {CloseButton} from "./components/closeButton";
import {Footer} from "./components/footer";
import {Clipper} from "./frontEndGlobals";
import {OneNoteApiUtils} from "./oneNoteApiUtils";
import {ClippingPanel} from "./panels/clippingPanel";
import {ClippingPanelWithDelayedMessage} from "./panels/clippingPanelWithDelayedMessage";
import {ClippingPanelWithProgressIndicator} from "./panels/clippingPanelWithProgressIndicator";
import {DialogButton} from "./panels/dialogPanel";
import {ErrorDialogPanel} from "./panels/errorDialogPanel";
import {LoadingPanel} from "./panels/loadingPanel";
import {OptionsPanel} from "./panels/optionsPanel";
import {RatingsPanel} from "./panels/ratingsPanel";
import {RegionSelectingPanel} from "./panels/regionSelectingPanel";
import {SignInPanel} from "./panels/signInPanel";
import {SuccessPanel} from "./panels/successPanel";
import {Status} from "./status";
export enum CloseReason {
CloseButton,
EscPress
}
export enum PanelType {
None,
BadState,
Loading,
SignInNeeded,
ClipOptions,
RegionInstructions,
ClippingToApi,
ClippingFailure,
ClippingSuccess
}
export interface MainControllerState {
currentPanel?: PanelType;
ratingsPanelAnimationState?: SmartValue<AnimationState>; // stored for when the ratings subpanel re-renders while the MainController does not
}
export interface MainControllerProps extends ClipperStateProp {
onSignInInvoked: () => void;
onSignOutInvoked: (authType: string) => void;
updateFrameHeight: (newContainerHeight: number) => void;
onStartClip: () => void;
clearKeepAlive: () => void;
}
export class MainControllerClass extends ComponentBase<MainControllerState, MainControllerProps> {
private popupIsOpen: boolean;
private controllerAnimationStrategy: AnimationStrategy;
private panelAnimationStrategy: AnimationStrategy;
private heightAnimationStrategy: AnimationStrategy;
constructor(props: MainControllerProps) {
super(props);
this.initAnimationStrategy();
// The user could have pressed esc when Clipper iframe was not in focus
Clipper.getInjectCommunicator().registerFunction(Constants.FunctionKeys.escHandler, () => {
this.handleEscPress();
});
document.onkeydown = (event) => {
if (event.keyCode === Constants.KeyCodes.esc) {
this.handleEscPress();
}
};
}
getInitialState(): MainControllerState {
return {
currentPanel: PanelType.None
};
}
handleEscPress() {
if (this.isCloseable()) {
this.closeClipper(CloseReason.EscPress);
}
}
initAnimationStrategy() {
this.controllerAnimationStrategy = new ExpandFromRightAnimationStrategy({
extShouldAnimateIn: () => { return this.props.clipperState.uiExpanded; },
extShouldAnimateOut: () => { return !this.props.clipperState.uiExpanded; },
onBeforeAnimateOut: () => { this.setState({currentPanel: PanelType.None}); },
onBeforeAnimateIn: () => { this.props.clipperState.reset(); },
onAnimateInExpand: () => { this.setState({currentPanel: this.getPanelTypeToShow()}); },
onAfterAnimateOut: () => { Clipper.getInjectCommunicator().callRemoteFunction(Constants.FunctionKeys.hideUi); }
});
this.panelAnimationStrategy = new FadeInAnimationStrategy({
extShouldAnimateIn: () => { return this.state.currentPanel !== PanelType.None; },
extShouldAnimateOut: () => { return this.getPanelTypeToShow() !== this.state.currentPanel; },
onAfterAnimateOut: () => { this.setState({currentPanel: this.getPanelTypeToShow()}); },
onAfterAnimateIn: () => { this.setState({currentPanel: this.getPanelTypeToShow()}); }
});
this.heightAnimationStrategy = new SlidingHeightAnimationStrategy(Constants.Ids.mainController, {
onAfterHeightAnimatorDraw: (newHeightInfo) => {
if (this.popupIsOpen) {
// Sometimes during the delay, we open the popup, so the frame height update needs to account for that too
this.updateFrameHeightAfterPopupToggle(this.popupIsOpen);
} else {
this.props.updateFrameHeight(newHeightInfo.newContainerHeight);
}
}
});
}
isCloseable() {
let panelType = this.state.currentPanel;
return panelType !== PanelType.None && panelType !== PanelType.ClippingToApi;
}
onPopupToggle(shouldNowBeOpen: boolean) {
this.popupIsOpen = shouldNowBeOpen;
this.updateFrameHeightAfterPopupToggle(shouldNowBeOpen);
}
private updateFrameHeightAfterPopupToggle(shouldNowBeOpen: boolean) {
let saveToLocationContainer = document.getElementById(Constants.Ids.saveToLocationContainer);
if (saveToLocationContainer) {
let currentLocationContainerPosition: ClientRect = saveToLocationContainer.getBoundingClientRect();
let aboutToOpenHeight = Constants.Styles.sectionPickerContainerHeight + currentLocationContainerPosition.top + currentLocationContainerPosition.height;
let aboutToCloseHeight = document.getElementById(Constants.Ids.mainController).offsetHeight;
let newHeight = shouldNowBeOpen ? aboutToOpenHeight : aboutToCloseHeight;
this.props.updateFrameHeight(newHeight);
}
}
getPanelTypeToShow(): PanelType {
if (this.props.clipperState.badState) {
return PanelType.BadState;
}
if (!this.props.clipperState.uiExpanded) {
return PanelType.None;
}
if ((this.props.clipperState.userResult && this.props.clipperState.userResult.status === Status.InProgress) ||
this.props.clipperState.fetchLocStringStatus === Status.InProgress || !this.props.clipperState.invokeOptions) {
return PanelType.Loading;
}
if (!ClipperStateUtilities.isUserLoggedIn(this.props.clipperState, true)) {
return PanelType.SignInNeeded;
}
if (this.props.clipperState.currentMode.get() === ClipMode.Region && this.props.clipperState.regionResult.status !== Status.Succeeded) {
switch (this.props.clipperState.regionResult.status) {
case Status.InProgress:
return PanelType.Loading;
default:
return PanelType.RegionInstructions;
}
}
switch (this.props.clipperState.oneNoteApiResult.status) {
default:
case Status.NotStarted:
return PanelType.ClipOptions;
case Status.InProgress:
return PanelType.ClippingToApi;
case Status.Failed:
return PanelType.ClippingFailure;
case Status.Succeeded:
return PanelType.ClippingSuccess;
}
}
closeClipper(closeReason?: CloseReason) {
let closeEvent = new Log.Event.BaseEvent(Log.Event.Label.CloseClipper);
closeEvent.setCustomProperty(Log.PropertyName.Custom.CurrentPanel, PanelType[this.state.currentPanel]);
closeEvent.setCustomProperty(Log.PropertyName.Custom.CloseReason, CloseReason[closeReason]);
Clipper.logger.logEvent(closeEvent);
this.props.clearKeepAlive();
// Clear region selections on clipper exit rather than invoke to avoid conflicting logic with scenarios like context menu image selection
this.props.clipperState.setState({
uiExpanded: false,
regionResult: {status: Status.NotStarted, data: []}
});
}
onMainControllerDraw(mainControllerElement: HTMLElement) {
this.controllerAnimationStrategy.animate(mainControllerElement);
}
onPanelAnimatorDraw(panelAnimator: HTMLElement) {
let panelTypeToShow: PanelType = this.getPanelTypeToShow();
let panelIsCorrect = panelTypeToShow === this.state.currentPanel;
if (panelTypeToShow === PanelType.ClipOptions && this.state.currentPanel !== PanelType.ClipOptions) {
this.popupIsOpen = false;
}
this.panelAnimationStrategy.animate(panelAnimator);
if (!panelIsCorrect && this.panelAnimationStrategy.getAnimationState() === AnimationState.GoingIn) {
// We'll speed things up by stopping the current one, and trigger the next one
AnimationHelper.stopAnimationsThen(panelAnimator, () => {
this.panelAnimationStrategy.setAnimationState(AnimationState.In);
this.setState({});
});
}
}
onHeightAnimatorDraw(heightAnimator: HTMLElement) {
this.heightAnimationStrategy.animate(heightAnimator);
}
getCurrentPanel() {
let panelType = this.state.currentPanel;
let buttons: DialogButton[] = [];
switch (panelType) {
case PanelType.BadState:
buttons.push({
id: Constants.Ids.refreshPageButton,
label: Localization.getLocalizedString("WebClipper.Action.RefreshPage"),
handler: () => {
Clipper.getInjectCommunicator().callRemoteFunction(Constants.FunctionKeys.refreshPage);
}
});
return <ErrorDialogPanel message={Localization.getLocalizedString("WebClipper.Error.OrphanedWebClipperDetected")} buttons={buttons}/>;
case PanelType.Loading:
return <LoadingPanel clipperState={this.props.clipperState}/>;
case PanelType.SignInNeeded:
return <SignInPanel clipperState={this.props.clipperState} onSignInInvoked={this.props.onSignInInvoked}/>;
case PanelType.ClipOptions:
return <OptionsPanel
onPopupToggle={this.onPopupToggle.bind(this)}
clipperState={this.props.clipperState}
onStartClip={this.props.onStartClip}/>;
case PanelType.RegionInstructions:
return <RegionSelectingPanel clipperState={this.props.clipperState}/>;
case PanelType.ClippingToApi:
if (this.props.clipperState.currentMode.get() === ClipMode.Pdf) {
if (this.props.clipperState.pdfPreviewInfo.shouldDistributePages) {
return <ClippingPanelWithProgressIndicator clipperState={this.props.clipperState}/>;
}
return <ClippingPanelWithDelayedMessage
clipperState={this.props.clipperState}
delay={Constants.Settings.pdfClippingMessageDelay}
message={Localization.getLocalizedString("WebClipper.ClipType.Pdf.ProgressLabelDelay")}/>;
}
return <ClippingPanel clipperState={this.props.clipperState}/>;
case PanelType.ClippingFailure:
let error = this.props.clipperState.oneNoteApiResult.data as OneNoteApi.RequestError;
let apiResponseCode: string = OneNoteApiUtils.getApiResponseCode(error);
if (OneNoteApiUtils.isRetryable(apiResponseCode)) {
buttons.push({
id: Constants.Ids.dialogTryAgainButton,
label: Localization.getLocalizedString("WebClipper.Action.TryAgain"),
handler: this.props.onStartClip
});
}
if (OneNoteApiUtils.requiresSignout(apiResponseCode)) {
buttons.push({
id: Constants.Ids.dialogSignOutButton,
label: Localization.getLocalizedString("WebClipper.Action.SignOut"),
handler: () => {
if (this.props.onSignOutInvoked) {
this.props.onSignOutInvoked(this.props.clipperState.userResult.data.user.authType);
}
}
});
} else {
buttons.push({
id: Constants.Ids.dialogBackButton,
label: Localization.getLocalizedString("WebClipper.Action.BackToHome"),
handler: () => {
this.props.clipperState.setState({
oneNoteApiResult: {
data: this.props.clipperState.oneNoteApiResult.data,
status: Status.NotStarted
}
});
}
});
}
return <ErrorDialogPanel
message={OneNoteApiUtils.getLocalizedErrorMessage(apiResponseCode)} buttons={buttons}/>;
case PanelType.ClippingSuccess:
let panels: any[] = [<SuccessPanel clipperState={this.props.clipperState}/>];
if (!this.state.ratingsPanelAnimationState) {
this.state.ratingsPanelAnimationState = new SmartValue<AnimationState>(AnimationState.Out);
}
if (this.props.clipperState.showRatingsPrompt) {
panels.push(<RatingsPanel clipperState={this.props.clipperState} animationState={this.state.ratingsPanelAnimationState}/>);
}
return panels;
default:
return undefined;
}
}
getCloseButtonForType() {
if (this.isCloseable()) {
return (
<CloseButton onClickHandler={this.closeClipper.bind(this)} onClickHandlerParams={[CloseReason.CloseButton]}/>
);
} else {
return undefined;
}
}
getCurrentFooter(): any {
let panelType = this.state.currentPanel;
switch (panelType) {
case PanelType.ClipOptions:
case PanelType.ClippingFailure:
let error = this.props.clipperState.oneNoteApiResult.data as OneNoteApi.RequestError;
let apiResponseCode: string = OneNoteApiUtils.getApiResponseCode(error);
if (OneNoteApiUtils.requiresSignout(apiResponseCode)) {
// If the ResponseCode requires a SignOut to fix, then the dialogButton will handle SignOut
// so we will not show the footer. If it doesn't require signout, show the Footer
return undefined;
}
/* falls through */
case PanelType.SignInNeeded:
return <Footer clipperState={this.props.clipperState} onSignOutInvoked={this.props.onSignOutInvoked}/>;
case PanelType.ClippingSuccess:
/* falls through */
default:
return undefined;
}
}
render() {
let panelToRender = this.getCurrentPanel();
let closeButtonToRender = this.getCloseButtonForType();
let footerToRender = this.getCurrentFooter();
return (
<div
id={Constants.Ids.mainController}
{...this.onElementDraw(this.onMainControllerDraw)}
role="dialog"
aria-modal="true"
aria-label={Localization.getLocalizedString("WebClipper.Label.OneNoteWebClipper")} >
{closeButtonToRender}
<div className="panelContent">
<div className={Constants.Classes.heightAnimator} {...this.onElementDraw(this.onHeightAnimatorDraw)}>
<div className={Constants.Classes.panelAnimator} {...this.onElementDraw(this.onPanelAnimatorDraw)}>
{panelToRender}
{footerToRender}
</div>
</div>
</div>
</div>
);
}
}
let component = MainControllerClass.componentize();
export {component as MainController};