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._searchControlBoxElement = document.getElementById("toolbar-search-navigation-control");
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);
this._populateSearchNavigationButtons();
}
WebInspector.SearchController.prototype = {
updateSearchMatchesCount: function(matches, panel)
{
if (!panel)
panel = WebInspector.inspectorView.currentPanel();
panel.currentSearchMatches = matches;
if (panel === WebInspector.inspectorView.currentPanel())
this._updateSearchMatchesCountAndCurrentMatchIndex(WebInspector.inspectorView.currentPanel().currentQuery && matches);
},
updateCurrentMatchIndex: function(currentMatchIndex, panel)
{
if (panel === WebInspector.inspectorView.currentPanel())
this._updateSearchMatchesCountAndCurrentMatchIndex(panel.currentSearchMatches, currentMatchIndex);
},
updateSearchLabel: function()
{
var panelName = WebInspector.inspectorView.currentPanel() && WebInspector.inspectorView.currentPanel().toolbarItemLabel;
if (!panelName)
return;
var newLabel = WebInspector.UIString("Search %s", panelName);
if (WebInspector.isCompactMode())
this.element.setAttribute("placeholder", newLabel);
else {
this.element.removeAttribute("placeholder");
this._toolbarLabelElement.textContent = newLabel;
}
},
cancelSearch: function()
{
this.element.value = "";
this._performSearch("");
},
disableSearchUntilExplicitAction: function(event)
{
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.inspectorView.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;
var panel = WebInspector.inspectorView.currentPanel();
if (panel.performSearch) {
function performPanelSearch()
{
this._updateSearchMatchesCountAndCurrentMatchIndex();
panel.currentQuery = this._currentQuery;
panel.performSearch(this._currentQuery);
}
setTimeout(performPanelSearch.bind(this), 0);
} else {
this._updateSearchMatchesCountAndCurrentMatchIndex();
}
},
_updateSearchNavigationButtonState: function(visible)
{
if (visible) {
this._searchNavigationNext.removeStyleClass("hidden");
this._searchNavigationPrev.removeStyleClass("hidden");
} else {
this._searchNavigationNext.addStyleClass("hidden");
this._searchNavigationPrev.addStyleClass("hidden");
}
},
_updateSearchMatchesCountAndCurrentMatchIndex: function(matches, currentMatchIndex)
{
if (matches == null) {
this._matchesElement.addStyleClass("hidden");
this._updateSearchNavigationButtonState(false);
return;
}
if (matches) {
if (matches === 1) {
if (currentMatchIndex === 0)
var matchesString = WebInspector.UIString("1 of 1 match");
else
var matchesString = WebInspector.UIString("1 match");
} else {
if (typeof currentMatchIndex === "number")
var matchesString = WebInspector.UIString("%d of %d matches", currentMatchIndex + 1, matches);
else
var matchesString = WebInspector.UIString("%d matches", matches);
this._updateSearchNavigationButtonState(true);
}
} else {
var matchesString = WebInspector.UIString("Not Found");
this._updateSearchNavigationButtonState(false);
}
this._matchesElement.removeStyleClass("hidden");
this._matchesElement.textContent = matchesString;
WebInspector.toolbar.resize();
},
focusSearchField: function()
{
this.element.focus();
this.element.select();
},
_onSearchFieldManualFocus: function(event)
{
WebInspector.setCurrentFocusElement(event.target);
},
_onKeyDown: function(event)
{
if (event.keyCode === WebInspector.KeyboardShortcut.Keys.Esc.code) {
if (event.target.value === "")
return;
event.consume(true);
this.cancelSearch();
WebInspector.setCurrentFocusElement(WebInspector.previousFocusElement());
if (WebInspector.currentFocusElement() === event.target)
WebInspector.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);
},
_onNextButtonSearch: function(event)
{
this._performSearch(this.element.value, true, false, false);
},
_onPrevButtonSearch: function(event)
{
this._performSearch(this.element.value, true, true, 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._updateSearchMatchesCountAndCurrentMatchIndex();
return;
}
var currentPanel = WebInspector.inspectorView.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._updateSearchMatchesCountAndCurrentMatchIndex();
if (!currentPanel.performSearch)
return;
currentPanel.currentQuery = query;
currentPanel.performSearch(query);
},
_createSearchNavigationButton: function(direction)
{
var searchNavigationControlElement = document.createElement("div");
var searchNavigationIconElement = document.createElement("div");
searchNavigationControlElement.className = "toolbar-search-navigation-label hidden";
switch (direction) {
case "prev":
var searchTitle = WebInspector.UIString("Search Previous");
searchNavigationIconElement.className = "toolbar-search-navigation-icon-prev";
this._searchNavigationPrev = searchNavigationControlElement;
this._searchNavigationPrev.addEventListener("mousedown", this._onPrevButtonSearch.bind(this), false);
break;
case "next":
var searchTitle = WebInspector.UIString("Search Next");
searchNavigationIconElement.className = "toolbar-search-navigation-icon-next";
this._searchNavigationNext = searchNavigationControlElement;
this._searchNavigationNext.addEventListener("mousedown", this._onNextButtonSearch.bind(this), false);
break;
}
searchNavigationControlElement.setAttribute("title" , searchTitle);
searchNavigationControlElement.appendChild(searchNavigationIconElement);
this._searchControlBoxElement.appendChild(searchNavigationControlElement);
},
_populateSearchNavigationButtons: function()
{
this._createSearchNavigationButton("prev");
this._createSearchNavigationButton("next");
}
}
WebInspector.searchController = null;