| author | gibus |
| Wed, 11 Sep 2013 23:13:01 +0200 | |
| changeset 532 | 0bad3613f59d |
| parent 525 | 89ef5ed3c48b |
| child 600 | fda73ac53450 |
| permissions | -rw-r--r-- |
| 0 | 1 |
// DOM MANIPULATION TO DYNAMICALLY RENDER COMMENTS SCOPES IN TEXT |
2 |
||
3 |
// cf. http://www.quirksmode.org/dom/w3c_core.html#nodemanipulation (informative!) |
|
4 |
||
5 |
// classes : |
|
6 |
// c-s is for 'textnode wrapper span' |
|
7 |
// c-c is a comment marker |
|
8 |
||
9 |
paintCommentScope = function(comment) { |
|
| 341 | 10 |
if (comment.reply_to_id == null && comment['start_wrapper'] != -1) { |
11 |
var selection = { 'start' : { 'elt' : document.getElementById("sv_"+comment['start_wrapper']), 'offset' : comment['start_offset'] }, |
|
12 |
'end' : { 'elt' : document.getElementById("sv_"+comment['end_wrapper']), 'offset' : comment['end_offset'] } |
|
13 |
} ; |
|
14 |
if (document.getElementById("sv_"+comment['start_wrapper'])== null) { |
|
15 |
warn_server({'from':'paintCommentScope', 'start_wrapper':comment['start_wrapper']}) ; |
|
16 |
} |
|
17 |
else { |
|
18 |
if (document.getElementById("sv_"+comment['end_wrapper'])== null) |
|
19 |
warn_server({'from':'paintCommentScope', 'end_wrapper':comment['end_wrapper']}) ; |
|
20 |
else { |
|
21 |
selection['start'] = _convertSelectionFromCSToCC(selection['start']) ; |
|
22 |
selection['end'] = _convertSelectionFromCSToCC(selection['end']) ; |
|
23 |
|
|
24 |
renderComment(selection, comment['id']) ; |
|
25 |
} |
|
26 |
} |
|
27 |
} |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
28 |
}; |
| 0 | 29 |
|
30 |
getCommentIdsFromClasses = function (elt) { |
|
| 341 | 31 |
var commentIds = [] ; |
32 |
var classes = elt['className'].split(" ") ; |
|
33 |
for (var i = 0, ilen = classes.length ; i < ilen ; i++) { |
|
34 |
if (classes[i].indexOf('c-id-') == 0) { |
|
35 |
commentIds.push(parseInt(classes[i].substring('c-id-'.length))) ; |
|
36 |
} |
|
37 |
} |
|
38 |
return commentIds ; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
39 |
}; |
| 0 | 40 |
|
41 |
renderComment = function (selection, commentId) { |
|
| 341 | 42 |
var startOffset = selection['start']['offset'] ; |
43 |
var endOffset = selection['end']['offset'] ; |
|
44 |
var startElt = selection['start']['elt'] ; |
|
45 |
var endElt = selection['end']['elt'] ; |
|
| 0 | 46 |
|
| 341 | 47 |
|
| 0 | 48 |
if ((startElt != null) && (endElt != null) && _getTextNodeContent(startElt) != '' && _getTextNodeContent(endElt) != ''){ |
49 |
// log('startElt.id : ' + startElt.id) ; |
|
50 |
// log('endElt.id : ' + endElt.id) ; |
|
| 341 | 51 |
// log('startElt.innerHTML : ' + startElt.innerHTML) ; |
52 |
// log('endElt.innerHTML : ' + endElt.innerHTML) ; |
|
53 |
markWholeNodesAsComments(startElt, endElt, commentId) ; |
|
54 |
markEndsAsComments(startElt, startOffset, endElt, endOffset, commentId) ; |
|
55 |
} |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
56 |
}; |
| 0 | 57 |
|
58 |
markWholeNodesAsComments = function (startElt, endElt, commentId) { |
|
59 |
var commonAncestor = _findCommonAncestor(startElt, endElt) ; |
|
60 |
_dynSpanToAnc(startElt, commonAncestor, commentId, false) ; |
|
61 |
|
|
62 |
_dynSpanToAnc(endElt, commonAncestor, commentId, true) ; |
|
63 |
||
64 |
_dynSpanInBetween(commonAncestor, startElt, endElt, commentId) ; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
65 |
}; |
| 0 | 66 |
|
67 |
_setTextNodeContent = function(txtNodeParent, contentString) { |
|
68 |
CY.DOM.setText(txtNodeParent, contentString); |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
69 |
}; |
| 0 | 70 |
|
71 |
_getTextNodeContent = function(txtNodeParent) { |
|
72 |
return CY.DOM.getText(txtNodeParent); |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
73 |
}; |
| 0 | 74 |
|
75 |
markEndsAsComments = function(startElt, startOffset, endElt, endOffset, commentId) { |
|
| 341 | 76 |
|
77 |
// alert('starting with: ' + startElt.childNodes.length + 'and a length of : ' + startElt.firstChild.data.length) ; |
|
78 |
// alert('2 and a length of : ' + CY.DOM.getText(startElt).length) ; |
|
| 0 | 79 |
|
| 341 | 80 |
var beforeStart = _getTextNodeContent(startElt).substring(0, startOffset) ; |
81 |
var afterStart = _getTextNodeContent(startElt).substring(startOffset) ; |
|
82 |
var beforeEnd = _getTextNodeContent(endElt).substring(0, endOffset) ; |
|
83 |
var afterEnd = _getTextNodeContent(endElt).substring(endOffset) ; |
|
| 0 | 84 |
|
85 |
var sameNode = (startElt === endElt) ; |
|
86 |
|
|
87 |
// log('beforeStart : ' + beforeStart + ' , afterStart : ' + afterStart + ' , beforeEnd : ' + beforeEnd + ' , afterEnd : ' + afterEnd + ' , sameNode : ' + sameNode) ; |
|
88 |
|
|
89 |
// taking care of start node : (and 'sameNode' case when start and end lie |
|
| 341 | 90 |
// on same node) |
| 0 | 91 |
if (afterStart != "") { // otherwise nothing to do on the start node |
| 341 | 92 |
if (CY.DOM.hasClass(startElt, 'c-c')) { |
93 |
var lastElt = null, afterStartElt = null, afterEndElt = null, beforeStartElt = null ; |
|
94 |
|
|
95 |
var btw = (sameNode) ? _getTextNodeContent(startElt).substring(startOffset, endOffset) : afterStart ; |
|
96 |
|
|
| 0 | 97 |
|
| 341 | 98 |
if (sameNode && (afterEnd != "")) { |
99 |
afterEndElt = startElt ; |
|
100 |
lastElt = afterEndElt ; |
|
101 |
} |
|
102 |
if (btw != "") { |
|
103 |
if (lastElt == null) { |
|
104 |
afterStartElt = startElt ; |
|
105 |
} |
|
106 |
else { |
|
107 |
afterStartElt = _yuiCloneNode(startElt) ; |
|
108 |
lastElt.parentNode.insertBefore(afterStartElt, lastElt) ; |
|
109 |
} |
|
110 |
lastElt = afterStartElt ; |
|
111 |
} |
|
112 |
if (beforeStart != "") { |
|
113 |
if (lastElt == null) { |
|
114 |
beforeStartElt = startElt ; |
|
115 |
} |
|
116 |
else { |
|
117 |
beforeStartElt = _yuiCloneNode(startElt) ; |
|
118 |
lastElt.parentNode.insertBefore(beforeStartElt, lastElt) ; |
|
119 |
} |
|
120 |
lastElt = beforeStartElt ; |
|
121 |
} |
|
122 |
|
|
123 |
if (afterEndElt != null) { |
|
124 |
_setTextNodeContent(afterEndElt, afterEnd) ; |
|
125 |
} |
|
126 |
|
|
127 |
if (afterStartElt != null) { |
|
128 |
_setTextNodeContent(afterStartElt, btw) ; |
|
129 |
_addIdClass(afterStartElt, commentId) ; |
|
130 |
} |
|
| 0 | 131 |
|
| 341 | 132 |
// alert('beforeStartElt.firstChild.data.length: ' + beforeStartElt.firstChild.data.length); |
133 |
// alert('beforeStartElt.childNodes.length: ' + beforeStartElt.childNodes.length); |
|
134 |
if (beforeStartElt != null) { |
|
135 |
_setTextNodeContent(beforeStartElt, beforeStart) ; |
|
136 |
} |
|
137 |
// alert('beforeStartElt.childNodes.length: ' + beforeStartElt.childNodes.length); |
|
138 |
//alert('typeof beforeStartElt: ' + typeof beforeStartElt); |
|
139 |
// alert('beforeStartElt.lastChild == beforeStartElt.firstChild : ' + (beforeStartElt.lastChild == beforeStartElt.firstChild)); |
|
140 |
|
|
141 |
// alert('beforeStartElt.firstChild.id : ' + beforeStartElt.firstChild.id); |
|
142 |
// alert('beforeStartElt.lastChild.data : ' + beforeStartElt.lastChild.data); |
|
143 |
// alert('beforeStartElt.firstChild.data : ' + beforeStartElt.firstChild.data); |
|
144 |
// alert('afterStartElt.firstChild.data : ' + afterStartElt.firstChild.data); |
|
145 |
// alert('afterEndElt.firstChild.data : ' + afterEndElt.firstChild.data); |
|
146 |
|
|
147 |
} |
|
| 0 | 148 |
} |
149 |
if ( ( !sameNode ) && ( beforeEnd != "" ) ) { // otherwise nothing to do |
|
| 341 | 150 |
// on the end node |
151 |
if (CY.DOM.hasClass(endElt, 'c-c')) { |
|
152 |
var lastElt = null, beforeEndElt = null, afterEndElt = null ; |
|
153 |
|
|
154 |
if (afterEnd != "") { |
|
155 |
afterEndElt = endElt ; |
|
156 |
lastElt = endElt ; |
|
157 |
} |
|
158 |
|
|
159 |
if (beforeEnd != "") { |
|
160 |
if (lastElt == null) |
|
161 |
beforeEndElt = endElt ; |
|
162 |
else { |
|
163 |
beforeEndElt = _yuiCloneNode(endElt) ; |
|
164 |
lastElt.parentNode.insertBefore(beforeEndElt, lastElt) ; |
|
165 |
} |
|
166 |
lastElt = beforeEndElt ; |
|
167 |
} |
|
168 |
if (afterEndElt != null) { |
|
169 |
_setTextNodeContent(afterEndElt, afterEnd) ; |
|
170 |
} |
|
171 |
|
|
172 |
if (beforeEndElt != null) { |
|
173 |
_addIdClass(beforeEndElt, commentId) ; |
|
174 |
_setTextNodeContent(beforeEndElt, beforeEnd) ; |
|
175 |
} |
|
176 |
} |
|
| 0 | 177 |
} |
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
178 |
}; |
| 0 | 179 |
|
180 |
// WARNING (200891108): had to use YUI cloneNode instead of the native cloneNode |
|
181 |
// (because of the _yuid that is cloned under IE, cf tests made in textYUIcloneNode.html) |
|
182 |
// so code like : |
|
183 |
// afterStartElt = startElt.cloneNode(true) ; |
|
184 |
// afterStartElt.id = CY.guid() ; |
|
185 |
// was replaced with : |
|
186 |
// afterStartElt = _yuiCloneNode(startElt) ; |
|
187 |
_yuiCloneNode = function (srcElt) { |
|
|
532
0bad3613f59d
Reverse to YUI 3.0.0 since with YUI.3.10.3, comment content including words 'paragraph' or 'section' do not show up on Firefox, this is weird and has to be investigated.
gibus
parents:
525
diff
changeset
|
188 |
var ret = CY.Node.getDOMNode(CY.get('#'+srcElt.id).cloneNode(true)) ; |
| 341 | 189 |
ret.id = CY.guid(); |
190 |
return ret ; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
191 |
}; |
| 0 | 192 |
|
193 |
// will wrap textNodes into c-c spans going up the DOM tree from elt to ancestor |
|
194 |
// textNodes impacted here will be those that are : |
|
195 |
// the same generation or older than elt (but elt is excluded) |
|
196 |
// AND not older than grand children of to |
|
197 |
// assumption : |
|
198 |
// 1 ancestor is an ancestor of elt (we'll be going up the DOM tree) |
|
199 |
// 2 elt is a textNode. |
|
200 |
// argumentss : |
|
201 |
// prev : will spannify previous siblings if true, next siblings otherwise |
|
202 |
_dynSpanToAnc = function (elt, ancestor, commentId, prev) { |
|
203 |
// log('in : _dynSpanToAnc, elt : ' + elt.id) ; |
|
| 341 | 204 |
var c = elt ; |
| 0 | 205 |
while ((c != null) && (c !== ancestor) && (c.parentNode !== ancestor)) { |
| 341 | 206 |
var c_sib = null ; |
207 |
if (prev) { |
|
208 |
c_sib = c.previousSibling ; |
|
209 |
} |
|
210 |
else { |
|
211 |
c_sib = c.nextSibling ; |
|
212 |
} |
|
213 |
|
|
214 |
if (c_sib == null) { |
|
215 |
c = c.parentNode ; |
|
216 |
} |
|
217 |
else { |
|
218 |
c = c_sib ; |
|
219 |
_recAddComment(c, commentId) ; |
|
220 |
} |
|
221 |
} ; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
222 |
}; |
| 0 | 223 |
|
224 |
// between elt1 and elt2 (which are excluded) |
|
225 |
_dynSpanInBetween = function (anc, elt1, elt2, commentId) { |
|
| 341 | 226 |
var a = elt1 ; |
227 |
var elt1Anc = null ; |
|
| 0 | 228 |
while (a) { |
229 |
if (a.parentNode === anc) { |
|
| 341 | 230 |
elt1Anc = a ; |
231 |
break; |
|
| 0 | 232 |
} |
233 |
a = a.parentNode; |
|
234 |
} |
|
| 341 | 235 |
if (elt1Anc != null) { |
236 |
a = elt2 ; |
|
237 |
var elt2Anc = null ; |
|
238 |
while (a) { |
|
239 |
if (a.parentNode === anc) { |
|
240 |
elt2Anc = a ; |
|
241 |
break; |
|
242 |
} |
|
243 |
a = a.parentNode; |
|
244 |
} |
|
245 |
if (elt2Anc != null) { // found both ancestor, now doing the work |
|
246 |
a = elt1Anc.nextSibling ; |
|
247 |
while ((a != null) && (a !== elt2Anc)) { |
|
248 |
_recAddComment(a, commentId) ; |
|
249 |
a = a.nextSibling ; |
|
250 |
} |
|
251 |
} |
|
252 |
} |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
253 |
}; |
| 0 | 254 |
|
255 |
// (copied from YUI dom-base) |
|
256 |
_bruteContains = function(element, needle) { |
|
257 |
while (needle) { |
|
258 |
if (element === needle) { |
|
259 |
return true; |
|
260 |
} |
|
261 |
needle = needle.parentNode; |
|
262 |
} |
|
263 |
return false; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
264 |
}; |
| 0 | 265 |
|
266 |
//elt is supposed to be c-c classed |
|
267 |
_addIdClass = function (elt, commentId) { |
|
| 341 | 268 |
CY.DOM.addClass(elt, 'c-id-' + commentId) ; |
|
504
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
269 |
var block_elt = _findParentBlockElt(elt); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
270 |
if (block_elt != null) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
271 |
_unpaintCategories(block_elt); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
272 |
_repaintCategories(elt, block_elt); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
273 |
} |
| 341 | 274 |
_updateCommentCounter(elt) ; |
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
275 |
}; |
| 0 | 276 |
|
277 |
//elt is supposed to be c-c classed |
|
278 |
_removeIdClass = function (elt, commentId) { |
|
| 341 | 279 |
CY.DOM.removeClass(elt, 'c-id-' + commentId) ; |
|
504
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
280 |
var block_elt = _findParentBlockElt(elt); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
281 |
if (block_elt != null) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
282 |
_unpaintCategories(block_elt); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
283 |
_repaintCategories(elt, block_elt, commentId); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
284 |
} |
| 341 | 285 |
_updateCommentCounter(elt) ; |
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
286 |
}; |
| 0 | 287 |
|
288 |
//elt is supposed to be c-c classed |
|
289 |
_removeIdClasses = function (elt) { |
|
| 341 | 290 |
var re = _cgetRegExp('(?:^|\\s+)c-id-(?:\\d+)', 'g'); |
291 |
elt['className'] = elt['className'].replace(re, " ") ; |
|
292 |
_updateCommentCounter(elt) ; |
|
|
504
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
293 |
var block_elt = _findParentBlockElt(elt); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
294 |
if (block_elt != null) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
295 |
_unpaintCategories(block_elt); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
296 |
} |
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
297 |
}; |
| 0 | 298 |
|
|
504
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
299 |
// Finds the closest parent of an element which is a block. |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
300 |
_findParentBlockElt = function(elt) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
301 |
var block_elt = elt; |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
302 |
var block_elt_style = block_elt.currentStyle || window.getComputedStyle(block_elt, ""); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
303 |
var block_elt_display = block_elt_style.display; |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
304 |
while (block_elt != null && block_elt_display != 'block') { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
305 |
block_elt = block_elt.parentElement; |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
306 |
block_elt_style = block_elt.currentStyle || window.getComputedStyle(block_elt, ""); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
307 |
block_elt_display = block_elt_style.display; |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
308 |
} |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
309 |
return block_elt; |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
310 |
}; |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
311 |
|
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
312 |
// Removes all vertical bars from a block element. |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
313 |
_unpaintCategories = function(block_elt) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
314 |
CY.DOM.removeClass(block_elt, 'cat1'); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
315 |
CY.DOM.removeClass(block_elt, 'cat2'); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
316 |
CY.DOM.removeClass(block_elt, 'cat3'); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
317 |
CY.DOM.removeClass(block_elt, 'cat4'); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
318 |
CY.DOM.removeClass(block_elt, 'cat5'); |
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
319 |
}; |
|
504
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
320 |
|
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
321 |
// Paints all vertical bars of a block element but the one for commentId if not null. |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
322 |
_repaintCategories = function(elt, block_elt, commentId) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
323 |
// Loop through all comments in this wrapper id |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
324 |
var wrapper_id = parseInt(getWrapperAncestor(elt).id.substr(3)); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
325 |
var len = gDb.comments.length; |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
326 |
for (var i=0; i<len; i++) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
327 |
if (i in gDb.comments) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
328 |
var comment = gDb.comments[i]; |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
329 |
if ((commentId == null || comment.id != commentId) && comment.start_wrapper <= wrapper_id && comment.end_wrapper >= wrapper_id) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
330 |
if (comment.category) { |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
331 |
CY.DOM.addClass(block_elt, 'cat' + comment.category); |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
332 |
} |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
333 |
} |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
334 |
} |
|
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
335 |
} |
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
336 |
}; |
|
504
b2e0186daa5b
Adds a category to comments, painted with colored vertical bar.
gibus
parents:
373
diff
changeset
|
337 |
|
| 0 | 338 |
_recAddComment = function (elt, commentId) { |
| 341 | 339 |
if (CY.DOM.hasClass(elt, 'c-c')) { |
340 |
_addIdClass(elt, commentId) ; |
|
341 |
} |
|
342 |
else { |
|
343 |
var c = elt.firstChild ; |
|
| 0 | 344 |
while (c != null) { |
| 341 | 345 |
_recAddComment(c, commentId) ; |
346 |
c = c.nextSibling ; |
|
| 0 | 347 |
} |
348 |
} |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
349 |
}; |
| 0 | 350 |
|
351 |
// might be expensive ... (? maybe should use contains when available, instead |
|
352 |
// of custom _bruteContains) |
|
353 |
_findCommonAncestor = function (elt1, elt2) { |
|
| 341 | 354 |
if (_bruteContains(elt1, elt2)) |
355 |
return elt1 ; |
|
356 |
else { |
|
357 |
var e = elt2 ; |
|
358 |
while ((e != null) && !_bruteContains(e, elt1)) { |
|
359 |
e = e.parentNode ; |
|
360 |
} |
|
361 |
return e ; |
|
362 |
} |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
363 |
}; |
| 0 | 364 |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
365 |
_cregexCache = {}; |
| 0 | 366 |
// inspired (copied) from dom-base-debug in yui |
367 |
_cgetRegExp = function(str, flags) { |
|
368 |
flags = flags || ''; |
|
369 |
if (!_cregexCache[str + flags]) { |
|
370 |
_cregexCache[str + flags] = new RegExp(str, flags); |
|
371 |
} |
|
372 |
return _cregexCache[str + flags]; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
373 |
}; |
| 0 | 374 |
|
375 |
//c-c should be classed with a c-count-x where x is a number for color graduation |
|
376 |
//c-c should be classed with many c-id-xid where xid is the comment db id of comment that apply to it |
|
377 |
_updateCommentCounter = function (elt) { |
|
| 341 | 378 |
var re = _cgetRegExp('(?:^|\\s+)c-id-(?:\\d+)', 'g'); |
379 |
var matches = elt['className'].match(re); |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
380 |
var countIds = (matches == null) ? 0 : gDb.getThreads(CY.Array.map(matches, function(item) {return gDb.getComment(parseInt(item.replace(/\D/g, '')));})).length |
| 341 | 381 |
|
382 |
re = _cgetRegExp('(?:^|\\s+)c-count-(?:\\d+)', 'g'); |
|
383 |
elt['className'] = elt['className'].replace(re, " ") ; |
|
384 |
CY.DOM.addClass(elt, 'c-count-'+countIds+' ') ; |
|
|
373
f6fe14eb51bc
Add a title attribute on comment scopes to indicate the number of comments in the scope.
Simon Descarpentries <sid1@sopinspace.com>
parents:
341
diff
changeset
|
385 |
if (countIds > 0) { |
|
f6fe14eb51bc
Add a title attribute on comment scopes to indicate the number of comments in the scope.
Simon Descarpentries <sid1@sopinspace.com>
parents:
341
diff
changeset
|
386 |
elt.setAttribute ('title', countIds + ngettext(' comment', ' comments', countIds)); |
|
f6fe14eb51bc
Add a title attribute on comment scopes to indicate the number of comments in the scope.
Simon Descarpentries <sid1@sopinspace.com>
parents:
341
diff
changeset
|
387 |
if (countIds > 25) { |
|
f6fe14eb51bc
Add a title attribute on comment scopes to indicate the number of comments in the scope.
Simon Descarpentries <sid1@sopinspace.com>
parents:
341
diff
changeset
|
388 |
// ensure that we have the last color even if there are too many comments on the same place |
|
f6fe14eb51bc
Add a title attribute on comment scopes to indicate the number of comments in the scope.
Simon Descarpentries <sid1@sopinspace.com>
parents:
341
diff
changeset
|
389 |
CY.DOM.addClass(elt, 'c-count-25') ; |
|
f6fe14eb51bc
Add a title attribute on comment scopes to indicate the number of comments in the scope.
Simon Descarpentries <sid1@sopinspace.com>
parents:
341
diff
changeset
|
390 |
} |
|
f6fe14eb51bc
Add a title attribute on comment scopes to indicate the number of comments in the scope.
Simon Descarpentries <sid1@sopinspace.com>
parents:
341
diff
changeset
|
391 |
} |
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
392 |
}; |
| 0 | 393 |
|
394 |
_convertSelectionFromCCToCS = function (sel) { |
|
| 341 | 395 |
var offset = sel['offset'] ; |
396 |
var elt = sel['elt'].parentNode ; |
|
397 |
|
|
398 |
var e = sel['elt'].previousSibling ; |
|
399 |
while (e != null) { |
|
400 |
offset += _getTextNodeContent(e).length ; |
|
401 |
e = e.previousSibling ; // will be a c-c !! |
|
402 |
} |
|
403 |
|
|
404 |
return {'elt':elt, 'offset':offset} ; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
405 |
}; |
| 0 | 406 |
|
407 |
_convertSelectionFromCSToCC = function (sel) { |
|
| 341 | 408 |
var ret = {'elt':null, 'offset':-1} ; |
409 |
|
|
410 |
var cc = null ; |
|
411 |
var ccElt = sel['elt'].firstChild ; |
|
412 |
var length = 0 ; |
|
413 |
while (ccElt != null) { |
|
414 |
var prevLength = length ; |
|
415 |
length += _getTextNodeContent(ccElt).length ; |
|
416 |
if (length >= sel['offset']) { |
|
417 |
ret['elt'] = ccElt ; |
|
418 |
ret['offset'] = sel['offset'] - prevLength ; |
|
419 |
break ; |
|
420 |
} |
|
421 |
ccElt = ccElt.nextSibling ; // will be a c-c !! |
|
422 |
} |
|
423 |
return ret ; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
424 |
}; |
| 0 | 425 |
|
426 |
||
427 |
/*******************************************************************************/ |
|
428 |
/* SCOPE REMOVAL */ |
|
429 |
/*******************************************************************************/ |
|
430 |
||
431 |
unpaintCommentScope = function(comment) { |
|
432 |
var dbId = comment.id; |
|
433 |
||
| 341 | 434 |
var classeId = 'c-id-' + dbId ; |
435 |
var toBeRemovedElts = [] ; |
|
| 0 | 436 |
|
| 341 | 437 |
var cNodeList = CY.all("."+classeId) ; |
438 |
if (cNodeList != null) { // null in case of a reply ... |
|
439 |
for (var i = 0, ilen = cNodeList.size() ; i < ilen ; i++) { |
|
440 |
var c = cNodeList.item(i) ; |
|
441 |
if (c.hasClass('c-c')) { // always will !! |
|
442 |
var cElt = CY.Node.getDOMNode(c) ; |
|
443 |
_removeIdClass(cElt, dbId) ; |
|
444 |
|
|
445 |
var cIds = getCommentIdsFromClasses(cElt) ; |
|
446 |
quicksort(cIds) ; |
|
447 |
|
|
448 |
var p = c.get('previousSibling') ; |
|
449 |
if (p != null) { |
|
450 |
var pElt = CY.Node.getDOMNode(p) ; |
|
451 |
var pIds = getCommentIdsFromClasses(pElt) ; |
|
452 |
quicksort(pIds) ; |
|
453 |
if (areSortedArraysEqual(cIds, pIds)) { |
|
454 |
_setTextNodeContent(cElt, _getTextNodeContent(pElt) + _getTextNodeContent(cElt)) ; |
|
455 |
toBeRemovedElts.push(pElt) ; |
|
456 |
} |
|
457 |
} |
|
458 |
|
|
459 |
var n = c.get('nextSibling') ; |
|
460 |
if (n != null) { |
|
461 |
var nElt = CY.Node.getDOMNode(n) ; |
|
462 |
var nIds = getCommentIdsFromClasses(nElt) ; |
|
463 |
quicksort(nIds) ; |
|
464 |
if (areSortedArraysEqual(cIds, nIds)) { |
|
465 |
cElt.firstChild.data = cElt.firstChild.data + nElt.firstChild.data; |
|
466 |
toBeRemovedElts.push(nElt) ; |
|
467 |
} |
|
468 |
} |
|
469 |
} |
|
470 |
else { |
|
471 |
alert('HAS NO c-c ? : ' + commentNode.get('id') + " , innerHTML :" + commentNode.get('innerHTML')) ; |
|
472 |
return ; |
|
473 |
} |
|
474 |
} |
|
475 |
} |
|
476 |
for (var i = 0, ilen = toBeRemovedElts.length ; i < ilen ; i++) { |
|
477 |
toBeRemovedElts[i].parentNode.removeChild(toBeRemovedElts[i]) ; |
|
478 |
} |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
479 |
}; |
| 0 | 480 |
|
481 |
// not related to the unpaintCommentScope function (faster) |
|
482 |
unpaintAllComments = function() { |
|
| 341 | 483 |
var cNodeList= CY.all(".c-s") ; |
484 |
var toBeRemovedElts = [] ; |
|
485 |
for (var i = 0, ilen = cNodeList.size() ; i < ilen ; i++) { |
|
486 |
var c = cNodeList.item(i) ; |
|
487 |
|
|
488 |
// remove Classes |
|
489 |
var fc = c.get('firstChild') ; |
|
490 |
var fcElt = CY.Node.getDOMNode(c.get('firstChild')) ; |
|
491 |
_removeIdClasses(fcElt) ; |
|
492 |
|
|
493 |
// merge nodes |
|
494 |
var n = fc.get('nextSibling') ; |
|
495 |
while (n != null) { |
|
496 |
var nElt = CY.Node.getDOMNode(n) ; |
|
497 |
fcElt.firstChild.data = fcElt.firstChild.data + nElt.firstChild.data; |
|
498 |
toBeRemovedElts.push(nElt) ; |
|
499 |
n = n.get('nextSibling') ; |
|
500 |
} |
|
501 |
} |
|
502 |
for (var i = 0, ilen = toBeRemovedElts.length ; i < ilen ; i++) { |
|
503 |
toBeRemovedElts[i].parentNode.removeChild(toBeRemovedElts[i]) ; |
|
504 |
} |
|
| 0 | 505 |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
506 |
}; |
| 0 | 507 |
|
508 |
showScope = function(commentDbId) { |
|
| 341 | 509 |
var s = CY.all('.c-id-' + commentDbId); |
510 |
if (s != null) |
|
511 |
s.addClass('c-scope') ; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
512 |
}; |
| 0 | 513 |
|
514 |
hideScopeAnyway = function() { |
|
| 341 | 515 |
var s = CY.all('.c-scope'); |
516 |
if (s != null) |
|
517 |
s.removeClass('c-scope') ; |
|
|
519
e89c25780e6e
Gradient of colors for commented text depends on number of comments including replies, instead of depending only on number of threads.
gibus
parents:
504
diff
changeset
|
518 |
}; |