2018-09-13 19:38:07 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["UrlbarView"];
|
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2018-10-02 17:22:29 +03:00
|
|
|
XPCOMUtils.defineLazyModuleGetters(this, {
|
2020-04-16 01:38:49 +03:00
|
|
|
UrlbarPrefs: "resource:///modules/UrlbarPrefs.jsm",
|
2019-02-17 21:12:01 +03:00
|
|
|
UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.jsm",
|
2018-10-02 17:22:29 +03:00
|
|
|
UrlbarUtils: "resource:///modules/UrlbarUtils.jsm",
|
|
|
|
});
|
|
|
|
|
2019-07-18 13:57:50 +03:00
|
|
|
// Stale rows are removed on a timer with this timeout. Tests can override this
|
|
|
|
// by setting UrlbarView.removeStaleRowsTimeout.
|
|
|
|
const DEFAULT_REMOVE_STALE_ROWS_TIMEOUT = 400;
|
|
|
|
|
2019-11-28 22:52:20 +03:00
|
|
|
const getBoundsWithoutFlushing = element =>
|
|
|
|
element.ownerGlobal.windowUtils.getBoundsWithoutFlushing(element);
|
|
|
|
|
2020-02-14 19:15:24 +03:00
|
|
|
// Used to get a unique id to use for row elements, it wraps at 9999, that
|
|
|
|
// should be plenty for our needs.
|
|
|
|
let gUniqueIdSerial = 1;
|
|
|
|
function getUniqueId(prefix) {
|
|
|
|
return prefix + (gUniqueIdSerial++ % 9999);
|
|
|
|
}
|
|
|
|
|
2018-09-13 19:38:07 +03:00
|
|
|
/**
|
|
|
|
* Receives and displays address bar autocomplete results.
|
|
|
|
*/
|
|
|
|
class UrlbarView {
|
|
|
|
/**
|
2018-12-07 00:33:30 +03:00
|
|
|
* @param {UrlbarInput} input
|
2018-09-13 19:38:07 +03:00
|
|
|
* The UrlbarInput instance belonging to this UrlbarView instance.
|
|
|
|
*/
|
2018-12-07 00:33:30 +03:00
|
|
|
constructor(input) {
|
|
|
|
this.input = input;
|
|
|
|
this.panel = input.panel;
|
|
|
|
this.controller = input.controller;
|
|
|
|
this.document = this.panel.ownerDocument;
|
2018-09-13 19:38:07 +03:00
|
|
|
this.window = this.document.defaultView;
|
|
|
|
|
|
|
|
this._mainContainer = this.panel.querySelector(".urlbarView-body-inner");
|
2019-08-20 18:14:25 +03:00
|
|
|
this._rows = this.panel.querySelector(".urlbarView-results");
|
2018-09-13 19:38:07 +03:00
|
|
|
|
2019-03-29 16:28:27 +03:00
|
|
|
this._rows.addEventListener("mousedown", this);
|
2019-03-29 17:53:52 +03:00
|
|
|
this._rows.addEventListener("mouseup", this);
|
2018-12-07 00:33:30 +03:00
|
|
|
|
2018-09-13 19:38:07 +03:00
|
|
|
// For the horizontal fade-out effect, set the overflow attribute on result
|
|
|
|
// rows when they overflow.
|
2018-12-04 22:26:49 +03:00
|
|
|
this._rows.addEventListener("overflow", this);
|
|
|
|
this._rows.addEventListener("underflow", this);
|
2018-09-20 16:07:18 +03:00
|
|
|
|
2018-12-07 00:33:30 +03:00
|
|
|
this.controller.setView(this);
|
2018-09-20 16:07:18 +03:00
|
|
|
this.controller.addQueryListener(this);
|
2020-01-31 11:30:34 +03:00
|
|
|
// This is used by autoOpen to avoid flickering results when reopening
|
|
|
|
// previously abandoned searches.
|
|
|
|
this._queryContextCache = new QueryContextCache(5);
|
2018-09-13 19:38:07 +03:00
|
|
|
}
|
|
|
|
|
2018-11-17 11:46:35 +03:00
|
|
|
get oneOffSearchButtons() {
|
2019-02-19 01:32:05 +03:00
|
|
|
if (!this._oneOffSearchButtons) {
|
|
|
|
this._oneOffSearchButtons = new this.window.SearchOneOffs(
|
|
|
|
this.panel.querySelector(".search-one-offs")
|
|
|
|
);
|
|
|
|
this._oneOffSearchButtons.addEventListener(
|
|
|
|
"SelectedOneOffButtonChanged",
|
|
|
|
this
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return this._oneOffSearchButtons;
|
2018-11-17 11:46:35 +03:00
|
|
|
}
|
|
|
|
|
2018-12-07 00:33:30 +03:00
|
|
|
/**
|
|
|
|
* @returns {boolean}
|
|
|
|
* Whether the panel is open.
|
|
|
|
*/
|
|
|
|
get isOpen() {
|
2019-10-30 17:21:33 +03:00
|
|
|
return this.input.hasAttribute("open");
|
2018-12-07 00:33:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-13 18:06:54 +03:00
|
|
|
get allowEmptySelection() {
|
|
|
|
return !(
|
|
|
|
this._queryContext &&
|
|
|
|
this._queryContext.results[0] &&
|
|
|
|
this._queryContext.results[0].heuristic
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
get selectedRowIndex() {
|
|
|
|
if (!this.isOpen) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let selectedRow = this._getSelectedRow();
|
|
|
|
|
|
|
|
if (!selectedRow) {
|
2019-02-12 17:53:22 +03:00
|
|
|
return -1;
|
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
|
2019-09-24 21:45:18 +03:00
|
|
|
return selectedRow.result.rowIndex;
|
2019-02-12 17:53:22 +03:00
|
|
|
}
|
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
set selectedRowIndex(val) {
|
2019-02-12 17:53:22 +03:00
|
|
|
if (!this.isOpen) {
|
|
|
|
throw new Error(
|
|
|
|
"UrlbarView: Cannot select an item if the view isn't open."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val < 0) {
|
2019-09-14 01:47:41 +03:00
|
|
|
this._selectElement(null);
|
2019-02-12 17:53:22 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-05-09 00:14:35 +03:00
|
|
|
let items = Array.from(this._rows.children).filter(r =>
|
2019-09-14 01:47:41 +03:00
|
|
|
this._isElementVisible(r)
|
2019-06-04 13:51:46 +03:00
|
|
|
);
|
2019-02-12 17:53:22 +03:00
|
|
|
if (val >= items.length) {
|
|
|
|
throw new Error(`UrlbarView: Index ${val} is out of bounds.`);
|
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
this._selectElement(items[val]);
|
2019-02-12 17:53:22 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-09-24 21:45:18 +03:00
|
|
|
get selectedElementIndex() {
|
|
|
|
if (!this.isOpen || !this._selectedElement) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._selectedElement.elementIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
set selectedElementIndex(val) {
|
|
|
|
if (!this.isOpen) {
|
|
|
|
throw new Error(
|
|
|
|
"UrlbarView: Cannot select an item if the view isn't open."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (val < 0) {
|
|
|
|
this._selectElement(null);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
let selectableElement = this._getFirstSelectableElement();
|
|
|
|
while (selectableElement && selectableElement.elementIndex != val) {
|
|
|
|
selectableElement = this._getNextSelectableElement(selectableElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!selectableElement) {
|
|
|
|
throw new Error(`UrlbarView: Index ${val} is out of bounds.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._selectElement(selectableElement);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2019-01-14 19:58:21 +03:00
|
|
|
/**
|
2019-01-24 14:23:20 +03:00
|
|
|
* @returns {UrlbarResult}
|
2019-01-14 19:58:21 +03:00
|
|
|
* The currently selected result.
|
|
|
|
*/
|
|
|
|
get selectedResult() {
|
2019-09-14 01:47:41 +03:00
|
|
|
if (!this.isOpen) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let selectedRow = this._getSelectedRow();
|
|
|
|
|
|
|
|
if (!selectedRow) {
|
2019-01-14 19:58:21 +03:00
|
|
|
return null;
|
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
|
|
|
|
return selectedRow.result;
|
2019-01-14 19:58:21 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 20:37:09 +03:00
|
|
|
/**
|
|
|
|
* @returns {Element}
|
|
|
|
* The currently selected element.
|
|
|
|
*/
|
|
|
|
get selectedElement() {
|
|
|
|
if (!this.isOpen) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._selectedElement;
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:24:18 +03:00
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
* The number of visible results in the view. Note that this may be larger
|
|
|
|
* than the number of results in the current query context since the view
|
|
|
|
* may be showing stale results.
|
|
|
|
*/
|
2019-09-14 01:47:41 +03:00
|
|
|
get visibleRowCount() {
|
2019-07-03 13:48:49 +03:00
|
|
|
let sum = 0;
|
|
|
|
for (let row of this._rows.children) {
|
2019-09-14 01:47:41 +03:00
|
|
|
sum += Number(this._isElementVisible(row));
|
2019-07-03 13:48:49 +03:00
|
|
|
}
|
|
|
|
return sum;
|
2019-07-02 17:24:18 +03:00
|
|
|
}
|
|
|
|
|
2019-09-24 21:45:18 +03:00
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
* The number of selectable elements in the view.
|
|
|
|
*/
|
|
|
|
get visibleElementCount() {
|
|
|
|
let sum = 0;
|
|
|
|
let element = this._getFirstSelectableElement();
|
|
|
|
while (element) {
|
|
|
|
if (this._isElementVisible(element)) {
|
|
|
|
sum++;
|
|
|
|
}
|
|
|
|
element = this._getNextSelectableElement(element);
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:37:09 +03:00
|
|
|
/**
|
2019-12-04 21:55:51 +03:00
|
|
|
* Returns the result of the row containing the given element, or the result
|
|
|
|
* of the element if it itself is a row.
|
|
|
|
*
|
2019-09-25 20:37:09 +03:00
|
|
|
* @param {Element} element
|
|
|
|
* An element in the view.
|
|
|
|
* @returns {UrlbarResult}
|
2019-12-04 21:55:51 +03:00
|
|
|
* The result of the element's row.
|
2019-09-25 20:37:09 +03:00
|
|
|
*/
|
|
|
|
getResultFromElement(element) {
|
|
|
|
if (!this.isOpen) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let row = this._getRowFromElement(element);
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return row.result;
|
|
|
|
}
|
|
|
|
|
2019-12-04 21:55:51 +03:00
|
|
|
/**
|
|
|
|
* Returns the element closest to the given element that can be
|
|
|
|
* selected/picked. If the element itself can be selected, it's returned. If
|
|
|
|
* there is no such element, null is returned.
|
|
|
|
*
|
|
|
|
* @param {Element} element
|
|
|
|
* An element in the view.
|
|
|
|
* @returns {Element}
|
|
|
|
* The closest element that can be picked including the element itself, or
|
|
|
|
* null if there is no such element.
|
|
|
|
*/
|
|
|
|
getClosestSelectableElement(element) {
|
|
|
|
let result = this.getResultFromElement(element);
|
|
|
|
if (result && result.type == UrlbarUtils.RESULT_TYPE.TIP) {
|
|
|
|
if (
|
|
|
|
element.classList.contains("urlbarView-tip-button") ||
|
|
|
|
element.classList.contains("urlbarView-tip-help")
|
|
|
|
) {
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return element.closest(".urlbarView-row");
|
|
|
|
}
|
|
|
|
|
2018-12-07 00:33:30 +03:00
|
|
|
/**
|
2019-02-21 21:53:07 +03:00
|
|
|
* Moves the view selection forward or backward.
|
2018-12-07 00:33:30 +03:00
|
|
|
*
|
2019-02-21 21:53:07 +03:00
|
|
|
* @param {number} amount
|
|
|
|
* The number of steps to move.
|
2018-12-07 00:33:30 +03:00
|
|
|
* @param {boolean} options.reverse
|
|
|
|
* Set to true to select the previous item. By default the next item
|
|
|
|
* will be selected.
|
|
|
|
*/
|
2019-02-21 21:53:07 +03:00
|
|
|
selectBy(amount, { reverse = false } = {}) {
|
2018-12-07 00:33:30 +03:00
|
|
|
if (!this.isOpen) {
|
2018-12-11 18:14:56 +03:00
|
|
|
throw new Error(
|
|
|
|
"UrlbarView: Cannot select an item if the view isn't open."
|
|
|
|
);
|
2018-12-07 00:33:30 +03:00
|
|
|
}
|
|
|
|
|
2019-03-29 17:53:52 +03:00
|
|
|
// Freeze results as the user is interacting with them.
|
|
|
|
this.controller.cancelQuery();
|
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
let selectedElement = this._selectedElement;
|
2019-02-21 21:53:07 +03:00
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
// We cache the first and last rows since they will not change while
|
|
|
|
// selectBy is running.
|
|
|
|
let firstSelectableElement = this._getFirstSelectableElement();
|
|
|
|
// _getLastSelectableElement will not return an element that is over
|
|
|
|
// maxResults and thus may be hidden and not selectable.
|
|
|
|
let lastSelectableElement = this._getLastSelectableElement();
|
2019-05-09 00:14:35 +03:00
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
if (!selectedElement) {
|
|
|
|
this._selectElement(
|
|
|
|
reverse ? lastSelectableElement : firstSelectableElement
|
2019-02-21 21:53:07 +03:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let endReached = reverse
|
2019-09-14 01:47:41 +03:00
|
|
|
? selectedElement == firstSelectableElement
|
|
|
|
: selectedElement == lastSelectableElement;
|
2019-02-21 21:53:07 +03:00
|
|
|
if (endReached) {
|
|
|
|
if (this.allowEmptySelection) {
|
2019-09-14 01:47:41 +03:00
|
|
|
selectedElement = null;
|
2019-02-21 21:53:07 +03:00
|
|
|
} else {
|
2019-09-14 01:47:41 +03:00
|
|
|
selectedElement = reverse
|
|
|
|
? lastSelectableElement
|
|
|
|
: firstSelectableElement;
|
2019-02-21 21:53:07 +03:00
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
this._selectElement(selectedElement);
|
2019-02-21 21:53:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (amount-- > 0) {
|
2019-09-14 01:47:41 +03:00
|
|
|
let next = reverse
|
|
|
|
? this._getPreviousSelectableElement(selectedElement)
|
|
|
|
: this._getNextSelectableElement(selectedElement);
|
2019-02-21 21:53:07 +03:00
|
|
|
if (!next) {
|
|
|
|
break;
|
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
if (!this._isElementVisible(next)) {
|
2019-05-09 00:14:35 +03:00
|
|
|
continue;
|
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
selectedElement = next;
|
2018-12-07 00:33:30 +03:00
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
this._selectElement(selectedElement);
|
2018-12-07 00:33:30 +03:00
|
|
|
}
|
|
|
|
|
2019-03-27 13:38:52 +03:00
|
|
|
removeAccessibleFocus() {
|
|
|
|
this._setAccessibleFocus(null);
|
|
|
|
}
|
|
|
|
|
2019-10-15 19:05:05 +03:00
|
|
|
clear() {
|
|
|
|
this._rows.textContent = "";
|
|
|
|
}
|
|
|
|
|
2018-09-13 19:38:07 +03:00
|
|
|
/**
|
2019-07-25 23:13:33 +03:00
|
|
|
* Closes the view, cancelling the query if necessary.
|
2020-01-29 19:45:46 +03:00
|
|
|
* @param {boolean} [elementPicked]
|
|
|
|
* True if the view is being closed because a result was picked.
|
2018-09-13 19:38:07 +03:00
|
|
|
*/
|
2020-01-29 19:45:46 +03:00
|
|
|
close(elementPicked = false) {
|
2019-05-09 00:14:33 +03:00
|
|
|
this.controller.cancelQuery();
|
2019-08-02 10:25:25 +03:00
|
|
|
|
|
|
|
if (!this.isOpen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-25 23:13:33 +03:00
|
|
|
this.removeAccessibleFocus();
|
|
|
|
this.input.inputField.setAttribute("aria-expanded", "false");
|
2019-08-02 10:25:25 +03:00
|
|
|
|
2019-08-20 18:14:25 +03:00
|
|
|
this.input.removeAttribute("open");
|
2019-09-12 01:44:41 +03:00
|
|
|
this.input.endLayoutExtend();
|
2019-08-20 18:14:25 +03:00
|
|
|
|
2020-01-29 19:45:46 +03:00
|
|
|
// Search Tips can open the view without the Urlbar being focused. If the
|
|
|
|
// tip is ignored (e.g. the page content is clicked or the window loses
|
|
|
|
// focus) we should discard the telemetry event created when the view was
|
|
|
|
// opened.
|
|
|
|
if (!this.input.focused && !elementPicked) {
|
2020-02-04 12:58:38 +03:00
|
|
|
this.controller.engagementEvent.discard();
|
|
|
|
this.controller.engagementEvent.record(null, {});
|
2020-01-29 19:45:46 +03:00
|
|
|
}
|
|
|
|
|
2019-07-25 23:13:33 +03:00
|
|
|
this.window.removeEventListener("resize", this);
|
2020-01-14 19:28:46 +03:00
|
|
|
this.window.removeEventListener("blur", this);
|
2019-08-02 10:25:25 +03:00
|
|
|
|
|
|
|
this.controller.notify(this.controller.NOTIFICATIONS.VIEW_CLOSE);
|
2018-09-13 19:38:07 +03:00
|
|
|
}
|
|
|
|
|
2020-01-31 11:30:34 +03:00
|
|
|
/**
|
|
|
|
* This can be used to open the view automatically as a consequence of
|
|
|
|
* specific user actions. For Top Sites searches (without a search string)
|
|
|
|
* the view is opened only for mouse or keyboard interactions.
|
|
|
|
* If the user abandoned a search (there is a search string) the view is
|
|
|
|
* reopened, and we try to use cached results to reduce flickering, then a new
|
|
|
|
* query is started to refresh results.
|
|
|
|
* @param {Event} queryOptions Options to use when starting a new query. The
|
|
|
|
* event property is mandatory for proper telemetry tracking.
|
|
|
|
* @returns {boolean} Whether the view was opened.
|
|
|
|
*/
|
|
|
|
autoOpen(queryOptions = {}) {
|
2020-02-04 12:58:38 +03:00
|
|
|
if (this._pickSearchTipIfPresent(queryOptions.event)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:07:42 +03:00
|
|
|
if (!queryOptions.event) {
|
2020-01-31 11:30:34 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!this.input.value ||
|
|
|
|
this.input.getAttribute("pageproxystate") == "valid"
|
|
|
|
) {
|
|
|
|
if (
|
2020-02-20 17:05:04 +03:00
|
|
|
// Do not show Top Sites in private windows.
|
|
|
|
!this.input.isPrivate &&
|
2020-04-29 12:07:42 +03:00
|
|
|
this.input.openViewOnFocus &&
|
2020-01-31 11:30:34 +03:00
|
|
|
!this.isOpen &&
|
|
|
|
["mousedown", "command"].includes(queryOptions.event.type)
|
|
|
|
) {
|
|
|
|
this.input.startQuery(queryOptions);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reopen abandoned searches only if the input is focused.
|
2020-01-31 18:10:27 +03:00
|
|
|
if (!this.input.focused) {
|
2020-01-31 11:30:34 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-06 15:30:56 +03:00
|
|
|
// Tab switch is the only case where we requery if the view is open, because
|
|
|
|
// switching tabs doesn't necessarily close the view.
|
|
|
|
if (this.isOpen && queryOptions.event.type != "tabswitch") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-03 23:52:37 +03:00
|
|
|
if (
|
|
|
|
this._rows.firstElementChild &&
|
2020-01-31 11:30:34 +03:00
|
|
|
this._queryContext.searchString == this.input.value
|
2019-12-03 23:52:37 +03:00
|
|
|
) {
|
2020-01-31 11:30:34 +03:00
|
|
|
// We can reuse the current results.
|
|
|
|
queryOptions.allowAutofill = this._queryContext.allowAutofill;
|
|
|
|
} else {
|
|
|
|
// To reduce results flickering, try to reuse a cached UrlbarQueryContext.
|
|
|
|
let cachedQueryContext = this._queryContextCache.get(this.input.value);
|
|
|
|
if (cachedQueryContext) {
|
|
|
|
this.onQueryResults(cachedQueryContext);
|
2019-12-03 23:52:37 +03:00
|
|
|
}
|
2019-10-15 19:05:05 +03:00
|
|
|
}
|
2020-01-31 11:30:34 +03:00
|
|
|
|
|
|
|
this.controller.engagementEvent.discard();
|
|
|
|
queryOptions.searchString = this.input.value;
|
|
|
|
queryOptions.autofillIgnoresSelection = true;
|
|
|
|
queryOptions.event.interactionType = "returned";
|
2020-01-31 18:10:27 +03:00
|
|
|
|
|
|
|
this._openPanel();
|
|
|
|
|
2020-01-31 11:30:34 +03:00
|
|
|
// If we had cached results, this will just refresh them, avoiding results
|
|
|
|
// flicker, otherwise there may be some noise.
|
|
|
|
this.input.startQuery(queryOptions);
|
|
|
|
return true;
|
2019-10-15 19:05:05 +03:00
|
|
|
}
|
|
|
|
|
2018-09-20 16:07:18 +03:00
|
|
|
// UrlbarController listener methods.
|
|
|
|
onQueryStarted(queryContext) {
|
2019-05-09 00:14:33 +03:00
|
|
|
this._queryWasCancelled = false;
|
2020-04-30 18:43:16 +03:00
|
|
|
this._queryUpdatedResults = false;
|
2019-04-19 23:09:13 +03:00
|
|
|
this._startRemoveStaleRowsTimer();
|
2018-09-20 16:07:18 +03:00
|
|
|
}
|
|
|
|
|
2018-11-06 00:54:09 +03:00
|
|
|
onQueryCancelled(queryContext) {
|
2019-05-09 00:14:33 +03:00
|
|
|
this._queryWasCancelled = true;
|
2019-04-19 23:09:13 +03:00
|
|
|
this._cancelRemoveStaleRowsTimer();
|
2018-11-06 00:54:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onQueryFinished(queryContext) {
|
2019-04-19 23:09:13 +03:00
|
|
|
this._cancelRemoveStaleRowsTimer();
|
2019-05-09 00:14:33 +03:00
|
|
|
if (!this._queryWasCancelled) {
|
2020-04-30 18:43:16 +03:00
|
|
|
// If the query has not been canceled and returned some results, remove
|
|
|
|
// stale rows immediately. If no results were returned, just clear and
|
|
|
|
// close the view.
|
|
|
|
if (this._queryUpdatedResults) {
|
|
|
|
this._removeStaleRows();
|
|
|
|
} else {
|
|
|
|
this.clear();
|
|
|
|
this.close();
|
|
|
|
}
|
2019-05-09 00:14:33 +03:00
|
|
|
}
|
2018-11-06 00:54:09 +03:00
|
|
|
}
|
|
|
|
|
2018-09-20 16:07:18 +03:00
|
|
|
onQueryResults(queryContext) {
|
2020-01-31 11:30:34 +03:00
|
|
|
this._queryContextCache.put(queryContext);
|
2018-10-12 19:13:42 +03:00
|
|
|
this._queryContext = queryContext;
|
2019-01-29 23:47:42 +03:00
|
|
|
|
2019-10-15 19:05:05 +03:00
|
|
|
if (!this.isOpen) {
|
|
|
|
this.clear();
|
|
|
|
}
|
2020-04-30 18:43:16 +03:00
|
|
|
this._queryUpdatedResults = true;
|
2019-04-19 23:09:13 +03:00
|
|
|
this._updateResults(queryContext);
|
2019-01-17 20:13:51 +03:00
|
|
|
|
2020-01-23 02:52:21 +03:00
|
|
|
let firstResult = queryContext.results[0];
|
|
|
|
|
2019-02-09 18:39:20 +03:00
|
|
|
if (queryContext.lastResultCount == 0) {
|
2020-01-23 02:52:21 +03:00
|
|
|
// Clear the selection when we get a new set of results.
|
|
|
|
this._selectElement(null, {
|
|
|
|
updateInput: false,
|
|
|
|
});
|
|
|
|
|
2019-05-17 21:49:17 +03:00
|
|
|
// Hide the one-off search buttons if the search string is empty, or
|
|
|
|
// starts with a potential @ search alias or the search restriction
|
|
|
|
// character.
|
|
|
|
let trimmedValue = queryContext.searchString.trim();
|
2019-02-17 21:12:01 +03:00
|
|
|
this._enableOrDisableOneOffSearches(
|
2019-05-17 21:49:17 +03:00
|
|
|
trimmedValue &&
|
|
|
|
trimmedValue[0] != "@" &&
|
|
|
|
(trimmedValue[0] != UrlbarTokenizer.RESTRICT.SEARCH ||
|
|
|
|
trimmedValue.length != 1)
|
2019-02-17 21:12:01 +03:00
|
|
|
);
|
2019-10-07 17:43:31 +03:00
|
|
|
|
|
|
|
// The input field applies autofill on input, without waiting for results.
|
|
|
|
// Once we get results, we can ask it to correct wrong predictions.
|
2019-11-08 21:50:00 +03:00
|
|
|
this.input.maybeClearAutofillPlaceholder(firstResult);
|
2019-03-28 20:08:04 +03:00
|
|
|
}
|
2019-01-29 23:47:42 +03:00
|
|
|
|
2020-01-30 19:28:29 +03:00
|
|
|
if (
|
|
|
|
firstResult.heuristic &&
|
|
|
|
!this.selectedElement &&
|
|
|
|
!this.oneOffSearchButtons.selectedButton
|
|
|
|
) {
|
2020-01-23 02:52:21 +03:00
|
|
|
// Select the heuristic result. The heuristic may not be the first result
|
|
|
|
// added, which is why we do this check here when each result is added and
|
|
|
|
// not above.
|
|
|
|
this._selectElement(this._getFirstSelectableElement(), {
|
|
|
|
updateInput: false,
|
|
|
|
setAccessibleFocus: this.controller._userSelectionBehavior == "arrow",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-11 18:14:56 +03:00
|
|
|
this._openPanel();
|
2019-02-17 21:12:14 +03:00
|
|
|
|
2020-01-23 02:52:21 +03:00
|
|
|
if (firstResult.heuristic) {
|
2019-11-08 21:50:00 +03:00
|
|
|
// The heuristic result may be a search alias result, so apply formatting
|
|
|
|
// if necessary. Conversely, the heuristic result of the previous query
|
|
|
|
// may have been an alias, so remove formatting if necessary.
|
2019-02-17 21:12:14 +03:00
|
|
|
this.input.formatValue();
|
|
|
|
}
|
2018-09-20 16:07:18 +03:00
|
|
|
}
|
|
|
|
|
2019-01-15 15:20:48 +03:00
|
|
|
/**
|
|
|
|
* Handles removing a result from the view when it is removed from the query,
|
|
|
|
* and attempts to select the new result on the same row.
|
|
|
|
*
|
|
|
|
* This assumes that the result rows are in index order.
|
|
|
|
*
|
|
|
|
* @param {number} index The index of the result that has been removed.
|
|
|
|
*/
|
|
|
|
onQueryResultRemoved(index) {
|
|
|
|
let rowToRemove = this._rows.children[index];
|
|
|
|
rowToRemove.remove();
|
|
|
|
|
2019-05-30 04:13:59 +03:00
|
|
|
this._updateIndices();
|
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
if (rowToRemove != this._getSelectedRow()) {
|
2019-01-15 15:20:48 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select the row at the same index, if possible.
|
|
|
|
let newSelectionIndex = index;
|
|
|
|
if (index >= this._queryContext.results.length) {
|
|
|
|
newSelectionIndex = this._queryContext.results.length - 1;
|
|
|
|
}
|
|
|
|
if (newSelectionIndex >= 0) {
|
2019-09-14 01:47:41 +03:00
|
|
|
this.selectedRowIndex = newSelectionIndex;
|
2019-01-15 15:20:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 19:51:43 +03:00
|
|
|
/**
|
|
|
|
* Passes DOM events for the view to the _on_<event type> methods.
|
|
|
|
* @param {Event} event
|
|
|
|
* DOM event from the <view>.
|
|
|
|
*/
|
|
|
|
handleEvent(event) {
|
|
|
|
let methodName = "_on_" + event.type;
|
|
|
|
if (methodName in this) {
|
|
|
|
this[methodName](event);
|
|
|
|
} else {
|
|
|
|
throw new Error("Unrecognized UrlbarView event: " + event.type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is called when a one-off is clicked and when "search in new tab"
|
|
|
|
* is selected from a one-off context menu.
|
|
|
|
* @param {Event} event
|
|
|
|
* @param {nsISearchEngine} engine
|
|
|
|
* @param {string} where
|
|
|
|
* @param {object} params
|
|
|
|
*/
|
|
|
|
handleOneOffSearch(event, engine, where, params) {
|
|
|
|
this.input.handleCommand(event, where, params);
|
|
|
|
}
|
|
|
|
|
2018-09-13 19:38:07 +03:00
|
|
|
// Private methods below.
|
|
|
|
|
|
|
|
_createElement(name) {
|
|
|
|
return this.document.createElementNS("http://www.w3.org/1999/xhtml", name);
|
|
|
|
}
|
|
|
|
|
2018-12-11 18:14:56 +03:00
|
|
|
_openPanel() {
|
2018-12-10 19:05:39 +03:00
|
|
|
if (this.isOpen) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-17 09:57:58 +03:00
|
|
|
this.controller.userSelectionBehavior = "none";
|
2018-12-10 19:05:39 +03:00
|
|
|
|
2019-02-05 16:24:39 +03:00
|
|
|
this.panel.removeAttribute("actionoverride");
|
2018-12-11 18:14:56 +03:00
|
|
|
|
2019-11-28 22:52:20 +03:00
|
|
|
this._enableOrDisableRowWrap();
|
|
|
|
|
2019-07-25 23:13:33 +03:00
|
|
|
this.input.inputField.setAttribute("aria-expanded", "true");
|
|
|
|
|
2019-08-20 18:14:25 +03:00
|
|
|
this.input.setAttribute("open", "true");
|
2019-09-12 01:44:41 +03:00
|
|
|
this.input.startLayoutExtend();
|
2019-08-20 18:14:25 +03:00
|
|
|
|
2019-07-25 23:13:33 +03:00
|
|
|
this.window.addEventListener("resize", this);
|
2020-01-14 19:28:46 +03:00
|
|
|
this.window.addEventListener("blur", this);
|
2019-08-02 10:25:25 +03:00
|
|
|
|
|
|
|
this.controller.notify(this.controller.NOTIFICATIONS.VIEW_OPEN);
|
2018-11-22 19:29:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-09 00:14:35 +03:00
|
|
|
/**
|
|
|
|
* Whether a result is a search suggestion.
|
|
|
|
* @param {UrlbarResult} result The result to examine.
|
|
|
|
* @returns {boolean} Whether the result is a search suggestion.
|
|
|
|
*/
|
|
|
|
_resultIsSearchSuggestion(result) {
|
|
|
|
return Boolean(
|
|
|
|
result &&
|
|
|
|
result.type == UrlbarUtils.RESULT_TYPE.SEARCH &&
|
|
|
|
result.payload.suggestion
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the given row index can be update to the result we want
|
|
|
|
* to apply. This is used in _updateResults to avoid flickering of results, by
|
|
|
|
* reusing existing rows.
|
|
|
|
* @param {number} rowIndex Index of the row to examine.
|
|
|
|
* @param {UrlbarResult} result The result we'd like to apply.
|
|
|
|
* @param {number} firstSearchSuggestionIndex Index of the first search suggestion.
|
|
|
|
* @param {number} lastSearchSuggestionIndex Index of the last search suggestion.
|
|
|
|
* @returns {boolean} Whether the row can be updated to this result.
|
|
|
|
*/
|
|
|
|
_rowCanUpdateToResult(
|
|
|
|
rowIndex,
|
|
|
|
result,
|
|
|
|
firstSearchSuggestionIndex,
|
|
|
|
lastSearchSuggestionIndex
|
|
|
|
) {
|
|
|
|
// The heuristic result must always be current, thus it's always compatible.
|
|
|
|
if (result.heuristic) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let row = this._rows.children[rowIndex];
|
|
|
|
let resultIsSearchSuggestion = this._resultIsSearchSuggestion(result);
|
|
|
|
// If the row is same type, just update it.
|
|
|
|
if (
|
|
|
|
resultIsSearchSuggestion == this._resultIsSearchSuggestion(row.result)
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// If the row has a different type, update it if we are in a compatible
|
|
|
|
// index range.
|
|
|
|
// In practice we don't want to overwrite a search suggestion with a non
|
|
|
|
// search suggestion, but we allow the opposite.
|
|
|
|
return resultIsSearchSuggestion && rowIndex >= firstSearchSuggestionIndex;
|
|
|
|
}
|
|
|
|
|
2019-04-19 23:09:13 +03:00
|
|
|
_updateResults(queryContext) {
|
2019-05-09 00:14:35 +03:00
|
|
|
// TODO: For now this just compares search suggestions to the rest, in the
|
|
|
|
// future we should make it support any type of result. Or, even better,
|
|
|
|
// results should be grouped, thus we can directly update groups.
|
|
|
|
|
|
|
|
// Find where are existing search suggestions.
|
|
|
|
let firstSearchSuggestionIndex = -1;
|
|
|
|
let lastSearchSuggestionIndex = -1;
|
|
|
|
for (let i = 0; i < this._rows.children.length; ++i) {
|
|
|
|
let row = this._rows.children[i];
|
|
|
|
// Mark every row as stale, _updateRow will unmark them later.
|
|
|
|
row.setAttribute("stale", "true");
|
|
|
|
// Skip any row that isn't a search suggestion, or is non-visible because
|
|
|
|
// over maxResults.
|
|
|
|
if (
|
|
|
|
row.result.heuristic ||
|
|
|
|
i >= queryContext.maxResults ||
|
|
|
|
!this._resultIsSearchSuggestion(row.result)
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (firstSearchSuggestionIndex == -1) {
|
|
|
|
firstSearchSuggestionIndex = i;
|
|
|
|
}
|
|
|
|
lastSearchSuggestionIndex = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk rows and find an insertion index for results. To avoid flicker, we
|
|
|
|
// skip rows until we find one compatible with the result we want to apply.
|
|
|
|
// If we couldn't find a compatible range, we'll just update.
|
2019-04-19 23:09:13 +03:00
|
|
|
let results = queryContext.results;
|
2019-05-09 00:14:35 +03:00
|
|
|
let resultIndex = 0;
|
|
|
|
// We can have more rows than the visible ones.
|
|
|
|
for (
|
|
|
|
let rowIndex = 0;
|
|
|
|
rowIndex < this._rows.children.length && resultIndex < results.length;
|
|
|
|
++rowIndex
|
|
|
|
) {
|
|
|
|
let row = this._rows.children[rowIndex];
|
|
|
|
let result = results[resultIndex];
|
|
|
|
if (
|
|
|
|
this._rowCanUpdateToResult(
|
|
|
|
rowIndex,
|
|
|
|
result,
|
|
|
|
firstSearchSuggestionIndex,
|
|
|
|
lastSearchSuggestionIndex
|
2019-07-05 10:53:32 +03:00
|
|
|
)
|
2019-05-09 00:14:35 +03:00
|
|
|
) {
|
|
|
|
this._updateRow(row, result);
|
|
|
|
resultIndex++;
|
2019-04-19 23:09:13 +03:00
|
|
|
}
|
|
|
|
}
|
2019-05-09 00:14:35 +03:00
|
|
|
// Add remaining results, if we have fewer rows than results.
|
|
|
|
for (; resultIndex < results.length; ++resultIndex) {
|
2019-11-05 05:39:17 +03:00
|
|
|
let row = this._createRow();
|
2019-05-09 00:14:35 +03:00
|
|
|
this._updateRow(row, results[resultIndex]);
|
|
|
|
// Due to stale rows, we may have more rows than maxResults, thus we must
|
|
|
|
// hide them, and we'll revert this when stale rows are removed.
|
|
|
|
if (this._rows.children.length >= queryContext.maxResults) {
|
2019-06-04 13:51:46 +03:00
|
|
|
this._setRowVisibility(row, false);
|
2019-05-09 00:14:35 +03:00
|
|
|
}
|
2019-04-19 23:09:13 +03:00
|
|
|
this._rows.appendChild(row);
|
|
|
|
}
|
2019-05-30 04:13:59 +03:00
|
|
|
|
|
|
|
this._updateIndices();
|
2019-04-19 23:09:13 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 05:39:17 +03:00
|
|
|
_createRow() {
|
2019-03-28 18:57:20 +03:00
|
|
|
let item = this._createElement("div");
|
|
|
|
item.className = "urlbarView-row";
|
|
|
|
item.setAttribute("role", "option");
|
2019-03-28 20:08:04 +03:00
|
|
|
item._elements = new Map();
|
2019-11-05 05:39:17 +03:00
|
|
|
return item;
|
|
|
|
}
|
2019-03-28 20:08:04 +03:00
|
|
|
|
2019-11-05 05:39:17 +03:00
|
|
|
_createRowContent(item) {
|
2019-11-22 13:05:45 +03:00
|
|
|
// The url is the only element that can wrap, thus all the other elements
|
|
|
|
// are child of noWrap.
|
|
|
|
let noWrap = this._createElement("span");
|
|
|
|
noWrap.className = "urlbarView-no-wrap";
|
|
|
|
item._content.appendChild(noWrap);
|
|
|
|
|
2019-11-05 03:37:41 +03:00
|
|
|
let favicon = this._createElement("img");
|
|
|
|
favicon.className = "urlbarView-favicon";
|
2019-11-22 13:05:45 +03:00
|
|
|
noWrap.appendChild(favicon);
|
2019-11-05 03:37:41 +03:00
|
|
|
item._elements.set("favicon", favicon);
|
|
|
|
|
2020-04-08 22:04:24 +03:00
|
|
|
let typeIcon = this._createElement("span");
|
|
|
|
typeIcon.className = "urlbarView-type-icon";
|
|
|
|
noWrap.appendChild(typeIcon);
|
2019-11-21 17:58:15 +03:00
|
|
|
|
2019-11-05 03:37:41 +03:00
|
|
|
let title = this._createElement("span");
|
|
|
|
title.className = "urlbarView-title";
|
2019-11-22 13:05:45 +03:00
|
|
|
noWrap.appendChild(title);
|
2019-11-05 03:37:41 +03:00
|
|
|
item._elements.set("title", title);
|
|
|
|
|
2019-11-05 05:39:17 +03:00
|
|
|
let tagsContainer = this._createElement("span");
|
|
|
|
tagsContainer.className = "urlbarView-tags";
|
2019-11-22 13:05:45 +03:00
|
|
|
noWrap.appendChild(tagsContainer);
|
2019-11-05 05:39:17 +03:00
|
|
|
item._elements.set("tagsContainer", tagsContainer);
|
|
|
|
|
|
|
|
let titleSeparator = this._createElement("span");
|
|
|
|
titleSeparator.className = "urlbarView-title-separator";
|
2019-11-22 13:05:45 +03:00
|
|
|
noWrap.appendChild(titleSeparator);
|
2019-11-05 05:39:17 +03:00
|
|
|
item._elements.set("titleSeparator", titleSeparator);
|
|
|
|
|
|
|
|
let action = this._createElement("span");
|
2019-11-22 13:05:45 +03:00
|
|
|
action.className = "urlbarView-action";
|
|
|
|
noWrap.appendChild(action);
|
2019-11-05 05:39:17 +03:00
|
|
|
item._elements.set("action", action);
|
|
|
|
|
|
|
|
let url = this._createElement("span");
|
2019-11-22 13:05:45 +03:00
|
|
|
url.className = "urlbarView-url";
|
2019-11-05 05:39:17 +03:00
|
|
|
item._content.appendChild(url);
|
|
|
|
item._elements.set("url", url);
|
|
|
|
}
|
|
|
|
|
|
|
|
_createRowContentForTip(item) {
|
|
|
|
// We use role="group" so screen readers will read the group's label when a
|
|
|
|
// button inside it gets focus. (Screen readers don't do this for
|
|
|
|
// role="option".) We set aria-labelledby for the group in _updateIndices.
|
|
|
|
item._content.setAttribute("role", "group");
|
|
|
|
|
|
|
|
let favicon = this._createElement("img");
|
|
|
|
favicon.className = "urlbarView-favicon";
|
2020-02-06 15:45:37 +03:00
|
|
|
favicon.setAttribute("data-l10n-id", "urlbar-tip-icon-description");
|
2019-11-05 05:39:17 +03:00
|
|
|
item._content.appendChild(favicon);
|
|
|
|
item._elements.set("favicon", favicon);
|
|
|
|
|
|
|
|
let title = this._createElement("span");
|
|
|
|
title.className = "urlbarView-title";
|
|
|
|
item._content.appendChild(title);
|
|
|
|
item._elements.set("title", title);
|
|
|
|
|
|
|
|
let buttonSpacer = this._createElement("span");
|
|
|
|
buttonSpacer.className = "urlbarView-tip-button-spacer";
|
|
|
|
item._content.appendChild(buttonSpacer);
|
|
|
|
|
|
|
|
let tipButton = this._createElement("span");
|
|
|
|
tipButton.className = "urlbarView-tip-button";
|
|
|
|
tipButton.setAttribute("role", "button");
|
|
|
|
item._content.appendChild(tipButton);
|
|
|
|
item._elements.set("tipButton", tipButton);
|
|
|
|
|
|
|
|
let helpIcon = this._createElement("span");
|
|
|
|
helpIcon.className = "urlbarView-tip-help";
|
|
|
|
helpIcon.setAttribute("role", "button");
|
|
|
|
helpIcon.setAttribute("data-l10n-id", "urlbar-tip-help-icon");
|
|
|
|
item._elements.set("helpButton", helpIcon);
|
|
|
|
item._content.appendChild(helpIcon);
|
2019-12-04 01:05:16 +03:00
|
|
|
|
|
|
|
// Due to role=button, the button and help icon can sometimes become
|
|
|
|
// focused. We want to prevent that because the input should always be
|
|
|
|
// focused instead. (This happens when input.search("", { focus: false })
|
|
|
|
// is called, a tip is the first result but not heuristic, and the user tabs
|
|
|
|
// the into the button from the navbar buttons. The input is skipped and
|
|
|
|
// the focus goes straight to the tip button.)
|
|
|
|
item.addEventListener("focus", () => this.input.focus(), true);
|
2019-03-28 20:08:04 +03:00
|
|
|
}
|
|
|
|
|
2019-04-19 23:09:13 +03:00
|
|
|
_updateRow(item, result) {
|
2019-11-05 05:39:17 +03:00
|
|
|
let oldResultType = item.result && item.result.type;
|
2019-04-19 23:09:13 +03:00
|
|
|
item.result = result;
|
|
|
|
item.removeAttribute("stale");
|
2020-02-14 19:15:24 +03:00
|
|
|
item.id = getUniqueId("urlbarView-row-");
|
2018-12-08 00:21:41 +03:00
|
|
|
|
2019-11-05 05:39:17 +03:00
|
|
|
let needsNewContent =
|
|
|
|
oldResultType === undefined ||
|
|
|
|
(oldResultType == UrlbarUtils.RESULT_TYPE.TIP) !=
|
|
|
|
(result.type == UrlbarUtils.RESULT_TYPE.TIP);
|
|
|
|
|
|
|
|
if (needsNewContent) {
|
|
|
|
if (item._content) {
|
|
|
|
item._content.remove();
|
|
|
|
item._elements.clear();
|
|
|
|
}
|
|
|
|
item._content = this._createElement("span");
|
|
|
|
item._content.className = "urlbarView-row-inner";
|
|
|
|
item.appendChild(item._content);
|
|
|
|
if (item.result.type == UrlbarUtils.RESULT_TYPE.TIP) {
|
|
|
|
this._createRowContentForTip(item);
|
|
|
|
} else {
|
|
|
|
this._createRowContent(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 11:53:07 +03:00
|
|
|
if (
|
|
|
|
result.type == UrlbarUtils.RESULT_TYPE.SEARCH &&
|
2019-10-07 18:51:59 +03:00
|
|
|
!result.payload.keywordOffer &&
|
|
|
|
!result.payload.inPrivateWindow
|
2019-02-08 11:53:07 +03:00
|
|
|
) {
|
2019-02-01 04:07:33 +03:00
|
|
|
item.setAttribute("type", "search");
|
|
|
|
} else if (result.type == UrlbarUtils.RESULT_TYPE.REMOTE_TAB) {
|
|
|
|
item.setAttribute("type", "remotetab");
|
2019-02-05 16:24:39 +03:00
|
|
|
} else if (result.type == UrlbarUtils.RESULT_TYPE.TAB_SWITCH) {
|
|
|
|
item.setAttribute("type", "switchtab");
|
2019-09-10 19:08:22 +03:00
|
|
|
} else if (result.type == UrlbarUtils.RESULT_TYPE.TIP) {
|
|
|
|
item.setAttribute("type", "tip");
|
2019-11-05 05:39:17 +03:00
|
|
|
this._updateRowForTip(item, result);
|
|
|
|
return;
|
2019-01-29 12:29:21 +03:00
|
|
|
} else if (result.source == UrlbarUtils.RESULT_SOURCE.BOOKMARKS) {
|
2018-12-08 00:21:41 +03:00
|
|
|
item.setAttribute("type", "bookmark");
|
2019-03-28 20:08:04 +03:00
|
|
|
} else {
|
|
|
|
item.removeAttribute("type");
|
2018-09-13 19:38:07 +03:00
|
|
|
}
|
|
|
|
|
2019-03-28 20:08:04 +03:00
|
|
|
let favicon = item._elements.get("favicon");
|
2019-01-25 19:14:21 +03:00
|
|
|
if (
|
|
|
|
result.type == UrlbarUtils.RESULT_TYPE.SEARCH ||
|
|
|
|
result.type == UrlbarUtils.RESULT_TYPE.KEYWORD
|
|
|
|
) {
|
2019-02-15 00:41:31 +03:00
|
|
|
favicon.src = result.payload.icon || UrlbarUtils.ICON.SEARCH_GLASS;
|
2019-01-15 22:21:11 +03:00
|
|
|
} else {
|
2019-02-01 19:33:24 +03:00
|
|
|
favicon.src = result.payload.icon || UrlbarUtils.ICON.DEFAULT;
|
2019-01-15 22:21:11 +03:00
|
|
|
}
|
2018-09-13 19:38:07 +03:00
|
|
|
|
2020-04-27 20:14:44 +03:00
|
|
|
if (result.payload.isPinned) {
|
|
|
|
item.toggleAttribute("pinned", true);
|
|
|
|
} else {
|
|
|
|
item.removeAttribute("pinned");
|
|
|
|
}
|
|
|
|
|
2019-05-07 19:39:04 +03:00
|
|
|
let title = item._elements.get("title");
|
2018-12-12 02:22:54 +03:00
|
|
|
this._addTextContentWithHighlights(
|
2019-05-07 19:39:04 +03:00
|
|
|
title,
|
|
|
|
result.title,
|
|
|
|
result.titleHighlights
|
|
|
|
);
|
2019-05-09 16:06:53 +03:00
|
|
|
title._tooltip = result.title;
|
|
|
|
if (title.hasAttribute("overflow")) {
|
|
|
|
title.setAttribute("title", title._tooltip);
|
|
|
|
}
|
2018-09-13 19:38:07 +03:00
|
|
|
|
2019-03-28 20:08:04 +03:00
|
|
|
let tagsContainer = item._elements.get("tagsContainer");
|
2019-03-29 21:50:27 +03:00
|
|
|
tagsContainer.textContent = "";
|
2019-09-14 12:39:26 +03:00
|
|
|
if (result.payload.tags && result.payload.tags.length) {
|
2019-01-22 00:57:22 +03:00
|
|
|
tagsContainer.append(
|
|
|
|
...result.payload.tags.map((tag, i) => {
|
|
|
|
const element = this._createElement("span");
|
|
|
|
element.className = "urlbarView-tag";
|
|
|
|
this._addTextContentWithHighlights(
|
|
|
|
element,
|
|
|
|
tag,
|
|
|
|
result.payloadHighlights.tags[i]
|
|
|
|
);
|
|
|
|
return element;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-28 20:08:04 +03:00
|
|
|
let action = "";
|
2019-05-07 19:39:04 +03:00
|
|
|
let isVisitAction = false;
|
2019-03-28 20:08:04 +03:00
|
|
|
let setURL = false;
|
2019-01-18 13:40:22 +03:00
|
|
|
switch (result.type) {
|
2019-01-25 19:14:21 +03:00
|
|
|
case UrlbarUtils.RESULT_TYPE.TAB_SWITCH:
|
2019-07-13 01:43:06 +03:00
|
|
|
action = UrlbarUtils.strings.GetStringFromName("switchToTab2");
|
2019-03-28 20:08:04 +03:00
|
|
|
setURL = true;
|
2019-01-18 13:40:22 +03:00
|
|
|
break;
|
2019-01-31 19:51:46 +03:00
|
|
|
case UrlbarUtils.RESULT_TYPE.REMOTE_TAB:
|
2019-03-28 20:08:04 +03:00
|
|
|
action = result.payload.device;
|
|
|
|
setURL = true;
|
2019-01-31 19:51:46 +03:00
|
|
|
break;
|
2019-01-25 19:14:21 +03:00
|
|
|
case UrlbarUtils.RESULT_TYPE.SEARCH:
|
2019-10-07 18:51:59 +03:00
|
|
|
if (result.payload.inPrivateWindow) {
|
|
|
|
if (result.payload.isPrivateEngine) {
|
|
|
|
action = UrlbarUtils.strings.formatStringFromName(
|
|
|
|
"searchInPrivateWindowWithEngine",
|
|
|
|
[result.payload.engine]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
action = UrlbarUtils.strings.GetStringFromName(
|
|
|
|
"searchInPrivateWindow"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
action = UrlbarUtils.strings.formatStringFromName(
|
|
|
|
"searchWithEngine",
|
|
|
|
[result.payload.engine]
|
|
|
|
);
|
|
|
|
}
|
2019-01-18 13:40:22 +03:00
|
|
|
break;
|
2019-02-11 00:44:46 +03:00
|
|
|
case UrlbarUtils.RESULT_TYPE.KEYWORD:
|
2019-05-07 19:39:04 +03:00
|
|
|
isVisitAction = result.payload.input.trim() == result.payload.keyword;
|
2019-02-11 00:44:46 +03:00
|
|
|
break;
|
2019-02-28 12:25:22 +03:00
|
|
|
case UrlbarUtils.RESULT_TYPE.OMNIBOX:
|
2019-03-28 20:08:04 +03:00
|
|
|
action = result.payload.content;
|
2019-02-28 12:25:22 +03:00
|
|
|
break;
|
2019-01-18 13:40:22 +03:00
|
|
|
default:
|
2019-02-15 23:55:30 +03:00
|
|
|
if (result.heuristic) {
|
2019-05-07 19:39:04 +03:00
|
|
|
isVisitAction = true;
|
2019-01-30 13:10:34 +03:00
|
|
|
} else {
|
2019-03-28 20:08:04 +03:00
|
|
|
setURL = true;
|
2019-01-30 13:10:34 +03:00
|
|
|
}
|
2019-01-18 13:40:22 +03:00
|
|
|
break;
|
2018-09-13 19:38:07 +03:00
|
|
|
}
|
2019-05-07 19:39:04 +03:00
|
|
|
|
2019-03-28 20:08:04 +03:00
|
|
|
let url = item._elements.get("url");
|
|
|
|
if (setURL) {
|
2019-11-22 13:05:45 +03:00
|
|
|
item.setAttribute("has-url", "true");
|
2019-03-28 20:08:04 +03:00
|
|
|
this._addTextContentWithHighlights(
|
|
|
|
url,
|
|
|
|
result.payload.displayUrl,
|
|
|
|
result.payloadHighlights.displayUrl || []
|
|
|
|
);
|
2019-05-09 16:06:53 +03:00
|
|
|
url._tooltip = result.payload.displayUrl;
|
2019-03-28 20:08:04 +03:00
|
|
|
} else {
|
2019-11-22 13:05:45 +03:00
|
|
|
item.removeAttribute("has-url");
|
2019-03-28 20:08:04 +03:00
|
|
|
url.textContent = "";
|
2019-05-09 16:06:53 +03:00
|
|
|
url._tooltip = "";
|
|
|
|
}
|
|
|
|
if (url.hasAttribute("overflow")) {
|
|
|
|
url.setAttribute("title", url._tooltip);
|
2019-02-01 04:07:33 +03:00
|
|
|
}
|
2019-05-07 19:39:04 +03:00
|
|
|
|
|
|
|
if (isVisitAction) {
|
2019-07-13 01:43:06 +03:00
|
|
|
action = UrlbarUtils.strings.GetStringFromName("visit");
|
2019-05-07 19:39:04 +03:00
|
|
|
title.setAttribute("isurl", "true");
|
|
|
|
} else {
|
|
|
|
title.removeAttribute("isurl");
|
|
|
|
}
|
2019-03-28 20:08:04 +03:00
|
|
|
item._elements.get("action").textContent = action;
|
2019-05-07 19:39:04 +03:00
|
|
|
|
2019-11-21 13:29:24 +03:00
|
|
|
if (!title.hasAttribute("isurl")) {
|
|
|
|
title.setAttribute("dir", "auto");
|
|
|
|
} else {
|
|
|
|
title.removeAttribute("dir");
|
|
|
|
}
|
|
|
|
|
2019-05-02 16:00:16 +03:00
|
|
|
item._elements.get("titleSeparator").hidden = !action && !setURL;
|
2018-09-13 19:38:07 +03:00
|
|
|
}
|
2018-12-12 02:22:54 +03:00
|
|
|
|
2019-11-05 05:39:17 +03:00
|
|
|
_updateRowForTip(item, result) {
|
|
|
|
let favicon = item._elements.get("favicon");
|
|
|
|
favicon.src = result.payload.icon || UrlbarUtils.ICON.TIP;
|
2020-02-14 19:15:24 +03:00
|
|
|
favicon.id = item.id + "-icon";
|
2019-11-05 05:39:17 +03:00
|
|
|
|
|
|
|
let title = item._elements.get("title");
|
2020-02-14 19:15:24 +03:00
|
|
|
title.id = item.id + "-title";
|
2020-02-04 14:10:59 +03:00
|
|
|
// Add-ons will provide text, rather than l10n ids.
|
|
|
|
if (result.payload.textData) {
|
|
|
|
this.document.l10n.setAttributes(
|
|
|
|
title,
|
|
|
|
result.payload.textData.id,
|
|
|
|
result.payload.textData.args
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
title.textContent = result.payload.text;
|
|
|
|
}
|
2019-11-05 05:39:17 +03:00
|
|
|
|
2020-02-14 19:15:24 +03:00
|
|
|
item._content.setAttribute("aria-labelledby", `${favicon.id} ${title.id}`);
|
|
|
|
|
2019-11-05 05:39:17 +03:00
|
|
|
let tipButton = item._elements.get("tipButton");
|
2020-02-14 19:15:24 +03:00
|
|
|
tipButton.id = item.id + "-tip-button";
|
2020-02-04 14:10:59 +03:00
|
|
|
// Add-ons will provide buttonText, rather than l10n ids.
|
|
|
|
if (result.payload.buttonTextData) {
|
|
|
|
this.document.l10n.setAttributes(
|
|
|
|
tipButton,
|
|
|
|
result.payload.buttonTextData.id,
|
|
|
|
result.payload.buttonTextData.args
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
tipButton.textContent = result.payload.buttonText;
|
|
|
|
}
|
2019-11-05 05:39:17 +03:00
|
|
|
|
|
|
|
let helpIcon = item._elements.get("helpButton");
|
2020-02-14 19:15:24 +03:00
|
|
|
helpIcon.id = item.id + "-tip-help";
|
2019-11-05 05:39:17 +03:00
|
|
|
helpIcon.style.display = result.payload.helpUrl ? "" : "none";
|
2020-02-26 11:44:07 +03:00
|
|
|
|
|
|
|
if (result.providerName == "UrlbarProviderSearchTips") {
|
|
|
|
// For a11y, we treat search tips as alerts. We use A11yUtils.announce
|
|
|
|
// instead of role="alert" because role="alert" will only fire an alert
|
|
|
|
// event when the alert (or something inside it) is the root of an
|
|
|
|
// insertion. In this case, the entire tip result gets inserted into the
|
|
|
|
// a11y tree as a single insertion, so no alert event would be fired.
|
|
|
|
this.window.A11yUtils.announce(result.payload.textData);
|
|
|
|
}
|
2019-11-05 05:39:17 +03:00
|
|
|
}
|
|
|
|
|
2019-05-30 04:13:59 +03:00
|
|
|
_updateIndices() {
|
|
|
|
for (let i = 0; i < this._rows.children.length; i++) {
|
|
|
|
let item = this._rows.children[i];
|
2019-09-24 21:45:18 +03:00
|
|
|
item.result.rowIndex = i;
|
2019-05-30 04:13:59 +03:00
|
|
|
}
|
2019-09-24 21:45:18 +03:00
|
|
|
let selectableElement = this._getFirstSelectableElement();
|
|
|
|
let uiIndex = 0;
|
|
|
|
while (selectableElement) {
|
|
|
|
selectableElement.elementIndex = uiIndex++;
|
|
|
|
selectableElement = this._getNextSelectableElement(selectableElement);
|
|
|
|
}
|
2019-05-30 04:13:59 +03:00
|
|
|
}
|
|
|
|
|
2019-06-04 13:51:46 +03:00
|
|
|
_setRowVisibility(row, visible) {
|
|
|
|
row.style.display = visible ? "" : "none";
|
2019-11-05 05:39:17 +03:00
|
|
|
if (!visible && row.result.type != UrlbarUtils.RESULT_TYPE.TIP) {
|
2019-06-04 13:51:46 +03:00
|
|
|
// Reset the overflow state of elements that can overflow in case their
|
|
|
|
// content changes while they're hidden. When making the row visible
|
|
|
|
// again, we'll get new overflow events if needed.
|
|
|
|
this._setElementOverflowing(row._elements.get("title"), false);
|
|
|
|
this._setElementOverflowing(row._elements.get("url"), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
/**
|
|
|
|
* Returns true if a row or a descendant in the view is visible.
|
|
|
|
*
|
|
|
|
* @param {Element} element
|
|
|
|
* A row in the view or a descendant of the row.
|
|
|
|
* @returns {boolean}
|
|
|
|
* True if `element` or `element`'s ancestor row is visible in the view.
|
|
|
|
*/
|
|
|
|
_isElementVisible(element) {
|
|
|
|
if (!element.classList.contains("urlbarView-row")) {
|
|
|
|
element = element.closest(".urlbarView-row");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return element.style.display != "none";
|
2019-06-04 13:51:46 +03:00
|
|
|
}
|
|
|
|
|
2019-04-19 23:09:13 +03:00
|
|
|
_removeStaleRows() {
|
|
|
|
let row = this._rows.lastElementChild;
|
|
|
|
while (row) {
|
|
|
|
let next = row.previousElementSibling;
|
|
|
|
if (row.hasAttribute("stale")) {
|
|
|
|
row.remove();
|
2019-05-09 00:14:35 +03:00
|
|
|
} else {
|
2019-06-04 13:51:46 +03:00
|
|
|
this._setRowVisibility(row, true);
|
2019-04-19 23:09:13 +03:00
|
|
|
}
|
|
|
|
row = next;
|
|
|
|
}
|
2019-07-10 18:55:30 +03:00
|
|
|
this._updateIndices();
|
2019-04-19 23:09:13 +03:00
|
|
|
}
|
2019-04-19 23:54:08 +03:00
|
|
|
|
2019-04-19 23:09:13 +03:00
|
|
|
_startRemoveStaleRowsTimer() {
|
|
|
|
this._removeStaleRowsTimer = this.window.setTimeout(() => {
|
|
|
|
this._removeStaleRowsTimer = null;
|
|
|
|
this._removeStaleRows();
|
2019-07-18 13:57:50 +03:00
|
|
|
}, UrlbarView.removeStaleRowsTimeout);
|
2019-04-19 23:09:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_cancelRemoveStaleRowsTimer() {
|
|
|
|
if (this._removeStaleRowsTimer) {
|
|
|
|
this.window.clearTimeout(this._removeStaleRowsTimer);
|
|
|
|
this._removeStaleRowsTimer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
_selectElement(item, { updateInput = true, setAccessibleFocus = true } = {}) {
|
|
|
|
if (this._selectedElement) {
|
|
|
|
this._selectedElement.toggleAttribute("selected", false);
|
|
|
|
this._selectedElement.removeAttribute("aria-selected");
|
2019-02-12 17:53:22 +03:00
|
|
|
}
|
2019-03-09 15:15:26 +03:00
|
|
|
if (item) {
|
|
|
|
item.toggleAttribute("selected", true);
|
2019-03-27 13:38:52 +03:00
|
|
|
item.setAttribute("aria-selected", "true");
|
2019-02-12 17:53:22 +03:00
|
|
|
}
|
2019-03-27 13:38:52 +03:00
|
|
|
this._setAccessibleFocus(setAccessibleFocus && item);
|
2019-09-14 01:47:41 +03:00
|
|
|
this._selectedElement = item;
|
2019-02-12 17:53:22 +03:00
|
|
|
|
2019-03-09 15:15:26 +03:00
|
|
|
if (updateInput) {
|
2019-04-19 23:09:13 +03:00
|
|
|
this.input.setValueFromResult(item && item.result);
|
2019-02-12 17:53:22 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 01:47:41 +03:00
|
|
|
/**
|
|
|
|
* Returns the first selectable element in the view.
|
|
|
|
*
|
|
|
|
* @returns {Element} The first selectable element in the view.
|
|
|
|
*/
|
|
|
|
_getFirstSelectableElement() {
|
|
|
|
let firstElementChild = this._rows.firstElementChild;
|
|
|
|
if (
|
2019-10-16 04:14:30 +03:00
|
|
|
firstElementChild &&
|
2019-09-14 01:47:41 +03:00
|
|
|
firstElementChild.result &&
|
|
|
|
firstElementChild.result.type == UrlbarUtils.RESULT_TYPE.TIP
|
|
|
|
) {
|
2019-10-17 20:46:01 +03:00
|
|
|
firstElementChild = firstElementChild._elements.get("tipButton");
|
2019-09-14 01:47:41 +03:00
|
|
|
}
|
|
|
|
return firstElementChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the last selectable element in the view.
|
|
|
|
*
|
|
|
|
* @returns {Element} The last selectable element in the view.
|
|
|
|
*/
|
|
|
|
_getLastSelectableElement() {
|
|
|
|
let lastElementChild = this._rows.lastElementChild;
|
|
|
|
|
|
|
|
// We are only interested in visible elements.
|
|
|
|
while (lastElementChild && !this._isElementVisible(lastElementChild)) {
|
|
|
|
lastElementChild = this._getPreviousSelectableElement(lastElementChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
lastElementChild.result &&
|
|
|
|
lastElementChild.result.type == UrlbarUtils.RESULT_TYPE.TIP
|
|
|
|
) {
|
2019-10-17 20:46:01 +03:00
|
|
|
lastElementChild = lastElementChild._elements.get("helpButton");
|
2019-10-23 18:26:08 +03:00
|
|
|
if (lastElementChild.style.display == "none") {
|
|
|
|
lastElementChild = this._getPreviousSelectableElement(lastElementChild);
|
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return lastElementChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the next selectable element after the parameter `element`.
|
|
|
|
* @param {Element} element A selectable element in the view.
|
|
|
|
* @returns {Element} The next selectable element after the parameter `element`.
|
|
|
|
*/
|
|
|
|
_getNextSelectableElement(element) {
|
|
|
|
let next;
|
|
|
|
if (element.classList.contains("urlbarView-tip-button")) {
|
2019-10-17 20:46:01 +03:00
|
|
|
next = element.closest(".urlbarView-row")._elements.get("helpButton");
|
2019-10-23 18:26:08 +03:00
|
|
|
if (next.style.display == "none") {
|
|
|
|
next = this._getNextSelectableElement(next);
|
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
} else if (element.classList.contains("urlbarView-tip-help")) {
|
|
|
|
next = element.closest(".urlbarView-row").nextElementSibling;
|
|
|
|
} else {
|
|
|
|
next = element.nextElementSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!next) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (next.result && next.result.type == UrlbarUtils.RESULT_TYPE.TIP) {
|
2019-10-17 20:46:01 +03:00
|
|
|
next = next._elements.get("tipButton");
|
2019-09-14 01:47:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the previous selectable element before the parameter `element`.
|
|
|
|
* @param {Element} element A selectable element in the view.
|
|
|
|
* @returns {Element} The previous selectable element before the parameter `element`.
|
|
|
|
*/
|
|
|
|
_getPreviousSelectableElement(element) {
|
|
|
|
let previous;
|
|
|
|
if (element.classList.contains("urlbarView-tip-button")) {
|
|
|
|
previous = element.closest(".urlbarView-row").previousElementSibling;
|
|
|
|
} else if (element.classList.contains("urlbarView-tip-help")) {
|
2019-10-17 20:46:01 +03:00
|
|
|
previous = element.closest(".urlbarView-row")._elements.get("tipButton");
|
2019-09-14 01:47:41 +03:00
|
|
|
} else {
|
|
|
|
previous = element.previousElementSibling;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!previous) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
previous.result &&
|
|
|
|
previous.result.type == UrlbarUtils.RESULT_TYPE.TIP
|
|
|
|
) {
|
2019-10-17 20:46:01 +03:00
|
|
|
previous = previous._elements.get("helpButton");
|
2019-10-23 18:26:08 +03:00
|
|
|
if (previous.style.display == "none") {
|
|
|
|
previous = this._getPreviousSelectableElement(previous);
|
|
|
|
}
|
2019-09-14 01:47:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the currently selected row. Useful when this._selectedElement may be a
|
|
|
|
* non-row element, such as a descendant element of RESULT_TYPE.TIP.
|
|
|
|
*
|
|
|
|
* @returns {Element}
|
|
|
|
* The currently selected row, or ancestor row of the currently selected item.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
_getSelectedRow() {
|
|
|
|
if (!this.isOpen || !this._selectedElement) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let selected = this._selectedElement;
|
|
|
|
|
|
|
|
if (!selected.classList.contains("urlbarView-row")) {
|
|
|
|
// selected may be an element in a result group, like RESULT_TYPE.TIP.
|
|
|
|
selected = selected.closest(".urlbarView-row");
|
|
|
|
}
|
|
|
|
|
|
|
|
return selected;
|
|
|
|
}
|
|
|
|
|
2019-09-25 20:37:09 +03:00
|
|
|
/**
|
|
|
|
* @param {Element} element
|
|
|
|
* An element that is potentially a row or descendant of a row.
|
|
|
|
* @returns {Element}
|
|
|
|
* The row containing `element`, or `element` itself if it is a row.
|
|
|
|
*/
|
|
|
|
_getRowFromElement(element) {
|
|
|
|
if (!this.isOpen || !element) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!element.classList.contains("urlbarView-row")) {
|
|
|
|
element = element.closest(".urlbarView-row");
|
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2019-03-27 13:38:52 +03:00
|
|
|
_setAccessibleFocus(item) {
|
|
|
|
if (item) {
|
|
|
|
this.input.inputField.setAttribute("aria-activedescendant", item.id);
|
|
|
|
} else {
|
|
|
|
this.input.inputField.removeAttribute("aria-activedescendant");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 02:22:54 +03:00
|
|
|
/**
|
|
|
|
* Adds text content to a node, placing substrings that should be highlighted
|
|
|
|
* inside <em> nodes.
|
|
|
|
*
|
|
|
|
* @param {Node} parentNode
|
|
|
|
* The text content will be added to this node.
|
|
|
|
* @param {string} textContent
|
|
|
|
* The text content to give the node.
|
|
|
|
* @param {array} highlights
|
|
|
|
* The matches to highlight in the text.
|
|
|
|
*/
|
|
|
|
_addTextContentWithHighlights(parentNode, textContent, highlights) {
|
2019-03-28 20:08:04 +03:00
|
|
|
parentNode.textContent = "";
|
2018-12-12 02:22:54 +03:00
|
|
|
if (!textContent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
highlights = (highlights || []).concat([[textContent.length, 0]]);
|
|
|
|
let index = 0;
|
|
|
|
for (let [highlightIndex, highlightLength] of highlights) {
|
|
|
|
if (highlightIndex - index > 0) {
|
|
|
|
parentNode.appendChild(
|
|
|
|
this.document.createTextNode(
|
|
|
|
textContent.substring(index, highlightIndex)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (highlightLength > 0) {
|
|
|
|
let strong = this._createElement("strong");
|
|
|
|
strong.textContent = textContent.substring(
|
|
|
|
highlightIndex,
|
|
|
|
highlightIndex + highlightLength
|
|
|
|
);
|
|
|
|
parentNode.appendChild(strong);
|
|
|
|
}
|
|
|
|
index = highlightIndex + highlightLength;
|
|
|
|
}
|
|
|
|
}
|
2018-10-12 19:13:42 +03:00
|
|
|
|
2019-02-17 21:12:01 +03:00
|
|
|
_enableOrDisableOneOffSearches(enable = true) {
|
2020-04-16 03:00:55 +03:00
|
|
|
if (enable) {
|
2019-02-05 19:51:43 +03:00
|
|
|
this.oneOffSearchButtons.telemetryOrigin = "urlbar";
|
|
|
|
this.oneOffSearchButtons.style.display = "";
|
2019-09-04 12:32:51 +03:00
|
|
|
this.oneOffSearchButtons.textbox = this.input.inputField;
|
2019-02-05 19:51:43 +03:00
|
|
|
this.oneOffSearchButtons.view = this;
|
2019-03-27 22:09:01 +03:00
|
|
|
} else {
|
2019-02-05 19:51:43 +03:00
|
|
|
this.oneOffSearchButtons.telemetryOrigin = null;
|
|
|
|
this.oneOffSearchButtons.style.display = "none";
|
|
|
|
this.oneOffSearchButtons.textbox = null;
|
|
|
|
this.oneOffSearchButtons.view = null;
|
2018-10-12 19:13:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 22:52:20 +03:00
|
|
|
_enableOrDisableRowWrap() {
|
2020-04-22 23:49:14 +03:00
|
|
|
if (getBoundsWithoutFlushing(this.input.textbox).width < 650) {
|
2019-11-28 22:52:20 +03:00
|
|
|
this._rows.setAttribute("wrap", "true");
|
|
|
|
} else {
|
|
|
|
this._rows.removeAttribute("wrap");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 13:51:46 +03:00
|
|
|
_setElementOverflowing(element, overflowing) {
|
|
|
|
element.toggleAttribute("overflow", overflowing);
|
|
|
|
if (overflowing) {
|
|
|
|
element.setAttribute("title", element._tooltip);
|
|
|
|
} else {
|
|
|
|
element.removeAttribute("title");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 12:58:38 +03:00
|
|
|
/**
|
|
|
|
* If the view is open and showing a single search tip, this method picks it
|
|
|
|
* and closes the view. This counts as an engagement, so this method should
|
|
|
|
* only be called due to user interaction.
|
|
|
|
*
|
|
|
|
* @param {event} event
|
|
|
|
* The user-initiated event for the interaction. Should not be null.
|
|
|
|
* @returns {boolean}
|
|
|
|
* True if this method picked a tip, false otherwise.
|
|
|
|
*/
|
|
|
|
_pickSearchTipIfPresent(event) {
|
|
|
|
if (
|
|
|
|
!this.isOpen ||
|
|
|
|
!this._queryContext ||
|
|
|
|
this._queryContext.results.length != 1
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let result = this._queryContext.results[0];
|
|
|
|
if (result.type != UrlbarUtils.RESULT_TYPE.TIP) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
let tipButton = this._rows.firstElementChild.querySelector(
|
|
|
|
".urlbarView-tip-button"
|
|
|
|
);
|
|
|
|
if (!tipButton) {
|
|
|
|
throw new Error("Expected a tip button");
|
|
|
|
}
|
|
|
|
this.input.pickElement(tipButton, event);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-06-04 13:51:46 +03:00
|
|
|
// Event handlers below.
|
|
|
|
|
2019-02-19 01:32:05 +03:00
|
|
|
_on_SelectedOneOffButtonChanged() {
|
2019-03-06 11:25:44 +03:00
|
|
|
if (!this.isOpen || !this._queryContext) {
|
2019-02-19 01:32:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update all search suggestion results to use the newly selected engine, or
|
|
|
|
// if no engine is selected, revert to their original engines.
|
|
|
|
let engine =
|
2019-03-27 22:09:01 +03:00
|
|
|
this.oneOffSearchButtons.selectedButton &&
|
|
|
|
this.oneOffSearchButtons.selectedButton.engine;
|
2019-02-19 01:32:05 +03:00
|
|
|
for (let i = 0; i < this._queryContext.results.length; i++) {
|
|
|
|
let result = this._queryContext.results[i];
|
|
|
|
if (
|
|
|
|
result.type != UrlbarUtils.RESULT_TYPE.SEARCH ||
|
2019-10-07 18:51:59 +03:00
|
|
|
(!result.heuristic &&
|
2020-04-22 20:41:00 +03:00
|
|
|
(!result.payload.suggestion || result.payload.isSearchHistory) &&
|
2019-10-07 18:51:59 +03:00
|
|
|
(!result.payload.inPrivateWindow || result.payload.isPrivateEngine))
|
2019-02-19 01:32:05 +03:00
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (engine) {
|
|
|
|
if (!result.payload.originalEngine) {
|
|
|
|
result.payload.originalEngine = result.payload.engine;
|
|
|
|
}
|
|
|
|
result.payload.engine = engine.name;
|
|
|
|
} else if (result.payload.originalEngine) {
|
|
|
|
result.payload.engine = result.payload.originalEngine;
|
|
|
|
delete result.payload.originalEngine;
|
|
|
|
}
|
|
|
|
let item = this._rows.children[i];
|
2019-11-19 18:53:59 +03:00
|
|
|
// If a one-off button is the only selection, force the heuristic result
|
|
|
|
// to show its action text, so the engine name is visible.
|
|
|
|
if (result.heuristic && engine && !this.selectedElement) {
|
|
|
|
item.setAttribute("show-action-text", "true");
|
|
|
|
} else {
|
|
|
|
item.removeAttribute("show-action-text");
|
|
|
|
}
|
2019-10-07 18:51:59 +03:00
|
|
|
if (!result.payload.inPrivateWindow) {
|
|
|
|
let action = item.querySelector(".urlbarView-action");
|
|
|
|
action.textContent = UrlbarUtils.strings.formatStringFromName(
|
|
|
|
"searchWithEngine",
|
|
|
|
[(engine && engine.name) || result.payload.engine]
|
|
|
|
);
|
|
|
|
}
|
2019-02-19 01:32:05 +03:00
|
|
|
// If we just changed the engine from the original engine and it had an
|
|
|
|
// icon, then make sure the result now uses the new engine's icon or
|
|
|
|
// failing that the default icon. If we changed it back to the original
|
|
|
|
// engine, go back to the original or default icon.
|
|
|
|
let favicon = item.querySelector(".urlbarView-favicon");
|
|
|
|
if (engine && result.payload.icon) {
|
|
|
|
favicon.src =
|
|
|
|
(engine.iconURI && engine.iconURI.spec) ||
|
|
|
|
UrlbarUtils.ICON.SEARCH_GLASS;
|
|
|
|
} else if (!engine) {
|
|
|
|
favicon.src = result.payload.icon || UrlbarUtils.ICON.SEARCH_GLASS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-14 19:28:46 +03:00
|
|
|
_on_blur(event) {
|
|
|
|
// If the view is open without the input being focused, it will not close
|
|
|
|
// automatically when the window loses focus. We might be in this state
|
|
|
|
// after a Search Tip is shown on an engine homepage.
|
2020-04-14 23:40:32 +03:00
|
|
|
if (!UrlbarPrefs.get("ui.popup.disable_autohide")) {
|
|
|
|
this.close();
|
|
|
|
}
|
2020-01-14 19:28:46 +03:00
|
|
|
}
|
|
|
|
|
2019-01-28 16:45:48 +03:00
|
|
|
_on_mousedown(event) {
|
2019-09-26 17:11:50 +03:00
|
|
|
if (event.button == 2) {
|
|
|
|
// Ignore right clicks.
|
|
|
|
return;
|
2019-01-28 16:45:48 +03:00
|
|
|
}
|
2019-12-04 21:55:51 +03:00
|
|
|
let element = this.getClosestSelectableElement(event.target);
|
|
|
|
if (!element) {
|
|
|
|
// Ignore clicks on elements that can't be selected/picked.
|
|
|
|
return;
|
2019-09-26 17:11:50 +03:00
|
|
|
}
|
2019-12-04 21:55:51 +03:00
|
|
|
this._selectElement(element, { updateInput: false });
|
2019-09-26 17:11:50 +03:00
|
|
|
this.controller.speculativeConnect(
|
|
|
|
this.selectedResult,
|
|
|
|
this._queryContext,
|
|
|
|
"mousedown"
|
|
|
|
);
|
2019-01-28 16:45:48 +03:00
|
|
|
}
|
|
|
|
|
2019-01-16 14:51:44 +03:00
|
|
|
_on_mouseup(event) {
|
|
|
|
if (event.button == 2) {
|
|
|
|
// Ignore right clicks.
|
|
|
|
return;
|
|
|
|
}
|
2019-12-04 21:55:51 +03:00
|
|
|
let element = this.getClosestSelectableElement(event.target);
|
|
|
|
if (!element) {
|
|
|
|
// Ignore clicks on elements that can't be selected/picked.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.input.pickElement(element, event);
|
2018-10-12 19:13:42 +03:00
|
|
|
}
|
2018-12-04 22:26:49 +03:00
|
|
|
|
|
|
|
_on_overflow(event) {
|
2019-05-08 18:49:33 +03:00
|
|
|
if (
|
|
|
|
event.detail == 1 &&
|
|
|
|
(event.target.classList.contains("urlbarView-url") ||
|
|
|
|
event.target.classList.contains("urlbarView-title"))
|
|
|
|
) {
|
2019-06-04 13:51:46 +03:00
|
|
|
this._setElementOverflowing(event.target, true);
|
2018-12-04 22:26:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_on_underflow(event) {
|
2019-05-08 18:49:33 +03:00
|
|
|
if (
|
|
|
|
event.detail == 1 &&
|
|
|
|
(event.target.classList.contains("urlbarView-url") ||
|
|
|
|
event.target.classList.contains("urlbarView-title"))
|
|
|
|
) {
|
2019-06-04 13:51:46 +03:00
|
|
|
this._setElementOverflowing(event.target, false);
|
2018-12-04 22:26:49 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-24 15:22:14 +03:00
|
|
|
|
2019-02-10 16:07:27 +03:00
|
|
|
_on_resize() {
|
2020-04-08 22:04:24 +03:00
|
|
|
this._enableOrDisableRowWrap();
|
2019-01-24 15:22:14 +03:00
|
|
|
}
|
2018-09-13 19:38:07 +03:00
|
|
|
}
|
2019-07-18 13:57:50 +03:00
|
|
|
|
|
|
|
UrlbarView.removeStaleRowsTimeout = DEFAULT_REMOVE_STALE_ROWS_TIMEOUT;
|
2020-01-31 11:30:34 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements a QueryContext cache, working as a circular buffer, when a new
|
|
|
|
* entry is added at the top, the last item is remove from the bottom.
|
|
|
|
*/
|
|
|
|
class QueryContextCache {
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
* @param {number} size The number of entries to keep in the cache.
|
|
|
|
*/
|
|
|
|
constructor(size) {
|
|
|
|
this.size = size;
|
|
|
|
this._cache = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new entry to the cache.
|
|
|
|
* @param {UrlbarQueryContext} queryContext The UrlbarQueryContext to add.
|
|
|
|
* @note QueryContexts without a searchString or without results are ignored
|
|
|
|
* and not added.
|
|
|
|
*/
|
|
|
|
put(queryContext) {
|
|
|
|
let searchString = queryContext.searchString;
|
|
|
|
if (!searchString || !queryContext.results.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = this._cache.findIndex(e => e.searchString == searchString);
|
|
|
|
if (index != -1) {
|
|
|
|
if (this._cache[index] == queryContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._cache.splice(index, 1);
|
|
|
|
}
|
|
|
|
if (this._cache.unshift(queryContext) > this.size) {
|
|
|
|
this._cache.length = this.size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get(searchString) {
|
|
|
|
return this._cache.find(e => e.searchString == searchString);
|
|
|
|
}
|
|
|
|
}
|