2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
\r
3 * Copyright (C) 2003-2010 Frederico Caldeira Knabben
\r
5 * == BEGIN LICENSE ==
\r
7 * Licensed under the terms of any of the following licenses at your
\r
10 * - GNU General Public License Version 2 or later (the "GPL")
\r
11 * http://www.gnu.org/licenses/gpl.html
\r
13 * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
\r
14 * http://www.gnu.org/licenses/lgpl.html
\r
16 * - Mozilla Public License Version 1.1 or later (the "MPL")
\r
17 * http://www.mozilla.org/MPL/MPL-1.1.html
\r
21 * This is the integration file for JavaScript.
\r
23 * It defines the FCKeditor class that can be used to create editor
\r
24 * instances in a HTML page in the client side. For server side
\r
25 * operations, use the specific integration system.
\r
29 var FCKeditor = function( instanceName, width, height, toolbarSet, value )
\r
32 this.InstanceName = instanceName ;
\r
33 this.Width = width || '100%' ;
\r
34 this.Height = height || '200' ;
\r
35 this.ToolbarSet = toolbarSet || 'Default' ;
\r
36 this.Value = value || '' ;
\r
37 this.BasePath = FCKeditor.BasePath ;
\r
38 this.CheckBrowser = true ;
\r
39 this.DisplayErrors = true ;
\r
41 this.Config = new Object() ;
\r
44 this.OnError = null ; // function( source, errorNumber, errorDescription )
\r
48 * This is the default BasePath used by all editor instances.
\r
50 FCKeditor.BasePath = '/fckeditor/' ;
\r
53 * The minimum height used when replacing textareas.
\r
55 FCKeditor.MinHeight = 200 ;
\r
58 * The minimum width used when replacing textareas.
\r
60 FCKeditor.MinWidth = 750 ;
\r
62 FCKeditor.prototype.Version = '2.6.6' ;
\r
63 FCKeditor.prototype.VersionBuild = '25427' ;
\r
65 FCKeditor.prototype.Create = function()
\r
67 document.write( this.CreateHtml() ) ;
\r
70 FCKeditor.prototype.CreateHtml = function()
\r
73 if ( !this.InstanceName || this.InstanceName.length == 0 )
\r
75 this._ThrowError( 701, 'You must specify an instance name.' ) ;
\r
81 if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
\r
83 sHtml += '<input type="hidden" id="' + this.InstanceName + '" name="' + this.InstanceName + '" value="' + this._HTMLEncode( this.Value ) + '" style="display:none" />' ;
\r
84 sHtml += this._GetConfigHtml() ;
\r
85 sHtml += this._GetIFrameHtml() ;
\r
89 var sWidth = this.Width.toString().indexOf('%') > 0 ? this.Width : this.Width + 'px' ;
\r
90 var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ;
\r
92 sHtml += '<textarea name="' + this.InstanceName +
\r
93 '" rows="4" cols="40" style="width:' + sWidth +
\r
94 ';height:' + sHeight ;
\r
96 if ( this.TabIndex )
\r
97 sHtml += '" tabindex="' + this.TabIndex ;
\r
100 this._HTMLEncode( this.Value ) +
\r
107 FCKeditor.prototype.ReplaceTextarea = function()
\r
109 if ( document.getElementById( this.InstanceName + '___Frame' ) )
\r
111 if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
\r
113 // We must check the elements firstly using the Id and then the name.
\r
114 var oTextarea = document.getElementById( this.InstanceName ) ;
\r
115 var colElementsByName = document.getElementsByName( this.InstanceName ) ;
\r
117 while ( oTextarea || i == 0 )
\r
119 if ( oTextarea && oTextarea.tagName.toLowerCase() == 'textarea' )
\r
121 oTextarea = colElementsByName[i++] ;
\r
126 alert( 'Error: The TEXTAREA with id or name set to "' + this.InstanceName + '" was not found' ) ;
\r
130 oTextarea.style.display = 'none' ;
\r
132 if ( oTextarea.tabIndex )
\r
133 this.TabIndex = oTextarea.tabIndex ;
\r
135 this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ;
\r
136 this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ;
\r
140 FCKeditor.prototype._InsertHtmlBefore = function( html, element )
\r
142 if ( element.insertAdjacentHTML ) // IE
\r
143 element.insertAdjacentHTML( 'beforeBegin', html ) ;
\r
146 var oRange = document.createRange() ;
\r
147 oRange.setStartBefore( element ) ;
\r
148 var oFragment = oRange.createContextualFragment( html );
\r
149 element.parentNode.insertBefore( oFragment, element ) ;
\r
153 FCKeditor.prototype._GetConfigHtml = function()
\r
156 for ( var o in this.Config )
\r
158 if ( sConfig.length > 0 ) sConfig += '&' ;
\r
159 sConfig += encodeURIComponent( o ) + '=' + encodeURIComponent( this.Config[o] ) ;
\r
162 return '<input type="hidden" id="' + this.InstanceName + '___Config" value="' + sConfig + '" style="display:none" />' ;
\r
165 FCKeditor.prototype._GetIFrameHtml = function()
\r
167 var sFile = 'fckeditor.html' ;
\r
171 if ( (/fcksource=true/i).test( window.top.location.search ) )
\r
172 sFile = 'fckeditor.original.html' ;
\r
174 catch (e) { /* Ignore it. Much probably we are inside a FRAME where the "top" is in another domain (security error). */ }
\r
176 var sLink = this.BasePath + 'editor/' + sFile + '?InstanceName=' + encodeURIComponent( this.InstanceName ) ;
\r
177 if (this.ToolbarSet)
\r
178 sLink += '&Toolbar=' + this.ToolbarSet ;
\r
180 var html = '<iframe id="' + this.InstanceName +
\r
181 '___Frame" src="' + sLink +
\r
182 '" width="' + this.Width +
\r
183 '" height="' + this.Height ;
\r
185 if ( this.TabIndex )
\r
186 html += '" tabindex="' + this.TabIndex ;
\r
188 html += '" frameborder="0" scrolling="no"></iframe>' ;
\r
193 FCKeditor.prototype._IsCompatibleBrowser = function()
\r
195 return FCKeditor_IsCompatibleBrowser() ;
\r
198 FCKeditor.prototype._ThrowError = function( errorNumber, errorDescription )
\r
200 this.ErrorNumber = errorNumber ;
\r
201 this.ErrorDescription = errorDescription ;
\r
203 if ( this.DisplayErrors )
\r
205 document.write( '<div style="COLOR: #ff0000">' ) ;
\r
206 document.write( '[ FCKeditor Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ;
\r
207 document.write( '</div>' ) ;
\r
210 if ( typeof( this.OnError ) == 'function' )
\r
211 this.OnError( this, errorNumber, errorDescription ) ;
\r
214 FCKeditor.prototype._HTMLEncode = function( text )
\r
216 if ( typeof( text ) != "string" )
\r
217 text = text.toString() ;
\r
219 text = text.replace(
\r
220 /&/g, "&").replace(
\r
221 /"/g, """).replace(
\r
222 /</g, "<").replace(
\r
230 var textareaToEditor = function( textarea )
\r
232 var editor = new FCKeditor( textarea.name ) ;
\r
234 editor.Width = Math.max( textarea.offsetWidth, FCKeditor.MinWidth ) ;
\r
235 editor.Height = Math.max( textarea.offsetHeight, FCKeditor.MinHeight ) ;
\r
241 * Replace all <textarea> elements available in the document with FCKeditor
\r
244 * // Replace all <textarea> elements in the page.
\r
245 * FCKeditor.ReplaceAllTextareas() ;
\r
247 * // Replace all <textarea class="myClassName"> elements in the page.
\r
248 * FCKeditor.ReplaceAllTextareas( 'myClassName' ) ;
\r
250 * // Selectively replace <textarea> elements, based on custom assertions.
\r
251 * FCKeditor.ReplaceAllTextareas( function( textarea, editor )
\r
253 * // Custom code to evaluate the replace, returning false if it
\r
254 * // must not be done.
\r
255 * // It also passes the "editor" parameter, so the developer can
\r
256 * // customize the instance.
\r
259 FCKeditor.ReplaceAllTextareas = function()
\r
261 var textareas = document.getElementsByTagName( 'textarea' ) ;
\r
263 for ( var i = 0 ; i < textareas.length ; i++ )
\r
265 var editor = null ;
\r
266 var textarea = textareas[i] ;
\r
267 var name = textarea.name ;
\r
269 // The "name" attribute must exist.
\r
270 if ( !name || name.length == 0 )
\r
273 if ( typeof arguments[0] == 'string' )
\r
275 // The textarea class name could be passed as the function
\r
278 var classRegex = new RegExp( '(?:^| )' + arguments[0] + '(?:$| )' ) ;
\r
280 if ( !classRegex.test( textarea.className ) )
\r
283 else if ( typeof arguments[0] == 'function' )
\r
285 // An assertion function could be passed as the function parameter.
\r
286 // It must explicitly return "false" to ignore a specific <textarea>.
\r
287 editor = textareaToEditor( textarea ) ;
\r
288 if ( arguments[0]( textarea, editor ) === false )
\r
293 editor = textareaToEditor( textarea ) ;
\r
295 editor.ReplaceTextarea() ;
\r
300 function FCKeditor_IsCompatibleBrowser()
\r
302 var sAgent = navigator.userAgent.toLowerCase() ;
\r
304 // Internet Explorer 5.5+
\r
305 if ( /*@cc_on!@*/false && sAgent.indexOf("mac") == -1 )
\r
307 var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
\r
308 return ( sBrowserVersion >= 5.5 ) ;
\r
311 // Gecko (Opera 9 tries to behave like Gecko at this point).
\r
312 if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) )
\r
316 if ( window.opera && window.opera.version && parseFloat( window.opera.version() ) >= 9.5 )
\r
320 // Checked before Safari because AIR have the WebKit rich text editor
\r
321 // features from Safari 3.0.4, but the version reported is 420.
\r
322 if ( sAgent.indexOf( ' adobeair/' ) != -1 )
\r
323 return ( sAgent.match( / adobeair\/(\d+)/ )[1] >= 1 ) ; // Build must be at least v1
\r
326 if ( sAgent.indexOf( ' applewebkit/' ) != -1 )
\r
327 return ( sAgent.match( / applewebkit\/(\d+)/ )[1] >= 522 ) ; // Build must be at least 522 (v3)
\r