OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 /** | |
6 * @fileoverview Helper functions and objects for the JS debugger UI. | |
7 * @see debugger.html | |
8 */ | |
9 | |
10 /** | |
11 * Document load listener. | |
12 */ | |
13 function onLoad() { | |
14 var debuggerConsole = new DebuggerConsole(); | |
15 DebuggerIPC.init(debuggerConsole); | |
16 DebugShell.initDebugShell(debuggerConsole); | |
17 debuggerConsole.focusOnCommandLine(); | |
18 } | |
19 | |
20 /** | |
21 * @constructor | |
22 */ | |
23 function DebuggerConsole() | |
24 { | |
25 this._output = document.getElementById("output"); | |
26 | |
27 var input = document.getElementById("command-line-text"); | |
28 var self = this; | |
29 input.addEventListener( | |
30 'keydown', | |
31 function(e) { | |
32 return self._onInputKeyDown(e); | |
33 }, | |
34 true); | |
35 input.addEventListener( | |
36 'keypress', | |
37 function(e) { | |
38 return self._onInputKeyPress(e); | |
39 }, | |
40 true); | |
41 this._commandLineInput = input; | |
42 | |
43 // command object stores command-line history state. | |
44 this._command = { | |
45 history: [], | |
46 history_index: 0, | |
47 pending: null | |
48 }; | |
49 }; | |
50 | |
51 DebuggerConsole.prototype = { | |
52 /** | |
53 * Sets focus to command-line-text element. | |
54 */ | |
55 focusOnCommandLine: function() { | |
56 this._commandLineInput.focus(); | |
57 }, | |
58 | |
59 /** | |
60 * Called by chrome code when there's output to display. | |
61 * @param {string} txt | |
62 */ | |
63 appendText: function(txt) | |
64 { | |
65 this._output.appendChild(document.createTextNode(txt)); | |
66 this._output.appendChild(document.createElement('br')); | |
67 document.body.scrollTop = document.body.scrollHeight; | |
68 }, | |
69 | |
70 /** | |
71 * Called by chrome code to set the current state as to whether the debugger | |
72 * is stopped at a breakpoint or is running. | |
73 * @param {boolean} isBroken | |
74 */ | |
75 setDebuggerBreak: function(isBroken) | |
76 { | |
77 var out = this._output; | |
78 if (isBroken) { | |
79 out.style.color = "black"; | |
80 this.focusOnCommandLine(); | |
81 } else { | |
82 out.style.color = "gray"; | |
83 } | |
84 }, | |
85 | |
86 /** | |
87 * Execute a debugger command, add it to the command history and display it
in | |
88 * the output window. | |
89 * @param {string} str | |
90 */ | |
91 executeCommand: function(str) { | |
92 this.appendText("$ " + str); | |
93 // Sends message to DebuggerContents.HandleCommand. | |
94 if (DebugShell.singleton) { | |
95 DebugShell.singleton.command(str); | |
96 } else { | |
97 this.appendText("FAILED to send the command as DebugShell is null"); | |
98 } | |
99 | |
100 this._command.history.push(str); | |
101 this._command.history_index = this._command.history.length; | |
102 this._command.pending = null; | |
103 }, | |
104 | |
105 | |
106 /** | |
107 * Display the previous history item in the given text field. | |
108 * @param {HTMLInputElement} field | |
109 */ | |
110 selectPreviousCommand_: function(field) { | |
111 var command = this._command; | |
112 if (command.history_index > 0) { | |
113 // Remember the current field value as a pending command if we're at
the | |
114 // end (it's something the user typed in). | |
115 if (command.history_index == command.history.length) | |
116 command.pending = field.value; | |
117 command.history_index--; | |
118 field.value = command.history[command.history_index]; | |
119 field.select(); | |
120 } | |
121 }, | |
122 | |
123 /** | |
124 * Display the next history item in the given text field. | |
125 * @param {HTMLInputElement} field | |
126 */ | |
127 selectNextCommand_: function(field) { | |
128 var command = this._command; | |
129 if (command.history_index < command.history.length) { | |
130 command.history_index++; | |
131 if (command.history_index == command.history.length) { | |
132 field.value = command.pending || ""; | |
133 } else { | |
134 field.value = command.history[command.history_index]; | |
135 } | |
136 field.select(); | |
137 } | |
138 }, | |
139 | |
140 /** | |
141 * command-line-text's onkeydown handler. | |
142 * @param {KeyboardEvent} e | |
143 * @return {boolean} | |
144 */ | |
145 _onInputKeyDown: function (e) { | |
146 var field = e.target; | |
147 var key = e.keyCode; | |
148 if (key == 38) { // up arrow | |
149 this.selectPreviousCommand_(field); | |
150 return false; | |
151 } else if (key == 40) { // down arrow | |
152 this.selectNextCommand_(field); | |
153 return false; | |
154 } | |
155 return true; | |
156 }, | |
157 | |
158 /** | |
159 * command-line-text's onkeypress handler. | |
160 * @param {KeyboardEvent} e | |
161 * @return {boolean} | |
162 */ | |
163 _onInputKeyPress: function (e) { | |
164 var field = e.target; | |
165 var key = e.keyCode; | |
166 if (key == 13) { // enter | |
167 this.executeCommand(field.value); | |
168 field.value = ""; | |
169 return false; | |
170 } | |
171 return true; | |
172 } | |
173 }; | |
OLD | NEW |