-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
UI.ts
266 lines (218 loc) · 8.17 KB
/
UI.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
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
import { PageFlip } from '../PageFlip';
import { Point } from '../BasicTypes';
import { FlipSetting, SizeType } from '../Settings';
import { FlipCorner, FlippingState } from '../Flip/Flip';
import { Orientation } from '../Render/Render';
type SwipeData = {
point: Point;
time: number;
};
/**
* UI Class, represents work with DOM
*/
export abstract class UI {
protected readonly app: PageFlip;
protected readonly wrapper: HTMLElement;
protected distElement: HTMLElement;
private touchPoint: SwipeData = null;
private readonly swipeTimeout = 250;
private readonly swipeDistance: number;
private onResize = (): void => {
this.update();
};
/**
* @constructor
*
* @param {HTMLElement} inBlock - Root HTML Element
* @param {PageFlip} app - PageFlip instanse
* @param {FlipSetting} setting - Configuration object
*/
protected constructor(inBlock: HTMLElement, app: PageFlip, setting: FlipSetting) {
inBlock.classList.add('stf__parent');
// Add first wrapper
inBlock.insertAdjacentHTML('afterbegin', '<div class="stf__wrapper"></div>');
this.wrapper = inBlock.querySelector('.stf__wrapper');
this.app = app;
const k = this.app.getSettings().usePortrait ? 1 : 2;
// Setting block sizes based on configuration
inBlock.style.minWidth = setting.minWidth * k + 'px';
inBlock.style.minHeight = setting.minHeight * k + 'px';
if (setting.size === SizeType.FIXED) {
inBlock.style.minWidth = setting.width * k + 'px';
inBlock.style.minHeight = setting.height * k + 'px';
}
if (setting.autoSize) {
inBlock.style.width = '100%';
inBlock.style.maxWidth = setting.maxWidth * 2 + 'px';
}
inBlock.style.display = 'block';
window.addEventListener('resize', this.onResize, false);
this.swipeDistance = setting.swipeDistance;
}
/**
* Destructor. Remove all HTML elements and all event handlers
*/
public destroy(): void {
this.removeHandlers();
this.distElement.remove();
this.wrapper.remove();
}
/**
* Updating child components when resizing
*/
public abstract update(): void;
/**
* Get parent element for book
*
* @returns {HTMLElement}
*/
public getDistElement(): HTMLElement {
return this.distElement;
}
/**
* Get wrapper element
*
* @returns {HTMLElement}
*/
public getWrapper(): HTMLElement {
return this.wrapper;
}
/**
* Updates styles and sizes based on book orientation
*
* @param {Orientation} orientation - New book orientation
*/
public setOrientationStyle(orientation: Orientation): void {
this.wrapper.classList.remove('--portrait', '--landscape');
if (orientation === Orientation.PORTRAIT) {
if (this.app.getSettings().autoSize)
this.wrapper.style.paddingBottom =
(this.app.getSettings().height / this.app.getSettings().width) * 100 + '%';
this.wrapper.classList.add('--portrait');
} else {
if (this.app.getSettings().autoSize)
this.wrapper.style.paddingBottom =
(this.app.getSettings().height / (this.app.getSettings().width * 2)) * 100 +
'%';
this.wrapper.classList.add('--landscape');
}
this.update();
}
protected removeHandlers(): void {
window.removeEventListener('resize', this.onResize);
this.distElement.removeEventListener('mousedown', this.onMouseDown);
this.distElement.removeEventListener('touchstart', this.onTouchStart);
window.removeEventListener('mousemove', this.onMouseMove);
window.removeEventListener('touchmove', this.onTouchMove);
window.removeEventListener('mouseup', this.onMouseUp);
window.removeEventListener('touchend', this.onTouchEnd);
}
protected setHandlers(): void {
this.distElement.addEventListener('mousedown', this.onMouseDown);
this.distElement.addEventListener('touchstart', this.onTouchStart);
window.addEventListener('mousemove', this.onMouseMove);
window.addEventListener('touchmove', this.onTouchMove, {
passive: !this.app.getSettings().mobileScrollSupport,
});
window.addEventListener('mouseup', this.onMouseUp);
window.addEventListener('touchend', this.onTouchEnd);
}
/**
* Convert global coordinates to relative book coordinates
*
* @param x
* @param y
*/
private getMousePos(x: number, y: number): Point {
const rect = this.distElement.getBoundingClientRect();
return {
x: x - rect.left,
y: y - rect.top,
};
}
private onMouseDown = (e: MouseEvent): void => {
const pos = this.getMousePos(e.clientX, e.clientY);
this.app.startUserTouch(pos);
e.preventDefault();
};
private onTouchStart = (e: TouchEvent): void => {
if (e.changedTouches.length > 0) {
const t = e.changedTouches[0];
const pos = this.getMousePos(t.clientX, t.clientY);
this.touchPoint = {
point: pos,
time: Date.now(),
};
// part of swipe detection
setTimeout(() => {
if (this.touchPoint !== null) {
this.app.startUserTouch(pos);
}
}, this.swipeTimeout);
if (!this.app.getSettings().mobileScrollSupport) e.preventDefault();
}
};
private onMouseUp = (e: MouseEvent): void => {
const pos = this.getMousePos(e.clientX, e.clientY);
this.app.userStop(pos);
};
private onMouseMove = (e: MouseEvent): void => {
const pos = this.getMousePos(e.clientX, e.clientY);
this.app.userMove(pos, false);
};
private onTouchMove = (e: TouchEvent): void => {
if (e.changedTouches.length > 0) {
const t = e.changedTouches[0];
const pos = this.getMousePos(t.clientX, t.clientY);
if (this.app.getSettings().mobileScrollSupport) {
if (this.touchPoint !== null) {
if (
Math.abs(this.touchPoint.point.x - pos.x) > 10 ||
this.app.getState() !== FlippingState.READ
) {
if (e.cancelable) this.app.userMove(pos, true);
}
}
if (this.app.getState() !== FlippingState.READ) {
e.preventDefault();
}
} else {
this.app.userMove(pos, true);
}
}
};
private onTouchEnd = (e: TouchEvent): void => {
if (e.changedTouches.length > 0) {
const t = e.changedTouches[0];
const pos = this.getMousePos(t.clientX, t.clientY);
let isSwipe = false;
// swipe detection
if (this.touchPoint !== null) {
const dx = pos.x - this.touchPoint.point.x;
const distY = Math.abs(pos.y - this.touchPoint.point.y);
if (
Math.abs(dx) > this.swipeDistance &&
distY < this.swipeDistance * 2 &&
Date.now() - this.touchPoint.time < this.swipeTimeout
) {
if (dx > 0) {
this.app.flipPrev(
this.touchPoint.point.y < this.app.getRender().getRect().height / 2
? FlipCorner.TOP
: FlipCorner.BOTTOM
);
} else {
this.app.flipNext(
this.touchPoint.point.y < this.app.getRender().getRect().height / 2
? FlipCorner.TOP
: FlipCorner.BOTTOM
);
}
isSwipe = true;
}
this.touchPoint = null;
}
this.app.userStop(pos, isSwipe);
}
};
}