1 // Copyright 2013 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.
10 * @param {element} element Root element of the search box.
13 function SearchBox(element) {
16 * @type {AutocompleteList}
18 this.autocompleteList = new SearchBox.AutocompleteList(element.ownerDocument);
21 * Root element of the search box.
24 this.element = element;
27 * Text input of the search box.
30 this.inputElement = element.querySelector('input');
33 * Clear button of the search box.
36 this.clearButton = element.querySelector('.clear');
43 this.textMeasure_ = new TextMeasure(this.inputElement);
48 this.inputElement.addEventListener('input', this.updateStyles_.bind(this));
49 this.inputElement.addEventListener('keydown', this.onKeyDown_.bind(this));
50 this.inputElement.addEventListener('focus', this.onFocus_.bind(this));
51 this.inputElement.addEventListener('blur', this.onBlur_.bind(this));
52 this.inputElement.ownerDocument.addEventListener('dragover',
53 this.onDragEnter_.bind(this),
55 this.inputElement.ownerDocument.addEventListener('dragend',
56 this.onDragEnd_.bind(this),
58 element.querySelector('.icon').addEventListener(
59 'click', this.onIconClick_.bind(this));
60 element.parentNode.appendChild(this.autocompleteList);
64 * Autocomplete list for search box.
65 * @param {HTMLDocument} document Document.
68 SearchBox.AutocompleteList = function(document) {
69 var self = cr.ui.AutocompleteList.call(this);
70 self.__proto__ = SearchBox.AutocompleteList.prototype;
71 self.id = 'autocomplete-list';
72 self.autoExpands = true;
73 self.itemConstructor = SearchBox.AutocompleteListItem_.bind(null, document);
74 self.addEventListener('mouseover', self.onMouseOver_.bind(self));
78 SearchBox.AutocompleteList.prototype = {
79 __proto__: cr.ui.AutocompleteList.prototype
83 * Do nothing when a suggestion is selected.
86 SearchBox.AutocompleteList.prototype.handleSelectedSuggestion = function() {};
89 * Change the selection by a mouse over instead of just changing the
90 * color of moused over element with :hover in CSS. Here's why:
92 * 1) The user selects an item A with up/down keys (item A is highlighted)
93 * 2) Then the user moves the cursor to another item B
95 * If we just change the color of moused over element (item B), both
96 * the item A and B are highlighted. This is bad. We should change the
97 * selection so only the item B is highlighted.
99 * @param {Event} event Event.
102 SearchBox.AutocompleteList.prototype.onMouseOver_ = function(event) {
103 if (event.target.itemInfo)
104 this.selectedItem = event.target.itemInfo;
108 * ListItem element for autocomple.
110 * @param {HTMLDocument} document Document.
111 * @param {Object} item An object representing a suggestion.
115 SearchBox.AutocompleteListItem_ = function(document, item) {
116 var li = new cr.ui.ListItem();
119 var icon = document.createElement('div');
120 icon.className = 'detail-icon';
122 var text = document.createElement('div');
123 text.className = 'detail-text';
125 if (item.isHeaderItem) {
126 icon.setAttribute('search-icon', '');
128 strf('SEARCH_DRIVE_HTML', util.htmlEscape(item.searchQuery));
130 var iconType = FileType.getIcon(item.entry);
131 icon.setAttribute('file-type-icon', iconType);
132 // highlightedBaseName is a piece of HTML with meta characters properly
133 // escaped. See the comment at fileBrowserPrivate.searchDriveMetadata().
134 text.innerHTML = item.highlightedBaseName;
136 li.appendChild(icon);
137 li.appendChild(text);
142 * Updates the size related style.
144 SearchBox.prototype.updateSizeRelatedStyle = function() {
145 // Hide the search box if there is not enough space.
146 this.element.classList.toggle(
148 this.element.clientWidth < 100);
152 * Clears the search query.
154 SearchBox.prototype.clear = function() {
155 this.inputElement.value = '';
156 this.updateStyles_();
160 * Handles a focus event of the search box.
163 SearchBox.prototype.onFocus_ = function() {
164 this.element.classList.toggle('has-cursor', true);
165 this.inputElement.tabIndex = '99'; // See: go/filesapp-tabindex.
166 this.autocompleteList.attachToInput(this.inputElement);
170 * Handles a blur event of the search box.
173 SearchBox.prototype.onBlur_ = function() {
174 this.element.classList.toggle('has-cursor', false);
175 this.inputElement.tabIndex = '-1';
176 this.autocompleteList.detach();
180 * Handles a keydown event of the search box.
183 SearchBox.prototype.onKeyDown_ = function() {
184 // Handle only Esc key now.
185 if (event.keyCode != 27 || this.inputElement.value)
187 this.inputElement.blur();
191 * Handles a click event of the search icon.
194 SearchBox.prototype.onIconClick_ = function() {
195 this.inputElement.focus();
199 * Handles a dragenter event and refuses a drag source of files.
200 * @param {DragEvent} event The dragenter event.
203 SearchBox.prototype.onDragEnter_ = function(event) {
204 // For normal elements, they does not accept drag drop by default, and accept
205 // it by using event.preventDefault. But input elements accept drag drop
206 // by default. So disalbe the input element here to prohibit drag drop.
207 if (event.dataTransfer.types.indexOf('text/plain') === -1)
208 this.inputElement.style.pointerEvents = 'none';
212 * Handles a dragend event.
215 SearchBox.prototype.onDragEnd_ = function() {
216 this.inputElement.style.pointerEvents = '';
220 * Updates styles of the search box.
223 SearchBox.prototype.updateStyles_ = function() {
224 this.element.classList.toggle('has-text',
225 !!this.inputElement.value);
226 var width = this.textMeasure_.getWidth(this.inputElement.value) +
227 16 /* Extra space to allow leeway. */;
228 this.inputElement.style.width = width + 'px';