fckeditor.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * FCKeditor - The text editor for internet
  3. * Copyright (C) 2003-2004 Frederico Caldeira Knabben
  4. *
  5. * Licensed under the terms of the GNU Lesser General Public License:
  6. * http://www.opensource.org/licenses/lgpl-license.php
  7. *
  8. * For further information visit:
  9. * http://www.fckeditor.net/
  10. *
  11. * File Name: fckeditor.js
  12. * This is the integration file for JavaScript.
  13. *
  14. * It defines the FCKeditor class that can be used to create editor
  15. * instances in a HTML page in the client side. For server side
  16. * operations, use the specific integration system.
  17. *
  18. * Version: 2.0 RC2
  19. * Modified: 2004-11-23 20:35:14
  20. *
  21. * File Authors:
  22. * Frederico Caldeira Knabben (fredck@fckeditor.net)
  23. */
  24. // FCKeditor Class
  25. var FCKeditor = function( instanceName, width, height, toolbarSet, value )
  26. {
  27. // Properties
  28. this.InstanceName = instanceName ;
  29. this.Width = width || '100%' ;
  30. this.Height = height || '200' ;
  31. this.ToolbarSet = toolbarSet || 'Default' ;
  32. this.Value = value || '' ;
  33. this.BasePath = '/fckeditor/' ;
  34. this.Local = null;
  35. this.CheckBrowser = true ;
  36. this.DisplayErrors = true ;
  37. this.Config = new Object() ;
  38. // Events
  39. this.OnError = null ; // function( source, errorNumber, errorDescription )
  40. }
  41. FCKeditor.prototype.Create = function()
  42. {
  43. // Check for errors
  44. if ( !this.InstanceName || this.InstanceName.length == 0 )
  45. {
  46. this._ThrowError( 701, 'You must specify a instance name.' ) ;
  47. return ;
  48. }
  49. document.write( '<div>' ) ;
  50. if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
  51. {
  52. document.write( '<input type="hidden" id="' + this.InstanceName + '" name="' + this.InstanceName + '" value="' + this._HTMLEncode( this.Value ) + '">' ) ;
  53. document.write( this._GetConfigHtml() ) ;
  54. document.write( this._GetIFrameHtml() ) ;
  55. }
  56. else
  57. {
  58. var sWidth = this.Width.toString().indexOf('%') > 0 ? this.Width : this.Width + 'px' ;
  59. var sHeight = this.Height.toString().indexOf('%') > 0 ? this.Height : this.Height + 'px' ;
  60. document.write('<textarea name="' + this.InstanceName + '" rows="4" cols="40" style="WIDTH: ' + sWidth + '; HEIGHT: ' + sHeight + '" wrap="virtual">' + this._HTMLEncode( this.Value ) + '<\/textarea>') ;
  61. }
  62. document.write( '</div>' ) ;
  63. }
  64. FCKeditor.prototype.ReplaceTextarea = function()
  65. {
  66. if ( !this.CheckBrowser || this._IsCompatibleBrowser() )
  67. {
  68. var oTextarea = document.getElementsByName( this.InstanceName )[0] ;
  69. oTextarea.style.display = 'none' ;
  70. this._InsertHtmlBefore( this._GetConfigHtml(), oTextarea ) ;
  71. this._InsertHtmlBefore( this._GetIFrameHtml(), oTextarea ) ;
  72. }
  73. }
  74. FCKeditor.prototype._InsertHtmlBefore = function( html, element )
  75. {
  76. if ( element.insertAdjacentHTML ) // IE
  77. element.insertAdjacentHTML( 'beforeBegin', html ) ;
  78. else // Gecko
  79. {
  80. var oRange = document.createRange() ;
  81. oRange.setStartBefore( element ) ;
  82. var oFragment = oRange.createContextualFragment( html );
  83. element.parentNode.insertBefore( oFragment, element ) ;
  84. }
  85. }
  86. FCKeditor.prototype._GetConfigHtml = function()
  87. {
  88. var sConfig = '' ;
  89. for ( var o in this.Config )
  90. {
  91. if ( sConfig.length > 0 ) sConfig += '&' ;
  92. sConfig += this._HTMLEncode(o) + '=' + this._HTMLEncode( this.Config[o] ) ;
  93. }
  94. return '<input type="hidden" id="' + this.InstanceName + '___Config" value="' + sConfig + '">' ;
  95. }
  96. FCKeditor.prototype._GetIFrameHtml = function()
  97. {
  98. var sLink = this.BasePath + 'editor/fckeditor.html?InstanceName=' + this.InstanceName+"&Local="+this.Local ;
  99. if (this.ToolbarSet) sLink += '&Toolbar=' + this.ToolbarSet ;
  100. return '<iframe id="' + this.InstanceName + '___Frame" src="' + sLink + '" width="' + this.Width + '" height="' + this.Height + '" frameborder="no" scrolling="no"></iframe>' ;
  101. }
  102. FCKeditor.prototype._IsCompatibleBrowser = function()
  103. {
  104. var sAgent = navigator.userAgent.toLowerCase() ;
  105. // Internet Explorer
  106. if ( sAgent.indexOf("msie") != -1 && sAgent.indexOf("mac") == -1 && sAgent.indexOf("opera") == -1 )
  107. {
  108. var sBrowserVersion = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
  109. return ( sBrowserVersion >= 5.5 ) ;
  110. }
  111. // Gecko
  112. else if ( navigator.product == "Gecko" && navigator.productSub >= 20030210 )
  113. return true ;
  114. else
  115. return false ;
  116. }
  117. FCKeditor.prototype._ThrowError = function( errorNumber, errorDescription )
  118. {
  119. this.ErrorNumber = errorNumber ;
  120. this.ErrorDescription = errorDescription ;
  121. if ( this.DisplayErrors )
  122. {
  123. document.write( '<div style="COLOR: #ff0000">' ) ;
  124. document.write( '[ FCKeditor Error ' + this.ErrorNumber + ': ' + this.ErrorDescription + ' ]' ) ;
  125. document.write( '</div>' ) ;
  126. }
  127. if ( typeof( this.OnError ) == 'function' )
  128. this.OnError( this, errorNumber, errorDescription ) ;
  129. }
  130. FCKeditor.prototype._HTMLEncode = function( text )
  131. {
  132. if ( typeof( text ) != "string" )
  133. text = text.toString() ;
  134. text = text.replace(/&/g, "&amp;") ;
  135. text = text.replace(/"/g, "&quot;") ;
  136. text = text.replace(/</g, "&lt;") ;
  137. text = text.replace(/>/g, "&gt;") ;
  138. text = text.replace(/'/g, "&#39;") ;
  139. return text ;
  140. }