fckeditor.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. // FCKeditor Class
  28. var FCKeditor = function( instanceName, width, height, toolbarSet, value, url_insertImg )
  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.url_insertImg = url_insertImg || '';
  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. this.Config = new Object() ;
  43. // Events
  44. this.OnError = null ; // function( source, errorNumber, errorDescription )
  45. }
  46. FCKeditor.prototype.Version = '2.4.3' ;
  47. FCKeditor.prototype.VersionBuild = '15657' ;
  48. FCKeditor.prototype.Create = function()
  49. {
  50. document.write( this.CreateHtml() ) ;
  51. }
  52. FCKeditor.prototype.CreateHtml = function()
  53. {
  54. // Check for errors
  55. if ( !this.InstanceName || this.InstanceName.length == 0 )
  56. {
  57. this._ThrowError( 701, 'You must specify an instance name.' ) ;
  58. return '' ;
  59. }
  60. var sHtml = '<div>' ;
  61. if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
  62. {
  63. sHtml += '<input type="hidden" id="' + this.InstanceName + '" name="' + this.InstanceName + '" value="' + this._HTMLEncode( this.Value ) + '" style="display:none" />' ;
  64. sHtml += this._GetConfigHtml() ;
  65. sHtml += this._GetIFrameHtml() ;
  66. }
  67. else
  68. {
  69. var sWidth = this.Width.toString().indexOf('%') > 0 ? this.Width : this.Width + 'px' ;
  70. var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ;
  71. sHtml += '<textarea name="' + this.InstanceName + '" rows="4" cols="40" style="width:' + sWidth + ';height:' + sHeight + '">' + this._HTMLEncode( this.Value ) + '<\/textarea>' ;
  72. }
  73. sHtml += '</div>' ;
  74. return sHtml ;
  75. }
  76. FCKeditor.prototype.ReplaceTextarea = function()
  77. {
  78. if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
  79. {
  80. // We must check the elements firstly using the Id and then the name.
  81. var oTextarea = document.getElementById( this.InstanceName ) ;
  82. var colElementsByName = document.getElementsByName( this.InstanceName ) ;
  83. var i = 0;
  84. while ( oTextarea || i == 0 )
  85. {
  86. if ( oTextarea && oTextarea.tagName.toLowerCase() == 'textarea' )
  87. break ;
  88. oTextarea = colElementsByName[i++] ;
  89. }
  90. if ( !oTextarea )
  91. {
  92. alert( 'Error: The TEXTAREA with id or name set to "' + this.InstanceName + '" was not found' ) ;
  93. return ;
  94. }
  95. oTextarea.style.display = 'none' ;
  96. this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ;
  97. this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ;
  98. }
  99. }
  100. FCKeditor.prototype._InsertHtmlBefore = function( html, element )
  101. {
  102. if ( element.insertAdjacentHTML ) // IE
  103. element.insertAdjacentHTML( 'beforeBegin', html ) ;
  104. else // Gecko
  105. {
  106. var oRange = document.createRange() ;
  107. oRange.setStartBefore( element ) ;
  108. var oFragment = oRange.createContextualFragment( html );
  109. element.parentNode.insertBefore( oFragment, element ) ;
  110. }
  111. }
  112. FCKeditor.prototype._GetConfigHtml = function()
  113. {
  114. var sConfig = '' ;
  115. for ( var o in this.Config )
  116. {
  117. if ( sConfig.length > 0 ) sConfig += '&amp;' ;
  118. sConfig += encodeURIComponent( o ) + '=' + encodeURIComponent( this.Config[o] ) ;
  119. }
  120. return '<input type="hidden" id="' + this.InstanceName + '___Config" value="' + sConfig + '" style="display:none" />' ;
  121. }
  122. FCKeditor.prototype._GetIFrameHtml = function()
  123. {
  124. var sFile = 'fckeditor.html' ;
  125. try
  126. {
  127. if ( (/fcksource=true/i).test( window.top.location.search ) )
  128. sFile = 'fckeditor.original.html' ;
  129. }
  130. catch (e) { /* Ignore it. Much probably we are inside a FRAME where the "top" is in another domain (security error). */ }
  131. var sLink = this.BasePath + 'editor/' + sFile + '?InstanceName=' + encodeURIComponent( this.InstanceName ) ;
  132. if (this.ToolbarSet) sLink += '&amp;Toolbar=' + this.ToolbarSet ;
  133. return '<iframe onfocus="childFocus(\'' + this.InstanceName + '___Frame\')" id="' + this.InstanceName + '___Frame" src="' + sLink + '&v=2.3.html" width="' + this.Width + '" height="' + this.Height + '" frameborder="0" scrolling="no"></iframe>' ;
  134. }
  135. function childFocus(elm) {
  136. try{
  137. var ifr = document.getElementById(elm);
  138. ifr.contentWindow.document.getElementsByTagName('iframe')[0].contentWindow.focus();
  139. }catch(e){}
  140. }
  141. FCKeditor.prototype._IsCompatibleBrowser = function()
  142. {
  143. return FCKeditor_IsCompatibleBrowser( this.EnableSafari, this.EnableOpera ) ;
  144. }
  145. FCKeditor.prototype._ThrowError = function( errorNumber, errorDescription )
  146. {
  147. this.ErrorNumber = errorNumber ;
  148. this.ErrorDescription = errorDescription ;
  149. if ( this.DisplayErrors )
  150. {
  151. document.write( '<div style="COLOR: #ff0000">' ) ;
  152. document.write( '[ FCKeditor Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ;
  153. document.write( '</div>' ) ;
  154. }
  155. if ( typeof( this.OnError ) == 'function' )
  156. this.OnError( this, errorNumber, errorDescription ) ;
  157. }
  158. FCKeditor.prototype._HTMLEncode = function( text )
  159. {
  160. if ( typeof( text ) != "string" )
  161. text = text.toString() ;
  162. text = text.replace(
  163. /&/g, "&amp;").replace(
  164. /"/g, "&quot;").replace(
  165. /</g, "&lt;").replace(
  166. />/g, "&gt;") ;
  167. return text ;
  168. }
  169. function FCKeditor_IsCompatibleBrowser( enableSafari, enableOpera )
  170. {
  171. var sAgent = navigator.userAgent.toLowerCase() ;
  172. // Internet Explorer
  173. if ( sAgent.indexOf("msie") != -1 && sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1 )
  174. {
  175. var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
  176. return ( sBrowserVersion >= 5.5 ) ;
  177. }
  178. // Gecko (Opera 9 tries to behave like Gecko at this point).
  179. if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 && !( typeof(opera) == 'object' && opera.postError ) )
  180. return true ;
  181. // Opera
  182. if ( enableOpera && sAgent.indexOf( 'opera' ) == 0 && parseInt( navigator.appVersion, 10 ) >= 9 )
  183. return true ;
  184. // Safari
  185. if ( enableSafari && sAgent.indexOf( 'safari' ) != -1 )
  186. return ( sAgent.match( /safari\/(\d+)/ )[1] >= 312 ) ; // Build must be at least 312 (1.3)
  187. return false ;
  188. }
  189. var BD_F1 = true;