Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / ui / HistoryInput.js
blob54daad1e1836375e4ca2b68186b965b9167d9560
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
3 // found in the LICENSE file.
5 /**
6 * @constructor
7 * @extends {HTMLInputElement}
8 */
9 WebInspector.HistoryInput = function()
13 /**
14 * @return {!WebInspector.HistoryInput}
16 WebInspector.HistoryInput.create = function()
18 if (!WebInspector.HistoryInput._constructor)
19 WebInspector.HistoryInput._constructor = registerCustomElement("input", "history-input", WebInspector.HistoryInput.prototype);
21 return /** @type {!WebInspector.HistoryInput} */(new WebInspector.HistoryInput._constructor());
24 WebInspector.HistoryInput.prototype = {
25 createdCallback: function()
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);
33 /**
34 * @param {!Event} event
36 _onInput: function(event)
38 if (this._history.length === this._historyPosition + 1)
39 this._history[this._history.length - 1] = this.value;
42 /**
43 * @param {!Event} event
45 _onKeyDown: function(event)
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(createEvent("input", true, true));
51 event.consume(true);
52 } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Down.code) {
53 this._historyPosition = Math.min(this._historyPosition + 1, this._history.length - 1);
54 this.value = this._history[this._historyPosition];
55 this.dispatchEvent(createEvent("input", true, true));
56 event.consume(true);
57 } else if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Enter.code) {
58 this._saveToHistory();
62 _saveToHistory: function()
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("");
71 __proto__: HTMLInputElement.prototype