Initial patch.
[vuplus_webkit] / Source / WebCore / inspector / front-end / GoToLineDialog.js
1 /*
2  * Copyright (C) 2010 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 WebInspector.GoToLineDialog = function(view)
32 {
33     this._element = document.createElement("div");
34     this._element.className = "go-to-line-dialog";
35     this._element.addEventListener("keydown", this._onKeyDown.bind(this), false);
36     this._closeKeys = [
37         WebInspector.KeyboardShortcut.Keys.Enter.code,
38         WebInspector.KeyboardShortcut.Keys.Esc.code,
39     ];
40
41     var dialogWindow = this._element;
42
43     dialogWindow.createChild("label").textContent = WebInspector.UIString("Go to line: ");
44
45     this._input = dialogWindow.createChild("input");
46     this._input.setAttribute("type", "text");
47     this._input.setAttribute("size", 6);
48     var blurHandler = this._onBlur.bind(this);
49     this._input.addEventListener("blur", blurHandler, false);
50
51
52     var go = dialogWindow.createChild("button");
53     go.textContent = WebInspector.UIString("Go");
54     go.addEventListener("click", this._onClick.bind(this), false);
55     go.addEventListener("mousedown", function(e) {
56         // Ok button click will close the dialog, removing onBlur listener
57         // to let click event be handled.
58         this._input.removeEventListener("blur", blurHandler, false);
59     }.bind(this), false);
60
61     this._view = view;
62     view.element.appendChild(this._element);
63
64     this._previousFocusElement = WebInspector.currentFocusElement;
65     WebInspector.currentFocusElement = this._input;
66     this._input.select();
67 }
68
69 WebInspector.GoToLineDialog.show = function(sourceView)
70 {
71     if (!sourceView || typeof sourceView.highlightLine !== "function")
72         return;
73     if (this._instance)
74         return;
75     this._instance = new WebInspector.GoToLineDialog(sourceView);
76 }
77
78 WebInspector.GoToLineDialog.createShortcut = function()
79 {
80     var isMac = WebInspector.isMac();
81     var shortcut;
82     if (isMac)
83         return WebInspector.KeyboardShortcut.makeDescriptor("l", WebInspector.KeyboardShortcut.Modifiers.Meta);
84     return WebInspector.KeyboardShortcut.makeDescriptor("g", WebInspector.KeyboardShortcut.Modifiers.Ctrl);
85 }
86
87 WebInspector.GoToLineDialog.prototype = {
88     _hide: function()
89     {
90         if (this._isHiding)
91             return;
92         this._isHiding = true;
93
94         WebInspector.currentFocusElement = this._previousFocusElement;
95         WebInspector.GoToLineDialog._instance = null;
96         this._element.parentElement.removeChild(this._element);
97     },
98
99     _onBlur: function(event)
100     {
101         this._hide();
102     },
103
104     _onKeyDown: function(event)
105     {
106         if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Tab.code) {
107             event.preventDefault();
108             return;
109         }
110
111         if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code)
112             this._highlightSelectedLine();
113
114         if (this._closeKeys.indexOf(event.keyCode) >= 0) {
115             this._hide();
116             event.preventDefault();
117             event.stopPropagation();
118         }
119     },
120
121     _onClick: function(event)
122     {
123         this._highlightSelectedLine();
124         this._hide();
125     },
126
127     _highlightSelectedLine: function()
128     {
129         var value = this._input.value;
130         var lineNumber = parseInt(value, 10) - 1;
131         if (!isNaN(lineNumber) && lineNumber >= 0)
132             this._view.highlightLine(lineNumber);
133     }
134 };