1 // Copyright (c) 2012 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 cr.define('print_preview', function() {
9 * Component that renders a search box for searching through destinations.
11 * @extends {print_preview.Component}
13 function SearchBox() {
14 print_preview.Component.call(this);
17 * Timeout used to control incremental search.
24 * Input box where the query is entered.
25 * @type {HTMLInputElement}
32 * Enumeration of event types dispatched from the search box.
35 SearchBox.EventType = {
36 SEARCH: 'print_preview.SearchBox.SEARCH'
40 * CSS classes used by the search box.
44 SearchBox.Classes_ = {
45 INPUT: 'search-box-input'
49 * Delay in milliseconds before dispatching a SEARCH event.
54 SearchBox.SEARCH_DELAY_ = 150;
56 SearchBox.prototype = {
57 __proto__: print_preview.Component.prototype,
59 /** @param {string} New query to set the search box's query to. */
60 setQuery: function(query) {
62 this.input_.value = query.trim();
65 /** Sets the input element of the search box in focus. */
71 enterDocument: function() {
72 print_preview.Component.prototype.enterDocument.call(this);
73 this.tracker.add(this.input_, 'input', this.onInputInput_.bind(this));
77 exitDocument: function() {
78 print_preview.Component.prototype.exitDocument.call(this);
83 decorateInternal: function() {
84 this.input_ = this.getElement().getElementsByClassName(
85 SearchBox.Classes_.INPUT)[0];
89 * @return {string} The current query of the search box.
92 getQuery_: function() {
93 return this.input_.value.trim();
97 * Dispatches a SEARCH event.
100 dispatchSearchEvent_: function() {
101 this.timeout_ = null;
102 var searchEvent = new Event(SearchBox.EventType.SEARCH);
103 searchEvent.query = this.getQuery_();
104 this.dispatchEvent(searchEvent);
108 * Called when the input element's value changes. Dispatches a search event.
111 onInputInput_: function() {
113 clearTimeout(this.timeout_);
115 this.timeout_ = setTimeout(
116 this.dispatchSearchEvent_.bind(this), SearchBox.SEARCH_DELAY_);