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