1/*
2 * Copyright (C) 2013 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26WebInspector.SearchBar = function(identifier, placeholder, delegate) {
27    WebInspector.NavigationItem.call(this, identifier);
28
29    this.delegate = delegate;
30
31    this._element.classList.add(WebInspector.SearchBar.StyleClassName);
32
33    this._keyboardShortcutEsc = new WebInspector.KeyboardShortcut(null, WebInspector.KeyboardShortcut.Key.Escape);
34    this._keyboardShortcutEnter = new WebInspector.KeyboardShortcut(null, WebInspector.KeyboardShortcut.Key.Enter);
35
36    this._searchInput = this._element.appendChild(document.createElement("input"));
37    this._searchInput.type = "search";
38    this._searchInput.spellcheck = false;
39    this._searchInput.incremental = true;
40    this._searchInput.setAttribute("results", 5);
41    this._searchInput.setAttribute("autosave", identifier + "-autosave");
42    this._searchInput.setAttribute("placeholder", placeholder);
43    this._searchInput.addEventListener("search", this._handleSearchEvent.bind(this), false);
44    this._searchInput.addEventListener("keydown", this._handleKeydownEvent.bind(this), false);
45};
46
47WebInspector.SearchBar.StyleClassName = "search-bar";
48WebInspector.SearchBar.Event = {
49    TextChanged: "searchbar-text-did-change"
50};
51
52WebInspector.SearchBar.prototype = {
53    constructor: WebInspector.SearchBar,
54
55    // Public
56
57    get text()
58    {
59        return this._searchInput.value;
60    },
61
62    set text(newText)
63    {
64        this._searchInput.value = newText;
65    },
66
67    focus: function()
68    {
69        this._searchInput.focus();
70        this._searchInput.select();
71    },
72
73    // Private
74
75    _handleSearchEvent: function(event)
76    {
77        this.dispatchEventToListeners(WebInspector.SearchBar.Event.TextChanged);
78    },
79
80    _handleKeydownEvent: function(event)
81    {
82        if (this._keyboardShortcutEsc.matchesEvent(event)) {
83            if (this.delegate && typeof this.delegate.searchBarWantsToLoseFocus === "function") {
84                this.delegate.searchBarWantsToLoseFocus(this);
85                event.stopPropagation();
86                event.preventDefault();
87            }
88        } else if (this._keyboardShortcutEnter.matchesEvent(event)) {
89            if (this.delegate && typeof this.delegate.searchBarDidActivate === "function") {
90                this.delegate.searchBarDidActivate(this);
91                event.stopPropagation();
92                event.preventDefault();
93            }
94        }
95    }
96};
97
98WebInspector.SearchBar.prototype.__proto__ = WebInspector.NavigationItem.prototype;
99