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