home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 123 / cdrom123.iso / essenc / extens / wweb / Web Developer.xpi / chrome / webdeveloper.jar / content / webdeveloper / show_comments.js < prev    next >
Encoding:
Text File  |  2004-11-21  |  7.0 KB  |  226 lines

  1. var webdeveloper_lastCommentShown = null;
  2.  
  3. // Clears the comments from the page
  4. function webdeveloper_clearComments(pageDocument)
  5. {
  6.     var commentIconDiv = null;
  7.     var i              = 0;
  8.  
  9.     webdeveloper_lastCommentShown = null;
  10.  
  11.     // While there is a comment icon div
  12.     while((commentIconDiv = pageDocument.getElementById("webdeveloper-comment-icon" + i)) != null)
  13.     {
  14.         pageDocument.body.removeChild(commentIconDiv);
  15.         pageDocument.body.removeChild(pageDocument.getElementById("webdeveloper-comment-text" + i));
  16.  
  17.         i++;
  18.     }
  19. }
  20.  
  21. // Shows the comment text
  22. function webdeveloper_showCommentText(event)
  23. {
  24.     const target = event.target;
  25.  
  26.     // If there is an event target
  27.     if(target)
  28.     {
  29.         const content              = window.document.getElementById("content");
  30.         const mainTabBox           = content.mTabBox;
  31.         const currentDocument      = content.browsers[mainTabBox.selectedIndex].contentDocument;
  32.         const targetComment        = target.id.replace(new RegExp("icon", "gi"), "text");
  33.         const targetCommentElement = currentDocument.getElementById(targetComment);
  34.  
  35.         var lastCommentElement = null;
  36.  
  37.         // If this is not the last comment shown
  38.         if(webdeveloper_lastCommentShown && webdeveloper_lastCommentShown != targetComment)
  39.         {
  40.             lastCommentElement = currentDocument.getElementById(webdeveloper_lastCommentShown);
  41.  
  42.             // If the last comment element exists
  43.             if(lastCommentElement)
  44.             {
  45.                 lastCommentElement.style.display = "none";
  46.             }
  47.         }
  48.  
  49.         // If the element is currently hidden
  50.         if(targetCommentElement.style.display == "none")
  51.         {
  52.             targetCommentElement.style.display = "block";
  53.         }
  54.         else
  55.         {
  56.             targetCommentElement.style.display = "none";
  57.         }
  58.  
  59.         webdeveloper_lastCommentShown = targetComment;
  60.     }
  61. }
  62.  
  63. // Toggles the comments for a document
  64. function webdeveloper_toggleCommentsForDocument(pageDocument, show)
  65. {
  66.     const treeWalker = pageDocument.createTreeWalker(pageDocument.body, NodeFilter.SHOW_COMMENT, null, false);
  67.  
  68.     var comment        = null;
  69.     var commentsLength = 0;
  70.     var commentsList   = new Array();
  71.     var parentElement  = null;
  72.  
  73.     webdeveloper_lastCommentShown = null;
  74.  
  75.     // While the tree walker has more nodes
  76.     while((comment = treeWalker.nextNode()) != null)
  77.     {
  78.         commentsList.push(comment);
  79.     }
  80.  
  81.     // Remove XML declaration
  82.     if(commentsList.length > 0 && commentsList[0].nodeValue.indexOf("encoding") != -1)
  83.     {
  84.         commentsList.shift();
  85.     }
  86.  
  87.     // Remove DOCTYPE
  88.     if(commentsList.length > 0 && commentsList[0].nodeValue.indexOf("DOCTYPE") != -1)
  89.     {
  90.         commentsList.shift();
  91.     }
  92.  
  93.     commentsLength = commentsList.length;
  94.  
  95.     // If showing comments
  96.     if(show)
  97.     {
  98.         const iconSize = 17;
  99.  
  100.         var commentDiv              = null;
  101.         var commentIconDiv          = null;
  102.         var commentLeft             = 0;
  103.         var commentParent           = null;
  104.         var commentParentOffsetLeft = 0;
  105.         var commentParentOffsetTop  = 0;
  106.         var commentText             = null;
  107.         var commentTop              = 0;
  108.  
  109.         // Loop through the comments
  110.         for(var i = 0; i < commentsLength; i++)
  111.         {
  112.             comment       = commentsList[i];
  113.             commentLeft   = 0;
  114.             commentParent = comment.parentNode;
  115.             commentText   = comment.nodeValue;
  116.             commentTop    = 0;
  117.  
  118.             commentText = commentText.replace(new RegExp("<!--[\s]*", "gi"), "");
  119.             commentText = commentText.replace(new RegExp("[\s]*-->", "gi"), "");
  120.             commentText = commentText.replace(new RegExp("<([^\/][^>]*)class=\"[^\"]*\">", "gi"), "<$1>");
  121.             commentText = commentText.replace(new RegExp("<([^\/][^>]*)class=[^ >]*>", "gi"), "<$1>");
  122.  
  123.             // While the comment has a parent
  124.             while(commentParent)
  125.             {
  126.                 // If the parent node is not a document node or a table row
  127.                 if(commentParent.nodeType != 9 && commentParent.tagName && commentParent.tagName.toLowerCase() != "tr")
  128.                 {
  129.                     commentParentOffsetLeft = commentParent.offsetLeft;
  130.                     commentParentOffsetTop  = commentParent.offsetTop;
  131.  
  132.                     // If the parent node has a left offset
  133.                     if(commentParentOffsetLeft)
  134.                     {
  135.                         commentLeft += commentParentOffsetLeft;
  136.                     }
  137.  
  138.                     // If the parent node has a top offset
  139.                     if(commentParentOffsetTop)
  140.                     {
  141.                         commentTop += commentParentOffsetTop;
  142.                     }
  143.                 }
  144.  
  145.                 commentParent = commentParent.parentNode;
  146.             }
  147.  
  148.             // If the comment left offset is 0
  149.             if(commentLeft == 0)
  150.             {
  151.                 commentLeft = 1;
  152.             }
  153.             else if(commentLeft + iconSize > pageDocument.body.offsetWidth)
  154.             {
  155.                 commentLeft = pageDocument.body.offsetWidth;
  156.             }
  157.  
  158.             // If the comment top offset is 0
  159.             if(commentTop == 0)
  160.             {
  161.                 commentTop = 1;
  162.             }
  163.  
  164.             // If this is not the first comment
  165.             if(i > 0)
  166.             {
  167.                 // Loop through previous comments
  168.                 for(var j = 0; j < i; j++)
  169.                 {
  170.                     // If the top off set of a previous comment matches this comment
  171.                     if(parseInt(pageDocument.getElementById("webdeveloper-comment-icon" + j).style.top) == commentTop)
  172.                     {
  173.                         commentTop += iconSize;
  174.                     }
  175.                 }
  176.             }
  177.  
  178.             commentIconDiv            = pageDocument.createElement("div");
  179.             commentIconDiv.onclick    = webdeveloper_showCommentText;
  180.             commentIconDiv.style.left = commentLeft + "px";
  181.             commentIconDiv.style.top  = commentTop + "px";
  182.  
  183.             commentIconDiv.appendChild(pageDocument.createTextNode("!"));
  184.             commentIconDiv.setAttribute("class", "webdeveloper-comment-icon");
  185.             commentIconDiv.setAttribute("id", "webdeveloper-comment-icon" + i);
  186.  
  187.             pageDocument.body.appendChild(commentIconDiv);
  188.  
  189.             commentDiv            = pageDocument.createElement("div");
  190.             commentDiv.style.left = commentLeft + iconSize + "px";
  191.             commentDiv.style.top  = commentTop + "px";
  192.  
  193.             commentDiv.appendChild(pageDocument.createTextNode(commentText));
  194.             commentDiv.setAttribute("class", "webdeveloper-comment-text");
  195.             commentDiv.setAttribute("id", "webdeveloper-comment-text" + i);
  196.  
  197.             pageDocument.body.appendChild(commentDiv);
  198.  
  199.             // If the offset width is greater than 200
  200.             if(commentDiv.offsetWidth > 200)
  201.             {
  202.                 commentDiv.style.width = "200px";
  203.             }
  204.  
  205.             // If the offset height is greater than 100
  206.             if(commentDiv.offsetHeight > 100)
  207.             {
  208.                 commentDiv.style.height   = "100px";
  209.                 commentDiv.style.overflow = "auto";
  210.             }
  211.  
  212.             // If the comment is positioned outside the document
  213.             if(commentLeft + iconSize + commentDiv.offsetWidth > pageDocument.body.offsetWidth)
  214.             {
  215.                 commentDiv.style.left = commentLeft - commentDiv.offsetWidth - iconSize + "px";
  216.             }
  217.  
  218.             commentDiv.style.display = "none";
  219.         }
  220.     }
  221.     else
  222.     {
  223.         webdeveloper_clearComments(pageDocument);
  224.     }
  225. }
  226.