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()
140 WebInspector.DialogDelegate.prototype = {
142 * @param {!Element} element
144 show: function(element)
146 element.appendChild(this.element);
147 this.element.classList.add("dialog-contents");
148 element.classList.add("dialog");
152 * @param {!Element} element
153 * @param {!Element} relativeToElement
155 position: function(element, relativeToElement)
157 var container = WebInspector.Dialog._modalHostView.element;
158 var box = relativeToElement.boxInWindow(window).relativeToElement(container);
160 var positionX = box.x + (relativeToElement.offsetWidth - element.offsetWidth) / 2;
161 positionX = Number.constrain(positionX, 0, container.offsetWidth - element.offsetWidth);
163 var positionY = box.y + (relativeToElement.offsetHeight - element.offsetHeight) / 2;
164 positionY = Number.constrain(positionY, 0, container.offsetHeight - element.offsetHeight);
166 element.style.position = "absolute";
167 element.positionAt(positionX, positionY, container);
170 focus: function() { },
172 onEnter: function() { },
174 willHide: function() { },
176 __proto__: WebInspector.Object.prototype
179 /** @type {?WebInspector.View} */
180 WebInspector.Dialog._modalHostView = null;
183 * @param {!WebInspector.View} view
185 WebInspector.Dialog.setModalHostView = function(view)
187 WebInspector.Dialog._modalHostView = view;
191 * FIXME: make utility method in Dialog, so clients use it instead of this getter.
192 * Method should be like Dialog.showModalElement(position params, reposition callback).
193 * @return {?WebInspector.View}
195 WebInspector.Dialog.modalHostView = function()
197 return WebInspector.Dialog._modalHostView;
200 WebInspector.Dialog.modalHostRepositioned = function()
202 if (WebInspector.Dialog._instance)
203 WebInspector.Dialog._instance._position();