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
36 WebInspector.Dialog = function(relativeToElement, delegate)
38 this._delegate = delegate;
39 this._relativeToElement = relativeToElement;
41 this._glassPane = new WebInspector.GlassPane();
42 // Install glass pane capturing events.
43 this._glassPane.element.tabIndex = 0;
44 this._glassPane.element.addEventListener("focus", this._onGlassPaneFocus.bind(this), false);
46 this._element = this._glassPane.element.createChild("div");
47 this._element.tabIndex = 0;
48 this._element.addEventListener("focus", this._onFocus.bind(this), false);
49 this._element.addEventListener("keydown", this._onKeyDown.bind(this), false);
51 WebInspector.KeyboardShortcut.Keys.Enter.code,
52 WebInspector.KeyboardShortcut.Keys.Esc.code,
55 delegate.show(this._element);
58 this._delegate.focus();
62 * @return {?WebInspector.Dialog}
64 WebInspector.Dialog.currentInstance = function()
66 return WebInspector.Dialog._instance;
70 * @param {!Element} relativeToElement
71 * @param {!WebInspector.DialogDelegate} delegate
73 WebInspector.Dialog.show = function(relativeToElement, delegate)
75 if (WebInspector.Dialog._instance)
77 WebInspector.Dialog._instance = new WebInspector.Dialog(relativeToElement, delegate);
80 WebInspector.Dialog.hide = function()
82 if (!WebInspector.Dialog._instance)
84 WebInspector.Dialog._instance._hide();
87 WebInspector.Dialog.prototype = {
92 this._isHiding = true;
94 this._delegate.willHide();
96 delete WebInspector.Dialog._instance;
97 this._glassPane.dispose();
100 _onGlassPaneFocus: function(event)
105 _onFocus: function(event)
107 this._delegate.focus();
110 _position: function()
112 this._delegate.position(this._element, this._relativeToElement);
115 _onKeyDown: function(event)
117 if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Tab.code) {
118 event.preventDefault();
122 if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code)
123 this._delegate.onEnter();
125 if (this._closeKeys.indexOf(event.keyCode) >= 0) {
134 * @extends {WebInspector.Object}
136 WebInspector.DialogDelegate = function()
138 /** @type {!Element} */
142 WebInspector.DialogDelegate.prototype = {
144 * @param {!Element} element
146 show: function(element)
148 element.appendChild(this.element);
149 this.element.classList.add("dialog-contents");
150 element.classList.add("dialog");
154 * @param {!Element} element
155 * @param {!Element} relativeToElement
157 position: function(element, relativeToElement)
159 var container = WebInspector.Dialog._modalHostView.element;
160 var box = relativeToElement.boxInWindow(window).relativeToElement(container);
162 var positionX = box.x + (relativeToElement.offsetWidth - element.offsetWidth) / 2;
163 positionX = Number.constrain(positionX, 0, container.offsetWidth - element.offsetWidth);
165 var positionY = box.y + (relativeToElement.offsetHeight - element.offsetHeight) / 2;
166 positionY = Number.constrain(positionY, 0, container.offsetHeight - element.offsetHeight);
168 element.style.position = "absolute";
169 element.positionAt(positionX, positionY, container);
172 focus: function() { },
174 onEnter: function() { },
176 willHide: function() { },
178 __proto__: WebInspector.Object.prototype
181 /** @type {?WebInspector.View} */
182 WebInspector.Dialog._modalHostView = null;
185 * @param {!WebInspector.View} view
187 WebInspector.Dialog.setModalHostView = function(view)
189 WebInspector.Dialog._modalHostView = view;
193 * FIXME: make utility method in Dialog, so clients use it instead of this getter.
194 * Method should be like Dialog.showModalElement(position params, reposition callback).
195 * @return {?WebInspector.View}
197 WebInspector.Dialog.modalHostView = function()
199 return WebInspector.Dialog._modalHostView;
202 WebInspector.Dialog.modalHostRepositioned = function()
204 if (WebInspector.Dialog._instance)
205 WebInspector.Dialog._instance._position();