forked from futurepress/epub.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender_iframe.js
222 lines (171 loc) · 5.82 KB
/
render_iframe.js
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
EPUBJS.Render.Iframe = function() {
this.iframe = null;
this.document = null;
this.window = null;
this.docEl = null;
this.bodyEl = null;
this.leftPos = 0;
this.pageWidth = 0;
};
//-- Build up any html needed
EPUBJS.Render.Iframe.prototype.create = function(){
this.iframe = document.createElement('iframe');
this.iframe.id = "epubjs-iframe:" + EPUBJS.core.uuid();
this.iframe.scrolling = "no";
this.iframe.seamless = "seamless";
// Back up if seamless isn't supported
this.iframe.style.border = "none";
this.iframe.addEventListener("load", this.loaded.bind(this), false);
return this.iframe;
};
/**
* Sets the source of the iframe with the given URL string
* Takes: URL string
* Returns: promise with document element
*/
EPUBJS.Render.Iframe.prototype.load = function(chapter){
var render = this,
deferred = new RSVP.defer();
chapter.url().then(function(url){
// Reset the scroll position
render.leftPos = 0;
if(this.window) {
this.unload();
}
this.iframe.onload = function(e) {
render.document = render.iframe.contentDocument;
render.docEl = render.document.documentElement;
render.headEl = render.document.head;
render.bodyEl = render.document.body || render.document.querySelector("body");
render.window = render.iframe.contentWindow;
render.window.addEventListener("resize", render.resized.bind(render), false);
//-- Clear Margins
if(render.bodyEl) {
render.bodyEl.style.margin = "0";
}
deferred.resolve(render.docEl);
};
this.iframe.onerror = function(e) {
//console.error("Error Loading Contents", e);
deferred.reject({
message : "Error Loading Contents: " + e,
stack : new Error().stack
});
};
this.iframe.contentWindow.location.replace(url);
}.bind(this));
return deferred.promise;
};
EPUBJS.Render.Iframe.prototype.loaded = function(v){
var url = this.iframe.contentWindow.location.href;
if(url != "about:blank"){
this.trigger("render:loaded", url);
}
};
// Resize the iframe to the given width and height
EPUBJS.Render.Iframe.prototype.resize = function(width, height){
var iframeBox;
if(!this.iframe) return;
this.iframe.height = height;
if(!isNaN(width) && width % 2 !== 0){
width += 1; //-- Prevent cutting off edges of text in columns
}
this.iframe.width = width;
// Get the fractional height and width of the iframe
// Default to orginal if bounding rect is 0
this.width = this.iframe.getBoundingClientRect().width || width;
this.height = this.iframe.getBoundingClientRect().height || height;
};
EPUBJS.Render.Iframe.prototype.resized = function(e){
// Get the fractional height and width of the iframe
this.width = this.iframe.getBoundingClientRect().width;
this.height = this.iframe.getBoundingClientRect().height;
};
EPUBJS.Render.Iframe.prototype.totalWidth = function(){
return this.docEl.scrollWidth;
};
EPUBJS.Render.Iframe.prototype.totalHeight = function(){
return this.docEl.scrollHeight;
};
EPUBJS.Render.Iframe.prototype.setPageDimensions = function(pageWidth, pageHeight){
this.pageWidth = pageWidth;
this.pageHeight = pageHeight;
//-- Add a page to the width of the document to account an for odd number of pages
// this.docEl.style.width = this.docEl.scrollWidth + pageWidth + "px";
};
EPUBJS.Render.Iframe.prototype.setLeft = function(leftPos){
// this.bodyEl.style.marginLeft = -leftPos + "px";
// this.docEl.style.marginLeft = -leftPos + "px";
// this.docEl.style[EPUBJS.Render.Iframe.transform] = 'translate('+ (-leftPos) + 'px, 0)';
this.document.defaultView.scrollTo(leftPos, 0);
};
EPUBJS.Render.Iframe.prototype.setStyle = function(style, val, prefixed){
if(prefixed) {
style = EPUBJS.core.prefixed(style);
}
if(this.bodyEl) this.bodyEl.style[style] = val;
};
EPUBJS.Render.Iframe.prototype.removeStyle = function(style){
if(this.bodyEl) this.bodyEl.style[style] = '';
};
EPUBJS.Render.Iframe.prototype.addHeadTag = function(tag, attrs, _doc) {
var doc = _doc || this.document;
var tagEl = doc.createElement(tag);
var headEl = doc.head;
for(var attr in attrs) {
tagEl.setAttribute(attr, attrs[attr]);
}
if(headEl) headEl.insertBefore(tagEl, headEl.firstChild);
};
EPUBJS.Render.Iframe.prototype.page = function(pg){
this.leftPos = this.pageWidth * (pg-1); //-- pages start at 1
this.setLeft(this.leftPos);
};
//-- Show the page containing an Element
EPUBJS.Render.Iframe.prototype.getPageNumberByElement = function(el){
var left, pg;
if(!el) return;
left = this.leftPos + el.getBoundingClientRect().left; //-- Calculate left offset compaired to scrolled position
pg = Math.floor(left / this.pageWidth) + 1; //-- pages start at 1
return pg;
};
//-- Show the page containing an Element
EPUBJS.Render.Iframe.prototype.getPageNumberByRect = function(boundingClientRect){
var left, pg;
left = this.leftPos + boundingClientRect.left; //-- Calculate left offset compaired to scrolled position
pg = Math.floor(left / this.pageWidth) + 1; //-- pages start at 1
return pg;
};
// Return the root element of the content
EPUBJS.Render.Iframe.prototype.getBaseElement = function(){
return this.bodyEl;
};
// Checks if an element is on the screen
EPUBJS.Render.Iframe.prototype.isElementVisible = function(el){
var rect;
var left;
if(el && typeof el.getBoundingClientRect === 'function'){
rect = el.getBoundingClientRect();
left = rect.left; //+ rect.width;
if( rect.width !== 0 &&
rect.height !== 0 && // Element not visible
left >= 0 &&
left < this.pageWidth ) {
return true;
}
}
return false;
};
EPUBJS.Render.Iframe.prototype.scroll = function(bool){
if(bool) {
this.iframe.scrolling = "yes";
} else {
this.iframe.scrolling = "no";
}
};
// Cleanup event listeners
EPUBJS.Render.Iframe.prototype.unload = function(){
this.window.removeEventListener("resize", this.resized);
};
//-- Enable binding events to Render
RSVP.EventTarget.mixin(EPUBJS.Render.Iframe.prototype);