2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * @param {!Element} relativeToElement
34 * @param {!WebInspector.DialogDelegate} delegate
35 * @param {boolean=} modal
37 WebInspector
.Dialog = function(relativeToElement
, delegate
, modal
)
39 this._delegate
= delegate
;
40 this._relativeToElement
= relativeToElement
;
43 this._glassPane
= new WebInspector
.GlassPane(/** @type {!Document} */ (relativeToElement
.ownerDocument
));
44 WebInspector
.GlassPane
.DefaultFocusedViewStack
.push(this);
46 // Install glass pane capturing events.
47 this._glassPane
.element
.tabIndex
= 0;
48 this._glassPane
.element
.addEventListener("focus", this._onGlassPaneFocus
.bind(this), false);
50 this._glassPane
.element
.classList
.add("tinted");
52 this._element
= this._glassPane
.element
.createChild("div");
53 this._element
.tabIndex
= 0;
54 this._element
.addEventListener("focus", this._onFocus
.bind(this), false);
55 this._element
.addEventListener("keydown", this._onKeyDown
.bind(this), false);
57 WebInspector
.KeyboardShortcut
.Keys
.Enter
.code
,
58 WebInspector
.KeyboardShortcut
.Keys
.Esc
.code
,
61 delegate
.show(this._element
);
64 this._delegate
.focus();
68 * @return {?WebInspector.Dialog}
70 WebInspector
.Dialog
.currentInstance = function()
72 return WebInspector
.Dialog
._instance
;
76 * @param {?Element} relativeToElement
77 * @param {!WebInspector.DialogDelegate} delegate
78 * @param {boolean=} modal
80 WebInspector
.Dialog
.show = function(relativeToElement
, delegate
, modal
)
82 if (WebInspector
.Dialog
._instance
)
84 WebInspector
.Dialog
._instance
= new WebInspector
.Dialog(relativeToElement
|| WebInspector
.Dialog
.modalHostView().element
, delegate
, modal
);
85 WebInspector
.Dialog
._instance
.focus();
88 WebInspector
.Dialog
.hide = function()
90 if (!WebInspector
.Dialog
._instance
)
92 WebInspector
.Dialog
._instance
._hide();
95 WebInspector
.Dialog
.prototype = {
98 this._element
.focus();
105 this._isHiding
= true;
107 this._delegate
.willHide();
109 delete WebInspector
.Dialog
._instance
;
110 WebInspector
.GlassPane
.DefaultFocusedViewStack
.pop();
111 this._glassPane
.dispose();
115 * @param {!Event} event
117 _onGlassPaneFocus: function(event
)
124 _onFocus: function(event
)
126 this._delegate
.focus();
129 _position: function()
131 this._delegate
.position(this._element
, this._relativeToElement
);
134 _onKeyDown: function(event
)
136 if (event
.keyCode
=== WebInspector
.KeyboardShortcut
.Keys
.Tab
.code
) {
137 event
.preventDefault();
141 if (event
.keyCode
=== WebInspector
.KeyboardShortcut
.Keys
.Enter
.code
)
142 this._delegate
.onEnter(event
);
144 this._delegate
.onKeyDown(event
);
146 if (!event
.handled
&& this._closeKeys
.indexOf(event
.keyCode
) >= 0) {
155 * @extends {WebInspector.Object}
157 WebInspector
.DialogDelegate = function()
159 this.element
= createElement("div");
162 WebInspector
.DialogDelegate
.prototype = {
164 * @param {!Element} element
166 show: function(element
)
168 element
.appendChild(this.element
);
169 this.element
.classList
.add("dialog-contents");
170 element
.classList
.add("dialog");
174 * @param {!Element} element
175 * @param {!Element} relativeToElement
177 position: function(element
, relativeToElement
)
179 var container
= WebInspector
.Dialog
._modalHostView
.element
;
180 var box
= relativeToElement
.boxInWindow(window
).relativeToElement(container
);
182 var positionX
= box
.x
+ (relativeToElement
.offsetWidth
- element
.offsetWidth
) / 2;
183 positionX
= Number
.constrain(positionX
, 0, container
.offsetWidth
- element
.offsetWidth
);
185 var positionY
= box
.y
+ (relativeToElement
.offsetHeight
- element
.offsetHeight
) / 2;
186 positionY
= Number
.constrain(positionY
, 0, container
.offsetHeight
- element
.offsetHeight
);
188 element
.style
.position
= "absolute";
189 element
.positionAt(positionX
, positionY
, container
);
192 focus: function() { },
195 * @param {!KeyboardEvent} event
197 onEnter: function(event
) { },
200 * @param {!KeyboardEvent} event
202 onKeyDown: function(event
) { },
204 willHide: function() { },
206 __proto__
: WebInspector
.Object
.prototype
209 /** @type {?WebInspector.Widget} */
210 WebInspector
.Dialog
._modalHostView
= null;
213 * @param {!WebInspector.Widget} view
215 WebInspector
.Dialog
.setModalHostView = function(view
)
217 WebInspector
.Dialog
._modalHostView
= view
;
221 * FIXME: make utility method in Dialog, so clients use it instead of this getter.
222 * Method should be like Dialog.showModalElement(position params, reposition callback).
223 * @return {?WebInspector.Widget}
225 WebInspector
.Dialog
.modalHostView = function()
227 return WebInspector
.Dialog
._modalHostView
;
230 WebInspector
.Dialog
.modalHostRepositioned = function()
232 if (WebInspector
.Dialog
._instance
)
233 WebInspector
.Dialog
._instance
._position();