| 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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 this._container = createElementWithClass("div", "suggest-box-container"); | 64 this._container = createElementWithClass("div", "suggest-box-container"); |
| 65 this._element = this._container.createChild("div", "suggest-box"); | 65 this._element = this._container.createChild("div", "suggest-box"); |
| 66 this._element.addEventListener("mousedown", this._onBoxMouseDown.bind(this),
true); | 66 this._element.addEventListener("mousedown", this._onBoxMouseDown.bind(this),
true); |
| 67 this._detailsPopup = this._container.createChild("div", "suggest-box details
-popup monospace"); | 67 this._detailsPopup = this._container.createChild("div", "suggest-box details
-popup monospace"); |
| 68 this._detailsPopup.classList.add("hidden"); | 68 this._detailsPopup.classList.add("hidden"); |
| 69 this._asyncDetailsCallback = null; | 69 this._asyncDetailsCallback = null; |
| 70 /** @type {!Map<number, !Promise<{detail: string, description: string}>>} */ | 70 /** @type {!Map<number, !Promise<{detail: string, description: string}>>} */ |
| 71 this._asyncDetailsPromises = new Map(); | 71 this._asyncDetailsPromises = new Map(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 /** |
| 75 * @typedef Array.<{title: string, className: (string|undefined)}> |
| 76 */ |
| 77 WebInspector.SuggestBox.Suggestions; |
| 78 |
| 74 WebInspector.SuggestBox.prototype = { | 79 WebInspector.SuggestBox.prototype = { |
| 75 /** | 80 /** |
| 76 * @return {boolean} | 81 * @return {boolean} |
| 77 */ | 82 */ |
| 78 visible: function() | 83 visible: function() |
| 79 { | 84 { |
| 80 return !!this._container.parentElement; | 85 return !!this._container.parentElement; |
| 81 }, | 86 }, |
| 82 | 87 |
| 83 /** | 88 /** |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 _onItemMouseDown: function(event) | 238 _onItemMouseDown: function(event) |
| 234 { | 239 { |
| 235 this._selectedElement = event.currentTarget; | 240 this._selectedElement = event.currentTarget; |
| 236 this.acceptSuggestion(); | 241 this.acceptSuggestion(); |
| 237 event.consume(true); | 242 event.consume(true); |
| 238 }, | 243 }, |
| 239 | 244 |
| 240 /** | 245 /** |
| 241 * @param {string} prefix | 246 * @param {string} prefix |
| 242 * @param {string} text | 247 * @param {string} text |
| 248 * @param {string|undefined} className |
| 243 * @param {number} index | 249 * @param {number} index |
| 244 */ | 250 */ |
| 245 _createItemElement: function(prefix, text, index) | 251 _createItemElement: function(prefix, text, className, index) |
| 246 { | 252 { |
| 247 var element = createElementWithClass("div", "suggest-box-content-item so
urce-code"); | 253 var element = createElementWithClass("div", "suggest-box-content-item so
urce-code " + (className || "")); |
| 248 element.tabIndex = -1; | 254 element.tabIndex = -1; |
| 249 if (prefix && prefix.length && !text.indexOf(prefix)) { | 255 if (prefix && prefix.length && !text.indexOf(prefix)) { |
| 250 element.createChild("span", "prefix").textContent = prefix; | 256 element.createChild("span", "prefix").textContent = prefix; |
| 251 element.createChild("span", "suffix").textContent = text.substring(p
refix.length).trimEnd(50); | 257 element.createChild("span", "suffix").textContent = text.substring(p
refix.length).trimEnd(50); |
| 252 } else { | 258 } else { |
| 253 element.createChild("span", "suffix").textContent = text.trimEnd(50)
; | 259 element.createChild("span", "suffix").textContent = text.trimEnd(50)
; |
| 254 } | 260 } |
| 255 element.__fullValue = text; | 261 element.__fullValue = text; |
| 256 element.createChild("span", "spacer"); | 262 element.createChild("span", "spacer"); |
| 257 element.addEventListener("mousedown", this._onItemMouseDown.bind(this),
false); | 263 element.addEventListener("mousedown", this._onItemMouseDown.bind(this),
false); |
| 258 return element; | 264 return element; |
| 259 }, | 265 }, |
| 260 | 266 |
| 261 /** | 267 /** |
| 262 * @param {!Array.<string>} items | 268 * @param {!WebInspector.SuggestBox.Suggestions} items |
| 263 * @param {string} userEnteredText | 269 * @param {string} userEnteredText |
| 264 * @param {function(number): !Promise<{detail:string, description:string}>=}
asyncDetails | 270 * @param {function(number): !Promise<{detail:string, description:string}>=}
asyncDetails |
| 265 */ | 271 */ |
| 266 _updateItems: function(items, userEnteredText, asyncDetails) | 272 _updateItems: function(items, userEnteredText, asyncDetails) |
| 267 { | 273 { |
| 268 this._length = items.length; | 274 this._length = items.length; |
| 269 this._asyncDetailsPromises.clear(); | 275 this._asyncDetailsPromises.clear(); |
| 270 this._asyncDetailsCallback = asyncDetails; | 276 this._asyncDetailsCallback = asyncDetails; |
| 271 this._element.removeChildren(); | 277 this._element.removeChildren(); |
| 272 delete this._selectedElement; | 278 delete this._selectedElement; |
| 273 | 279 |
| 274 for (var i = 0; i < items.length; ++i) { | 280 for (var i = 0; i < items.length; ++i) { |
| 275 var item = items[i]; | 281 var item = items[i]; |
| 276 var currentItemElement = this._createItemElement(userEnteredText, it
em, i); | 282 var currentItemElement = this._createItemElement(userEnteredText, it
em.title, item.className, i); |
| 277 this._element.appendChild(currentItemElement); | 283 this._element.appendChild(currentItemElement); |
| 278 } | 284 } |
| 279 }, | 285 }, |
| 280 | 286 |
| 281 /** | 287 /** |
| 282 * @param {number} index | 288 * @param {number} index |
| 283 * @return {!Promise<?{detail: string, description: string}>} | 289 * @return {!Promise<?{detail: string, description: string}>} |
| 284 */ | 290 */ |
| 285 _asyncDetails: function(index) | 291 _asyncDetails: function(index) |
| 286 { | 292 { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 * @this {WebInspector.SuggestBox} | 337 * @this {WebInspector.SuggestBox} |
| 332 */ | 338 */ |
| 333 function showDetails(details) | 339 function showDetails(details) |
| 334 { | 340 { |
| 335 if (elem === this._selectedElement) | 341 if (elem === this._selectedElement) |
| 336 this._showDetailsPopup(details); | 342 this._showDetailsPopup(details); |
| 337 } | 343 } |
| 338 }, | 344 }, |
| 339 | 345 |
| 340 /** | 346 /** |
| 341 * @param {!Array.<string>} completions | 347 * @param {!WebInspector.SuggestBox.Suggestions} completions |
| 342 * @param {boolean} canShowForSingleItem | 348 * @param {boolean} canShowForSingleItem |
| 343 * @param {string} userEnteredText | 349 * @param {string} userEnteredText |
| 344 */ | 350 */ |
| 345 _canShowBox: function(completions, canShowForSingleItem, userEnteredText) | 351 _canShowBox: function(completions, canShowForSingleItem, userEnteredText) |
| 346 { | 352 { |
| 347 if (!completions || !completions.length) | 353 if (!completions || !completions.length) |
| 348 return false; | 354 return false; |
| 349 | 355 |
| 350 if (completions.length > 1) | 356 if (completions.length > 1) |
| 351 return true; | 357 return true; |
| 352 | 358 |
| 353 // Do not show a single suggestion if it is the same as user-entered pre
fix, even if allowed to show single-item suggest boxes. | 359 // Do not show a single suggestion if it is the same as user-entered pre
fix, even if allowed to show single-item suggest boxes. |
| 354 return canShowForSingleItem && completions[0] !== userEnteredText; | 360 return canShowForSingleItem && completions[0].title !== userEnteredText; |
| 355 }, | 361 }, |
| 356 | 362 |
| 357 _ensureRowCountPerViewport: function() | 363 _ensureRowCountPerViewport: function() |
| 358 { | 364 { |
| 359 if (this._rowCountPerViewport) | 365 if (this._rowCountPerViewport) |
| 360 return; | 366 return; |
| 361 if (!this._element.firstChild) | 367 if (!this._element.firstChild) |
| 362 return; | 368 return; |
| 363 | 369 |
| 364 this._rowCountPerViewport = Math.floor(this._element.offsetHeight / this
._element.firstChild.offsetHeight); | 370 this._rowCountPerViewport = Math.floor(this._element.offsetHeight / this
._element.firstChild.offsetHeight); |
| 365 }, | 371 }, |
| 366 | 372 |
| 367 /** | 373 /** |
| 368 * @param {!AnchorBox} anchorBox | 374 * @param {!AnchorBox} anchorBox |
| 369 * @param {!Array.<string>} completions | 375 * @param {!WebInspector.SuggestBox.Suggestions} completions |
| 370 * @param {number} selectedIndex | 376 * @param {number} selectedIndex |
| 371 * @param {boolean} canShowForSingleItem | 377 * @param {boolean} canShowForSingleItem |
| 372 * @param {string} userEnteredText | 378 * @param {string} userEnteredText |
| 373 * @param {function(number): !Promise<{detail:string, description:string}>=}
asyncDetails | 379 * @param {function(number): !Promise<{detail:string, description:string}>=}
asyncDetails |
| 374 */ | 380 */ |
| 375 updateSuggestions: function(anchorBox, completions, selectedIndex, canShowFo
rSingleItem, userEnteredText, asyncDetails) | 381 updateSuggestions: function(anchorBox, completions, selectedIndex, canShowFo
rSingleItem, userEnteredText, asyncDetails) |
| 376 { | 382 { |
| 377 if (this._canShowBox(completions, canShowForSingleItem, userEnteredText)
) { | 383 if (this._canShowBox(completions, canShowForSingleItem, userEnteredText)
) { |
| 378 this._updateItems(completions, userEnteredText, asyncDetails); | 384 this._updateItems(completions, userEnteredText, asyncDetails); |
| 379 this._show(); | 385 this._show(); |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 this.element.style.top = containerBox.y + "px"; | 519 this.element.style.top = containerBox.y + "px"; |
| 514 this.element.style.height = containerBox.height + "px"; | 520 this.element.style.height = containerBox.height + "px"; |
| 515 this.element.style.width = containerBox.width + "px"; | 521 this.element.style.width = containerBox.width + "px"; |
| 516 }, | 522 }, |
| 517 | 523 |
| 518 dispose: function() | 524 dispose: function() |
| 519 { | 525 { |
| 520 this.element.remove(); | 526 this.element.remove(); |
| 521 } | 527 } |
| 522 } | 528 } |
| OLD | NEW |