SearchController.js [plain text]
WebInspector.SearchController = function()
{
this.element = document.getElementById("search");
this._matchesElement = document.getElementById("search-results-matches");
this._toolbarLabelElement = document.getElementById("search-toolbar-label");
this.element.addEventListener("search", this._onSearch.bind(this), false); this.element.addEventListener("mousedown", this._onSearchFieldManualFocus.bind(this), false); this.element.addEventListener("keydown", this._onKeyDown.bind(this), true);
}
WebInspector.SearchController.prototype = {
updateSearchMatchesCount: function(matches, panel)
{
if (!panel)
panel = WebInspector.currentPanel;
panel.currentSearchMatches = matches;
if (panel === WebInspector.currentPanel)
this._updateSearchMatchesCount(WebInspector.currentPanel.currentQuery && matches);
},
updateSearchLabel: function()
{
var panelName = WebInspector.currentPanel && WebInspector.currentPanel.toolbarItemLabel;
if (!panelName)
return;
var newLabel = WebInspector.UIString("Search %s", panelName);
if (WebInspector.attached)
this.element.setAttribute("placeholder", newLabel);
else {
this.element.removeAttribute("placeholder");
this._toolbarLabelElement.textContent = newLabel;
}
},
cancelSearch: function()
{
this.element.value = "";
this._performSearch("");
},
handleShortcut: function(event)
{
var isMac = WebInspector.isMac();
switch (event.keyIdentifier) {
case "U+0046": if (isMac)
var isFindKey = event.metaKey && !event.ctrlKey && !event.altKey && !event.shiftKey;
else
var isFindKey = event.ctrlKey && !event.metaKey && !event.altKey && !event.shiftKey;
if (isFindKey) {
this.focusSearchField();
event.handled = true;
}
break;
case "F3":
if (!isMac) {
this.focusSearchField();
event.handled = true;
}
break;
case "U+0047": var currentPanel = WebInspector.currentPanel;
if (isMac && event.metaKey && !event.ctrlKey && !event.altKey) {
if (event.shiftKey) {
if (currentPanel.jumpToPreviousSearchResult)
currentPanel.jumpToPreviousSearchResult();
} else if (currentPanel.jumpToNextSearchResult)
currentPanel.jumpToNextSearchResult();
event.handled = true;
}
break;
}
},
activePanelChanged: function()
{
this.updateSearchLabel();
if (!this._currentQuery)
return;
panel = WebInspector.currentPanel;
if (panel.performSearch) {
function performPanelSearch()
{
this._updateSearchMatchesCount();
panel.currentQuery = this._currentQuery;
panel.performSearch(this._currentQuery);
}
setTimeout(performPanelSearch.bind(this), 0);
} else {
this._updateSearchMatchesCount();
}
},
_updateSearchMatchesCount: function(matches)
{
if (matches == null) {
this._matchesElement.addStyleClass("hidden");
return;
}
if (matches) {
if (matches === 1)
var matchesString = WebInspector.UIString("1 match");
else
var matchesString = WebInspector.UIString("%d matches", matches);
} else
var matchesString = WebInspector.UIString("Not Found");
this._matchesElement.removeStyleClass("hidden");
this._matchesElement.textContent = matchesString;
WebInspector.toolbar.resize();
},
focusSearchField: function()
{
this.element.focus();
this.element.select();
},
_onSearchFieldManualFocus: function(event)
{
WebInspector.currentFocusElement = event.target;
},
_onKeyDown: function(event)
{
if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.code) {
if (event.target.value === "" && this.currentFocusElement === this.previousFocusElement)
return;
event.preventDefault();
event.stopPropagation();
this.cancelSearch(event);
WebInspector.currentFocusElement = WebInspector.previousFocusElement;
if (WebInspector.currentFocusElement === event.target)
WebInspector.currentFocusElement.currentFocusElement.select();
return false;
}
if (!isEnterKey(event))
return false;
event.target.select();
this._onSearch(event);
event.preventDefault();
},
_onSearch: function(event)
{
var forceSearch = event.keyIdentifier === "Enter";
this._performSearch(event.target.value, forceSearch, event.shiftKey, false);
},
_performSearch: function(query, forceSearch, isBackwardSearch, repeatSearch)
{
var isShortSearch = (query.length < 3);
if (isShortSearch && this._shortSearchWasForcedByKeyEvent && this._currentQuery !== query)
delete this._shortSearchWasForcedByKeyEvent;
if (isShortSearch && forceSearch)
this._shortSearchWasForcedByKeyEvent = true;
if (!query || !query.length || (!forceSearch && isShortSearch)) {
if (this._shortSearchWasForcedByKeyEvent) {
delete this._shortSearchWasForcedByKeyEvent;
return;
}
delete this._currentQuery;
for (var panelName in WebInspector.panels) {
var panel = WebInspector.panels[panelName];
var hadCurrentQuery = !!panel.currentQuery;
delete panel.currentQuery;
if (hadCurrentQuery && panel.searchCanceled)
panel.searchCanceled();
}
this._updateSearchMatchesCount();
return;
}
var currentPanel = WebInspector.currentPanel;
if (!repeatSearch && query === currentPanel.currentQuery && currentPanel.currentQuery === this._currentQuery) {
if (forceSearch) {
if (!isBackwardSearch && currentPanel.jumpToNextSearchResult)
currentPanel.jumpToNextSearchResult();
else if (isBackwardSearch && currentPanel.jumpToPreviousSearchResult)
currentPanel.jumpToPreviousSearchResult();
}
return;
}
this._currentQuery = query;
this._updateSearchMatchesCount();
if (!currentPanel.performSearch)
return;
currentPanel.currentQuery = query;
currentPanel.performSearch(query);
}
}