This repository has been archived by the owner on Feb 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy patharticle.js
137 lines (116 loc) · 3.97 KB
/
article.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
var contentTemplate = require('./templates/article-content.hbs');
var backgroundLoadTemplate = require('./templates/background-load.hbs');
var headerTemplate = require('../../../../shared-templates/article-header.hbs');
var Spinner = require('./spinner');
var utils = require('../utils');
class Article extends (require('events').EventEmitter) {
constructor() {
super();
this.container = document.querySelector('.article-container');
this._content = this.container.querySelector('.article-content');
this._backgroundLoadOffer = this.container.querySelector('.background-load-offer');
this._header = this.container.querySelector('.article-header');
this._spinner = new Spinner();
this.startedContentRender = false;
this.container.appendChild(this._spinner.container);
this.serverRendered = !!document.querySelector('.content.server-rendered');
this._header.addEventListener('change', event => {
if (event.target.name == 'cache') this.emit('cacheChange', {value: event.target.checked});
});
this._backgroundLoadOffer.addEventListener('click', event => {
// we're assuming there's only one button in here right now.
// yes I know that's bad
if (utils.closest(event.target, 'button')) {
this.emit('backgroundLoadRequest');
}
});
this._content.addEventListener('click', event => {
var heading = utils.closest(event.target, 'h2');
if (heading) this._onHeadingClick(heading);
});
}
_onHeadingClick(heading) {
var newDisplayVal = '';
if (heading.classList.contains('active')) {
heading.classList.remove('active');
}
else {
heading.classList.add('active');
newDisplayVal = 'block';
}
var element = heading;
while ((element = element.nextElementSibling) && !element.matches('h2')) {
element.style.display = newDisplayVal;
}
}
updateContent(articleHtml) {
this.stopLoading();
this.startedContentRender = true;
this._hideBackgroundLoadUI();
this._content.innerHTML = contentTemplate({
content: articleHtml
});
}
// this is a super hacky experiment
async streamContent(article) {
var response = await article.getHtmlResponse();
if (!response.body) {
// not supported
this.updateContent(await article.getHtml());
return;
}
// Here comes the haaaaack!
var fullContent = '';
var buffer = '';
var reader = response.body.getReader();
var decoder = new TextDecoder();
var result;
var awaitingInitialFlush = true;
while (true) {
var result = await reader.read();
buffer += decoder.decode(result.value || new Uint8Array, {
stream: !result.done
});
// so inefficient, but we don't have a better way to stream html
if (result.done || (awaitingInitialFlush && buffer.length > 9000)) {
this.stopLoading();
this.startedContentRender = true;
this._hideBackgroundLoadUI();
fullContent += buffer;
this._content.innerHTML = '<div id="content_wrapper" class="content card-content">' + fullContent + '</div>';
awaitingInitialFlush = false;
buffer = '';
}
if (result.done) break;
}
}
offerBackgroundLoad({
loadFailed = false
}={}) {
this._backgroundLoadOffer.innerHTML = backgroundLoadTemplate({loadFailed});
this._backgroundLoadOffer.style.display = 'block';
}
confirmBackgroundLoad() {
this._backgroundLoadOffer.innerHTML = backgroundLoadTemplate({confirmed: true});
}
_hideBackgroundLoadUI() {
this._backgroundLoadOffer.style.display = '';
}
updateMeta(data) {
if (!data) {
this._header.innerHTML = '';
return;
}
this._header.innerHTML = headerTemplate(data);
}
updateCachingAbility(cacheCapable) {
this.container.querySelector('.cache-toggle').style.visibility = cacheCapable ? '' : 'hidden';
}
startLoading() {
this._spinner.show(800);
}
stopLoading() {
this._spinner.hide();
}
}
module.exports = Article;