~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/inspector/front-end/Dialog.js

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2012 Google Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are
 
6
 * met:
 
7
 *
 
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
 
13
 * distribution.
 
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.
 
17
 *
 
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.
 
29
 */
 
30
 
 
31
/**
 
32
 * @constructor
 
33
 * @param {Element} relativeToElement
 
34
 * @param {WebInspector.DialogDelegate} delegate
 
35
 */
 
36
WebInspector.Dialog = function(relativeToElement, delegate)
 
37
{
 
38
    this._delegate = delegate;
 
39
    this._relativeToElement = relativeToElement;
 
40
 
 
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);
 
46
 
 
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);
 
51
    this._closeKeys = [
 
52
        WebInspector.KeyboardShortcut.Keys.Enter.code,
 
53
        WebInspector.KeyboardShortcut.Keys.Esc.code,
 
54
    ];
 
55
 
 
56
    delegate.show(this._element);
 
57
 
 
58
    this._position();
 
59
    this._windowResizeHandler = this._position.bind(this);
 
60
    window.addEventListener("resize", this._windowResizeHandler, true);
 
61
 
 
62
    this._previousFocusElement = WebInspector.currentFocusElement();
 
63
    this._delegate.focus();
 
64
}
 
65
 
 
66
/**
 
67
 * @return {WebInspector.Dialog}
 
68
 */
 
69
WebInspector.Dialog.currentInstance = function()
 
70
{
 
71
    return WebInspector.Dialog._instance;
 
72
}
 
73
 
 
74
/**
 
75
 * @param {Element} relativeToElement
 
76
 * @param {WebInspector.DialogDelegate} delegate
 
77
 */
 
78
WebInspector.Dialog.show = function(relativeToElement, delegate)
 
79
{
 
80
    if (WebInspector.Dialog._instance)
 
81
        return;
 
82
    WebInspector.Dialog._instance = new WebInspector.Dialog(relativeToElement, delegate);
 
83
}
 
84
 
 
85
WebInspector.Dialog.hide = function()
 
86
{
 
87
    if (!WebInspector.Dialog._instance)
 
88
        return;
 
89
    WebInspector.Dialog._instance._hide();
 
90
}
 
91
 
 
92
WebInspector.Dialog.prototype = {
 
93
    _hide: function()
 
94
    {
 
95
        if (this._isHiding)
 
96
            return;
 
97
        this._isHiding = true;
 
98
 
 
99
        this._delegate.willHide();
 
100
 
 
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);
 
106
    },
 
107
 
 
108
    _onGlassPaneFocus: function(event)
 
109
    {
 
110
        this._hide();
 
111
    },
 
112
 
 
113
    _onFocus: function(event)
 
114
    {
 
115
        this._delegate.focus();
 
116
    },
 
117
 
 
118
    _position: function()
 
119
    {
 
120
        this._delegate.position(this._element, this._relativeToElement);
 
121
    },
 
122
 
 
123
    _onKeyDown: function(event)
 
124
    {
 
125
        if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Tab.code) {
 
126
            event.preventDefault();
 
127
            return;
 
128
        }
 
129
 
 
130
        if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code)
 
131
            this._delegate.onEnter();
 
132
 
 
133
        if (this._closeKeys.indexOf(event.keyCode) >= 0) {
 
134
            this._hide();
 
135
            event.consume(true);
 
136
        }
 
137
    }
 
138
};
 
139
 
 
140
/**
 
141
 * @constructor
 
142
 * @extends {WebInspector.Object}
 
143
 */
 
144
WebInspector.DialogDelegate = function()
 
145
{
 
146
}
 
147
 
 
148
WebInspector.DialogDelegate.prototype = {
 
149
    /**
 
150
     * @param {Element} element
 
151
     */
 
152
    show: function(element)
 
153
    {
 
154
        element.appendChild(this.element);
 
155
        this.element.addStyleClass("dialog-contents");
 
156
        element.addStyleClass("dialog");    
 
157
    },
 
158
 
 
159
    /**
 
160
     * @param {Element} element
 
161
     * @param {Element} relativeToElement
 
162
     */
 
163
    position: function(element, relativeToElement)
 
164
    {
 
165
        var offset = relativeToElement.offsetRelativeToWindow(window);
 
166
 
 
167
        var positionX = offset.x + (relativeToElement.offsetWidth - element.offsetWidth) / 2;
 
168
        positionX = Number.constrain(positionX, 0, window.innerWidth - element.offsetWidth);
 
169
 
 
170
        var positionY = offset.y + (relativeToElement.offsetHeight - element.offsetHeight) / 2;
 
171
        positionY = Number.constrain(positionY, 0, window.innerHeight - element.offsetHeight);
 
172
 
 
173
        element.style.left = positionX + "px";
 
174
        element.style.top = positionY + "px";
 
175
    },
 
176
 
 
177
    focus: function() { },
 
178
 
 
179
    onEnter: function() { },
 
180
 
 
181
    willHide: function() { },
 
182
 
 
183
    __proto__: WebInspector.Object.prototype
 
184
}
 
185