You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
devdocs/assets/javascripts/views/pages/base.js

74 lines
1.7 KiB

app.views.BasePage = class BasePage extends app.View {
1 year ago
constructor(el, entry) {
super(el);
1 year ago
this.entry = entry;
}
11 years ago
deactivate() {
if (super.deactivate(...arguments)) {
1 year ago
return (this.highlightNodes = []);
}
}
render(content, fromCache) {
1 year ago
if (fromCache == null) {
fromCache = false;
}
this.highlightNodes = [];
this.previousTiming = null;
1 year ago
if (!this.constructor.className) {
this.addClass(`_${this.entry.doc.type}`);
}
this.html(content);
1 year ago
if (!fromCache) {
this.highlightCode();
}
this.activate();
1 year ago
if (this.afterRender) {
this.delay(this.afterRender);
}
if (this.highlightNodes.length > 0) {
requestAnimationFrame(() => this.paintCode());
}
}
11 years ago
highlightCode() {
for (var el of this.findAll("pre[data-language]")) {
1 year ago
var language = el.getAttribute("data-language");
el.classList.add(`language-${language}`);
this.highlightNodes.push(el);
}
}
paintCode(timing) {
if (this.previousTiming) {
1 year ago
if (Math.round(1000 / (timing - this.previousTiming)) > 50) {
// fps
this.nodesPerFrame = Math.round(
Math.min(this.nodesPerFrame * 1.25, 50),
);
} else {
1 year ago
this.nodesPerFrame = Math.round(Math.max(this.nodesPerFrame * 0.8, 10));
}
} else {
this.nodesPerFrame = 10;
}
for (var el of this.highlightNodes.splice(0, this.nodesPerFrame)) {
var clipEl;
1 year ago
if ((clipEl = el.lastElementChild)) {
$.remove(clipEl);
}
Prism.highlightElement(el);
1 year ago
if (clipEl) {
$.append(el, clipEl);
}
}
1 year ago
if (this.highlightNodes.length > 0) {
requestAnimationFrame(() => this.paintCode());
1 year ago
}
this.previousTiming = timing;
}
};