/source/Plug-in/fck/editor/_source/classes/fckpanel.js

http://prosporous.googlecode.com/ · JavaScript · 359 lines · 234 code · 69 blank · 56 comment · 52 complexity · 20ef51a6f716002607c9781e618e752a MD5 · raw file

  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. * Component that creates floating panels. It is used by many
  22. * other components, like the toolbar items, context menu, etc...
  23. */
  24. var FCKPanel = function( parentWindow )
  25. {
  26. this.IsRTL = ( FCKLang.Dir == 'rtl' ) ;
  27. this.IsContextMenu = false ;
  28. this._LockCounter = 0 ;
  29. this._Window = parentWindow || window ;
  30. var oDocument ;
  31. if ( FCKBrowserInfo.IsIE )
  32. {
  33. // Create the Popup that will hold the panel.
  34. this._Popup = this._Window.createPopup() ;
  35. oDocument = this.Document = this._Popup.document ;
  36. FCK.IECleanup.AddItem( this, FCKPanel_Cleanup ) ;
  37. }
  38. else
  39. {
  40. var oIFrame = this._IFrame = this._Window.document.createElement('iframe') ;
  41. oIFrame.src = 'javascript:void(0)' ;
  42. oIFrame.allowTransparency = true ;
  43. oIFrame.frameBorder = '0' ;
  44. oIFrame.scrolling = 'no' ;
  45. oIFrame.width = oIFrame.height = 0 ;
  46. FCKDomTools.SetElementStyles( oIFrame,
  47. {
  48. position : 'absolute',
  49. zIndex : FCKConfig.FloatingPanelsZIndex
  50. } ) ;
  51. if ( this._Window == window.parent && window.frameElement )
  52. {
  53. var scrollPos = null ;
  54. if ( FCKBrowserInfo.IsGecko && FCK && FCK.EditorDocument )
  55. scrollPos = [ FCK.EditorDocument.body.scrollLeft, FCK.EditorDocument.body.scrollTop ] ;
  56. window.frameElement.parentNode.insertBefore( oIFrame, window.frameElement ) ;
  57. if ( scrollPos )
  58. {
  59. var restoreFunc = function()
  60. {
  61. FCK.EditorDocument.body.scrollLeft = scrollPos[0] ;
  62. FCK.EditorDocument.body.scrollTop = scrollPos[1] ;
  63. }
  64. setTimeout( restoreFunc, 500 ) ;
  65. }
  66. }
  67. else
  68. this._Window.document.body.appendChild( oIFrame ) ;
  69. var oIFrameWindow = oIFrame.contentWindow ;
  70. oDocument = this.Document = oIFrameWindow.document ;
  71. // Workaround for Safari 12256. Ticket #63
  72. var sBase = '' ;
  73. if ( FCKBrowserInfo.IsSafari )
  74. sBase = '<base href="' + window.document.location + '">' ;
  75. // Initialize the IFRAME document body.
  76. oDocument.open() ;
  77. oDocument.write( '<html><head>' + sBase + '<\/head><body style="margin:0px;padding:0px;"><\/body><\/html>' ) ;
  78. oDocument.close() ;
  79. FCKTools.AddEventListenerEx( oIFrameWindow, 'focus', FCKPanel_Window_OnFocus, this ) ;
  80. FCKTools.AddEventListenerEx( oIFrameWindow, 'blur', FCKPanel_Window_OnBlur, this ) ;
  81. }
  82. oDocument.dir = FCKLang.Dir ;
  83. FCKTools.AddEventListener( oDocument, 'contextmenu', FCKTools.CancelEvent ) ;
  84. // Create the main DIV that is used as the panel base.
  85. this.MainNode = oDocument.body.appendChild( oDocument.createElement('DIV') ) ;
  86. // The "float" property must be set so Firefox calculates the size correctly.
  87. this.MainNode.style.cssFloat = this.IsRTL ? 'right' : 'left' ;
  88. }
  89. FCKPanel.prototype.AppendStyleSheet = function( styleSheet )
  90. {
  91. FCKTools.AppendStyleSheet( this.Document, styleSheet ) ;
  92. }
  93. FCKPanel.prototype.Preload = function( x, y, relElement )
  94. {
  95. // The offsetWidth and offsetHeight properties are not available if the
  96. // element is not visible. So we must "show" the popup with no size to
  97. // be able to use that values in the second call (IE only).
  98. if ( this._Popup )
  99. this._Popup.show( x, y, 0, 0, relElement ) ;
  100. }
  101. FCKPanel.prototype.Show = function( x, y, relElement, width, height )
  102. {
  103. var iMainWidth ;
  104. var eMainNode = this.MainNode ;
  105. if ( this._Popup )
  106. {
  107. // The offsetWidth and offsetHeight properties are not available if the
  108. // element is not visible. So we must "show" the popup with no size to
  109. // be able to use that values in the second call.
  110. this._Popup.show( x, y, 0, 0, relElement ) ;
  111. // The following lines must be place after the above "show", otherwise it
  112. // doesn't has the desired effect.
  113. FCKDomTools.SetElementStyles( eMainNode,
  114. {
  115. width : width ? width + 'px' : '',
  116. height : height ? height + 'px' : ''
  117. } ) ;
  118. iMainWidth = eMainNode.offsetWidth ;
  119. if ( this.IsRTL )
  120. {
  121. if ( this.IsContextMenu )
  122. x = x - iMainWidth + 1 ;
  123. else if ( relElement )
  124. x = ( x * -1 ) + relElement.offsetWidth - iMainWidth ;
  125. }
  126. // Second call: Show the Popup at the specified location, with the correct size.
  127. this._Popup.show( x, y, iMainWidth, eMainNode.offsetHeight, relElement ) ;
  128. if ( this.OnHide )
  129. {
  130. if ( this._Timer )
  131. CheckPopupOnHide.call( this, true ) ;
  132. this._Timer = FCKTools.SetInterval( CheckPopupOnHide, 100, this ) ;
  133. }
  134. }
  135. else
  136. {
  137. // Do not fire OnBlur while the panel is opened.
  138. if ( typeof( FCK.ToolbarSet.CurrentInstance.FocusManager ) != 'undefined' )
  139. FCK.ToolbarSet.CurrentInstance.FocusManager.Lock() ;
  140. if ( this.ParentPanel )
  141. {
  142. this.ParentPanel.Lock() ;
  143. // Due to a bug on FF3, we must ensure that the parent panel will
  144. // blur (#1584).
  145. FCKPanel_Window_OnBlur( null, this.ParentPanel ) ;
  146. }
  147. // Be sure we'll not have more than one Panel opened at the same time.
  148. if ( FCKPanel._OpenedPanel )
  149. FCKPanel._OpenedPanel.Hide() ;
  150. FCKDomTools.SetElementStyles( eMainNode,
  151. {
  152. width : width ? width + 'px' : '',
  153. height : height ? height + 'px' : ''
  154. } ) ;
  155. iMainWidth = eMainNode.offsetWidth ;
  156. if ( !width ) this._IFrame.width = 1 ;
  157. if ( !height ) this._IFrame.height = 1 ;
  158. // This is weird... but with Firefox, we must get the offsetWidth before
  159. // setting the _IFrame size (which returns "0"), and then after that,
  160. // to return the correct width. Remove the first step and it will not
  161. // work when the editor is in RTL.
  162. //
  163. // The "|| eMainNode.firstChild.offsetWidth" part has been added
  164. // for Opera compatibility (see #570).
  165. iMainWidth = eMainNode.offsetWidth || eMainNode.firstChild.offsetWidth ;
  166. // Base the popup coordinates upon the coordinates of relElement.
  167. var oPos = FCKTools.GetDocumentPosition( this._Window,
  168. relElement.nodeType == 9 ?
  169. ( FCKTools.IsStrictMode( relElement ) ? relElement.documentElement : relElement.body ) :
  170. relElement ) ;
  171. // Minus the offsets provided by any positioned parent element of the panel iframe.
  172. var positionedAncestor = FCKDomTools.GetPositionedAncestor( FCKTools.GetElementWindow( this._IFrame ), this._IFrame.parentNode ) ;
  173. if ( positionedAncestor )
  174. {
  175. var nPos = FCKTools.GetDocumentPosition( FCKTools.GetElementWindow( positionedAncestor ), positionedAncestor ) ;
  176. oPos.x -= nPos.x ;
  177. oPos.y -= nPos.y ;
  178. }
  179. if ( this.IsRTL && !this.IsContextMenu )
  180. x = ( x * -1 ) ;
  181. x += oPos.x ;
  182. y += oPos.y ;
  183. if ( this.IsRTL )
  184. {
  185. if ( this.IsContextMenu )
  186. x = x - iMainWidth + 1 ;
  187. else if ( relElement )
  188. x = x + relElement.offsetWidth - iMainWidth ;
  189. }
  190. else
  191. {
  192. var oViewPaneSize = FCKTools.GetViewPaneSize( this._Window ) ;
  193. var oScrollPosition = FCKTools.GetScrollPosition( this._Window ) ;
  194. var iViewPaneHeight = oViewPaneSize.Height + oScrollPosition.Y ;
  195. var iViewPaneWidth = oViewPaneSize.Width + oScrollPosition.X ;
  196. if ( ( x + iMainWidth ) > iViewPaneWidth )
  197. x -= x + iMainWidth - iViewPaneWidth ;
  198. if ( ( y + eMainNode.offsetHeight ) > iViewPaneHeight )
  199. y -= y + eMainNode.offsetHeight - iViewPaneHeight ;
  200. }
  201. if ( x < 0 )
  202. x = 0 ;
  203. // Set the context menu DIV in the specified location.
  204. FCKDomTools.SetElementStyles( this._IFrame,
  205. {
  206. left : x + 'px',
  207. top : y + 'px'
  208. } ) ;
  209. var iWidth = iMainWidth ;
  210. var iHeight = eMainNode.offsetHeight ;
  211. this._IFrame.width = iWidth ;
  212. this._IFrame.height = iHeight ;
  213. // Move the focus to the IFRAME so we catch the "onblur".
  214. this._IFrame.contentWindow.focus() ;
  215. FCKPanel._OpenedPanel = this ;
  216. }
  217. this._IsOpened = true ;
  218. FCKTools.RunFunction( this.OnShow, this ) ;
  219. }
  220. FCKPanel.prototype.Hide = function( ignoreOnHide )
  221. {
  222. if ( this._Popup )
  223. this._Popup.hide() ;
  224. else
  225. {
  226. if ( !this._IsOpened || this._LockCounter > 0 )
  227. return ;
  228. // Enable the editor to fire the "OnBlur".
  229. if ( typeof( FCKFocusManager ) != 'undefined' )
  230. FCKFocusManager.Unlock() ;
  231. // It is better to set the sizes to 0, otherwise Firefox would have
  232. // rendering problems.
  233. this._IFrame.width = this._IFrame.height = 0 ;
  234. this._IsOpened = false ;
  235. if ( this.ParentPanel )
  236. this.ParentPanel.Unlock() ;
  237. if ( !ignoreOnHide )
  238. FCKTools.RunFunction( this.OnHide, this ) ;
  239. }
  240. }
  241. FCKPanel.prototype.CheckIsOpened = function()
  242. {
  243. if ( this._Popup )
  244. return this._Popup.isOpen ;
  245. else
  246. return this._IsOpened ;
  247. }
  248. FCKPanel.prototype.CreateChildPanel = function()
  249. {
  250. var oWindow = this._Popup ? FCKTools.GetDocumentWindow( this.Document ) : this._Window ;
  251. var oChildPanel = new FCKPanel( oWindow ) ;
  252. oChildPanel.ParentPanel = this ;
  253. return oChildPanel ;
  254. }
  255. FCKPanel.prototype.Lock = function()
  256. {
  257. this._LockCounter++ ;
  258. }
  259. FCKPanel.prototype.Unlock = function()
  260. {
  261. if ( --this._LockCounter == 0 && !this.HasFocus )
  262. this.Hide() ;
  263. }
  264. /* Events */
  265. function FCKPanel_Window_OnFocus( e, panel )
  266. {
  267. panel.HasFocus = true ;
  268. }
  269. function FCKPanel_Window_OnBlur( e, panel )
  270. {
  271. panel.HasFocus = false ;
  272. if ( panel._LockCounter == 0 )
  273. FCKTools.RunFunction( panel.Hide, panel ) ;
  274. }
  275. function CheckPopupOnHide( forceHide )
  276. {
  277. if ( forceHide || !this._Popup.isOpen )
  278. {
  279. window.clearInterval( this._Timer ) ;
  280. this._Timer = null ;
  281. FCKTools.RunFunction( this.OnHide, this ) ;
  282. }
  283. }
  284. function FCKPanel_Cleanup()
  285. {
  286. this._Popup = null ;
  287. this._Window = null ;
  288. this.Document = null ;
  289. this.MainNode = null ;
  290. }