WebInspector.Dialog = function(relativeToElement, delegate)
{
this._delegate = delegate;
this._relativeToElement = relativeToElement;
this._glassPane = new WebInspector.GlassPane();
this._glassPane.element.tabIndex = 0;
this._glassPane.element.addEventListener("focus", this._onGlassPaneFocus.bind(this), false);
this._element = this._glassPane.element.createChild("div");
this._element.tabIndex = 0;
this._element.addEventListener("focus", this._onFocus.bind(this), false);
this._element.addEventListener("keydown", this._onKeyDown.bind(this), false);
this._closeKeys = [
WebInspector.KeyboardShortcut.Keys.Enter.code,
WebInspector.KeyboardShortcut.Keys.Esc.code,
];
delegate.show(this._element);
this._position();
this._windowResizeHandler = this._position.bind(this);
window.addEventListener("resize", this._windowResizeHandler, true);
this._delegate.focus();
}
WebInspector.Dialog.currentInstance = function()
{
return WebInspector.Dialog._instance;
}
WebInspector.Dialog.show = function(relativeToElement, delegate)
{
if (WebInspector.Dialog._instance)
return;
WebInspector.Dialog._instance = new WebInspector.Dialog(relativeToElement, delegate);
}
WebInspector.Dialog.hide = function()
{
if (!WebInspector.Dialog._instance)
return;
WebInspector.Dialog._instance._hide();
}
WebInspector.Dialog.prototype = {
_hide: function()
{
if (this._isHiding)
return;
this._isHiding = true;
this._delegate.willHide();
delete WebInspector.Dialog._instance;
this._glassPane.dispose();
window.removeEventListener("resize", this._windowResizeHandler, true);
},
_onGlassPaneFocus: function(event)
{
this._hide();
},
_onFocus: function(event)
{
this._delegate.focus();
},
_position: function()
{
this._delegate.position(this._element, this._relativeToElement);
},
_onKeyDown: function(event)
{
if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Tab.code) {
event.preventDefault();
return;
}
if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code)
this._delegate.onEnter();
if (this._closeKeys.indexOf(event.keyCode) >= 0) {
this._hide();
event.consume(true);
}
}
};
WebInspector.DialogDelegate = function()
{
}
WebInspector.DialogDelegate.prototype = {
show: function(element)
{
element.appendChild(this.element);
this.element.addStyleClass("dialog-contents");
element.addStyleClass("dialog");
},
position: function(element, relativeToElement)
{
var offset = relativeToElement.offsetRelativeToWindow(window);
var positionX = offset.x + (relativeToElement.offsetWidth - element.offsetWidth) / 2;
positionX = Number.constrain(positionX, 0, window.innerWidth - element.offsetWidth);
var positionY = offset.y + (relativeToElement.offsetHeight - element.offsetHeight) / 2;
positionY = Number.constrain(positionY, 0, window.innerHeight - element.offsetHeight);
element.style.left = positionX + "px";
element.style.top = positionY + "px";
},
focus: function() { },
onEnter: function() { },
willHide: function() { },
__proto__: WebInspector.Object.prototype
}