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
// Install glass pane capturing events.
42
this._glassPaneElement = document.body.createChild("div");
43
this._glassPaneElement.className = "dialog-glass-pane";
44
this._glassPaneElement.tabIndex = 0;
45
this._glassPaneElement.addEventListener("focus", this._onGlassPaneFocus.bind(this), false);
47
this._element = this._glassPaneElement.createChild("div");
48
this._element.tabIndex = 0;
49
this._element.addEventListener("focus", this._onFocus.bind(this), false);
50
this._element.addEventListener("keydown", this._onKeyDown.bind(this), false);
52
WebInspector.KeyboardShortcut.Keys.Enter.code,
53
WebInspector.KeyboardShortcut.Keys.Esc.code,
56
delegate.show(this._element);
59
this._windowResizeHandler = this._position.bind(this);
60
window.addEventListener("resize", this._windowResizeHandler, true);
62
this._previousFocusElement = WebInspector.currentFocusElement();
63
this._delegate.focus();
67
* @return {WebInspector.Dialog}
69
WebInspector.Dialog.currentInstance = function()
71
return WebInspector.Dialog._instance;
75
* @param {Element} relativeToElement
76
* @param {WebInspector.DialogDelegate} delegate
78
WebInspector.Dialog.show = function(relativeToElement, delegate)
80
if (WebInspector.Dialog._instance)
82
WebInspector.Dialog._instance = new WebInspector.Dialog(relativeToElement, delegate);
85
WebInspector.Dialog.hide = function()
87
if (!WebInspector.Dialog._instance)
89
WebInspector.Dialog._instance._hide();
92
WebInspector.Dialog.prototype = {
97
this._isHiding = true;
99
this._delegate.willHide();
101
if (this._element.isSelfOrAncestor(document.activeElement))
102
WebInspector.setCurrentFocusElement(this._previousFocusElement);
103
delete WebInspector.Dialog._instance;
104
document.body.removeChild(this._glassPaneElement);
105
window.removeEventListener("resize", this._windowResizeHandler, true);
108
_onGlassPaneFocus: function(event)
113
_onFocus: function(event)
115
this._delegate.focus();
118
_position: function()
120
this._delegate.position(this._element, this._relativeToElement);
123
_onKeyDown: function(event)
125
if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Tab.code) {
126
event.preventDefault();
130
if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code)
131
this._delegate.onEnter();
133
if (this._closeKeys.indexOf(event.keyCode) >= 0) {
142
* @extends {WebInspector.Object}
144
WebInspector.DialogDelegate = function()
148
WebInspector.DialogDelegate.prototype = {
150
* @param {Element} element
152
show: function(element)
154
element.appendChild(this.element);
155
this.element.addStyleClass("dialog-contents");
156
element.addStyleClass("dialog");
160
* @param {Element} element
161
* @param {Element} relativeToElement
163
position: function(element, relativeToElement)
165
var offset = relativeToElement.offsetRelativeToWindow(window);
167
var positionX = offset.x + (relativeToElement.offsetWidth - element.offsetWidth) / 2;
168
positionX = Number.constrain(positionX, 0, window.innerWidth - element.offsetWidth);
170
var positionY = offset.y + (relativeToElement.offsetHeight - element.offsetHeight) / 2;
171
positionY = Number.constrain(positionY, 0, window.innerHeight - element.offsetHeight);
173
element.style.left = positionX + "px";
174
element.style.top = positionY + "px";
177
focus: function() { },
179
onEnter: function() { },
181
willHide: function() { },
183
__proto__: WebInspector.Object.prototype