Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kms70847/73c74ec0390a326e4a43 to your computer and use it in GitHub Desktop.
Save kms70847/73c74ec0390a326e4a43 to your computer and use it in GitHub Desktop.
var Post = function(node){
this.node = node;
this.parent = undefined;
this.children = [];
this.isCollapsed = false;
}
Post.prototype.getIndentation = function(){
var ind = this.node.getElementsByClassName("ind")[0];
return ind.getElementsByTagName("IMG")[0].width / 40;
}
Post.prototype.getDescendants = function(){
var result = [];
this.children.forEach(function(child){
result.push(child);
Array.prototype.push.apply(result, child.getDescendants());
});
return result;
}
Post.prototype.hideBody = function(){
var body = this.node.getElementsByClassName("comment")[0];
body.style.display = "none";
}
Post.prototype.showBody = function(){
var body = this.node.getElementsByClassName("comment")[0];
body.style.display = "";
}
Post.prototype.hide = function(){
this.node.style.display = "none";
}
Post.prototype.show = function(){
this.node.style.display = "";
}
Post.prototype.collapse = function(){
this.hideBody();
this.isCollapsed = true;
this.getDescendants().forEach(function(descendant){
descendant.hide();
});
}
Post.prototype.expand = function(post){
this.showBody();
this.isCollapsed = false;
this.children.forEach(function(child){
child.show();
if (!child.isCollapsed){
child.expand();
}
});
}
Post.prototype.addCollapseButton = function(){
var head = this.node.getElementsByClassName("comhead")[0];
var button = makeToggleButton(
Post.prototype.collapse.bind(this),
Post.prototype.expand.bind(this)
);
head.appendChild(button);
}
function makeToggleButton(callbackA, callbackB){
var button = document.createElement("A");
var state = 0;
button.innerHTML = "[-]";
button.onclick = function(){
if (state == 0){
button.innerHTML = "[+]";
callbackA();
state = 1;
}
else{
button.innerHTML = "[-]";
callbackB();
state = 0;
}
};
return button;
}
function getPostNodes(){
var tree = document.getElementsByClassName("comment-tree")[0];
return tree.getElementsByClassName("athing");
}
function constructRelationships(posts){
function top(seq){return seq[seq.length-1];}
function level(post){ return post == undefined ? -1 : post.getIndentation();}
var topLevelPosts = [];
var stack = [undefined];
posts.forEach(function(post){
while (level(top(stack)) >= level(post)){
stack.pop();
}
post.parent = top(stack);
if (top(stack) == undefined){
topLevelPosts.push(post);
}else{
top(stack).children.push(post);
post.parent = top(stack);
}
stack.push(post);
});
return topLevelPosts;
}
try{
var nodes = getPostNodes();
var allPosts = [];
for (node of nodes){
allPosts.push(new Post(node));
}
constructRelationships(allPosts);
allPosts.forEach(function(post){ post.addCollapseButton(); });
}
catch(e){
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment