fckeditor.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  3. * Copyright (C) 2003-2008 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. // FCKeditor Class
  28. var FCKeditor = function( instanceName, width, height, toolbarSet, value )
  29. {
  30. // Properties
  31. this.InstanceName = instanceName ;
  32. this.Width = width || '100%' ;
  33. this.Height = height || '200' ;
  34. this.ToolbarSet = toolbarSet || 'Default' ;
  35. this.Value = value || '' ;
  36. this.BasePath = FCKeditor.BasePath ;
  37. this.CheckBrowser = true ;
  38. this.DisplayErrors = true ;
  39. this.Config = new Object() ;
  40. // Events
  41. this.OnError = null ; // function( source, errorNumber, errorDescription )
  42. }
  43. /**
  44. * This is the default BasePath used by all editor instances.
  45. */
  46. FCKeditor.BasePath = '/fckeditor/' ;
  47. /**
  48. * The minimum height used when replacing textareas.
  49. */
  50. FCKeditor.MinHeight = 200 ;
  51. /**
  52. * The minimum width used when replacing textareas.
  53. */
  54. FCKeditor.MinWidth = 750 ;
  55. FCKeditor.prototype.Version = '2.6' ;
  56. FCKeditor.prototype.VersionBuild = '18638' ;
  57. FCKeditor.prototype.Create = function()
  58. {
  59. document.write( this.CreateHtml() ) ;
  60. }
  61. FCKeditor.prototype.CreateHtml = function()
  62. {
  63. // Check for errors
  64. if ( !this.InstanceName || this.InstanceName.length == 0 )
  65. {
  66. this._ThrowError( 701, 'You must specify an instance name.' ) ;
  67. return '' ;
  68. }
  69. var sHtml = '' ;
  70. if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
  71. {
  72. sHtml += '<input type="hidden" id="' + this.InstanceName + '" name="' + this.InstanceName + '" value="' + this._HTMLEncode( this.Value ) + '" style="display:none" />' ;
  73. sHtml += this._GetConfigHtml() ;
  74. sHtml += this._GetIFrameHtml() ;
  75. }
  76. else
  77. {
  78. var sWidth = this.Width.toString().indexOf('%') > 0 ? this.Width : this.Width + 'px' ;
  79. var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ;
  80. sHtml += '<textarea name="' + this.InstanceName + '" rows="4" cols="40" style="width:' + sWidth + ';height:' + sHeight + '">' + this._HTMLEncode( this.Value ) + '<\/textarea>' ;
  81. }
  82. return sHtml ;
  83. }
  84. FCKeditor.prototype.ReplaceTextarea = function()
  85. {
  86. if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
  87. {
  88. // We must check the elements firstly using the Id and then the name.
  89. var oTextarea = document.getElementById( this.InstanceName ) ;
  90. var colElementsByName = document.getElementsByName( this.InstanceName ) ;
  91. var i = 0;
  92. while ( oTextarea || i == 0 )
  93. {
  94. if ( oTextarea && oTextarea.tagName.toLowerCase() == 'textarea' )
  95. break ;
  96. oTextarea = colElementsByName[i++] ;
  97. }
  98. if ( !oTextarea )
  99. {
  100. alert( 'Error: The TEXTAREA with id or name set to "' + this.InstanceName + '" was not found' ) ;
  101. return ;
  102. }
  103. oTextarea.style.display = 'none' ;
  104. this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ;
  105. this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ;
  106. }
  107. }
  108. FCKeditor.prototype._InsertHtmlBefore = function( html, element )
  109. {
  110. if ( element.insertAdjacentHTML ) // IE
  111. element.insertAdjacentHTML( 'beforeBegin', html ) ;
  112. else // Gecko
  113. {
  114. var oRange = document.createRange() ;
  115. oRange.setStartBefore( element ) ;
  116. var oFragment = oRange.createContextualFragment( html );
  117. element.parentNode.insertBefore( oFragment, element ) ;
  118. }
  119. }
  120. FCKeditor.prototype._GetConfigHtml = function()
  121. {
  122. var sConfig = '' ;
  123. for ( var o in this.Config )
  124. {
  125. if ( sConfig.length > 0 ) sConfig += '&amp;' ;
  126. sConfig += encodeURIComponent( o ) + '=' + encodeURIComponent( this.Config[o] ) ;
  127. }
  128. return '<input type="hidden" id="' + this.InstanceName + '___Config" value="' + sConfig + '" style="display:none" />' ;
  129. }
  130. FCKeditor.prototype._GetIFrameHtml = function()
  131. {
  132. var sFile = 'fckeditor.html' ;
  133. try
  134. {
  135. if ( (/fcksource=true/i).test( window.top.location.search ) )
  136. sFile = 'fckeditor.original.html' ;
  137. }
  138. catch (e) { /* Ignore it. Much probably we are inside a FRAME where the "top" is in another domain (security error). */ }
  139. var sLink = this.BasePath + 'editor/' + sFile + '?InstanceName=' + encodeURIComponent( this.InstanceName ) ;
  140. if (this.ToolbarSet) sLink += '&amp;Toolbar=' + this.ToolbarSet ;
  141. return '<iframe id="' + this.InstanceName + '___Frame" src="' + sLink + '" width="' + this.Width + '" height="' + this.Height + '" frameborder="0" scrolling="no"></iframe>' ;
  142. }
  143. FCKeditor.prototype._IsCompatibleBrowser = function()
  144. {
  145. return FCKeditor_IsCompatibleBrowser() ;
  146. }
  147. FCKeditor.prototype._ThrowError = function( errorNumber, errorDescription )
  148. {
  149. this.ErrorNumber = errorNumber ;
  150. this.ErrorDescription = errorDescription ;
  151. if ( this.DisplayErrors )
  152. {
  153. document.write( '<div style="COLOR: #ff0000">' ) ;
  154. document.write( '[ FCKeditor Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ;
  155. document.write( '</div>' ) ;
  156. }
  157. if ( typeof( this.OnError ) == 'function' )
  158. this.OnError( this, errorNumber, errorDescription ) ;
  159. }
  160. FCKeditor.prototype._HTMLEncode = function( text )
  161. {
  162. if ( typeof( text ) != "string" )
  163. text = text.toString() ;
  164. text = text.replace(
  165. /&/g, "&amp;").replace(
  166. /"/g, "&quot;").replace(
  167. /</g, "&lt;").replace(
  168. />/g, "&gt;") ;
  169. return text ;
  170. }
  171. ;(function()
  172. {
  173. var textareaToEditor = function( textarea )
  174. {
  175. var editor = new FCKeditor( textarea.name ) ;
  176. editor.Width = Math.max( textarea.offsetWidth, FCKeditor.MinWidth ) ;
  177. editor.Height = Math.max( textarea.offsetHeight, FCKeditor.MinHeight ) ;
  178. return editor ;
  179. }
  180. /**
  181. * Replace all <textarea> elements available in the document with FCKeditor
  182. * instances.
  183. *
  184. * // Replace all <textarea> elements in the page.
  185. * FCKeditor.ReplaceAllTextareas() ;
  186. *
  187. * // Replace all <textarea class="myClassName"> elements in the page.
  188. * FCKeditor.ReplaceAllTextareas( 'myClassName' ) ;
  189. *
  190. * // Selectively replace <textarea> elements, based on custom assertions.
  191. * FCKeditor.ReplaceAllTextareas( function( textarea, editor )
  192. * {
  193. * // Custom code to evaluate the replace, returning false if it
  194. * // must not be done.
  195. * // It also passes the "editor" parameter, so the developer can
  196. * // customize the instance.
  197. * } ) ;
  198. */
  199. FCKeditor.ReplaceAllTextareas = function()
  200. {
  201. var textareas = document.getElementsByTagName( 'textarea' ) ;
  202. for ( var i = 0 ; i < textareas.length ; i++ )
  203. {
  204. var editor = null ;
  205. var textarea = textareas[i] ;
  206. var name = textarea.name ;
  207. // The "name" attribute must exist.
  208. if ( !name || name.length == 0 )
  209. continue ;
  210. if ( typeof arguments[0] == 'string' )
  211. {
  212. // The textarea class name could be passed as the function
  213. // parameter.
  214. var classRegex = new RegExp( '(?:^| )' + arguments[0] + '(?:$| )' ) ;
  215. if ( !classRegex.test( textarea.className ) )
  216. continue ;
  217. }
  218. else if ( typeof arguments[0] == 'function' )
  219. {
  220. // An assertion function could be passed as the function parameter.
  221. // It must explicitly return "false" to ignore a specific <textarea>.
  222. editor = textareaToEditor( textarea ) ;
  223. if ( arguments[0]( textarea, editor ) === false )
  224. continue ;
  225. }
  226. if ( !editor )
  227. editor = textareaToEditor( textarea ) ;
  228. editor.ReplaceTextarea() ;
  229. }
  230. }
  231. })() ;
  232. function FCKeditor_IsCompatibleBrowser()
  233. {
  234. var sAgent = navigator.userAgent.toLowerCase() ;
  235. // Internet Explorer 5.5+
  236. if ( /*@cc_on!@*/false && sAgent.indexOf("mac") == -1 )
  237. {
  238. var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
  239. return ( sBrowserVersion >= 5.5 ) ;
  240. }
  241. // Gecko (Opera 9 tries to behave like Gecko at this point).
  242. if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) )
  243. return true ;
  244. // Opera 9.50+
  245. if ( window.opera && window.opera.version && parseFloat( window.opera.version() ) >= 9.5 )
  246. return true ;
  247. // Adobe AIR
  248. // Checked before Safari because AIR have the WebKit rich text editor
  249. // features from Safari 3.0.4, but the version reported is 420.
  250. if ( sAgent.indexOf( ' adobeair/' ) != -1 )
  251. return ( sAgent.match( / adobeair\/(\d+)/ )[1] >= 1 ) ; // Build must be at least v1
  252. // Safari 3+
  253. if ( sAgent.indexOf( ' applewebkit/' ) != -1 )
  254. return ( sAgent.match( / applewebkit\/(\d+)/ )[1] >= 522 ) ; // Build must be at least 522 (v3)
  255. return false ;
  256. }