-
Notifications
You must be signed in to change notification settings - Fork 3
/
ProvenanceSlidedeck.ts
192 lines (162 loc) · 4.98 KB
/
ProvenanceSlidedeck.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import mitt from './mitt';
import {
IProvenanceSlidedeck,
IProvenanceGraphTraverser,
IProvenanceGraph,
Application,
Handler,
IProvenanceSlide,
IScreenShotProvider
} from './api';
import { ProvenanceSlide } from './ProvenanceSlide';
export class ProvenanceSlidedeck implements IProvenanceSlidedeck {
private _application: Application;
private _graph: IProvenanceGraph;
private _mitt: any;
private _slides: IProvenanceSlide[] = [];
private _traverser: IProvenanceGraphTraverser;
private _selectedSlide: IProvenanceSlide | null;
private _screenShotProvider: IScreenShotProvider | null = null;
private _autoScreenShot = false;
constructor(application: Application, traverser: IProvenanceGraphTraverser) {
this._mitt = mitt();
this._application = application;
this._graph = traverser.graph;
this._traverser = traverser;
this._selectedSlide = null;
}
public get application() {
return this._application;
}
public addSlide(slide?: IProvenanceSlide, index?: number): IProvenanceSlide {
if (
!index ||
isNaN(index) ||
!Number.isInteger(index) ||
index > this._slides.length ||
index < 0
) {
index = this._slides.length;
}
if (slide && this._slides.indexOf(slide) >= 0) {
throw new Error('Cannot add a slide that is already in the deck');
}
if (!slide) {
const node = this._graph.current;
slide = new ProvenanceSlide(node.label, 1, 0, [], node);
}
if (this.autoScreenShot && this.screenShotProvider) {
try {
slide.metadata.screenShot = this.screenShotProvider();
} catch (e) {
console.warn('Error while getting screenshot', e);
}
}
if (this._slides.length === 0) {
this._selectedSlide = slide;
}
this._slides.splice(index, 0, slide);
this._mitt.emit('slideAdded', slide);
return slide;
}
public removeSlideAtIndex(index: number) {
const deletedSlides = this._slides.splice(index, 1);
// This can only be 1 slide now, therefore this is ok.
if (this._selectedSlide === deletedSlides[0]) {
this.selectedSlide = null;
}
this._mitt.emit('slideRemoved', deletedSlides[0]);
}
public removeSlide(slide: IProvenanceSlide) {
this.removeSlideAtIndex(this._slides.indexOf(slide));
}
public get selectedSlide(): IProvenanceSlide | null {
return this._selectedSlide;
}
public moveSlide(indexFrom: number, indexTo: number) {
if (indexTo < 0 || indexTo > this.slides.length - 1) {
throw new Error('target index out of bounds');
}
this._slides.splice(indexTo, 0, this._slides.splice(indexFrom, 1)[0]);
this._mitt.emit('slidesMoved', this._slides);
}
public startTime(slide: IProvenanceSlide) {
const index = this._slides.indexOf(slide);
let previousTime = 0;
for (let i = 0; i < index; i++) {
previousTime += this._slides[i].transitionTime;
previousTime += this._slides[i].duration;
}
return previousTime;
}
public slideAtTime(time: number) {
let index = 0;
let currentSlide = null;
while (time >= 0 && index < this.slides.length) {
currentSlide = this.slides[index];
const nextSlideOffset = currentSlide.transitionTime + currentSlide.duration;
if (time - nextSlideOffset < 0) {
break;
}
time -= nextSlideOffset;
index++;
}
return currentSlide;
}
public set selectedSlide(slide: IProvenanceSlide | null) {
if (slide && slide.node) {
this._traverser.toStateNode(slide.node.id, slide.transitionTime);
}
this._selectedSlide = slide;
this._mitt.emit('slideSelected', slide);
}
public get slides() {
return this._slides;
}
next() {
if (this._selectedSlide !== null) {
let currentIndex = this._slides.indexOf(this._selectedSlide);
if (currentIndex < this._slides.length - 1) {
currentIndex += 1;
this.selectedSlide = this._slides[currentIndex];
} else {
this.selectedSlide = this._slides[0];
}
}
}
previous() {
if (this._selectedSlide !== null) {
let currentIndex = this._slides.indexOf(this._selectedSlide);
if (currentIndex > 0) {
currentIndex -= 1;
this.selectedSlide = this._slides[currentIndex];
} else {
this.selectedSlide = this._slides[this._slides.length - 1];
}
}
}
public get graph() {
return this._graph;
}
get screenShotProvider() {
return this._screenShotProvider;
}
set screenShotProvider(provider: IScreenShotProvider | null) {
this._screenShotProvider = provider;
}
get autoScreenShot(): boolean {
return this._autoScreenShot;
}
set autoScreenShot(value: boolean) {
this._autoScreenShot = value;
if (value && !this._screenShotProvider) {
console.warn('Setting autoScreenShot to true, but no screenShotProvider is set');
}
}
on(type: string, handler: Handler) {
this._mitt.on(type, handler);
}
off(type: string, handler: Handler) {
this._mitt.off(type, handler);
}
}