From ab8048755fd3349eb593f455488555fe8918d42c Mon Sep 17 00:00:00 2001 From: "serya@google.com" Date: Thu, 9 Apr 2009 09:12:46 +0000 Subject: Implemented navigation through the search results (jump to next/next) Review URL: http://codereview.chromium.org/45062 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13411 0039d316-1c4b-4281-b951-d872f2087c98 --- webkit/glue/devtools/js/devtools.js | 51 +++++++++++++++++++++++++++++------ webkit/glue/devtools/js/dom_agent.js | 52 +++++++++++++++++++++++++++--------- 2 files changed, 83 insertions(+), 20 deletions(-) (limited to 'webkit') diff --git a/webkit/glue/devtools/js/devtools.js b/webkit/glue/devtools/js/devtools.js index 4454213..9e14e6f 100644 --- a/webkit/glue/devtools/js/devtools.js +++ b/webkit/glue/devtools/js/devtools.js @@ -218,12 +218,24 @@ WebInspector.ElementsTreeElement.prototype.updateChildren = function() { */ WebInspector.ElementsPanel.prototype.performSearch = function(query) { this.searchCanceled(); - var self = this; - devtools.tools.getDomAgent().performSearch(query, function(node) { - var treeElement = self.treeOutline.findTreeElement(node); + devtools.tools.getDomAgent().performSearch(query, + goog.bind(this.performSearchCallback_, this)); +}; + + +WebInspector.ElementsPanel.prototype.performSearchCallback_ = function(nodes) { + for (var i = 0; i < nodes.length; ++i) { + var treeElement = this.treeOutline.findTreeElement(nodes[i]); if (treeElement) treeElement.highlighted = true; - }); + } + + if (nodes.length) { + this.currentSearchResultIndex_ = 0; + this.focusedDOMNode = nodes[0]; + } + + this.searchResultCount_ = nodes.length; }; @@ -231,12 +243,19 @@ WebInspector.ElementsPanel.prototype.performSearch = function(query) { * @override */ WebInspector.ElementsPanel.prototype.searchCanceled = function() { - var self = this; - devtools.tools.getDomAgent().searchCanceled(function(node) { - var treeElement = self.treeOutline.findTreeElement(node); + this.currentSearchResultIndex_ = 0; + this.searchResultCount_ = 0; + devtools.tools.getDomAgent().searchCanceled( + goog.bind(this.searchCanceledCallback_, this)); +}; + + +WebInspector.ElementsPanel.prototype.searchCanceledCallback_ = function(nodes) { + for (var i = 0; i < nodes.length; i++) { + var treeElement = this.treeOutline.findTreeElement(nodes[i]); if (treeElement) treeElement.highlighted = false; - }); + } }; @@ -244,6 +263,14 @@ WebInspector.ElementsPanel.prototype.searchCanceled = function() { * @override */ WebInspector.ElementsPanel.prototype.jumpToNextSearchResult = function() { + if (!this.searchResultCount_) + return; + + if (++this.currentSearchResultIndex_ >= this.searchResultCount_) + this.currentSearchResultIndex_ = 0; + + this.focusedDOMNode = devtools.tools.getDomAgent(). + getSearchResultNode(this.currentSearchResultIndex_); }; @@ -251,6 +278,14 @@ WebInspector.ElementsPanel.prototype.jumpToNextSearchResult = function() { * @override */ WebInspector.ElementsPanel.prototype.jumpToPreviousSearchResult = function() { + if (!this.searchResultCount_) + return; + + if (--this.currentSearchResultIndex_ < 0) + this.currentSearchResultIndex_ = this.searchResultCount_ - 1; + + this.focusedDOMNode = devtools.tools.getDomAgent(). + getSearchResultNode(this.currentSearchResultIndex_); }; diff --git a/webkit/glue/devtools/js/dom_agent.js b/webkit/glue/devtools/js/dom_agent.js index c33b03f..2e4b547 100644 --- a/webkit/glue/devtools/js/dom_agent.js +++ b/webkit/glue/devtools/js/dom_agent.js @@ -780,40 +780,68 @@ devtools.DomAgent.prototype.childNodeRemoved = function( * @see DomAgentDelegate. * {@inheritDoc}. */ -devtools.DomAgent.prototype.performSearch = function(query, forEach) { +devtools.DomAgent.prototype.performSearch = function(query, callback) { + this.searchResults_ = []; RemoteDomAgent.PerformSearch( devtools.Callback.wrap( - goog.bind(this.performSearchCallback_, this, forEach)), + goog.bind(this.performSearchCallback_, this, callback, + this.searchResults_)), query); }; /** - * Invokes callback for each node that needs to clear highlighting. - * @param {function(devtools.DomNode):undefined} forEach callback to call. + * Invokes callback for nodes that needs to clear highlighting. + * @param {function(Array.)} callback to accept the result. */ -devtools.DomAgent.prototype.searchCanceled = function(forEach) { +devtools.DomAgent.prototype.searchCanceled = function(callback) { + if (!this.searchResults_) + return; + + var nodes = []; for (var i = 0; i < this.searchResults_.length; ++i) { var nodeId = this.searchResults_[i]; var node = this.idToDomNode_[nodeId]; - forEach(node); + nodes.push(node); } + + callback(nodes); + this.searchResults_ = null; }; /** * Invokes callback for each node that needs to gain highlighting. - * @param {function(devtools.DomNode):undefined} forEach callback to call. + * @param {function(Array.)} callback to accept the result. + * @param {Array.} searchResults to be populated. * @param {Array.} nodeIds Ids to highlight. */ -devtools.DomAgent.prototype.performSearchCallback_ = function(forEach, - nodeIds) { - this.searchResults_ = []; +devtools.DomAgent.prototype.performSearchCallback_ = function(callback, + searchResults, nodeIds) { + + if (this.searchResults_ !== searchResults) + return; // another search has requested and this results are obsolete + + var nodes = []; + for (var i = 0; i < nodeIds.length; ++i) { var node = this.idToDomNode_[nodeIds[i]]; - this.searchResults_.push(nodeIds[i]); - forEach(node); + searchResults.push(nodeIds[i]); + nodes.push(node); } + + callback(nodes); +}; + + +/** + * Returns a node by index from the actual search results + * (last performSearch). + * @param {number} index in the results. + * @return {devtools.DomNode} + */ +devtools.DomAgent.prototype.getSearchResultNode = function(index) { + return this.idToDomNode_[this.searchResults_[index]]; }; -- cgit v1.1