OLD | NEW |
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** |
| 5 * @unrestricted |
| 6 */ |
| 7 WebInspector.HistoryInput = class extends HTMLInputElement { |
| 8 constructor() { |
| 9 super(); |
| 10 } |
4 | 11 |
5 /** | 12 /** |
6 * @constructor | 13 * @return {!WebInspector.HistoryInput} |
7 * @extends {HTMLInputElement} | 14 */ |
8 */ | 15 static create() { |
9 WebInspector.HistoryInput = function() | 16 if (!WebInspector.HistoryInput._constructor) |
10 { | 17 WebInspector.HistoryInput._constructor = |
| 18 registerCustomElement('input', 'history-input', WebInspector.HistoryIn
put.prototype); |
| 19 |
| 20 return /** @type {!WebInspector.HistoryInput} */ (new WebInspector.HistoryIn
put._constructor()); |
| 21 } |
| 22 |
| 23 /** |
| 24 * @override |
| 25 */ |
| 26 createdCallback() { |
| 27 this._history = ['']; |
| 28 this._historyPosition = 0; |
| 29 this.addEventListener('keydown', this._onKeyDown.bind(this), false); |
| 30 this.addEventListener('input', this._onInput.bind(this), false); |
| 31 } |
| 32 |
| 33 /** |
| 34 * @param {!Event} event |
| 35 */ |
| 36 _onInput(event) { |
| 37 if (this._history.length === this._historyPosition + 1) |
| 38 this._history[this._history.length - 1] = this.value; |
| 39 } |
| 40 |
| 41 /** |
| 42 * @param {!Event} event |
| 43 */ |
| 44 _onKeyDown(event) { |
| 45 if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Up.code) { |
| 46 this._historyPosition = Math.max(this._historyPosition - 1, 0); |
| 47 this.value = this._history[this._historyPosition]; |
| 48 this.dispatchEvent(new Event('input', {'bubbles': true, 'cancelable': true
})); |
| 49 event.consume(true); |
| 50 } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Down.code) { |
| 51 this._historyPosition = Math.min(this._historyPosition + 1, this._history.
length - 1); |
| 52 this.value = this._history[this._historyPosition]; |
| 53 this.dispatchEvent(new Event('input', {'bubbles': true, 'cancelable': true
})); |
| 54 event.consume(true); |
| 55 } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code)
{ |
| 56 this._saveToHistory(); |
| 57 } |
| 58 } |
| 59 |
| 60 _saveToHistory() { |
| 61 if (this._history.length > 1 && this._history[this._history.length - 2] ===
this.value) |
| 62 return; |
| 63 this._history[this._history.length - 1] = this.value; |
| 64 this._historyPosition = this._history.length - 1; |
| 65 this._history.push(''); |
| 66 } |
11 }; | 67 }; |
12 | 68 |
13 /** | |
14 * @return {!WebInspector.HistoryInput} | |
15 */ | |
16 WebInspector.HistoryInput.create = function() | |
17 { | |
18 if (!WebInspector.HistoryInput._constructor) | |
19 WebInspector.HistoryInput._constructor = registerCustomElement("input",
"history-input", WebInspector.HistoryInput.prototype); | |
20 | 69 |
21 return /** @type {!WebInspector.HistoryInput} */(new WebInspector.HistoryInp
ut._constructor()); | |
22 }; | |
23 | |
24 WebInspector.HistoryInput.prototype = { | |
25 createdCallback: function() | |
26 { | |
27 this._history = [""]; | |
28 this._historyPosition = 0; | |
29 this.addEventListener("keydown", this._onKeyDown.bind(this), false); | |
30 this.addEventListener("input", this._onInput.bind(this), false); | |
31 }, | |
32 | |
33 /** | |
34 * @param {!Event} event | |
35 */ | |
36 _onInput: function(event) | |
37 { | |
38 if (this._history.length === this._historyPosition + 1) | |
39 this._history[this._history.length - 1] = this.value; | |
40 }, | |
41 | |
42 /** | |
43 * @param {!Event} event | |
44 */ | |
45 _onKeyDown: function(event) | |
46 { | |
47 if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Up.code) { | |
48 this._historyPosition = Math.max(this._historyPosition - 1, 0); | |
49 this.value = this._history[this._historyPosition]; | |
50 this.dispatchEvent(new Event("input", {"bubbles": true, "cancelable"
: true})); | |
51 event.consume(true); | |
52 } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Down.cod
e) { | |
53 this._historyPosition = Math.min(this._historyPosition + 1, this._hi
story.length - 1); | |
54 this.value = this._history[this._historyPosition]; | |
55 this.dispatchEvent(new Event("input", {"bubbles": true, "cancelable"
: true})); | |
56 event.consume(true); | |
57 } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.co
de) { | |
58 this._saveToHistory(); | |
59 } | |
60 }, | |
61 | |
62 _saveToHistory: function() | |
63 { | |
64 if (this._history.length > 1 && this._history[this._history.length - 2]
=== this.value) | |
65 return; | |
66 this._history[this._history.length - 1] = this.value; | |
67 this._historyPosition = this._history.length - 1; | |
68 this._history.push(""); | |
69 }, | |
70 | |
71 __proto__: HTMLInputElement.prototype | |
72 }; | |
73 | |
OLD | NEW |