src/cm/media/js/client/c_scopes.js
changeset 519 e89c25780e6e
parent 504 b2e0186daa5b
child 525 89ef5ed3c48b
equal deleted inserted replaced
518:9172f4c6ad70 519:e89c25780e6e
    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) ; 
   173           _addIdClass(beforeEndElt, commentId) ;
   173           _addIdClass(beforeEndElt, commentId) ;
   174           _setTextNodeContent(beforeEndElt, beforeEnd) ;
   174           _setTextNodeContent(beforeEndElt, beforeEnd) ;
   175         }
   175         }
   176       }
   176       }
   177     }
   177     }
   178 } ;
   178 };
   179 
   179 
   180 // WARNING (200891108): had to use YUI cloneNode instead of the native cloneNode
   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)
   181 // (because of the _yuid that is cloned under IE, cf tests made in textYUIcloneNode.html)
   182 // so code like :
   182 // so code like :
   183 // afterStartElt = startElt.cloneNode(true) ;
   183 // afterStartElt = startElt.cloneNode(true) ;
   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
   217     else {
   217     else {
   218       c = c_sib ;     
   218       c = c_sib ;     
   219       _recAddComment(c, commentId) ;
   219       _recAddComment(c, commentId) ;
   220     }
   220     }
   221   } ;
   221   } ;
   222 } ;
   222 };
   223 
   223 
   224 // between elt1 and elt2 (which are excluded)
   224 // between elt1 and elt2 (which are excluded)
   225 _dynSpanInBetween = function (anc, elt1, elt2, commentId) {
   225 _dynSpanInBetween = function (anc, elt1, elt2, commentId) {
   226   var a = elt1 ;
   226   var a = elt1 ;
   227   var elt1Anc = null ;
   227   var elt1Anc = null ;
   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));
   331           CY.DOM.addClass(block_elt, 'cat' + comment.category);
   331           CY.DOM.addClass(block_elt, 'cat' + comment.category);
   332         }
   332         }
   333       }
   333       }
   334     }
   334     }
   335   }
   335   }
   336 }
   336 };
   337 
   337 
   338 _recAddComment = function (elt, commentId) {
   338 _recAddComment = function (elt, commentId) {
   339   if (CY.DOM.hasClass(elt, 'c-c')) {
   339   if (CY.DOM.hasClass(elt, 'c-c')) {
   340     _addIdClass(elt, commentId) ;
   340     _addIdClass(elt, commentId) ;
   341   }
   341   }
   344         while (c != null) {
   344         while (c != null) {
   345           _recAddComment(c, commentId) ;
   345           _recAddComment(c, commentId) ;
   346       c = c.nextSibling ;
   346       c = c.nextSibling ;
   347         }
   347         }
   348     }
   348     }
   349 } ;
   349 };
   350 
   350 
   351 // might be expensive ... (? maybe should use contains when available, instead
   351 // might be expensive ... (? maybe should use contains when available, instead
   352 // of custom _bruteContains)
   352 // of custom _bruteContains)
   353 _findCommonAncestor = function (elt1, elt2) {
   353 _findCommonAncestor = function (elt1, elt2) {
   354   if (_bruteContains(elt1, elt2))
   354   if (_bruteContains(elt1, elt2))
   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) {
   387     if (countIds > 25) {
   387     if (countIds > 25) {
   388       // ensure that we have the last color even if there are too many comments on the same place
   388       // ensure that we have the last color even if there are too many comments on the same place
   389       CY.DOM.addClass(elt, 'c-count-25') ;
   389       CY.DOM.addClass(elt, 'c-count-25') ;
   390     }
   390     }
   391   }
   391   }
   392 } ;
   392 };
   393 
   393 
   394 _convertSelectionFromCCToCS = function (sel) {
   394 _convertSelectionFromCCToCS = function (sel) {
   395   var offset = sel['offset'] ;
   395   var offset = sel['offset'] ;
   396   var elt = sel['elt'].parentNode ;
   396   var elt = sel['elt'].parentNode ;
   397   
   397   
   400     offset += _getTextNodeContent(e).length ;
   400     offset += _getTextNodeContent(e).length ;
   401     e = e.previousSibling ; // will be a c-c !!
   401     e = e.previousSibling ; // will be a c-c !!
   402   }
   402   }
   403   
   403   
   404   return  {'elt':elt, 'offset':offset} ;
   404   return  {'elt':elt, 'offset':offset} ;
   405 } ;
   405 };
   406 
   406 
   407 _convertSelectionFromCSToCC = function (sel) {
   407 _convertSelectionFromCSToCC = function (sel) {
   408   var ret = {'elt':null, 'offset':-1} ;
   408   var ret = {'elt':null, 'offset':-1} ;
   409   
   409   
   410   var cc = null ;
   410   var cc = null ;
   419       break ;
   419       break ;
   420     }
   420     }
   421     ccElt = ccElt.nextSibling ; // will be a c-c !!
   421     ccElt = ccElt.nextSibling ; // will be a c-c !!
   422   }
   422   }
   423   return ret ;
   423   return ret ;
   424 } ;
   424 };
   425 
   425 
   426 
   426 
   427 /*******************************************************************************/
   427 /*******************************************************************************/
   428 /* SCOPE REMOVAL */
   428 /* SCOPE REMOVAL */
   429 /*******************************************************************************/
   429 /*******************************************************************************/
   474     }
   474     }
   475   }
   475   }
   476   for (var i = 0, ilen = toBeRemovedElts.length ; i < ilen ; i++)  {
   476   for (var i = 0, ilen = toBeRemovedElts.length ; i < ilen ; i++)  {
   477     toBeRemovedElts[i].parentNode.removeChild(toBeRemovedElts[i]) ;
   477     toBeRemovedElts[i].parentNode.removeChild(toBeRemovedElts[i]) ;
   478   }
   478   }
   479 } ;
   479 };
   480 
   480 
   481 // not related to the unpaintCommentScope function (faster)
   481 // not related to the unpaintCommentScope function (faster)
   482 unpaintAllComments = function() {
   482 unpaintAllComments = function() {
   483   var cNodeList= CY.all(".c-s") ;
   483   var cNodeList= CY.all(".c-s") ;
   484   var toBeRemovedElts = [] ;
   484   var toBeRemovedElts = [] ;
   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