Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / Dialog.js
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     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);
45
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);
50     this._closeKeys = [
51         WebInspector.KeyboardShortcut.Keys.Enter.code,
52         WebInspector.KeyboardShortcut.Keys.Esc.code,
53     ];
54
55     delegate.show(this._element);
56
57     this._position();
58     this._delegate.focus();
59 }
60
61 /**
62  * @return {?WebInspector.Dialog}
63  */
64 WebInspector.Dialog.currentInstance = function()
65 {
66     return WebInspector.Dialog._instance;
67 }
68
69 /**
70  * @param {!Element} relativeToElement
71  * @param {!WebInspector.DialogDelegate} delegate
72  */
73 WebInspector.Dialog.show = function(relativeToElement, delegate)
74 {
75     if (WebInspector.Dialog._instance)
76         return;
77     WebInspector.Dialog._instance = new WebInspector.Dialog(relativeToElement, delegate);
78 }
79
80 WebInspector.Dialog.hide = function()
81 {
82     if (!WebInspector.Dialog._instance)
83         return;
84     WebInspector.Dialog._instance._hide();
85 }
86
87 WebInspector.Dialog.prototype = {
88     _hide: function()
89     {
90         if (this._isHiding)
91             return;
92         this._isHiding = true;
93
94         this._delegate.willHide();
95
96         delete WebInspector.Dialog._instance;
97         this._glassPane.dispose();
98     },
99
100     _onGlassPaneFocus: function(event)
101     {
102         this._hide();
103     },
104
105     _onFocus: function(event)
106     {
107         this._delegate.focus();
108     },
109
110     _position: function()
111     {
112         this._delegate.position(this._element, this._relativeToElement);
113     },
114
115     _onKeyDown: function(event)
116     {
117         if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Tab.code) {
118             event.preventDefault();
119             return;
120         }
121
122         if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code)
123             this._delegate.onEnter();
124
125         if (this._closeKeys.indexOf(event.keyCode) >= 0) {
126             this._hide();
127             event.consume(true);
128         }
129     }
130 };
131
132 /**
133  * @constructor
134  * @extends {WebInspector.Object}
135  */
136 WebInspector.DialogDelegate = function()
137 {
138     /** @type {!Element} */
139     this.element;
140 }
141
142 WebInspector.DialogDelegate.prototype = {
143     /**
144      * @param {!Element} element
145      */
146     show: function(element)
147     {
148         element.appendChild(this.element);
149         this.element.classList.add("dialog-contents");
150         element.classList.add("dialog");
151     },
152
153     /**
154      * @param {!Element} element
155      * @param {!Element} relativeToElement
156      */
157     position: function(element, relativeToElement)
158     {
159         var container = WebInspector.Dialog._modalHostView.element;
160         var box = relativeToElement.boxInWindow(window).relativeToElement(container);
161
162         var positionX = box.x + (relativeToElement.offsetWidth - element.offsetWidth) / 2;
163         positionX = Number.constrain(positionX, 0, container.offsetWidth - element.offsetWidth);
164
165         var positionY = box.y + (relativeToElement.offsetHeight - element.offsetHeight) / 2;
166         positionY = Number.constrain(positionY, 0, container.offsetHeight - element.offsetHeight);
167
168         element.style.position = "absolute";
169         element.positionAt(positionX, positionY, container);
170     },
171
172     focus: function() { },
173
174     onEnter: function() { },
175
176     willHide: function() { },
177
178     __proto__: WebInspector.Object.prototype
179 }
180
181 /** @type {?WebInspector.View} */
182 WebInspector.Dialog._modalHostView = null;
183
184 /**
185  * @param {!WebInspector.View} view
186  */
187 WebInspector.Dialog.setModalHostView = function(view)
188 {
189     WebInspector.Dialog._modalHostView = view;
190 };
191
192 /**
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}
196  */
197 WebInspector.Dialog.modalHostView = function()
198 {
199     return WebInspector.Dialog._modalHostView;
200 };
201
202 WebInspector.Dialog.modalHostRepositioned = function()
203 {
204     if (WebInspector.Dialog._instance)
205         WebInspector.Dialog._instance._position();
206 };
207