| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 * @param {boolean=} captureEnter | 56 * @param {boolean=} captureEnter |
| 57 */ | 57 */ |
| 58 constructor(suggestBoxDelegate, maxItemsHeight, captureEnter) { | 58 constructor(suggestBoxDelegate, maxItemsHeight, captureEnter) { |
| 59 this._suggestBoxDelegate = suggestBoxDelegate; | 59 this._suggestBoxDelegate = suggestBoxDelegate; |
| 60 this._maxItemsHeight = maxItemsHeight; | 60 this._maxItemsHeight = maxItemsHeight; |
| 61 this._maybeHideBound = this._maybeHide.bind(this); | 61 this._maybeHideBound = this._maybeHide.bind(this); |
| 62 this._hideBound = this.hide.bind(this); | 62 this._hideBound = this.hide.bind(this); |
| 63 this._container = createElementWithClass('div', 'suggest-box-container'); | 63 this._container = createElementWithClass('div', 'suggest-box-container'); |
| 64 this._rowHeight = 17; | 64 this._rowHeight = 17; |
| 65 /** @type {!UI.ListControl<!UI.SuggestBox.Suggestion>} */ | 65 /** @type {!UI.ListControl<!UI.SuggestBox.Suggestion>} */ |
| 66 this._list = new UI.ListControl(this, UI.ListMode.ViewportFixedItems); | 66 this._list = new UI.ListControl(this, UI.ListMode.EqualHeightItems); |
| 67 this._element = this._list.element; | 67 this._element = this._list.element; |
| 68 this._element.classList.add('suggest-box'); | 68 this._element.classList.add('suggest-box'); |
| 69 this._container.appendChild(this._element); | 69 this._container.appendChild(this._element); |
| 70 this._element.addEventListener('mousedown', this._onBoxMouseDown.bind(this),
true); | 70 this._element.addEventListener('mousedown', this._onBoxMouseDown.bind(this),
true); |
| 71 this._userInteracted = false; | 71 this._userInteracted = false; |
| 72 this._captureEnter = captureEnter; | 72 this._captureEnter = captureEnter; |
| 73 this._viewportWidth = '100vw'; | 73 this._viewportWidth = '100vw'; |
| 74 this._hasVerticalScroll = false; | 74 this._hasVerticalScroll = false; |
| 75 this._userEnteredText = ''; | 75 this._userEnteredText = ''; |
| 76 } | 76 } |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 if (index > 0) | 255 if (index > 0) |
| 256 titleElement.createChild('span').textContent = displayText.substring(0, in
dex); | 256 titleElement.createChild('span').textContent = displayText.substring(0, in
dex); |
| 257 if (index > -1) | 257 if (index > -1) |
| 258 titleElement.createChild('span', 'query').textContent = displayText.substr
ing(index, index + query.length); | 258 titleElement.createChild('span', 'query').textContent = displayText.substr
ing(index, index + query.length); |
| 259 titleElement.createChild('span').textContent = displayText.substring(index >
-1 ? index + query.length : 0); | 259 titleElement.createChild('span').textContent = displayText.substring(index >
-1 ? index + query.length : 0); |
| 260 titleElement.createChild('span', 'spacer'); | 260 titleElement.createChild('span', 'spacer'); |
| 261 if (item.subtitle) { | 261 if (item.subtitle) { |
| 262 var subtitleElement = element.createChild('span', 'suggestion-subtitle'); | 262 var subtitleElement = element.createChild('span', 'suggestion-subtitle'); |
| 263 subtitleElement.textContent = item.subtitle.trimEnd(maxTextLength - displa
yText.length); | 263 subtitleElement.textContent = item.subtitle.trimEnd(maxTextLength - displa
yText.length); |
| 264 } | 264 } |
| 265 element.addEventListener('mousedown', this._onItemMouseDown.bind(this), fals
e); | 265 |
| 266 element.addEventListener('click', event => { |
| 267 this._list.selectItem(item); |
| 268 this._userInteracted = true; |
| 269 event.consume(true); |
| 270 this.acceptSuggestion(); |
| 271 }); |
| 266 return element; | 272 return element; |
| 267 } | 273 } |
| 268 | 274 |
| 269 /** | 275 /** |
| 270 * @override | 276 * @override |
| 271 * @param {!UI.SuggestBox.Suggestion} item | 277 * @param {!UI.SuggestBox.Suggestion} item |
| 272 * @return {number} | 278 * @return {number} |
| 273 */ | 279 */ |
| 274 heightForItem(item) { | 280 heightForItem(item) { |
| 275 return this._rowHeight; | 281 return this._rowHeight; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 295 if (fromElement) | 301 if (fromElement) |
| 296 fromElement.classList.remove('selected', 'force-white-icons'); | 302 fromElement.classList.remove('selected', 'force-white-icons'); |
| 297 if (toElement) | 303 if (toElement) |
| 298 toElement.classList.add('selected', 'force-white-icons'); | 304 toElement.classList.add('selected', 'force-white-icons'); |
| 299 if (!to) | 305 if (!to) |
| 300 return; | 306 return; |
| 301 this._applySuggestion(true); | 307 this._applySuggestion(true); |
| 302 } | 308 } |
| 303 | 309 |
| 304 /** | 310 /** |
| 305 * @param {!Event} event | |
| 306 */ | |
| 307 _onItemMouseDown(event) { | |
| 308 if (!this._list.onClick(event)) | |
| 309 return; | |
| 310 this._userInteracted = true; | |
| 311 this.acceptSuggestion(); | |
| 312 event.consume(true); | |
| 313 } | |
| 314 | |
| 315 /** | |
| 316 * @param {!UI.SuggestBox.Suggestions} completions | 311 * @param {!UI.SuggestBox.Suggestions} completions |
| 317 * @param {boolean} canShowForSingleItem | 312 * @param {boolean} canShowForSingleItem |
| 318 * @param {string} userEnteredText | 313 * @param {string} userEnteredText |
| 319 * @return {boolean} | 314 * @return {boolean} |
| 320 */ | 315 */ |
| 321 _canShowBox(completions, canShowForSingleItem, userEnteredText) { | 316 _canShowBox(completions, canShowForSingleItem, userEnteredText) { |
| 322 if (!completions || !completions.length) | 317 if (!completions || !completions.length) |
| 323 return false; | 318 return false; |
| 324 | 319 |
| 325 if (completions.length > 1) | 320 if (completions.length > 1) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 340 * @param {string} userEnteredText | 335 * @param {string} userEnteredText |
| 341 */ | 336 */ |
| 342 updateSuggestions(anchorBox, completions, selectHighestPriority, canShowForSin
gleItem, userEnteredText) { | 337 updateSuggestions(anchorBox, completions, selectHighestPriority, canShowForSin
gleItem, userEnteredText) { |
| 343 delete this._onlyCompletion; | 338 delete this._onlyCompletion; |
| 344 if (this._canShowBox(completions, canShowForSingleItem, userEnteredText)) { | 339 if (this._canShowBox(completions, canShowForSingleItem, userEnteredText)) { |
| 345 this._userEnteredText = userEnteredText; | 340 this._userEnteredText = userEnteredText; |
| 346 | 341 |
| 347 this._show(); | 342 this._show(); |
| 348 this._updateBoxPosition(anchorBox, completions.length); | 343 this._updateBoxPosition(anchorBox, completions.length); |
| 349 this._updateWidth(completions); | 344 this._updateWidth(completions); |
| 350 this._list.fixedHeightChanged(); | 345 this._list.invalidateItemHeight(); |
| 351 this._list.replaceAllItems(completions); | 346 this._list.replaceAllItems(completions); |
| 352 | 347 |
| 353 var highestPriorityItem = -1; | |
| 354 if (selectHighestPriority) { | 348 if (selectHighestPriority) { |
| 355 var highestPriority = -Infinity; | 349 var highestPriorityItem = completions[0]; |
| 350 var highestPriority = completions[0].priority || 0; |
| 356 for (var i = 0; i < completions.length; i++) { | 351 for (var i = 0; i < completions.length; i++) { |
| 357 var priority = completions[i].priority || 0; | 352 var priority = completions[i].priority || 0; |
| 358 if (highestPriority < priority) { | 353 if (highestPriority < priority) { |
| 359 highestPriority = priority; | 354 highestPriority = priority; |
| 360 highestPriorityItem = i; | 355 highestPriorityItem = completions[i]; |
| 361 } | 356 } |
| 362 } | 357 } |
| 358 this._list.selectItem(highestPriorityItem, true); |
| 363 } | 359 } |
| 364 this._list.selectItemAtIndex(highestPriorityItem, true); | |
| 365 } else { | 360 } else { |
| 366 if (completions.length === 1) { | 361 if (completions.length === 1) { |
| 367 this._onlyCompletion = completions[0].title; | 362 this._onlyCompletion = completions[0].title; |
| 368 this._applySuggestion(true); | 363 this._applySuggestion(true); |
| 369 } | 364 } |
| 370 this.hide(); | 365 this.hide(); |
| 371 } | 366 } |
| 372 } | 367 } |
| 373 | 368 |
| 374 /** | 369 /** |
| 375 * @param {!KeyboardEvent} event | 370 * @param {!KeyboardEvent} event |
| 376 * @return {boolean} | 371 * @return {boolean} |
| 377 */ | 372 */ |
| 378 keyPressed(event) { | 373 keyPressed(event) { |
| 379 if (this._list.onKeyDown(event)) { | 374 var selected = false; |
| 375 switch (event.key) { |
| 376 case 'Enter': |
| 377 return this.enterKeyPressed(); |
| 378 case 'ArrowUp': |
| 379 selected = this._list.selectPreviousItem(true, false); |
| 380 break; |
| 381 case 'ArrowDown': |
| 382 selected = this._list.selectNextItem(true, false); |
| 383 break; |
| 384 case 'PageUp': |
| 385 selected = this._list.selectItemPreviousPage(false); |
| 386 break; |
| 387 case 'PageDown': |
| 388 selected = this._list.selectItemNextPage(false); |
| 389 break; |
| 390 default: |
| 391 return false; |
| 392 } |
| 393 if (selected) { |
| 380 this._userInteracted = true; | 394 this._userInteracted = true; |
| 381 return true; | 395 return true; |
| 382 } | 396 } |
| 383 if (event.key === 'Enter') | |
| 384 return this.enterKeyPressed(); | |
| 385 return false; | 397 return false; |
| 386 } | 398 } |
| 387 | 399 |
| 388 /** | 400 /** |
| 389 * @return {boolean} | 401 * @return {boolean} |
| 390 */ | 402 */ |
| 391 enterKeyPressed() { | 403 enterKeyPressed() { |
| 392 if (!this._userInteracted && this._captureEnter) | 404 if (!this._userInteracted && this._captureEnter) |
| 393 return false; | 405 return false; |
| 394 | 406 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 this.element.style.left = containerBox.x + 'px'; | 479 this.element.style.left = containerBox.x + 'px'; |
| 468 this.element.style.top = containerBox.y + 'px'; | 480 this.element.style.top = containerBox.y + 'px'; |
| 469 this.element.style.height = containerBox.height + 'px'; | 481 this.element.style.height = containerBox.height + 'px'; |
| 470 this.element.style.width = containerBox.width + 'px'; | 482 this.element.style.width = containerBox.width + 'px'; |
| 471 } | 483 } |
| 472 | 484 |
| 473 dispose() { | 485 dispose() { |
| 474 this.element.remove(); | 486 this.element.remove(); |
| 475 } | 487 } |
| 476 }; | 488 }; |
| OLD | NEW |