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
|
import { observer } from "mobx-react";
import React = require("react");
import { observable, action, runInAction, reaction } from "mobx";
import "./PresentationView.scss";
import { DocumentManager } from "../../util/DocumentManager";
import { Utils } from "../../../Utils";
import { Doc, DocListCast, DocListCastAsync } from "../../../new_fields/Doc";
import { listSpec } from "../../../new_fields/Schema";
import { Cast, NumCast, FieldValue, PromiseValue, StrCast, BoolCast } from "../../../new_fields/Types";
import { Id } from "../../../new_fields/FieldSymbols";
import { List } from "../../../new_fields/List";
import { CurrentUserUtils } from "../../../server/authentication/models/current_user_utils";
import PresentationElement, { buttonIndex } from "./PresentationElement";
import { library } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faArrowRight, faArrowLeft } from '@fortawesome/free-solid-svg-icons';
library.add(faArrowLeft);
library.add(faArrowRight);
export interface PresViewProps {
Document: Doc;
}
interface PresListProps extends PresViewProps {
deleteDocument(index: number): void;
gotoDocument(index: number): void;
groupMappings: Map<String, Doc[]>;
presElementsMappings: Map<Doc, PresentationElement>;
setChildrenDocs: (docList: Doc[]) => void;
}
@observer
/**
* Component that takes in a document prop and a boolean whether it's collapsed or not.
*/
class PresentationViewList extends React.Component<PresListProps> {
/**
* Method that initializes presentation ids for the
* docs that is in the presentation, when presentation list
* gets re-rendered. It makes sure to not assign ids to the
* docs that are in the group, so that mapping won't be disrupted.
*/
@action
initializeGroupIds = (docList: Doc[]) => {
docList.forEach((doc: Doc, index: number) => {
let docGuid = StrCast(doc.presentId, null);
//checking if part of group
if (!this.props.groupMappings.has(docGuid)) {
doc.presentId = Utils.GenerateGuid();
}
});
}
render() {
const children = DocListCast(this.props.Document.data);
this.initializeGroupIds(children);
this.props.setChildrenDocs(children);
return (
<div className="presentationView-listCont">
{children.map((doc: Doc, index: number) => <PresentationElement ref={(e) => { if (e) { this.props.presElementsMappings.set(doc, e); } }} key={index} mainDocument={this.props.Document} document={doc} index={index} deleteDocument={this.props.deleteDocument} gotoDocument={this.props.gotoDocument} groupMappings={this.props.groupMappings} allListElements={children} />)}
</div>
);
}
}
@observer
export class PresentationView extends React.Component<PresViewProps> {
public static Instance: PresentationView;
//Mapping from presentation ids to a list of doc that represent a group
@observable groupMappings: Map<String, Doc[]> = new Map();
//mapping from docs to their rendered component
@observable presElementsMappings: Map<Doc, PresentationElement> = new Map();
//variable that holds all the docs in the presentation
@observable childrenDocs: Doc[] = [];
//observable means render is re-called every time variable is changed
@observable
collapsed: boolean = false;
closePresentation = action(() => this.props.Document.width = 0);
next = async () => {
const current = NumCast(this.props.Document.selectedDoc);
//asking to get document at current index
let docAtCurrent = await this.getDocAtIndex(current);
if (docAtCurrent === undefined) {
return;
}
//asking for it's presentation id
let curPresId = StrCast(docAtCurrent.presentId);
let nextSelected = current + 1;
//if curDoc is in a group, selection slides until last one, if not it's next one
if (this.groupMappings.has(curPresId)) {
let currentsArray = this.groupMappings.get(StrCast(docAtCurrent.presentId))!;
nextSelected = current + currentsArray.length - currentsArray.indexOf(docAtCurrent) - 1;
//end of grup so go beyond
if (nextSelected === current) nextSelected = current + 1;
}
this.gotoDocument(nextSelected);
}
back = async () => {
const current = NumCast(this.props.Document.selectedDoc);
//requesting for the doc at current index
let docAtCurrent = await this.getDocAtIndex(current);
if (docAtCurrent === undefined) {
return;
}
//asking for its presentation id.
let curPresId = StrCast(docAtCurrent.presentId);
let prevSelected = current - 1;
//checking if this presentation id is mapped to a group, if so chosing the first element in group
if (this.groupMappings.has(curPresId)) {
let currentsArray = this.groupMappings.get(StrCast(docAtCurrent.presentId))!;
prevSelected = current - currentsArray.length + (currentsArray.length - currentsArray.indexOf(docAtCurrent));
//end of grup so go beyond
if (prevSelected === current) prevSelected = current - 1;
}
this.gotoDocument(prevSelected);
}
/**
* This is the method that checks for the actions that need to be performed
* after the document has been presented, which involves 3 button options:
* Hide Until Presented, Hide After Presented, Fade After Presented
*/
showAfterPresented = (index: number) => {
this.presElementsMappings.forEach((presElem: PresentationElement, key: Doc) => {
let selectedButtons: boolean[] = presElem.selected;
//the order of cases is aligned based on priority
if (selectedButtons[buttonIndex.HideTillPressed]) {
if (this.childrenDocs.indexOf(key) <= index) {
key.opacity = 1;
}
}
if (selectedButtons[buttonIndex.HideAfter]) {
if (this.childrenDocs.indexOf(key) < index) {
key.opacity = 0;
}
}
if (selectedButtons[buttonIndex.FadeAfter]) {
if (this.childrenDocs.indexOf(key) < index) {
key.opacity = 0.5;
}
}
});
}
/**
* This is the method that checks for the actions that need to be performed
* before the document has been presented, which involves 3 button options:
* Hide Until Presented, Hide After Presented, Fade After Presented
*/
hideIfNotPresented = (index: number) => {
this.presElementsMappings.forEach((presElem: PresentationElement, key: Doc) => {
let selectedButtons: boolean[] = presElem.selected;
//the order of cases is aligned based on priority
if (selectedButtons[buttonIndex.HideAfter]) {
if (this.childrenDocs.indexOf(key) >= index) {
key.opacity = 1;
}
}
if (selectedButtons[buttonIndex.FadeAfter]) {
if (this.childrenDocs.indexOf(key) >= index) {
key.opacity = 1;
}
}
if (selectedButtons[buttonIndex.HideTillPressed]) {
if (this.childrenDocs.indexOf(key) > index) {
key.opacity = 0;
}
}
});
}
/**
* This method makes sure that cursor navigates to the element that
* has the option open and last in the group. If not in the group, and it has
* te option open, navigates to that element.
*/
navigateToElement = (curDoc: Doc) => {
let docToJump: Doc = curDoc;
let curDocPresId = StrCast(curDoc.presentId, null);
//checking if in group
if (curDocPresId !== undefined) {
if (this.groupMappings.has(curDocPresId)) {
let currentDocGroup = this.groupMappings.get(curDocPresId)!;
currentDocGroup.forEach((doc: Doc, index: number) => {
let selectedButtons: boolean[] = this.presElementsMappings.get(doc)!.selected;
if (selectedButtons[buttonIndex.Navigate]) {
docToJump = doc;
}
});
}
}
//docToJump stayed same meaning, it was not in the group or was the last element in the group
if (docToJump === curDoc) {
//checking if curDoc has navigation open
if (this.presElementsMappings.get(curDoc)!.selected[buttonIndex.Navigate]) {
DocumentManager.Instance.jumpToDocument(curDoc);
} else {
return;
}
}
DocumentManager.Instance.jumpToDocument(docToJump);
}
/**
* Async function that supposedly return the doc that is located at given index.
*/
getDocAtIndex = async (index: number) => {
const list = FieldValue(Cast(this.props.Document.data, listSpec(Doc)));
if (!list) {
return undefined;
}
if (index < 0 || index >= list.length) {
return undefined;
}
this.props.Document.selectedDoc = index;
//awaiting async call to finish to get Doc instance
const doc = await list[index];
return doc;
}
@action
public RemoveDoc = (index: number) => {
const value = FieldValue(Cast(this.props.Document.data, listSpec(Doc)));
if (value) {
value.splice(index, 1);
}
}
public gotoDocument = async (index: number) => {
const list = FieldValue(Cast(this.props.Document.data, listSpec(Doc)));
if (!list) {
return;
}
if (index < 0 || index >= list.length) {
return;
}
this.props.Document.selectedDoc = index;
const doc = await list[index];
this.navigateToElement(doc);
this.hideIfNotPresented(index);
this.showAfterPresented(index);
}
//initilize class variables
constructor(props: PresViewProps) {
super(props);
PresentationView.Instance = this;
}
/**
* Adds a document to the presentation view
**/
@action
public PinDoc(doc: Doc) {
//add this new doc to props.Document
const data = Cast(this.props.Document.data, listSpec(Doc));
if (data) {
data.push(doc);
} else {
this.props.Document.data = new List([doc]);
}
this.props.Document.width = 300;
}
@action
setChildrenDocs = (docList: Doc[]) => {
this.childrenDocs = docList;
}
render() {
let titleStr = StrCast(this.props.Document.title);
let width = NumCast(this.props.Document.width);
//TODO: next and back should be icons
return (
<div className="presentationView-cont" style={{ width: width, overflow: "hidden" }}>
<div className="presentationView-heading">
<div className="presentationView-title">{titleStr}</div>
<button className='presentation-icon' onClick={this.closePresentation}>X</button>
</div>
<div className="presentation-buttons">
<button className="presentation-button" onClick={this.back}><FontAwesomeIcon icon={"arrow-left"} /></button>
<button className="presentation-button" onClick={this.next}><FontAwesomeIcon icon={"arrow-right"} /></button>
</div>
<PresentationViewList Document={this.props.Document} deleteDocument={this.RemoveDoc} gotoDocument={this.gotoDocument} groupMappings={this.groupMappings} presElementsMappings={this.presElementsMappings} setChildrenDocs={this.setChildrenDocs} />
</div>
);
}
}
|