23 |
23 |
24 renderComment(selection, comment['id']) ; |
24 renderComment(selection, comment['id']) ; |
25 } |
25 } |
26 } |
26 } |
27 } |
27 } |
28 } ; |
28 }; |
29 |
29 |
30 getCommentIdsFromClasses = function (elt) { |
30 getCommentIdsFromClasses = function (elt) { |
31 var commentIds = [] ; |
31 var commentIds = [] ; |
32 var classes = elt['className'].split(" ") ; |
32 var classes = elt['className'].split(" ") ; |
33 for (var i = 0, ilen = classes.length ; i < ilen ; i++) { |
33 for (var i = 0, ilen = classes.length ; i < ilen ; i++) { |
34 if (classes[i].indexOf('c-id-') == 0) { |
34 if (classes[i].indexOf('c-id-') == 0) { |
35 commentIds.push(parseInt(classes[i].substring('c-id-'.length))) ; |
35 commentIds.push(parseInt(classes[i].substring('c-id-'.length))) ; |
36 } |
36 } |
37 } |
37 } |
38 return commentIds ; |
38 return commentIds ; |
39 } ; |
39 }; |
40 |
40 |
41 renderComment = function (selection, commentId) { |
41 renderComment = function (selection, commentId) { |
42 var startOffset = selection['start']['offset'] ; |
42 var startOffset = selection['start']['offset'] ; |
43 var endOffset = selection['end']['offset'] ; |
43 var endOffset = selection['end']['offset'] ; |
44 var startElt = selection['start']['elt'] ; |
44 var startElt = selection['start']['elt'] ; |
51 // log('startElt.innerHTML : ' + startElt.innerHTML) ; |
51 // log('startElt.innerHTML : ' + startElt.innerHTML) ; |
52 // log('endElt.innerHTML : ' + endElt.innerHTML) ; |
52 // log('endElt.innerHTML : ' + endElt.innerHTML) ; |
53 markWholeNodesAsComments(startElt, endElt, commentId) ; |
53 markWholeNodesAsComments(startElt, endElt, commentId) ; |
54 markEndsAsComments(startElt, startOffset, endElt, endOffset, commentId) ; |
54 markEndsAsComments(startElt, startOffset, endElt, endOffset, commentId) ; |
55 } |
55 } |
56 } ; |
56 }; |
57 |
57 |
58 markWholeNodesAsComments = function (startElt, endElt, commentId) { |
58 markWholeNodesAsComments = function (startElt, endElt, commentId) { |
59 var commonAncestor = _findCommonAncestor(startElt, endElt) ; |
59 var commonAncestor = _findCommonAncestor(startElt, endElt) ; |
60 _dynSpanToAnc(startElt, commonAncestor, commentId, false) ; |
60 _dynSpanToAnc(startElt, commonAncestor, commentId, false) ; |
61 |
61 |
62 _dynSpanToAnc(endElt, commonAncestor, commentId, true) ; |
62 _dynSpanToAnc(endElt, commonAncestor, commentId, true) ; |
63 |
63 |
64 _dynSpanInBetween(commonAncestor, startElt, endElt, commentId) ; |
64 _dynSpanInBetween(commonAncestor, startElt, endElt, commentId) ; |
65 } ; |
65 }; |
66 |
66 |
67 _setTextNodeContent = function(txtNodeParent, contentString) { |
67 _setTextNodeContent = function(txtNodeParent, contentString) { |
68 CY.DOM.setText(txtNodeParent, contentString); |
68 CY.DOM.setText(txtNodeParent, contentString); |
69 } ; |
69 }; |
70 |
70 |
71 _getTextNodeContent = function(txtNodeParent) { |
71 _getTextNodeContent = function(txtNodeParent) { |
72 return CY.DOM.getText(txtNodeParent); |
72 return CY.DOM.getText(txtNodeParent); |
73 } ; |
73 }; |
74 |
74 |
75 markEndsAsComments = function(startElt, startOffset, endElt, endOffset, commentId) { |
75 markEndsAsComments = function(startElt, startOffset, endElt, endOffset, commentId) { |
76 |
76 |
77 // alert('starting with: ' + startElt.childNodes.length + 'and a length of : ' + startElt.firstChild.data.length) ; |
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) ; |
78 // alert('2 and a length of : ' + CY.DOM.getText(startElt).length) ; |
186 // afterStartElt = _yuiCloneNode(startElt) ; |
186 // afterStartElt = _yuiCloneNode(startElt) ; |
187 _yuiCloneNode = function (srcElt) { |
187 _yuiCloneNode = function (srcElt) { |
188 var ret = CY.Node.getDOMNode(CY.get('#'+srcElt.id).cloneNode(true)) ; |
188 var ret = CY.Node.getDOMNode(CY.get('#'+srcElt.id).cloneNode(true)) ; |
189 ret.id = CY.guid(); |
189 ret.id = CY.guid(); |
190 return ret ; |
190 return ret ; |
191 } ; |
191 }; |
192 |
192 |
193 // will wrap textNodes into c-c spans going up the DOM tree from elt to ancestor |
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 : |
194 // textNodes impacted here will be those that are : |
195 // the same generation or older than elt (but elt is excluded) |
195 // the same generation or older than elt (but elt is excluded) |
196 // AND not older than grand children of to |
196 // AND not older than grand children of to |
248 _recAddComment(a, commentId) ; |
248 _recAddComment(a, commentId) ; |
249 a = a.nextSibling ; |
249 a = a.nextSibling ; |
250 } |
250 } |
251 } |
251 } |
252 } |
252 } |
253 } ; |
253 }; |
254 |
254 |
255 // (copied from YUI dom-base) |
255 // (copied from YUI dom-base) |
256 _bruteContains = function(element, needle) { |
256 _bruteContains = function(element, needle) { |
257 while (needle) { |
257 while (needle) { |
258 if (element === needle) { |
258 if (element === needle) { |
259 return true; |
259 return true; |
260 } |
260 } |
261 needle = needle.parentNode; |
261 needle = needle.parentNode; |
262 } |
262 } |
263 return false; |
263 return false; |
264 }, |
264 }; |
265 |
265 |
266 //elt is supposed to be c-c classed |
266 //elt is supposed to be c-c classed |
267 _addIdClass = function (elt, commentId) { |
267 _addIdClass = function (elt, commentId) { |
268 CY.DOM.addClass(elt, 'c-id-' + commentId) ; |
268 CY.DOM.addClass(elt, 'c-id-' + commentId) ; |
269 var block_elt = _findParentBlockElt(elt); |
269 var block_elt = _findParentBlockElt(elt); |
270 if (block_elt != null) { |
270 if (block_elt != null) { |
271 _unpaintCategories(block_elt); |
271 _unpaintCategories(block_elt); |
272 _repaintCategories(elt, block_elt); |
272 _repaintCategories(elt, block_elt); |
273 } |
273 } |
274 _updateCommentCounter(elt) ; |
274 _updateCommentCounter(elt) ; |
275 } ; |
275 }; |
276 |
276 |
277 //elt is supposed to be c-c classed |
277 //elt is supposed to be c-c classed |
278 _removeIdClass = function (elt, commentId) { |
278 _removeIdClass = function (elt, commentId) { |
279 CY.DOM.removeClass(elt, 'c-id-' + commentId) ; |
279 CY.DOM.removeClass(elt, 'c-id-' + commentId) ; |
280 var block_elt = _findParentBlockElt(elt); |
280 var block_elt = _findParentBlockElt(elt); |
281 if (block_elt != null) { |
281 if (block_elt != null) { |
282 _unpaintCategories(block_elt); |
282 _unpaintCategories(block_elt); |
283 _repaintCategories(elt, block_elt, commentId); |
283 _repaintCategories(elt, block_elt, commentId); |
284 } |
284 } |
285 _updateCommentCounter(elt) ; |
285 _updateCommentCounter(elt) ; |
286 } ; |
286 }; |
287 |
287 |
288 //elt is supposed to be c-c classed |
288 //elt is supposed to be c-c classed |
289 _removeIdClasses = function (elt) { |
289 _removeIdClasses = function (elt) { |
290 var re = _cgetRegExp('(?:^|\\s+)c-id-(?:\\d+)', 'g'); |
290 var re = _cgetRegExp('(?:^|\\s+)c-id-(?:\\d+)', 'g'); |
291 elt['className'] = elt['className'].replace(re, " ") ; |
291 elt['className'] = elt['className'].replace(re, " ") ; |
292 _updateCommentCounter(elt) ; |
292 _updateCommentCounter(elt) ; |
293 var block_elt = _findParentBlockElt(elt); |
293 var block_elt = _findParentBlockElt(elt); |
294 if (block_elt != null) { |
294 if (block_elt != null) { |
295 _unpaintCategories(block_elt); |
295 _unpaintCategories(block_elt); |
296 } |
296 } |
297 } ; |
297 }; |
298 |
298 |
299 // Finds the closest parent of an element which is a block. |
299 // Finds the closest parent of an element which is a block. |
300 _findParentBlockElt = function(elt) { |
300 _findParentBlockElt = function(elt) { |
301 var block_elt = elt; |
301 var block_elt = elt; |
302 var block_elt_style = block_elt.currentStyle || window.getComputedStyle(block_elt, ""); |
302 var block_elt_style = block_elt.currentStyle || window.getComputedStyle(block_elt, ""); |
314 CY.DOM.removeClass(block_elt, 'cat1'); |
314 CY.DOM.removeClass(block_elt, 'cat1'); |
315 CY.DOM.removeClass(block_elt, 'cat2'); |
315 CY.DOM.removeClass(block_elt, 'cat2'); |
316 CY.DOM.removeClass(block_elt, 'cat3'); |
316 CY.DOM.removeClass(block_elt, 'cat3'); |
317 CY.DOM.removeClass(block_elt, 'cat4'); |
317 CY.DOM.removeClass(block_elt, 'cat4'); |
318 CY.DOM.removeClass(block_elt, 'cat5'); |
318 CY.DOM.removeClass(block_elt, 'cat5'); |
319 } |
319 }; |
320 |
320 |
321 // Paints all vertical bars of a block element but the one for commentId if not null. |
321 // Paints all vertical bars of a block element but the one for commentId if not null. |
322 _repaintCategories = function(elt, block_elt, commentId) { |
322 _repaintCategories = function(elt, block_elt, commentId) { |
323 // Loop through all comments in this wrapper id |
323 // Loop through all comments in this wrapper id |
324 var wrapper_id = parseInt(getWrapperAncestor(elt).id.substr(3)); |
324 var wrapper_id = parseInt(getWrapperAncestor(elt).id.substr(3)); |
358 while ((e != null) && !_bruteContains(e, elt1)) { |
358 while ((e != null) && !_bruteContains(e, elt1)) { |
359 e = e.parentNode ; |
359 e = e.parentNode ; |
360 } |
360 } |
361 return e ; |
361 return e ; |
362 } |
362 } |
363 } ; |
363 }; |
364 |
364 |
365 _cregexCache = {} ; |
365 _cregexCache = {}; |
366 // inspired (copied) from dom-base-debug in yui |
366 // inspired (copied) from dom-base-debug in yui |
367 _cgetRegExp = function(str, flags) { |
367 _cgetRegExp = function(str, flags) { |
368 flags = flags || ''; |
368 flags = flags || ''; |
369 if (!_cregexCache[str + flags]) { |
369 if (!_cregexCache[str + flags]) { |
370 _cregexCache[str + flags] = new RegExp(str, flags); |
370 _cregexCache[str + flags] = new RegExp(str, flags); |
371 } |
371 } |
372 return _cregexCache[str + flags]; |
372 return _cregexCache[str + flags]; |
373 } ; |
373 }; |
374 |
374 |
375 //c-c should be classed with a c-count-x where x is a number for color graduation |
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 |
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) { |
377 _updateCommentCounter = function (elt) { |
378 var re = _cgetRegExp('(?:^|\\s+)c-id-(?:\\d+)', 'g'); |
378 var re = _cgetRegExp('(?:^|\\s+)c-id-(?:\\d+)', 'g'); |
379 var matches = elt['className'].match(re); |
379 var matches = elt['className'].match(re); |
380 var countIds = (matches == null) ? 0 : matches.length ; |
380 var countIds = (matches == null) ? 0 : gDb.getThreads(CY.Array.map(matches, function(item) {return gDb.getComment(parseInt(item.replace(/\D/g, '')));})).length |
381 |
381 |
382 re = _cgetRegExp('(?:^|\\s+)c-count-(?:\\d+)', 'g'); |
382 re = _cgetRegExp('(?:^|\\s+)c-count-(?:\\d+)', 'g'); |
383 elt['className'] = elt['className'].replace(re, " ") ; |
383 elt['className'] = elt['className'].replace(re, " ") ; |
384 CY.DOM.addClass(elt, 'c-count-'+countIds+' ') ; |
384 CY.DOM.addClass(elt, 'c-count-'+countIds+' ') ; |
385 if (countIds > 0) { |
385 if (countIds > 0) { |
501 } |
501 } |
502 for (var i = 0, ilen = toBeRemovedElts.length ; i < ilen ; i++) { |
502 for (var i = 0, ilen = toBeRemovedElts.length ; i < ilen ; i++) { |
503 toBeRemovedElts[i].parentNode.removeChild(toBeRemovedElts[i]) ; |
503 toBeRemovedElts[i].parentNode.removeChild(toBeRemovedElts[i]) ; |
504 } |
504 } |
505 |
505 |
506 } ; |
506 }; |
507 |
507 |
508 showScope = function(commentDbId) { |
508 showScope = function(commentDbId) { |
509 var s = CY.all('.c-id-' + commentDbId); |
509 var s = CY.all('.c-id-' + commentDbId); |
510 if (s != null) |
510 if (s != null) |
511 s.addClass('c-scope') ; |
511 s.addClass('c-scope') ; |
512 } ; |
512 }; |
513 |
513 |
514 hideScopeAnyway = function() { |
514 hideScopeAnyway = function() { |
515 var s = CY.all('.c-scope'); |
515 var s = CY.all('.c-scope'); |
516 if (s != null) |
516 if (s != null) |
517 s.removeClass('c-scope') ; |
517 s.removeClass('c-scope') ; |
518 } |
518 }; |
519 |
|