changeset 3972:97dd5fb0c75b

add comment to select plugin
author Byeongweon [tasyblue@gmail.com]
date Mon, 07 Jul 2008 10:18:20 +0900
parents cec23df5a1cf
children d2200a05c888
files wiki/htdocs/applets/moinFCKplugins/restrict_actions/fckplugin.js wiki/htdocs/applets/moinFCKplugins/selection/fckplugin.js
diffstat 2 files changed, 310 insertions(+), 243 deletions(-) [+]
line wrap: on
line diff
--- a/wiki/htdocs/applets/moinFCKplugins/restrict_actions/fckplugin.js	Mon Jul 07 01:58:44 2008 +0900
+++ b/wiki/htdocs/applets/moinFCKplugins/restrict_actions/fckplugin.js	Mon Jul 07 10:18:20 2008 +0900
@@ -14,16 +14,16 @@
 
 RestrictedNamedCommand.prototype.GetState = function()
 {
- var bState = FCK.GetNamedCommandState(this.Name);
- if (FCKSelection.GetType() == 'Control')
- {
+  var bState = FCK.GetNamedCommandState(this.Name);
+  if (FCKSelection.GetType() == 'Control')
+  {
+    return bState;
+  }
+  else if (FCKSelection.CheckForNodeNames(this.forbidden))
+  { 
+    return FCK_TRISTATE_DISABLED;
+  }
   return bState;
- }
- else if (FCKSelection.CheckForNodeNames(this.forbidden))
- { 
-  return FCK_TRISTATE_DISABLED;
- }
- return bState;
 }
 
 /* #######################################################
@@ -34,8 +34,8 @@
 
 var RestrictedUniqueNamedFormat = function(commandName, forbidden)
 {
- this.Name = commandName;
- this.forbidden = forbidden;
+  this.Name = commandName;
+  this.forbidden = forbidden;
 }
 
 RestrictedUniqueNamedFormat.prototype = new RestrictedNamedCommand();
@@ -44,6 +44,7 @@
 {
   if (FCK.GetNamedCommandState(this.Name)==FCK_TRISTATE_OFF)
     FCK.ExecuteNamedCommand('RemoveFormat');
+
   FCK.ExecuteNamedCommand(this.Name);
 }
 
@@ -63,8 +64,8 @@
 
 RestrictedFormatBlockCommand.prototype.GetState = function()
 {
- if (FCKSelection.CheckForNodeNames(this.forbidden))
-   return FCK_TRISTATE_DISABLED;
+  if (FCKSelection.CheckForNodeNames(this.forbidden))
+    return FCK_TRISTATE_DISABLED;
  else
    return FCK.GetNamedCommandValue( 'FormatBlock' ) ;
 }
@@ -133,8 +134,8 @@
 
 var StyleButtonCommand = function(stylename, unique)
 {
- this.style = FCK.Styles.GetStyle(stylename);   // using FCK.Style instead of fckstylesloader
- this.unique = unique;
+  this.style = FCK.Styles.GetStyle(stylename);   // using FCK.Style instead of fckstylesloader
+  this.unique = unique;
 }
 
 StyleButtonCommand.prototype = new FCKStyleCommand();
--- a/wiki/htdocs/applets/moinFCKplugins/selection/fckplugin.js	Mon Jul 07 01:58:44 2008 +0900
+++ b/wiki/htdocs/applets/moinFCKplugins/selection/fckplugin.js	Mon Jul 07 10:18:20 2008 +0900
@@ -13,6 +13,39 @@
 }
 
 
+/* 
+ * Checks if the name of any of the selected nodes or the nodes surrounding 
+ * the selection match the pattern RE.
+ * Returns FCK_TRISTATE_DISABLED insted of true.
+ */
+var sAlert = "";
+
+var Alert = function(sText)
+{
+  if (sText!=sAlert)
+  {
+    sAlert = sText;
+    alert(sText);
+  }
+}
+
+
+
+/**
+ * range usage is different on each browser
+ * internet explorer use Microsoft Text Range and others use W3C Range
+ * 
+ * follow link describe that
+ * http://www.quirksmode.org/dom/range_intro.html#t00
+ *
+ * because of this reason getting something on selected node is divided 2 part
+ * first for internet explorer and second for others
+ * each codes implement same function do same behavior
+ * to understand following code. it's better others part first.
+ * it's more easy and short
+ */
+
+
 // --------------------------------
 // IE
 // --------------------------------
@@ -24,13 +57,14 @@
       FCKSelection._endNode = null;
   }
 
+  // everytime selection changed. initialize internal variables 
   FCK.AttachToOnSelectionChange(invalidSelection);
 
   FCKSelection._startNode = null;
   FCKSelection._endNode = null;
 
 
-  // get StartNode
+  // get start node of seleced element
   FCKSelection.GetStartNode = function()
   {
     if (!FCKSelection._startNode) 
@@ -40,6 +74,7 @@
     return FCKSelection._startNode;
   }
 
+  
   FCKSelection._getStartNode = function()
   {
     if (FCKSelection._startNode) return FCKSelection._startNode;
@@ -56,12 +91,19 @@
       var parent = oRange.parentElement();
       var oNode = null;
       var following_text = 0;
-      if (!parent.hasChildNodes()) 
-        return parent; // selection in empty tag
 
+      // selection in empty tag
+      if ( !parent.hasChildNodes() ) 
+        return parent; 
+
+      // the first child of the parent of the selection
       oNode = parent.firstChild;
       var oLastText = oNode;
 
+      // looping all lower nodes of the parent of selected object
+      // and compare it with first selected node 
+      // if oNode is not a text compare start of oRange and end of oTmpRange
+      // and then compare start of both range(oRange, oTmpRange)
       while (oNode)
       {
         if (oNode.nodeName != "#text") 
@@ -72,7 +114,8 @@
           {
             // found
             if (oRange.compareEndPoints('StartToStart', oTmpRange)<=0)
-            return oLastText; // already inside selection
+              return oLastText; // already inside selection
+
             oNode = oNode.firstChild;
           }
           else 
@@ -80,6 +123,126 @@
             oNode = oNode.nextSibling;
           }
         }
+        else // oNode.nodeName == '#text'
+        {
+          if (!following_text)
+          {
+            oLastText = oNode;
+            following_text = true;
+          }
+
+          try {
+            oNode = oNode.nextSibling;
+          }
+          catch (e) {
+            if (parent.childNodes.length>=2)
+            {
+              oNode = parent.childNodes[1];
+            }
+            else 
+            {
+              return parent;
+            }
+          } // end of catch
+        } // end of else
+      } // end of while
+      return oLastText;
+    } // end of else
+  }
+
+  // getting start node offset
+  FCKSelection.GetStartOffset = function() 
+  {
+    // XXX does not work yet!
+    var oNode = FCKSelection.GetStartNode();
+    
+    // not a text node 
+    if (oNode.nodeType != 3) 
+      return 0; 
+
+    var startoffset = 0;
+    var selrange = FCKSelection.GetSelection().createRange();
+    var elrange = selrange.duplicate();
+
+    if (oNode.previousSilbing)
+    {
+      elrange.moveToElementText(oNode.previousSibling);
+      while (selrange.compareEndPoints('StartToEnd', elrange) > 0) 
+      {
+        startoffset++;
+        elrange.moveEnd('character', 1);
+      }
+    }
+    else
+    { 
+      elrange.moveToElementText(oNode.parentNode);
+      while (selrange.compareEndPoints('StartToStart', elrange) > 0) 
+      {
+        startoffset++;
+        elrange.moveStart('character', 1);
+      }
+    }
+    return startoffset;
+  }
+
+  // get end node of seleced element
+  FCKSelection.GetEndNode = function()
+  {
+    if (!FCKSelection._endNode) 
+    {
+      FCKSelection._endNode = FCKSelection._getEndNode()
+    }
+    return FCKSelection._endNode;
+  }
+
+
+  FCKSelection._getEndNode = function()
+  {
+    FCKSelection.sEnd = "";
+    var oRange = FCKSelection.GetSelection().createRange();
+
+    if (FCKSelection.GetType()=="Control")
+    {
+      return oRange.item(oRange.length-1);
+    }
+    else // Text, None
+    {
+      var oTmpRange = FCKSelection.GetSelection().createRange();
+      var oNode = oRange.parentElement()
+      var following_text = false;
+
+      // selection in empty tag
+      if (!oNode.hasChildNodes()) 
+        return oNode; 
+
+      oNode = oNode.lastChild;
+
+      var oLastText = oNode;
+
+      while (oNode)
+      {
+        if (oNode.nodeName!="#text") 
+        {
+          following_text = false;
+          oTmpRange.moveToElementText(oNode);
+
+          if (oRange.compareEndPoints('EndToStart', oTmpRange)>0)
+          {
+            if (oRange.compareEndPoints('EndToEnd', oTmpRange)>=0)
+              return oLastText; // already in selection
+              
+            // found
+            FCKSelection.sEnd += oNode.nodeName + "->";
+            oNode = oNode.lastChild;
+
+            if (oNode) 
+              FCKSelection.sEnd += oNode.nodeName + ";";
+          }
+          else 
+          {
+            oNode = oNode.previousSibling;
+          }
+        }
         else
         {
           if (!following_text)
@@ -87,246 +250,139 @@
             oLastText = oNode;
             following_text = true;
           }
-          try {
-            oNode = oNode.nextSibling;
-          }
-          catch (e) {
-            if (parent.childNodes.length>=2)
-              oNode = parent.childNodes[1];
-            else
-              // alert(e);
-              // showObj('parent', parent);
-              // showObj('node', oNode);
-              // showObj('childNodes[0]', parent.childNodes[0]);
-              // oNode = false;
-              return parent;
+
+          try
+          {
+            oNode = oNode.previousSibling;
+          } 
+          catch (e) 
+          {
+            return oRange.parentElement();
           } // end of catch
         } // end of else
       } // end of while
       return oLastText;
     } // end of else
+  } // end of function
+
+
+  // getting offset of end node
+  FCKSelection.GetEndOffset = function() 
+  {
+    var oNode = FCKSelection.GetEndNode();
+
+    // not a text node 
+    if (oNode.nodeType != 3)
+      return 0; 
+
+    var endoffset = 0;
+    var selrange = FCKSelection.GetSelection().createRange();
+    var elrange = selrange.duplicate();
+
+    if (oNode.nextSilbing)
+    {
+      elrange.moveToElementText(oNode.nextSibling);
+      while (selrange.compareEndPoints('EndToStart', elrange) < 0) 
+      {
+        endoffset++;
+        elrange.moveStart('character', -1);
+      }
+    }
+    else 
+    {
+      elrange.moveToElementText(oNode.parentNode);
+      while (selrange.compareEndPoints('EndToEnd', elrange) < 0) 
+      {
+        endoffset++;
+        elrange.moveStart('character', -11);
+      }
+    }
+    return endoffset;
   }
 
-  FCKSelection.GetStartOffset = function() 
-    {
-      // XXX does not work yet!
-      var oNode = FCKSelection.GetStartNode();
-      if (oNode.nodeType!=3) return 0; // not a text node 
-      var startoffset = 0;
-      var selrange = FCKSelection.GetSelection().createRange();
-      var elrange = selrange.duplicate();
-      if (oNode.previousSilbing)
-      {
-        elrange.moveToElementText(oNode.previousSibling);
-        while (selrange.compareEndPoints('StartToEnd', elrange) > 0) 
-          {
-            startoffset++;
-            elrange.moveEnd('character', 1);
-          }
-      }
-      else
-      { 
-        elrange.moveToElementText(oNode.parentNode);
-        while (selrange.compareEndPoints('StartToStart', elrange) > 0) 
-          {
-            startoffset++;
-            elrange.moveStart('character', 1);
-          }
-      }
-      return startoffset;
-    }
-
-  FCKSelection.GetEndNode = function()
-    {
-      if (!FCKSelection._endNode) 
-      {
-	FCKSelection._endNode = FCKSelection._getEndNode()
-      }
-      return FCKSelection._endNode;
-    }
+  // return selected node's text
+  FCKSelection.GetText = function()
+  {
+    var oRange = FCKSelection.GetSelection().createRange();
+    return oRange.text;
+  }
 
-  FCKSelection._getEndNode = function()
-    {
-      FCKSelection.sEnd = "";
-      var oRange = FCKSelection.GetSelection().createRange();
-      if (FCKSelection.GetType()=="Control")
-      {
-        return oRange.item(oRange.length-1);
-      }
-      else // Text, None
-      {
-        var oTmpRange = FCKSelection.GetSelection().createRange();
-        var oNode = oRange.parentElement()
-        var following_text = false;
-        if (!oNode.hasChildNodes()) return oNode; // selection in empty tag
-        oNode = oNode.lastChild;
-        var oLastText = oNode;
-        while (oNode)
-        {
-          if (oNode.nodeName!="#text") 
-            {
-              following_text = false;
-              oTmpRange.moveToElementText(oNode);
-              if (oRange.compareEndPoints('EndToStart', oTmpRange)>0)
-                {
-                  if (oRange.compareEndPoints('EndToEnd', oTmpRange)>=0)
-                    return oLastText; // already in selection
-                  // found
-                  FCKSelection.sEnd += oNode.nodeName + "->";
-                  oNode = oNode.lastChild;
-                  if (oNode) FCKSelection.sEnd += oNode.nodeName + ";";
-                  
-                }
-              else
-                oNode = oNode.previousSibling;
-            }
-          else
-            {
-	      if (!following_text)
-		{
-		  oLastText = oNode;
-		  following_text = true;
-		}
-              try{
-                oNode = oNode.previousSibling;
-              } catch (e) {
-                return oRange.parentElement();
-              }
-            }
-        }
-        return oLastText;
-      }
-    }
-
-  FCKSelection.GetEndOffset = function() 
-    {
-      // XXX does not work yet!
-      var oNode = FCKSelection.GetEndNode();
-      if (oNode.nodeType!=3) return 0; // not a text node 
-      var endoffset = 0;
-      var selrange = FCKSelection.GetSelection().createRange();
-      var elrange = selrange.duplicate();
-      if (oNode.nextSilbing)
-      {
-        elrange.moveToElementText(oNode.nextSibling);
-        while (selrange.compareEndPoints('EndToStart', elrange) < 0) 
-          {
-            endoffset++;
-            elrange.moveStart('character', -1);
-          }
-      }      
-      else 
-      {
-        elrange.moveToElementText(oNode.parentNode);
-        while (selrange.compareEndPoints('EndToEnd', elrange) < 0) 
-        {
-          endoffset++;
-          elrange.moveStart('character', -11);
-        }
-      }
-      return endoffset;
-    }
-
-
-  FCKSelection.GetText = function()
-    {
-      var oRange = FCKSelection.GetSelection().createRange();
-      return oRange.text;
-    }
+  // return true if selected node's type is None
   FCKSelection.IsCollapsed = function()
-    {
-      return FCKSelection.GetType()=='None';
-    }
+  {
+    return FCKSelection.GetType() == 'None';
+  }
 }
 // --------------------------------
-// Others
+// Others (firefox, safari, opera, ..)
 // --------------------------------
 else {
-  // assume exactly one selection
+  // assume exactly one selection. only first selection accepted.
   FCKSelection.GetStartNode = function()
     {
       var oSelection = FCKSelection.GetSelection();
+      // startContainer returns the parent of the first node in the selection
       var oContainer = oSelection.getRangeAt(0).startContainer;
+      // The offset within the startContainer where the range starts
       var iOffset = oSelection.getRangeAt(0).startOffset;
-      if (oContainer.childNodes.length>iOffset) 
-        return oContainer.childNodes[iOffset];
-      else
-        return oContainer;
-        
-    }
-  // only valid if GetStartNode() returns text node
-  FCKSelection.GetStartOffset = function()
-    {
-      var oSelection = FCKSelection.GetSelection();
-      return oSelection.getRangeAt(0).startOffset;    
-    }
-  FCKSelection.GetEndNode = function()
-    {
-      var oSelection = FCKSelection.GetSelection();
-      var oContainer = oSelection.getRangeAt(0).endContainer;
-      var iOffset = oSelection.getRangeAt(0).startEndset;
-      if (oContainer.childNodes.length>iOffset) 
+
+      // if oContainer has children return child at iOffset
+      // otherwise return oContainer
+      if (oContainer.childNodes.length > iOffset) 
         return oContainer.childNodes[iOffset];
       else
         return oContainer;
-
-    }
-  // only valid if GetEndNode() returns text node
-  FCKSelection.GetEndOffset = function()
-    {
-      var oSelection = FCKSelection.GetSelection();
-      return oSelection.getRangeAt(0).endOffset;    
-    }
-  FCKSelection.IsCollapsed = function()
-    {
-      var oSelection = FCKSelection.GetSelection();
-      return oSelection.getRangeAt(0).collapsed;    
-    }
-  FCKSelection.GetText = function()
-    {
-      return FCKSelection.GetSelection().toString();
     }
-}
-
-
 
-/* 
- * Checks if the name of any of the selected nodes or the nodes surrounding 
- * the selection match the pattern RE.
- * Returns FCK_TRISTATE_DISABLED insted of true.
- */
+  FCKSelection.GetStartOffset = function()
+  {
+    var oSelection = FCKSelection.GetSelection();
+    return oSelection.getRangeAt(0).startOffset;
+  }
 
-var sAlert = "";
+  FCKSelection.GetEndNode = function()
+  {
+    var oSelection = FCKSelection.GetSelection();
+    var oContainer = oSelection.getRangeAt(0).endContainer;
+    var iOffset = oSelection.getRangeAt(0).startEndset;
 
-var Alert = function(sText)
-{
-  if (sText!=sAlert)
+    if (oContainer.childNodes.length>iOffset) 
+      return oContainer.childNodes[iOffset];
+    else
+      return oContainer;
+  }
+
+  FCKSelection.GetEndOffset = function()
   {
-    sAlert = sText;
-    alert(sText);
+    var oSelection = FCKSelection.GetSelection();
+    return oSelection.getRangeAt(0).endOffset;    
+  }
+
+  FCKSelection.IsCollapsed = function()
+  {
+    var oSelection = FCKSelection.GetSelection();
+    return oSelection.getRangeAt(0).collapsed;
+  }
+
+  FCKSelection.GetText = function()
+  {
+    return FCKSelection.GetSelection().toString();
   }
 }
 
 
 
 // this function make toolbar's button enable/disable using inserted pattern.
-// check coverage is contains its parent and children.
+// check coverage contains its parent and children.
 FCKSelection.CheckForNodeNames = function(pattern)
 {
   var oStart = FCKSelection.GetStartNode();
   var oEnd = FCKSelection.GetEndNode();
 
-  if (!oStart){
-    //Alert("No Start");
+  // filtering invalid input and selection
+  if (!oStart || !oEnd || !pattern){
     return 0;
   }
-  if (!oEnd){
-    //Alert("No End");
-    return 0;
-  }
-  if (!pattern) {
-	  return 0;
-  }
   
   /* Crashes IE
   if ((FCKSelection.GetType()=="None") && (oStart!=oEnd))
@@ -343,11 +399,11 @@
   while (oElement)
   {
     if (pattern.test(oElement.nodeName))
-      { 
-        //Alert("Start:" + oStart.nodeName + ':' + oElement.nodeName + 
-        //      ':' + oEnd.nodeName);
-        return FCK_TRISTATE_DISABLED;
-      }
+    { 
+      //Alert("Start:" + oStart.nodeName + ':' + oElement.nodeName + 
+      //      ':' + oEnd.nodeName);
+      return FCK_TRISTATE_DISABLED;
+    }
     oElement = oElement.parentNode;
   }
 
@@ -355,11 +411,11 @@
   while (oElement)
   {
     if (pattern.test(oElement.nodeName))
-      { 
-        //Alert("End:" + oStart.nodeName + ':' + oElement.nodeName + 
-        //      ':' + oEnd.nodeName);
-        return FCK_TRISTATE_DISABLED;
-      }
+    { 
+      //Alert("End:" + oStart.nodeName + ':' + oElement.nodeName + 
+      //      ':' + oEnd.nodeName);
+      return FCK_TRISTATE_DISABLED;
+    }
     oElement = oElement.parentNode;
   }
 
@@ -368,40 +424,50 @@
   {
     return 0;
   }
+
   // check selected nodes
   var oNode = oStart;
 
   while (oNode)
   {
     if (pattern.test(oNode.nodeName))
-      {
-        //Alert("Down:" + oStart.nodeName + ':' + oNode.nodeName + ':' + 
-        //      oEnd.nodeName + FCKSelection.GetStartOffset() + ":" +
-        //      FCKSelection.GetEndOffset());
-        
-        return FCK_TRISTATE_DISABLED;
-      }
+    {
+      /*
+      Alert("Down:" + oStart.nodeName + ':' + oNode.nodeName + ':' + 
+            oEnd.nodeName + FCKSelection.GetStartOffset() + ":" +
+            FCKSelection.GetEndOffset());
+      */
+      return FCK_TRISTATE_DISABLED;
+    }
+
     if (oNode==oEnd && oNode!=oStart)
     {
       return 0;
     }
+
     if (oNode.hasChildNodes()) 
     {
       oNode = oNode.firstChild;
     }
     else
     {
-      if (oNode==oEnd) return 0; // happens if oStart==oEnd && no children
+      // happens if oStart==oEnd && no children
+      if (oNode==oEnd) 
+        return 0; 
+
       while (!oNode.nextSibling) 
       {
         oNode = oNode.parentNode;
+
         if (!oNode) return 0;
+
         if (pattern.test(oNode.nodeName))
-          {
-            //Alert("Up:" + oStart.nodeName +':'+ oNode.nodeName + ':' + 
-            //          oEnd.nodeName);
-            return FCK_TRISTATE_DISABLED;
-          }
+        {
+          //Alert("Up:" + oStart.nodeName +':'+ oNode.nodeName + ':' + 
+          //          oEnd.nodeName);
+          return FCK_TRISTATE_DISABLED;
+        }
+
         if (oNode==oEnd) return 0;
       }
       oNode = oNode.nextSibling;