FCKeditor 2.6.6
[freeside.git] / httemplate / elements / fckeditor / editor / dialog / common / fck_dialog_common.js
index 26b5628..478d3d5 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
  * FCKeditor - The text editor for Internet - http://www.fckeditor.net\r
- * Copyright (C) 2003-2007 Frederico Caldeira Knabben\r
+ * Copyright (C) 2003-2010 Frederico Caldeira Knabben\r
  *\r
  * == BEGIN LICENSE ==\r
  *\r
  * == END LICENSE ==\r
  *\r
  * Useful functions used by almost all dialog window pages.\r
+ * Dialogs should link to this file as the very first script on the page.\r
  */\r
 \r
-var GECKO_BOGUS = '<br type="_moz">' ;\r
+// Automatically detect the correct document.domain (#123).\r
+(function()\r
+{\r
+       var d = document.domain ;\r
+\r
+       while ( true )\r
+       {\r
+               // Test if we can access a parent property.\r
+               try\r
+               {\r
+                       var test = window.parent.document.domain ;\r
+                       break ;\r
+               }\r
+               catch( e ) {}\r
+\r
+               // Remove a domain part: www.mytest.example.com => mytest.example.com => example.com ...\r
+               d = d.replace( /.*?(?:\.|$)/, '' ) ;\r
+\r
+               if ( d.length == 0 )\r
+                       break ;         // It was not able to detect the domain.\r
+\r
+               try\r
+               {\r
+                       document.domain = d ;\r
+               }\r
+               catch (e)\r
+               {\r
+                       break ;\r
+               }\r
+       }\r
+})() ;\r
+\r
+// Attention: FCKConfig must be available in the page.\r
+function GetCommonDialogCss( prefix )\r
+{\r
+       // CSS minified by http://iceyboard.no-ip.org/projects/css_compressor (see _dev/css_compression.txt).\r
+       return FCKConfig.BasePath + 'dialog/common/' + '|.ImagePreviewArea{border:#000 1px solid;overflow:auto;width:100%;height:170px;background-color:#fff}.FlashPreviewArea{border:#000 1px solid;padding:5px;overflow:auto;width:100%;height:170px;background-color:#fff}.BtnReset{float:left;background-position:center center;background-image:url(images/reset.gif);width:16px;height:16px;background-repeat:no-repeat;border:1px none;font-size:1px}.BtnLocked,.BtnUnlocked{float:left;background-position:center center;background-image:url(images/locked.gif);width:16px;height:16px;background-repeat:no-repeat;border:none 1px;font-size:1px}.BtnUnlocked{background-image:url(images/unlocked.gif)}.BtnOver{border:outset 1px;cursor:pointer;cursor:hand}' ;\r
+}\r
 \r
 // Gets a element by its Id. Used for shorter coding.\r
 function GetE( elementId )\r
@@ -59,22 +97,48 @@ function GetAttribute( element, attName, valueIfNull )
        return ( oValue == null ? valueIfNull : oValue ) ;\r
 }\r
 \r
-// Functions used by text fiels to accept numbers only.\r
-function IsDigit( e )\r
+function SelectField( elementId )\r
 {\r
-       if ( !e )\r
-               e = event ;\r
+       var element = GetE( elementId ) ;\r
+       element.focus() ;\r
 \r
-       var iCode = ( e.keyCode || e.charCode ) ;\r
-\r
-       return (\r
-                       ( iCode >= 48 && iCode <= 57 )          // Numbers\r
-                       || (iCode >= 37 && iCode <= 40)         // Arrows\r
-                       || iCode == 8                                           // Backspace\r
-                       || iCode == 46                                          // Delete\r
-       ) ;\r
+       // element.select may not be available for some fields (like <select>).\r
+       if ( element.select )\r
+               element.select() ;\r
 }\r
 \r
+// Functions used by text fields to accept numbers only.\r
+var IsDigit = ( function()\r
+       {\r
+               var KeyIdentifierMap =\r
+               {\r
+                       End                     : 35,\r
+                       Home            : 36,\r
+                       Left            : 37,\r
+                       Right           : 39,\r
+                       'U+00007F'      : 46            // Delete\r
+               } ;\r
+\r
+               return function ( e )\r
+                       {\r
+                               if ( !e )\r
+                                       e = event ;\r
+\r
+                               var iCode = ( e.keyCode || e.charCode ) ;\r
+\r
+                               if ( !iCode && e.keyIdentifier && ( e.keyIdentifier in KeyIdentifierMap ) )\r
+                                               iCode = KeyIdentifierMap[ e.keyIdentifier ] ;\r
+\r
+                               return (\r
+                                               ( iCode >= 48 && iCode <= 57 )          // Numbers\r
+                                               || (iCode >= 35 && iCode <= 40)         // Arrows, Home, End\r
+                                               || iCode == 8                                           // Backspace\r
+                                               || iCode == 46                                          // Delete\r
+                                               || iCode == 9                                           // Tab\r
+                               ) ;\r
+                       }\r
+       } )() ;\r
+\r
 String.prototype.Trim = function()\r
 {\r
        return this.replace( /(^\s*)|(\s*$)/g, '' ) ;\r
@@ -123,32 +187,161 @@ function OpenFileBrowser( url, width, height )
        sOptions += ",left=" + iLeft ;\r
        sOptions += ",top=" + iTop ;\r
 \r
-       // The "PreserveSessionOnFileBrowser" because the above code could be\r
-       // blocked by popup blockers.\r
-       if ( oEditor.FCKConfig.PreserveSessionOnFileBrowser && oEditor.FCKBrowserInfo.IsIE )\r
+       window.open( url, 'FCKBrowseWindow', sOptions ) ;\r
+}\r
+\r
+/**\r
+ Utility function to create/update an element with a name attribute in IE, so it behaves properly when moved around\r
+ It also allows to change the name or other special attributes in an existing node\r
+       oEditor : instance of FCKeditor where the element will be created\r
+       oOriginal : current element being edited or null if it has to be created\r
+       nodeName : string with the name of the element to create\r
+       oAttributes : Hash object with the attributes that must be set at creation time in IE\r
+                                                               Those attributes will be set also after the element has been\r
+                                                               created for any other browser to avoid redudant code\r
+*/\r
+function CreateNamedElement( oEditor, oOriginal, nodeName, oAttributes )\r
+{\r
+       var oNewNode ;\r
+\r
+       // IE doesn't allow easily to change properties of an existing object,\r
+       // so remove the old and force the creation of a new one.\r
+       var oldNode = null ;\r
+       if ( oOriginal && oEditor.FCKBrowserInfo.IsIE )\r
        {\r
-               // The following change has been made otherwise IE will open the file\r
-               // browser on a different server session (on some cases):\r
-               // http://support.microsoft.com/default.aspx?scid=kb;en-us;831678\r
-               // by Simone Chiaretta.\r
-               var oWindow = oEditor.window.open( url, 'FCKBrowseWindow', sOptions ) ;\r
+               // Force the creation only if some of the special attributes have changed:\r
+               var bChanged = false;\r
+               for( var attName in oAttributes )\r
+                       bChanged |= ( oOriginal.getAttribute( attName, 2) != oAttributes[attName] ) ;\r
+\r
+               if ( bChanged )\r
+               {\r
+                       oldNode = oOriginal ;\r
+                       oOriginal = null ;\r
+               }\r
+       }\r
 \r
-               if ( oWindow )\r
+       // If the node existed (and it's not IE), then we just have to update its attributes\r
+       if ( oOriginal )\r
+       {\r
+               oNewNode = oOriginal ;\r
+       }\r
+       else\r
+       {\r
+               // #676, IE doesn't play nice with the name or type attribute\r
+               if ( oEditor.FCKBrowserInfo.IsIE )\r
                {\r
-                       // Detect Yahoo popup blocker.\r
-                       try\r
+                       var sbHTML = [] ;\r
+                       sbHTML.push( '<' + nodeName ) ;\r
+                       for( var prop in oAttributes )\r
                        {\r
-                               var sTest = oWindow.name ; // Yahoo returns "something", but we can't access it, so detect that and avoid strange errors for the user.\r
-                               oWindow.opener = window ;\r
+                               sbHTML.push( ' ' + prop + '="' + oAttributes[prop] + '"' ) ;\r
                        }\r
-                       catch(e)\r
+                       sbHTML.push( '>' ) ;\r
+                       if ( !oEditor.FCKListsLib.EmptyElements[nodeName.toLowerCase()] )\r
+                               sbHTML.push( '</' + nodeName + '>' ) ;\r
+\r
+                       oNewNode = oEditor.FCK.EditorDocument.createElement( sbHTML.join('') ) ;\r
+                       // Check if we are just changing the properties of an existing node: copy its properties\r
+                       if ( oldNode )\r
                        {\r
-                               alert( oEditor.FCKLang.BrowseServerBlocked ) ;\r
+                               CopyAttributes( oldNode, oNewNode, oAttributes ) ;\r
+                               oEditor.FCKDomTools.MoveChildren( oldNode, oNewNode ) ;\r
+                               oldNode.parentNode.removeChild( oldNode ) ;\r
+                               oldNode = null ;\r
+\r
+                               if ( oEditor.FCK.Selection.SelectionData )\r
+                               {\r
+                                       // Trick to refresh the selection object and avoid error in\r
+                                       // fckdialog.html Selection.EnsureSelection\r
+                                       var oSel = oEditor.FCK.EditorDocument.selection ;\r
+                                       oEditor.FCK.Selection.SelectionData = oSel.createRange() ; // Now oSel.type will be 'None' reflecting the real situation\r
+                               }\r
+                       }\r
+                       oNewNode = oEditor.FCK.InsertElement( oNewNode ) ;\r
+\r
+                       // FCK.Selection.SelectionData is broken by now since we've\r
+                       // deleted the previously selected element. So we need to reassign it.\r
+                       if ( oEditor.FCK.Selection.SelectionData )\r
+                       {\r
+                               var range = oEditor.FCK.EditorDocument.body.createControlRange() ;\r
+                               range.add( oNewNode ) ;\r
+                               oEditor.FCK.Selection.SelectionData = range ;\r
                        }\r
                }\r
                else\r
-                       alert( oEditor.FCKLang.BrowseServerBlocked ) ;\r
-    }\r
-    else\r
-               window.open( url, 'FCKBrowseWindow', sOptions ) ;\r
-}
\ No newline at end of file
+               {\r
+                       oNewNode = oEditor.FCK.InsertElement( nodeName ) ;\r
+               }\r
+       }\r
+\r
+       // Set the basic attributes\r
+       for( var attName in oAttributes )\r
+               oNewNode.setAttribute( attName, oAttributes[attName], 0 ) ;     // 0 : Case Insensitive\r
+\r
+       return oNewNode ;\r
+}\r
+\r
+// Copy all the attributes from one node to the other, kinda like a clone\r
+// But oSkipAttributes is an object with the attributes that must NOT be copied\r
+function CopyAttributes( oSource, oDest, oSkipAttributes )\r
+{\r
+       var aAttributes = oSource.attributes ;\r
+\r
+       for ( var n = 0 ; n < aAttributes.length ; n++ )\r
+       {\r
+               var oAttribute = aAttributes[n] ;\r
+\r
+               if ( oAttribute.specified )\r
+               {\r
+                       var sAttName = oAttribute.nodeName ;\r
+                       // We can set the type only once, so do it with the proper value, not copying it.\r
+                       if ( sAttName in oSkipAttributes )\r
+                               continue ;\r
+\r
+                       var sAttValue = oSource.getAttribute( sAttName, 2 ) ;\r
+                       if ( sAttValue == null )\r
+                               sAttValue = oAttribute.nodeValue ;\r
+\r
+                       oDest.setAttribute( sAttName, sAttValue, 0 ) ;  // 0 : Case Insensitive\r
+               }\r
+       }\r
+       // The style:\r
+       if ( oSource.style.cssText !== '' )\r
+               oDest.style.cssText = oSource.style.cssText ;\r
+}\r
+\r
+/**\r
+* Replaces a tag with another one, keeping its contents:\r
+* for example TD --> TH, and TH --> TD.\r
+* input: the original node, and the new tag name\r
+* http://www.w3.org/TR/DOM-Level-3-Core/core.html#Document3-renameNode\r
+*/\r
+function RenameNode( oNode , newTag )\r
+{\r
+       // TODO: if the browser natively supports document.renameNode call it.\r
+       // does any browser currently support it in order to test?\r
+\r
+       // Only rename element nodes.\r
+       if ( oNode.nodeType != 1 )\r
+               return null ;\r
+\r
+       // If it's already correct exit here.\r
+       if ( oNode.nodeName == newTag )\r
+               return oNode ;\r
+\r
+       var oDoc = oNode.ownerDocument ;\r
+       // Create the new node\r
+       var newNode = oDoc.createElement( newTag ) ;\r
+\r
+       // Copy all attributes\r
+       CopyAttributes( oNode, newNode, {} ) ;\r
+\r
+       // Move children to the new node\r
+       FCKDomTools.MoveChildren( oNode, newNode ) ;\r
+\r
+       // Finally replace the node and return the new one\r
+       oNode.parentNode.replaceChild( newNode, oNode ) ;\r
+\r
+       return newNode ;\r
+}\r