OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 | 4 |
5 cr.define('print_preview', function() { | 5 cr.define('print_preview', function() { |
6 'use strict'; | 6 'use strict'; |
7 | 7 |
8 /** | 8 /** |
9 * Component that renders a search box for searching through destinations. | 9 * Component that renders a search box for searching through destinations. |
| 10 * @param {string} searchBoxPlaceholderText Search box placeholder text. |
10 * @constructor | 11 * @constructor |
11 * @extends {print_preview.Component} | 12 * @extends {print_preview.Component} |
12 */ | 13 */ |
13 function SearchBox() { | 14 function SearchBox(searchBoxPlaceholderText) { |
14 print_preview.Component.call(this); | 15 print_preview.Component.call(this); |
15 | 16 |
16 /** | 17 /** |
| 18 * Search box placeholder text. |
| 19 * @private {string} |
| 20 */ |
| 21 this.searchBoxPlaceholderText_ = searchBoxPlaceholderText; |
| 22 |
| 23 /** |
17 * Timeout used to control incremental search. | 24 * Timeout used to control incremental search. |
18 * @type {?number} | 25 * @private {?number} |
19 * @private | |
20 */ | 26 */ |
21 this.timeout_ = null; | 27 this.timeout_ = null; |
22 | 28 |
23 /** | 29 /** |
24 * Input box where the query is entered. | 30 * Input box where the query is entered. |
25 * @type {HTMLInputElement} | 31 * @private {HTMLInputElement} |
26 * @private | |
27 */ | 32 */ |
28 this.input_ = null; | 33 this.input_ = null; |
29 }; | 34 }; |
30 | 35 |
31 /** | 36 /** |
32 * Enumeration of event types dispatched from the search box. | 37 * Enumeration of event types dispatched from the search box. |
33 * @enum {string} | 38 * @enum {string} |
34 */ | 39 */ |
35 SearchBox.EventType = { | 40 SearchBox.EventType = { |
36 SEARCH: 'print_preview.SearchBox.SEARCH' | 41 SEARCH: 'print_preview.SearchBox.SEARCH' |
37 }; | 42 }; |
38 | 43 |
39 /** | 44 /** |
40 * CSS classes used by the search box. | |
41 * @enum {string} | |
42 * @private | |
43 */ | |
44 SearchBox.Classes_ = { | |
45 INPUT: 'search-box-input' | |
46 }; | |
47 | |
48 /** | |
49 * Delay in milliseconds before dispatching a SEARCH event. | 45 * Delay in milliseconds before dispatching a SEARCH event. |
50 * @type {number} | 46 * @private {number} |
51 * @const | 47 * @const |
52 * @private | |
53 */ | 48 */ |
54 SearchBox.SEARCH_DELAY_ = 150; | 49 SearchBox.SEARCH_DELAY_ = 150; |
55 | 50 |
56 SearchBox.prototype = { | 51 SearchBox.prototype = { |
57 __proto__: print_preview.Component.prototype, | 52 __proto__: print_preview.Component.prototype, |
58 | 53 |
59 /** @param {string} New query to set the search box's query to. */ | 54 /** @param {string} New query to set the search box's query to. */ |
60 setQuery: function(query) { | 55 setQuery: function(query) { |
61 query = query || ''; | 56 query = query || ''; |
62 this.input_.value = query.trim(); | 57 this.input_.value = query.trim(); |
63 }, | 58 }, |
64 | 59 |
65 /** Sets the input element of the search box in focus. */ | 60 /** Sets the input element of the search box in focus. */ |
66 focus: function() { | 61 focus: function() { |
67 this.input_.focus(); | 62 this.input_.focus(); |
68 }, | 63 }, |
69 | 64 |
70 /** @override */ | 65 /** @override */ |
| 66 createDom: function() { |
| 67 this.setElementInternal(this.cloneTemplateInternal( |
| 68 'search-box-template')); |
| 69 this.input_ = this.getChildElement('.search-box-input'); |
| 70 this.input_.setAttribute('placeholder', this.searchBoxPlaceholderText_); |
| 71 }, |
| 72 |
| 73 /** @override */ |
71 enterDocument: function() { | 74 enterDocument: function() { |
72 print_preview.Component.prototype.enterDocument.call(this); | 75 print_preview.Component.prototype.enterDocument.call(this); |
73 this.tracker.add(this.input_, 'input', this.onInputInput_.bind(this)); | 76 this.tracker.add(this.input_, 'input', this.onInputInput_.bind(this)); |
74 }, | 77 }, |
75 | 78 |
76 /** @override */ | 79 /** @override */ |
77 exitDocument: function() { | 80 exitDocument: function() { |
78 print_preview.Component.prototype.exitDocument.call(this); | 81 print_preview.Component.prototype.exitDocument.call(this); |
79 this.input_ = null; | 82 this.input_ = null; |
80 }, | 83 }, |
81 | 84 |
82 /** @override */ | |
83 decorateInternal: function() { | |
84 this.input_ = this.getElement().getElementsByClassName( | |
85 SearchBox.Classes_.INPUT)[0]; | |
86 }, | |
87 | |
88 /** | 85 /** |
89 * @return {string} The current query of the search box. | 86 * @return {string} The current query of the search box. |
90 * @private | 87 * @private |
91 */ | 88 */ |
92 getQuery_: function() { | 89 getQuery_: function() { |
93 return this.input_.value.trim(); | 90 return this.input_.value.trim(); |
94 }, | 91 }, |
95 | 92 |
96 /** | 93 /** |
97 * Dispatches a SEARCH event. | 94 * Dispatches a SEARCH event. |
98 * @private | 95 * @private |
99 */ | 96 */ |
100 dispatchSearchEvent_: function() { | 97 dispatchSearchEvent_: function() { |
101 this.timeout_ = null; | 98 this.timeout_ = null; |
102 var searchEvent = new Event(SearchBox.EventType.SEARCH); | 99 var searchEvent = new Event(SearchBox.EventType.SEARCH); |
103 searchEvent.query = this.getQuery_(); | 100 searchEvent.query = this.getQuery_(); |
104 this.dispatchEvent(searchEvent); | 101 this.dispatchEvent(searchEvent); |
105 }, | 102 }, |
106 | 103 |
107 /** | 104 /** |
108 * Called when the input element's value changes. Dispatches a search event. | 105 * Called when the input element's value changes. Dispatches a search event. |
109 * @private | 106 * @private |
110 */ | 107 */ |
111 onInputInput_: function() { | 108 onInputInput_: function() { |
112 if (this.timeout_) { | 109 if (this.timeout_) |
113 clearTimeout(this.timeout_); | 110 clearTimeout(this.timeout_); |
114 } | |
115 this.timeout_ = setTimeout( | 111 this.timeout_ = setTimeout( |
116 this.dispatchSearchEvent_.bind(this), SearchBox.SEARCH_DELAY_); | 112 this.dispatchSearchEvent_.bind(this), SearchBox.SEARCH_DELAY_); |
117 } | 113 } |
118 }; | 114 }; |
119 | 115 |
120 // Export | 116 // Export |
121 return { | 117 return { |
122 SearchBox: SearchBox | 118 SearchBox: SearchBox |
123 }; | 119 }; |
124 }); | 120 }); |
OLD | NEW |