fckeditor.js 9.1 KB

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