summaryrefslogtreecommitdiff
path: root/lib/epub.js/src/managers/views/inline.js
blob: 072b5864a927889181caeeb71f32f23e1c96224d (plain)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import EventEmitter from "event-emitter";
import {extend, borders, uuid, isNumber, bounds, defer, qs, parse} from "../../utils/core";
import EpubCFI from "../../epubcfi";
import Contents from "../../contents";
import { EVENTS } from "../../utils/constants";

class InlineView {
	constructor(section, options) {
		this.settings = extend({
			ignoreClass : "",
			axis: "vertical",
			width: 0,
			height: 0,
			layout: undefined,
			globalLayoutProperties: {},
		}, options || {});

		this.id = "epubjs-view:" + uuid();
		this.section = section;
		this.index = section.index;

		this.element = this.container(this.settings.axis);

		this.added = false;
		this.displayed = false;
		this.rendered = false;

		this.width  = this.settings.width;
		this.height = this.settings.height;

		this.fixedWidth  = 0;
		this.fixedHeight = 0;

		// Blank Cfi for Parsing
		this.epubcfi = new EpubCFI();

		this.layout = this.settings.layout;
		// Dom events to listen for
		// this.listenedEvents = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"];

	}

	container(axis) {
		var element = document.createElement("div");

		element.classList.add("epub-view");

		// if(this.settings.axis === "horizontal") {
		//   element.style.width = "auto";
		//   element.style.height = "0";
		// } else {
		//   element.style.width = "0";
		//   element.style.height = "auto";
		// }

		element.style.overflow = "hidden";

		if(axis && axis == "horizontal"){
			element.style.display = "inline-block";
		} else {
			element.style.display = "block";
		}

		return element;
	}

	create() {

		if(this.frame) {
			return this.frame;
		}

		if(!this.element) {
			this.element = this.createContainer();
		}

		this.frame = document.createElement("div");
		this.frame.id = this.id;
		this.frame.style.overflow = "hidden";
		this.frame.style.wordSpacing = "initial";
		this.frame.style.lineHeight = "initial";

		this.resizing = true;

		// this.frame.style.display = "none";
		this.element.style.visibility = "hidden";
		this.frame.style.visibility = "hidden";

		if(this.settings.axis === "horizontal") {
			this.frame.style.width = "auto";
			this.frame.style.height = "0";
		} else {
			this.frame.style.width = "0";
			this.frame.style.height = "auto";
		}

		this._width = 0;
		this._height = 0;

		this.element.appendChild(this.frame);
		this.added = true;

		this.elementBounds = bounds(this.element);

		return this.frame;
	}

	render(request, show) {

		// view.onLayout = this.layout.format.bind(this.layout);
		this.create();

		// Fit to size of the container, apply padding
		this.size();

		// Render Chain
		return this.section.render(request)
			.then(function(contents){
				return this.load(contents);
			}.bind(this))
			// .then(function(doc){
			// 	return this.hooks.content.trigger(view, this);
			// }.bind(this))
			.then(function(){
				// this.settings.layout.format(view.contents);
				// return this.hooks.layout.trigger(view, this);
			}.bind(this))
			// .then(function(){
			// 	return this.display();
			// }.bind(this))
			// .then(function(){
			// 	return this.hooks.render.trigger(view, this);
			// }.bind(this))
			.then(function(){

				// apply the layout function to the contents
				this.settings.layout.format(this.contents);

				// Expand the iframe to the full size of the content
				// this.expand();

				// Listen for events that require an expansion of the iframe
				this.addListeners();

				if(show !== false) {
					//this.q.enqueue(function(view){
					this.show();
					//}, view);
				}
				// this.map = new Map(view, this.layout);
				//this.hooks.show.trigger(view, this);
				this.emit(EVENTS.VIEWS.RENDERED, this.section);

			}.bind(this))
			.catch(function(e){
				this.emit(EVENTS.VIEWS.LOAD_ERROR, e);
			}.bind(this));

	}

	// Determine locks base on settings
	size(_width, _height) {
		var width = _width || this.settings.width;
		var height = _height || this.settings.height;

		if(this.layout.name === "pre-paginated") {
			// TODO: check if these are different than the size set in chapter
			this.lock("both", width, height);
		} else if(this.settings.axis === "horizontal") {
			this.lock("height", width, height);
		} else {
			this.lock("width", width, height);
		}

	}

	// Lock an axis to element dimensions, taking borders into account
	lock(what, width, height) {
		var elBorders = borders(this.element);
		var iframeBorders;

		if(this.frame) {
			iframeBorders = borders(this.frame);
		} else {
			iframeBorders = {width: 0, height: 0};
		}

		if(what == "width" && isNumber(width)){
			this.lockedWidth = width - elBorders.width - iframeBorders.width;
			this.resize(this.lockedWidth, false); //  width keeps ratio correct
		}

		if(what == "height" && isNumber(height)){
			this.lockedHeight = height - elBorders.height - iframeBorders.height;
			this.resize(false, this.lockedHeight);
		}

		if(what === "both" &&
				isNumber(width) &&
				isNumber(height)){

			this.lockedWidth = width - elBorders.width - iframeBorders.width;
			this.lockedHeight = height - elBorders.height - iframeBorders.height;

			this.resize(this.lockedWidth, this.lockedHeight);
		}

	}

	// Resize a single axis based on content dimensions
	expand(force) {
		var width = this.lockedWidth;
		var height = this.lockedHeight;

		var textWidth, textHeight;

		if(!this.frame || this._expanding) return;

		this._expanding = true;

		// Expand Horizontally
		if(this.settings.axis === "horizontal") {
			width = this.contentWidth(textWidth);
		} // Expand Vertically
		else if(this.settings.axis === "vertical") {
			height = this.contentHeight(textHeight);
		}

		// Only Resize if dimensions have changed or
		// if Frame is still hidden, so needs reframing
		if(this._needsReframe || width != this._width || height != this._height){
			this.resize(width, height);
		}

		this._expanding = false;
	}

	contentWidth(min) {
		return this.frame.scrollWidth;
	}

	contentHeight(min) {
		return this.frame.scrollHeight;
	}


	resize(width, height) {

		if(!this.frame) return;

		if(isNumber(width)){
			this.frame.style.width = width + "px";
			this._width = width;
		}

		if(isNumber(height)){
			this.frame.style.height = height + "px";
			this._height = height;
		}

		this.prevBounds = this.elementBounds;

		this.elementBounds = bounds(this.element);

		let size = {
			width: this.elementBounds.width,
			height: this.elementBounds.height,
			widthDelta: this.elementBounds.width - this.prevBounds.width,
			heightDelta: this.elementBounds.height - this.prevBounds.height,
		};

		this.onResize(this, size);

		this.emit(EVENTS.VIEWS.RESIZED, size);

	}


	load(contents) {
		var loading = new defer();
		var loaded = loading.promise;
		var doc = parse(contents, "text/html");
		var body = qs(doc, "body");

		/*
		var srcs = doc.querySelectorAll("[src]");

		Array.prototype.slice.call(srcs)
			.forEach(function(item) {
				var src = item.getAttribute("src");
				var assetUri = URI(src);
				var origin = assetUri.origin();
				var absoluteUri;

				if (!origin) {
					absoluteUri = assetUri.absoluteTo(this.section.url);
					item.src = absoluteUri;
				}
			}.bind(this));
		*/
		this.frame.innerHTML = body.innerHTML;

		this.document = this.frame.ownerDocument;
		this.window = this.document.defaultView;

		this.contents = new Contents(this.document, this.frame);

		this.rendering = false;

		loading.resolve(this.contents);


		return loaded;
	}

	setLayout(layout) {
		this.layout = layout;
	}


	resizeListenters() {
		// Test size again
		// clearTimeout(this.expanding);
		// this.expanding = setTimeout(this.expand.bind(this), 350);
	}

	addListeners() {
		//TODO: Add content listeners for expanding
	}

	removeListeners(layoutFunc) {
		//TODO: remove content listeners for expanding
	}

	display(request) {
		var displayed = new defer();

		if (!this.displayed) {

			this.render(request).then(function () {

				this.emit(EVENTS.VIEWS.DISPLAYED, this);
				this.onDisplayed(this);

				this.displayed = true;

				displayed.resolve(this);

			}.bind(this));

		} else {
			displayed.resolve(this);
		}


		return displayed.promise;
	}

	show() {

		this.element.style.visibility = "visible";

		if(this.frame){
			this.frame.style.visibility = "visible";
		}

		this.emit(EVENTS.VIEWS.SHOWN, this);
	}

	hide() {
		// this.frame.style.display = "none";
		this.element.style.visibility = "hidden";
		this.frame.style.visibility = "hidden";

		this.stopExpanding = true;
		this.emit(EVENTS.VIEWS.HIDDEN, this);
	}

	position() {
		return this.element.getBoundingClientRect();
	}

	locationOf(target) {
		var parentPos = this.frame.getBoundingClientRect();
		var targetPos = this.contents.locationOf(target, this.settings.ignoreClass);

		return {
			"left": window.scrollX + parentPos.left + targetPos.left,
			"top": window.scrollY + parentPos.top + targetPos.top
		};
	}

	onDisplayed(view) {
		// Stub, override with a custom functions
	}

	onResize(view, e) {
		// Stub, override with a custom functions
	}

	bounds() {
		if(!this.elementBounds) {
			this.elementBounds = bounds(this.element);
		}
		return this.elementBounds;
	}

	destroy() {

		if(this.displayed){
			this.displayed = false;

			this.removeListeners();

			this.stopExpanding = true;
			this.element.removeChild(this.frame);
			this.displayed = false;
			this.frame = null;

			this._textWidth = null;
			this._textHeight = null;
			this._width = null;
			this._height = null;
		}
		// this.element.style.height = "0px";
		// this.element.style.width = "0px";
	}
}

EventEmitter(InlineView.prototype);

export default InlineView;