-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookify.js
234 lines (203 loc) · 7.02 KB
/
bookify.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
223
224
225
226
227
228
229
230
231
232
233
234
//@ sourceURL=bookify.js
window.bookify = {
// "Global" settings config
// This should be passed in at some point
settings: {
debug: false,
token: "YOU-NEED-TO-SPECIFY-ONE-SILLY"
},
/*
Concerned with retrieving parsable DOM objects for an article
*/
readability: {
isLeafNodeFilter: function() {
/* Where 'leaf' means a block level node with no block level children */
var blockLevelElements = ["ADDRESS", "ARTICLE", "ASIDE", "AUDIO",
"BLOCKQUOTE", "CANVAS", "DD", "DIV",
"DL", "FIELDSET", "FIGCAPTION", "FIGURE",
"FOOTER", "FORM", "H1", "H2", "H3",
"H4", "H5", "H6", "HEADER", "HGROUP",
"HR", "NOSCRIPT", "OL", "OUTPUT", "P", "PRE", "SECTION",
"TABLE", "TFOOT", "UL", "VIDEO"];
// Block level check
if ($.inArray($(this).prop('tagName'), blockLevelElements) == -1) {
return false;
}
// Children reverse block level check
var isLeafNode = true;
$(this).children().each(function() {
if ($.inArray($(this).prop('tagName'), blockLevelElements) != -1) {
isLeafNode = false;
return false;
}
});
/*
TODO check for a node that we'd lose content on, e.g.
<div>This would go<p>but this would stay</p></div>
This includes more pernicious situations where we lose useful data,
e.g. <blockquote><p>The blockquote node will be lost</p></blockquote>
*/
return isLeafNode;
},
extractContent: function(allContent) {
/* array of all the main content */
var filteredContent = $("<div></div>");
$(allContent).find("*").filter(bookify.readability.isLeafNodeFilter).each(function() {
filteredContent.append($(this).clone());
});
return filteredContent.children();
},
apiCallUrl: function(contentUrl) {
return "https://readability.com/api/content/v1/parser?token=" + bookify.settings.token + "&url=" + contentUrl;
},
getContent: function(contentUrl, params) {
var successFn = params.success;
var errorFn = params.error;
var dataType = params.dataType || 'json';
/* Slurps content from the given url and passes the first element to success */
var apiCallUrl = bookify.readability.apiCallUrl(contentUrl);
//console.log("Trying to load " + apiCallUrl);
$.ajax({
url: apiCallUrl,
dataType: dataType,
success: function(results){
//console.log("Loaded " + results.url);
results.content = bookify.readability.extractContent($.parseHTML(results.content));
successFn(results);
},
error: errorFn
});
}
},
/*
Concerned with rendering cloned DOM data to the page.
*/
renderer: {
elementOffPage: function(element) {
var elBottom = element.offset().top + element.height();
var screenHeight = $(window).height();
return (elBottom > screenHeight) ? true : false;
},
renderPageForward: function(element, surface) {
/* Renders the page forwards, starting with the given element */
var lastRendered = null;
var aborted = false;
var body = $("body");
element.nextAll().addBack().each(function() {
var pageElement = $(this).clone();
surface.append(pageElement);
if (bookify.renderer.elementOffPage(body)) {
aborted = true;
if (bookify.settings.debug) {
pageElement.addClass('debug');
} else {
pageElement.remove();
}
return false;
} else {
lastRendered = $(this);
}
});
return {
firstRendered: element,
lastRendered: lastRendered,
aborted: aborted
};
},
renderPageBackward: function(element, surface) {
/* Renders the page in reverse, starting with the given element. */
var lastRendered = null;
var aborted = false;
var body = $("body");
var pageTail = null;
element.next().prevAll().each(function() {
var pageElement = $(this).clone();
if (! pageTail) pageTail = pageElement;
surface.prepend(pageElement);
if (bookify.renderer.elementOffPage(body)) {
aborted = true;
pageElement.remove();
return false;
} else {
lastRendered = $(this);
}
});
return {
firstRendered: element,
lastRendered: lastRendered,
aborted: aborted
};
}
},
/*
Concerned with logic around rendering pages
*/
controller: {
updateProgressbar: function(progressbar, elements, currentEl) {
var progress = currentEl.length == 0 ? elements.length : elements.index(currentEl);
progressbar.progressbar("option", "value", progress);
},
largeElementHack: function(el, surface) {
//console.log("Element too large, forcing render");
surface.append(el.clone());
return {
pageHead: el,
nextPageHead: el.next()
};
},
renderCurrentPage: function(pointer, surface) {
surface.empty();
var report = bookify.renderer.renderPageForward(pointer.pageHead, surface);
// TEMP hack for when elements are too large to ever be rendered
if (!report.lastRendered) {
return bookify.controller.largeElementHack(pointer.pageHead, surface);
}
return {
pageHead: report.firstRendered,
nextPageHead: report.lastRendered.next()
};
},
renderNextPage: function(pointer, surface) {
if (pointer.nextPageHead.length == 0) {
surface.effect("shake", {direction: "left", distance: 3});
return pointer;
}
surface.empty();
var report = bookify.renderer.renderPageForward(pointer.nextPageHead, surface);
// TEMP hack for when elements are too large to ever be rendered
if (!report.lastRendered) {
return bookify.controller.largeElementHack(pointer.nextPageHead, surface);
}
return {
pageHead: report.firstRendered,
nextPageHead: report.lastRendered.next()
};
},
renderPreviousPage: function(pointer, surface) {
var prevEl = pointer.pageHead.prev();
if (prevEl.length == 0) {
surface.effect("shake", {direction: "right", distance: 3});
return pointer;
}
surface.empty();
var report = bookify.renderer.renderPageBackward(prevEl, surface);
if (!report.aborted) {
// Haven't run out of space yet, render forward as well
var forwardReport = bookify.renderer.renderPageForward(report.firstRendered.next(), surface);
if (forwardReport.lastRendered) {
return {
pageHead: report.lastRendered,
nextPageHead: forwardReport.lastRendered.next()
};
}
} else if (!report.lastRendered) {
// TEMP hack for when elements are too large to ever be rendered
return bookify.controller.largeElementHack(prevEl, surface);
}
return {
pageHead: report.lastRendered,
nextPageHead: report.firstRendered.next()
};
}
}
};