summaryrefslogtreecommitdiff
path: root/lib/epub.js/src/displayoptions.js
blob: a2793e295b67a57701d927a00cd3e4a5117d38b6 (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
import {qs, qsa } from "./utils/core";

/**
 * Open DisplayOptions Format Parser
 * @class
 * @param {document} displayOptionsDocument XML
 */
class DisplayOptions {
	constructor(displayOptionsDocument) {
		this.interactive = "";
		this.fixedLayout = "";
		this.openToSpread = "";
		this.orientationLock = "";

		if (displayOptionsDocument) {
			this.parse(displayOptionsDocument);
		}
	}

	/**
	 * Parse XML
	 * @param  {document} displayOptionsDocument XML
	 * @return {DisplayOptions} self
	 */
	parse(displayOptionsDocument) {
		if(!displayOptionsDocument) {
			return this;
		}

		const displayOptionsNode = qs(displayOptionsDocument, "display_options");
		if(!displayOptionsNode) {
			return this;
		} 

		const options = qsa(displayOptionsNode, "option");
		options.forEach((el) => {
			let value = "";

			if (el.childNodes.length) {
				value = el.childNodes[0].nodeValue;
			}

			switch (el.attributes.name.value) {
			    case "interactive":
			        this.interactive = value;
			        break;
			    case "fixed-layout":
			        this.fixedLayout = value;
			        break;
			    case "open-to-spread":
			        this.openToSpread = value;
			        break;
			    case "orientation-lock":
			        this.orientationLock = value;
			        break;
			}
		});

		return this;
	}

	destroy() {
		this.interactive = undefined;
		this.fixedLayout = undefined;
		this.openToSpread = undefined;
		this.orientationLock = undefined;
	}
}

export default DisplayOptions;