/
hideChildComments.js
119 lines (119 loc) · 4.64 KB
/
hideChildComments.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
modules['hideChildComments'] = {
moduleID: 'hideChildComments',
moduleName: 'Hide All Child Comments',
category: 'Comments',
options: {
// any configurable options you have go here...
// options must have a type and a value..
// valid types are: text, boolean (if boolean, value must be true or false)
// for example:
automatic: {
type: 'boolean',
value: false,
description: 'Automatically hide all but parent comments, or provide a link to hide them all?'
}
},
description: 'Allows you to hide all comments except for replies to the OP for easier reading.',
isEnabled: function() {
return RESUtils.options.getModulePrefs(this.moduleID);
},
include: [
'comments'
],
isMatchURL: function() {
return RESUtils.isMatchURL(this.moduleID);
},
go: function() {
if ((this.isEnabled()) && (this.isMatchURL())) {
var toggleButton = document.createElement('li');
this.toggleAllLink = document.createElement('a');
this.toggleAllLink.textContent = 'hide all child comments';
this.toggleAllLink.setAttribute('action', 'hide');
this.toggleAllLink.setAttribute('href', '#');
this.toggleAllLink.setAttribute('title', 'Show only replies to original poster.');
this.toggleAllLink.addEventListener('click', function(e) {
e.preventDefault();
modules['hideChildComments'].toggleComments(this.getAttribute('action'));
if (this.getAttribute('action') === 'hide') {
this.setAttribute('action', 'show');
this.setAttribute('title', 'Show all comments.');
this.textContent = 'show all child comments';
} else {
this.setAttribute('action', 'hide');
this.setAttribute('title', 'Show only replies to original poster.');
this.textContent = 'hide all child comments';
}
}, true);
toggleButton.appendChild(this.toggleAllLink);
var commentMenu = document.querySelector('ul.buttons');
if (commentMenu) {
commentMenu.appendChild(toggleButton);
var rootComments = document.querySelectorAll('div.commentarea > div.sitetable > div.thing > div.child > div.listing');
for (var i = 0, len = rootComments.length; i < len; i++) {
toggleButton = document.createElement('li');
var toggleLink = document.createElement('a');
toggleLink.setAttribute('data-text','hide child comments');
toggleLink.setAttribute('action', 'hide');
toggleLink.setAttribute('href', '#');
toggleLink.setAttribute('class', 'toggleChildren noCtrlF');
// toggleLink.setAttribute('title','Hide child comments.');
toggleLink.addEventListener('click', function(e) {
e.preventDefault();
modules['hideChildComments'].toggleComments(this.getAttribute('action'), this);
if (this.getAttribute('action') === 'hide') {
this.setAttribute('action', 'show');
// this.setAttribute('title','show child comments.');
this.setAttribute('data-text', 'show child comments');
} else {
this.setAttribute('action', 'hide');
// this.setAttribute('title','hide child comments.');
this.setAttribute('data-text','hide child comments');
}
}, true);
toggleButton.appendChild(toggleLink);
var sib = rootComments[i].parentNode.previousSibling;
if (typeof sib !== 'undefined') {
var sibMenu = sib.querySelector('ul.buttons');
if (sibMenu) sibMenu.appendChild(toggleButton);
}
}
if (this.options.automatic.value) {
// ensure we're not in a permalinked post..
var linkRE = /\/comments\/(?:\w+)\/(?:\w+)\/(\w+)/;
if (! location.pathname.match(linkRE)) {
RESUtils.click(this.toggleAllLink);
}
}
}
}
},
toggleComments: function(action, obj) {
var thisChildren;
if (obj) {
thisChildren = $(obj).closest('.thing').children('.child').children('.sitetable')[0];
thisChildren.style.display = (action === 'hide') ? 'none' : 'block';
} else {
// toggle all comments
var commentContainers = document.querySelectorAll('div.commentarea > div.sitetable > div.thing');
for (var i = 0, len = commentContainers.length; i < len; i++) {
thisChildren = commentContainers[i].querySelector('div.child > div.sitetable');
var thisToggleLink = commentContainers[i].querySelector('a.toggleChildren');
if (thisToggleLink !== null) {
if (action === 'hide') {
if (thisChildren !== null) {
thisChildren.style.display = 'none';
}
thisToggleLink.setAttribute('data-text','show child comments');
thisToggleLink.setAttribute('action', 'show');
} else {
if (thisChildren !== null) {
thisChildren.style.display = 'block';
}
thisToggleLink.setAttribute('data-text','hide child comments');
thisToggleLink.setAttribute('action', 'hide');
}
}
}
}
}
};