2016-07-18 18:49:44 +05:00
|
|
|
/** hidepost.js -- hides posts from page given $things */
|
|
|
|
|
|
|
|
|
2016-07-18 19:44:18 +05:00
|
|
|
function get_hidden_posts() {
|
|
|
|
var st = get_storage();
|
|
|
|
var prefix = "nntpchan_hide_post_";
|
|
|
|
return {
|
|
|
|
all : function() {
|
|
|
|
var msgids = [];
|
|
|
|
for ( var k in st) {
|
|
|
|
if (k.indexOf(prefix) == 0) {
|
|
|
|
var m = k.substring(prefix.length);
|
|
|
|
msgids.push(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return msgids;
|
|
|
|
},
|
|
|
|
|
|
|
|
add : function (msg) {
|
|
|
|
st[prefix+msg] = "post";
|
|
|
|
},
|
|
|
|
|
|
|
|
del : function (msg) {
|
|
|
|
st.removeItem(prefix+msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-18 18:49:44 +05:00
|
|
|
// is a post elem an OP?
|
|
|
|
function postIsOP(elem) {
|
|
|
|
var ds = elem.dataset;
|
|
|
|
return ds && ds.rootmsgid == ds.msgid ;
|
|
|
|
}
|
|
|
|
|
2016-07-18 20:12:47 +05:00
|
|
|
function _hide_elem(elem, fade) {
|
|
|
|
if(!fade) {
|
|
|
|
if (elem.style) {
|
|
|
|
elem.style.display = "none";
|
|
|
|
} else {
|
|
|
|
elem.style = {display: "none" };
|
|
|
|
}
|
|
|
|
elem.dataset.userhide = "yes";
|
2016-07-18 18:49:44 +05:00
|
|
|
} else {
|
2016-07-18 20:12:47 +05:00
|
|
|
$(elem).fadeOut(400, function() {
|
|
|
|
_hide_elem(elem);
|
|
|
|
});
|
2016-07-18 18:49:44 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function _unhide_elem(elem) {
|
2016-07-18 20:12:47 +05:00
|
|
|
$(elem).fadeIn();
|
2016-07-18 18:49:44 +05:00
|
|
|
elem.dataset.userhide = "no";
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if element is hidden
|
|
|
|
function _elemIsHidden(elem) {
|
|
|
|
return elem.dataset && elem.dataset.userhide == "yes";
|
|
|
|
}
|
|
|
|
|
|
|
|
// hide a post
|
2016-07-18 20:12:47 +05:00
|
|
|
function hidepost(elem, nofade) {
|
2016-07-18 18:49:44 +05:00
|
|
|
console.log("hidepost("+elem.dataset.msgid+")");
|
2016-07-18 19:44:18 +05:00
|
|
|
var posts = get_hidden_posts();
|
|
|
|
if (posts) {
|
|
|
|
// add to persitant hide
|
|
|
|
posts.add(elem.dataset.msgidhash);
|
|
|
|
}
|
2016-07-18 18:49:44 +05:00
|
|
|
if(postIsOP(elem)) {
|
|
|
|
// hide thread it's an OP
|
|
|
|
var thread = document.getElementById("thread_"+elem.dataset.rootmsgidhash);
|
|
|
|
if (thread) {
|
|
|
|
var e = thread.getElementsByClassName("post");
|
|
|
|
for ( var idx = 0; idx < e.length ; idx ++ ) {
|
|
|
|
if (e[idx].dataset.msgid == elem.dataset.msgid) continue; // don't apply
|
|
|
|
hidepost(e[idx]);
|
|
|
|
}
|
|
|
|
}
|
2016-07-18 20:12:47 +05:00
|
|
|
}
|
2016-07-18 18:49:44 +05:00
|
|
|
// hide attachments and post body
|
|
|
|
var es = elem.getElementsByClassName("attachments");
|
|
|
|
for (var idx = 0; idx < es.length ; idx ++ ) {
|
2016-07-18 20:12:47 +05:00
|
|
|
_hide_elem(es[idx], !nofade);
|
2016-07-18 18:49:44 +05:00
|
|
|
}
|
|
|
|
es = elem.getElementsByClassName("post_body");
|
|
|
|
for (var idx = 0; idx < es.length ; idx ++ ) {
|
2016-07-18 20:12:47 +05:00
|
|
|
_hide_elem(es[idx], !nofade);
|
2016-07-18 18:49:44 +05:00
|
|
|
}
|
|
|
|
es = elem.getElementsByClassName("postheader");
|
|
|
|
for (var idx = 0; idx < es.length ; idx ++ ) {
|
2016-07-18 20:12:47 +05:00
|
|
|
_hide_elem(es[idx], !nofade);
|
2016-07-18 18:49:44 +05:00
|
|
|
}
|
|
|
|
elem.dataset.userhide = "yes";
|
2016-07-18 19:44:18 +05:00
|
|
|
elem.setHideLabel("[show]");
|
2016-07-18 18:49:44 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// unhide a post
|
|
|
|
function unhidepost(elem) {
|
|
|
|
console.log("unhidepost("+elem.dataset.msgid+")");
|
2016-07-18 19:44:18 +05:00
|
|
|
var posts = get_hidden_posts();
|
|
|
|
if (posts) {
|
|
|
|
// remove from persiting hide
|
|
|
|
posts.del(elem.dataset.msgidhash);
|
|
|
|
}
|
2016-07-18 18:49:44 +05:00
|
|
|
if(postIsOP(elem)) {
|
|
|
|
var thread = document.getElementById("thread_"+elem.dataset.rootmsgidhash);
|
|
|
|
if(thread) {
|
|
|
|
var e = thread.getElementsByClassName("post");
|
|
|
|
for ( var idx = 0; idx < e.length ; idx ++ ) {
|
|
|
|
if(e[idx].dataset.msgid == elem.dataset.msgid) continue;
|
|
|
|
unhidepost(e[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// unhide attachments and post body
|
|
|
|
var es = elem.getElementsByClassName("attachments");
|
|
|
|
for (var idx = 0; idx < es.length ; idx ++ ) {
|
|
|
|
_unhide_elem(es[idx]);
|
|
|
|
}
|
|
|
|
es = elem.getElementsByClassName("post_body");
|
|
|
|
for (var idx = 0; idx < es.length ; idx ++ ) {
|
|
|
|
_unhide_elem(es[idx]);
|
|
|
|
}
|
|
|
|
es = elem.getElementsByClassName("postheader");
|
|
|
|
for (var idx = 0; idx < es.length ; idx ++ ) {
|
|
|
|
_unhide_elem(es[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
elem.dataset.userhide = "no";
|
|
|
|
elem.setHideLabel("[hide]");
|
|
|
|
}
|
|
|
|
|
|
|
|
// hide a post given a callback that checks each post
|
|
|
|
function hideposts(check_func) {
|
|
|
|
var es = document.getElementsByClassName("post");
|
|
|
|
for ( var idx = 0; idx < es.length ; idx ++ ) {
|
|
|
|
var elem = es[idx];
|
|
|
|
if(check_func && elem && check_func(elem)) {
|
|
|
|
hidepost(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// unhide all posts given callback
|
|
|
|
// if callback is null unhide all
|
|
|
|
function unhideall(check_func) {
|
|
|
|
var es = document.getElementsByClassName("post");
|
|
|
|
for (var idx=0 ; idx < es.length; idx ++ ) {
|
|
|
|
var elem = es[idx];
|
|
|
|
if(!check_func) { unhide(elem); }
|
|
|
|
else if(check_func(elem)) { unhide(elem); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// inject posthide into page
|
|
|
|
onready(function() {
|
|
|
|
var posts = document.getElementsByClassName("post");
|
|
|
|
for (var idx = 0 ; idx < posts.length; idx++ ) {
|
|
|
|
console.log("inject hide: "+posts[idx].dataset.msgid);
|
|
|
|
var inject = function (elem) {
|
|
|
|
var hider = document.createElement("a");
|
|
|
|
hider.setAttribute("class", "hider");
|
|
|
|
elem.setHideLabel = function (txt) {
|
|
|
|
var e_hider = hider;
|
|
|
|
e_hider.innerHTML = txt;
|
|
|
|
}
|
2016-07-18 20:12:47 +05:00
|
|
|
elem.hidepost = function() {
|
2016-07-18 18:49:44 +05:00
|
|
|
var e_self = elem;
|
|
|
|
var e_hider = hider;
|
|
|
|
hidepost(e_self);
|
|
|
|
}
|
2016-07-18 20:12:47 +05:00
|
|
|
elem.unhidepost = function() {
|
2016-07-18 18:49:44 +05:00
|
|
|
var e_self = elem;
|
|
|
|
var e_hider = hider;
|
|
|
|
unhidepost(e_self);
|
|
|
|
}
|
|
|
|
elem.isHiding = function() {
|
|
|
|
var e_self = elem;
|
|
|
|
return _elemIsHidden(e_self);
|
|
|
|
}
|
|
|
|
hider.appendChild(document.createTextNode("[hide]"));
|
|
|
|
hider.onclick = function() {
|
|
|
|
var e_self = elem;
|
|
|
|
if(e_self.isHiding()) {
|
2016-07-18 20:12:47 +05:00
|
|
|
e_self.unhidepost();
|
2016-07-18 18:49:44 +05:00
|
|
|
} else {
|
2016-07-18 20:12:47 +05:00
|
|
|
e_self.hidepost();
|
2016-07-18 18:49:44 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
elem.appendChild(hider);
|
|
|
|
};
|
|
|
|
inject(posts[idx]);
|
|
|
|
}
|
2016-07-18 19:44:18 +05:00
|
|
|
// apply persiting hidden posts
|
|
|
|
var posts = get_hidden_posts();
|
|
|
|
if(posts) {
|
|
|
|
var all = posts.all();
|
|
|
|
for ( var idx = 0 ; idx < all.length; idx ++ ) {
|
|
|
|
var id = all[idx];
|
|
|
|
var elem = document.getElementById(id);
|
|
|
|
if(elem)
|
2016-07-18 20:12:47 +05:00
|
|
|
hidepost(elem, true);
|
2016-07-18 19:44:18 +05:00
|
|
|
}
|
|
|
|
}
|
2016-07-18 18:49:44 +05:00
|
|
|
});
|