[1349] | 1 | /* |
---|
| 2 | * FCKeditor - The text editor for Internet - http://www.fckeditor.net |
---|
| 3 | * Copyright (C) 2003-2007 Frederico Caldeira Knabben |
---|
| 4 | * |
---|
| 5 | * == BEGIN LICENSE == |
---|
| 6 | * |
---|
| 7 | * Licensed under the terms of any of the following licenses at your |
---|
| 8 | * choice: |
---|
| 9 | * |
---|
| 10 | * - GNU General Public License Version 2 or later (the "GPL") |
---|
| 11 | * http://www.gnu.org/licenses/gpl.html |
---|
| 12 | * |
---|
| 13 | * - GNU Lesser General Public License Version 2.1 or later (the "LGPL") |
---|
| 14 | * http://www.gnu.org/licenses/lgpl.html |
---|
| 15 | * |
---|
| 16 | * - Mozilla Public License Version 1.1 or later (the "MPL") |
---|
| 17 | * http://www.mozilla.org/MPL/MPL-1.1.html |
---|
| 18 | * |
---|
| 19 | * == END LICENSE == |
---|
| 20 | * |
---|
| 21 | * This is the integration file for JavaScript. |
---|
| 22 | * |
---|
| 23 | * It defines the FCKeditor class that can be used to create editor |
---|
| 24 | * instances in a HTML page in the client side. For server side |
---|
| 25 | * operations, use the specific integration system. |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | // FCKeditor Class |
---|
| 29 | var FCKeditor = function( instanceName, width, height, toolbarSet, value ) |
---|
| 30 | { |
---|
| 31 | // Properties |
---|
| 32 | this.InstanceName = instanceName ; |
---|
| 33 | this.Width = width || '100%' ; |
---|
| 34 | this.Height = height || '200' ; |
---|
| 35 | this.ToolbarSet = toolbarSet || 'Default' ; |
---|
| 36 | this.Value = value || '' ; |
---|
| 37 | this.BasePath = '/fckeditor/' ; |
---|
| 38 | this.CheckBrowser = true ; |
---|
| 39 | this.DisplayErrors = true ; |
---|
| 40 | this.EnableSafari = false ; // This is a temporary property, while Safari support is under development. |
---|
| 41 | this.EnableOpera = false ; // This is a temporary property, while Opera support is under development. |
---|
| 42 | |
---|
| 43 | this.Config = new Object() ; |
---|
| 44 | |
---|
| 45 | // Events |
---|
| 46 | this.OnError = null ; // function( source, errorNumber, errorDescription ) |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | FCKeditor.prototype.Version = '2.4.2' ; |
---|
| 50 | FCKeditor.prototype.VersionBuild = '14978' ; |
---|
| 51 | |
---|
| 52 | FCKeditor.prototype.Create = function() |
---|
| 53 | { |
---|
| 54 | document.write( this.CreateHtml() ) ; |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | FCKeditor.prototype.CreateHtml = function() |
---|
| 58 | { |
---|
| 59 | // Check for errors |
---|
| 60 | if ( !this.InstanceName || this.InstanceName.length == 0 ) |
---|
| 61 | { |
---|
| 62 | this._ThrowError( 701, 'You must specify an instance name.' ) ; |
---|
| 63 | return '' ; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | var sHtml = '<div>' ; |
---|
| 67 | |
---|
| 68 | if ( !this.CheckBrowser || this._IsCompatibleBrowser() ) |
---|
| 69 | { |
---|
| 70 | sHtml += '<input type="hidden" id="' + this.InstanceName + '" name="' + this.InstanceName + '" value="' + this._HTMLEncode( this.Value ) + '" style="display:none" />' ; |
---|
| 71 | sHtml += this._GetConfigHtml() ; |
---|
| 72 | sHtml += this._GetIFrameHtml() ; |
---|
| 73 | } |
---|
| 74 | else |
---|
| 75 | { |
---|
| 76 | var sWidth = this.Width.toString().indexOf('%') > 0 ? this.Width : this.Width + 'px' ; |
---|
| 77 | var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ; |
---|
| 78 | sHtml += '<textarea name="' + this.InstanceName + '" rows="4" cols="40" style="width:' + sWidth + ';height:' + sHeight + '">' + this._HTMLEncode( this.Value ) + '<\/textarea>' ; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | sHtml += '</div>' ; |
---|
| 82 | |
---|
| 83 | return sHtml ; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | FCKeditor.prototype.ReplaceTextarea = function() |
---|
| 87 | { |
---|
| 88 | if ( !this.CheckBrowser || this._IsCompatibleBrowser() ) |
---|
| 89 | { |
---|
| 90 | // We must check the elements firstly using the Id and then the name. |
---|
| 91 | var oTextarea = document.getElementById( this.InstanceName ) ; |
---|
| 92 | var colElementsByName = document.getElementsByName( this.InstanceName ) ; |
---|
| 93 | var i = 0; |
---|
| 94 | while ( oTextarea || i == 0 ) |
---|
| 95 | { |
---|
| 96 | if ( oTextarea && oTextarea.tagName.toLowerCase() == 'textarea' ) |
---|
| 97 | break ; |
---|
| 98 | oTextarea = colElementsByName[i++] ; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | if ( !oTextarea ) |
---|
| 102 | { |
---|
| 103 | alert( 'Error: The TEXTAREA with id or name set to "' + this.InstanceName + '" was not found' ) ; |
---|
| 104 | return ; |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | oTextarea.style.display = 'none' ; |
---|
| 108 | this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ; |
---|
| 109 | this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | FCKeditor.prototype._InsertHtmlBefore = function( html, element ) |
---|
| 114 | { |
---|
| 115 | if ( element.insertAdjacentHTML ) // IE |
---|
| 116 | element.insertAdjacentHTML( 'beforeBegin', html ) ; |
---|
| 117 | else // Gecko |
---|
| 118 | { |
---|
| 119 | var oRange = document.createRange() ; |
---|
| 120 | oRange.setStartBefore( element ) ; |
---|
| 121 | var oFragment = oRange.createContextualFragment( html ); |
---|
| 122 | element.parentNode.insertBefore( oFragment, element ) ; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | FCKeditor.prototype._GetConfigHtml = function() |
---|
| 127 | { |
---|
| 128 | var sConfig = '' ; |
---|
| 129 | for ( var o in this.Config ) |
---|
| 130 | { |
---|
| 131 | if ( sConfig.length > 0 ) sConfig += '&' ; |
---|
| 132 | sConfig += encodeURIComponent( o ) + '=' + encodeURIComponent( this.Config[o] ) ; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | return '<input type="hidden" id="' + this.InstanceName + '___Config" value="' + sConfig + '" style="display:none" />' ; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | FCKeditor.prototype._GetIFrameHtml = function() |
---|
| 139 | { |
---|
| 140 | var sFile = 'fckeditor.html' ; |
---|
| 141 | |
---|
| 142 | try |
---|
| 143 | { |
---|
| 144 | if ( (/fcksource=true/i).test( window.top.location.search ) ) |
---|
| 145 | sFile = 'fckeditor.original.html' ; |
---|
| 146 | } |
---|
| 147 | catch (e) { /* Ignore it. Much probably we are inside a FRAME where the "top" is in another domain (security error). */ } |
---|
| 148 | |
---|
| 149 | var sLink = this.BasePath + 'editor/' + sFile + '?InstanceName=' + encodeURIComponent( this.InstanceName ) ; |
---|
| 150 | if (this.ToolbarSet) sLink += '&Toolbar=' + this.ToolbarSet ; |
---|
| 151 | |
---|
| 152 | return '<iframe id="' + this.InstanceName + '___Frame" src="' + sLink + '" width="' + this.Width + '" height="' + this.Height + '" frameborder="0" scrolling="no"></iframe>' ; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | FCKeditor.prototype._IsCompatibleBrowser = function() |
---|
| 156 | { |
---|
| 157 | return FCKeditor_IsCompatibleBrowser( this.EnableSafari, this.EnableOpera ) ; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | FCKeditor.prototype._ThrowError = function( errorNumber, errorDescription ) |
---|
| 161 | { |
---|
| 162 | this.ErrorNumber = errorNumber ; |
---|
| 163 | this.ErrorDescription = errorDescription ; |
---|
| 164 | |
---|
| 165 | if ( this.DisplayErrors ) |
---|
| 166 | { |
---|
| 167 | document.write( '<div style="COLOR: #ff0000">' ) ; |
---|
| 168 | document.write( '[ FCKeditor Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ; |
---|
| 169 | document.write( '</div>' ) ; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | if ( typeof( this.OnError ) == 'function' ) |
---|
| 173 | this.OnError( this, errorNumber, errorDescription ) ; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | FCKeditor.prototype._HTMLEncode = function( text ) |
---|
| 177 | { |
---|
| 178 | if ( typeof( text ) != "string" ) |
---|
| 179 | text = text.toString() ; |
---|
| 180 | |
---|
| 181 | text = text.replace( |
---|
| 182 | /&/g, "&").replace( |
---|
| 183 | /"/g, """).replace( |
---|
| 184 | /</g, "<").replace( |
---|
| 185 | />/g, ">") ; |
---|
| 186 | |
---|
| 187 | return text ; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | function FCKeditor_IsCompatibleBrowser( enableSafari, enableOpera ) |
---|
| 191 | { |
---|
| 192 | var sAgent = navigator.userAgent.toLowerCase() ; |
---|
| 193 | |
---|
| 194 | // Internet Explorer |
---|
| 195 | if ( sAgent.indexOf("msie") != -1 && sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1 ) |
---|
| 196 | { |
---|
| 197 | var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ; |
---|
| 198 | return ( sBrowserVersion >= 5.5 ) ; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | // Gecko (Opera 9 tries to behave like Gecko at this point). |
---|
| 202 | if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) ) |
---|
| 203 | return true ; |
---|
| 204 | |
---|
| 205 | // Opera |
---|
| 206 | if ( enableOpera && navigator.appName == 'Opera' && parseInt( navigator.appVersion, 10 ) >= 9 ) |
---|
| 207 | return true ; |
---|
| 208 | |
---|
| 209 | // Safari |
---|
| 210 | if ( enableSafari && sAgent.indexOf( 'safari' ) != -1 ) |
---|
| 211 | return ( sAgent.match( /safari\/(\d+)/ )[1] >= 312 ) ; // Build must be at least 312 (1.3) |
---|
| 212 | |
---|
| 213 | return false ; |
---|
| 214 | } |
---|