blob: 87dad0820a0b081f0846dea7a3cd41e65b0e3f84 [file] [log] [blame]
Chris Craikb122baf2015-03-05 13:58:42 -08001<!DOCTYPE html>
2<!--
3Copyright (c) 2015 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/base/task.html">
9<link rel="import" href="/core/scripting_controller.html">
10<link rel="import" href="/core/timeline_track_view.html">
Chris Craik44c28202015-05-12 17:25:16 -070011<link rel="import" href="/extras/tquery/tquery.html">
Chris Craikb122baf2015-03-05 13:58:42 -080012
13<polymer-element
14 name="tracing-scripting-control" constructor="TracingScriptingControl">
15 <template>
16 <style>
17 :host {
18 flex: 1 1 auto;
19 }
20 .root {
21 font-family: monospace;
22 cursor: text;
23
24 padding: 2px;
25 margin: 2px;
26 border: 1px solid rgba(0, 0, 0, 0.5);
27 background: white;
28
29 height: 100px;
30 overflow-y: auto;
31
32 transition-property: opacity, height, padding, margin;
33 transition-duration: .2s;
34 transition-timing-function: ease-out;
35 }
36 .hidden {
37 margin-top: 0px;
38 margin-bottom: 0px;
39 padding-top: 0px;
40 padding-bottom: 0px;
41 height: 0px;
42 opacity: 0;
43 }
44 .focused {
45 outline: auto 5px -webkit-focus-ring-color;
46 }
47 #history {
48 -webkit-user-select: text;
49 color: #777;
50 }
51 #prompt {
52 -webkit-user-select: text;
53 -webkit-user-modify: read-write-plaintext-only;
54 text-overflow: clip !important;
55 text-decoration: none !important;
56 }
57 #prompt:focus {
58 outline: none;
59 }
60 #prompt br {
61 display: none;
62 }
63 #prompt ::before {
64 content: ">";
65 color: #468;
66 }
67 </style>
68
69 <div id="root" class="root hidden" tabindex="0"
70 on-focus="{{ onConsoleFocus }}">
71 <div id='history'></div>
72 <div id='prompt'
73 on-keypress="{{ promptKeyPress }}"
74 on-keydown="{{ promptKeyDown }}"
75 on-blur="{{ onConsoleBlur }}">
76 </template>
77
78 <script>
79 'use strict';
80
81 Polymer({
82 _isEnterKey: function(event) {
83 // Check if in IME.
84 return event.keyCode !== 229 && event.keyIdentifier === 'Enter';
85 },
86
87 _setFocused: function(focused) {
88 var promptEl = this.$.prompt;
89 if (focused) {
90 promptEl.focus();
91 this.$.root.classList.add('focused');
92 // Move cursor to the end of any existing text.
93 if (promptEl.innerText.length > 0) {
94 var sel = window.getSelection();
95 sel.collapse(promptEl.firstChild, promptEl.innerText.length);
96 }
97 } else {
98 promptEl.blur();
99 this.$.root.classList.remove('focused');
100 // Workaround for crbug.com/89026 to ensure the prompt doesn't retain
101 // keyboard focus.
Chris Craik19832152015-04-16 15:43:38 -0700102 var parent = promptEl.parentElement;
103 var nextEl = promptEl.nextSibling;
104 promptEl.remove();
105 parent.insertBefore(promptEl, nextEl);
Chris Craikb122baf2015-03-05 13:58:42 -0800106 }
107 },
108
109 onConsoleFocus: function(e) {
110 e.stopPropagation();
111 this._setFocused(true);
112 },
113
114 onConsoleBlur: function(e) {
115 e.stopPropagation();
116 this._setFocused(false);
117 },
118
119 promptKeyDown: function(e) {
120 e.stopPropagation();
121 if (!this._isEnterKey(e))
122 return;
123 var promptEl = this.$.prompt;
124 var command = promptEl.innerText;
125 if (command.length === 0)
126 return;
127 promptEl.innerText = '';
128 this.addLine_(String.fromCharCode(187) + ' ' + command);
129
130 try {
131 var result = this.controller_.executeCommand(command);
132 } catch (e) {
133 result = e.stack || e.stackTrace;
134 }
135
136 if (result instanceof tv.b.Task) {
137 // TODO(skyostil): Show a cool spinner.
138 tv.b.Task.RunWhenIdle(result);
139 } else {
140 this.addLine_(result);
141 }
142 },
143
144 addLine_: function(line) {
145 var historyEl = this.$.history;
146 if (historyEl.innerText.length !== 0)
147 historyEl.innerText += '\n';
148 historyEl.innerText += line;
149 },
150
151 promptKeyPress: function(e) {
152 e.stopPropagation();
153 },
154
155 toggleVisibility: function() {
156 var root = this.$.root;
157 if (!this.visible) {
158 root.classList.remove('hidden');
159 this._setFocused(true);
160 } else {
161 root.classList.add('hidden');
162 this._setFocused(false);
163 }
164 },
165
166 get hasFocus() {
167 return this === document.activeElement;
168 },
169
170 get visible() {
171 var root = this.$.root;
172 return !root.classList.contains('hidden');
173 },
174
175 get controller() {
176 return this.controller_;
177 },
178
179 set controller(c) {
180 this.controller_ = c;
181 }
182 });
183 </script>
184</polymer-element>