Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1760)

Unified Diff: chrome/browser/resources/settings/search_settings.js

Issue 2103133007: MD Settings: Second iteration of search within settings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cr_search_migration1
Patch Set: Addressing comments. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/resources/settings/search_settings.js
diff --git a/chrome/browser/resources/settings/search_settings.js b/chrome/browser/resources/settings/search_settings.js
index 53d5d22986b3799ea336f7df4233775d81e69604..1c1201a739a268bd7741df16cdcadc00182856e8 100644
--- a/chrome/browser/resources/settings/search_settings.js
+++ b/chrome/browser/resources/settings/search_settings.js
@@ -2,6 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+/**
+ * @typedef {{
+ * id: number,
+ * rawQuery: ?string,
+ * regExp: ?RegExp
+ * }}
+ */
+var SearchContext;
+
cr.define('settings', function() {
/** @const {string} */
var WRAPPER_CSS_CLASS = 'search-highlight-wrapper';
@@ -97,18 +106,46 @@ cr.define('settings', function() {
}
/**
+ * Checks whether the given |node| requires force rendering and schedules an
+ * async RenderTask if necessary.
+ *
+ * @param {!SearchContext} context
+ * @param {!Node} node
+ * @return {boolean} Whether a forced rendering task was scheduled.
+ * @private
+ */
+ function forceRenderNeeded_(context, node) {
+ if (node.tagName != 'TEMPLATE' ||
Dan Beam 2016/07/11 21:37:04 if this is truly a node, tagName -> nodeName tagN
dpapad 2016/07/11 23:25:32 Done (here and elsewhere). Wondering why the compi
+ node.getAttribute('name') === null ||
Dan Beam 2016/07/11 21:37:05 !node.hasAttribute('name') ?
dpapad 2016/07/11 23:25:32 Done.
+ node.if) {
+ return false;
+ }
+
+ // TODO(dpapad): Temporarily ignore site-settings because it throws an
+ // assertion error during force-rendering.
+ if (node.getAttribute('name').indexOf('site-') != -1)
Dan Beam 2016/07/11 21:37:05 consider caching the result of getAttribute('name'
dpapad 2016/07/11 23:25:32 This is meant to be temporary. We should be search
+ return false;
+
+ SearchManager.getInstance().queue_.addRenderTask(
+ new RenderTask(context, node));
Dan Beam 2016/07/11 21:37:05 i might make sense to move this scheduling lower
dpapad 2016/07/11 23:25:32 Done.
+ return true;
+ }
+
+ /**
* Traverses the entire DOM (including Shadow DOM), finds text nodes that
* match the given regular expression and applies the highlight UI. It also
* ensures that <settings-section> instances become visible if any matches
* occurred under their subtree.
*
- * @param {!Element} page The page to be searched, should be either
- * <settings-basic-page> or <settings-advanced-page>.
- * @param {!RegExp} regExp The regular expression to detect matches.
+ * @param {!SearchContext} context
+ * @param {!Node} root The root of the sub-tree to be searched
* @private
*/
- function findAndHighlightMatches_(page, regExp) {
+ function findAndHighlightMatches_(context, root) {
function doSearch(node) {
+ if (forceRenderNeeded_(context, node))
+ return;
+
if (IGNORED_ELEMENTS.has(node.tagName))
return;
@@ -117,9 +154,9 @@ cr.define('settings', function() {
if (textContent.length == 0)
return;
- if (regExp.test(textContent)) {
+ if (context.regExp.test(textContent)) {
revealParentSection_(node);
- highlight_(node, textContent.split(regExp));
+ highlight_(node, textContent.split(context.regExp));
}
// Returning early since TEXT_NODE nodes never have children.
return;
@@ -139,7 +176,7 @@ cr.define('settings', function() {
doSearch(shadowRoot);
}
- doSearch(page);
+ doSearch(root);
}
/**
@@ -158,7 +195,7 @@ cr.define('settings', function() {
}
/**
- * @param {!Element} page
+ * @param {!Node} page
* @param {boolean} visible
* @private
*/
@@ -169,24 +206,234 @@ cr.define('settings', function() {
}
/**
- * Performs hierarchical search, starting at the given page element.
+ * @constructor
+ *
+ * @param {!SearchContext} context
+ * @param {!Node} node
+ */
+ function Task(context, node) {
+ /** @protected {!SearchContext} */
+ this.context = context;
+
+ /** @protected {!Node} */
+ this.node = node;
+ }
+
+ Task.prototype = {
+ /**
+ * @abstract
+ * @return {!Promise}
+ */
+ exec: function() {},
+ };
+
+ /**
+ * @constructor
+ * @extends {Task}
+ *
+ * @param {!SearchContext} context
+ * @param {!Node} node
+ */
+ function RenderTask(context, node) {
+ Task.call(this, context, node);
+ }
+
+ RenderTask.prototype = {
+ /** @override */
+ exec: function() {
+ var subpageTemplate =
+ this.node['_content'].querySelector('settings-subpage');
+ subpageTemplate.id = subpageTemplate.id || this.node.getAttribute('name');
+ assert(!this.node.if);
+ this.node.if = true;
+
+ return this.getRenderedNode_(subpageTemplate.id).then(function(node) {
+ // Register a SearchTask for the part of the DOM that was just
+ // rendered.
+ SearchManager.getInstance().queue_.addSearchTask(
+ new SearchTask(this.context, node));
+ }.bind(this));
+ },
+
+ /**
+ * @param {string} id
+ * @return {!Promise<!Node>}
+ */
+ getRenderedNode_: function(id) {
Dan Beam 2016/07/11 21:37:05 what is the point of making this a separate functi
dpapad 2016/07/11 23:25:32 Inlined. It was more readable IMO.
+ var parent = this.node.parentNode;
+ return new Promise(function(resolve, reject) {
+ parent.async(function() {
+ resolve(parent.querySelector('#' + id));
+ });
+ });
+ },
+ };
+
+ /**
+ * @constructor
+ * @extends {Task}
+ *
+ * @param {!SearchContext} context
+ * @param {!Node} node
+ */
+ function SearchTask(context, node) {
+ Task.call(this, context, node);
+ }
+
+ SearchTask.prototype = {
+ /** @override */
+ exec: function() {
+ findAndHighlightMatches_(this.context, this.node);
+ return Promise.resolve();
+ },
+ };
+
+ /**
+ * @constructor
+ * @extends {Task}
+ *
+ * @param {!SearchContext} context
+ * @param {!Node} page
+ */
+ function TopLevelSearchTask(context, page) {
+ Task.call(this, context, page);
+ }
+
+ TopLevelSearchTask.prototype = {
+ /** @override */
+ exec: function() {
+ findAndRemoveHighlights_(this.node);
+
+ if (this.context.regExp === null) {
+ setSectionsVisibility_(this.node, true);
+ return Promise.resolve();
+ }
+
+ setSectionsVisibility_(this.node, false);
+ findAndHighlightMatches_(this.context, this.node);
Dan Beam 2016/07/11 21:37:05 nit: slightly less duplication var searchActive =
dpapad 2016/07/11 23:25:32 Done.
+ return Promise.resolve();
+ },
+ };
+
+ /**
+ * @constructor
+ */
+ function TaskQueue() {
+ /**
+ * @private {{
+ * high: !Array<!Task>,
+ * middle: !Array<!Task>,
+ * low: !Array<!Task>
+ * }}
+ */
+ this.queues_ = {high: [], middle: [], low: []};
+
+ /** @private {?Task} */
+ this.activeTask_ = null;
+ }
+
+ TaskQueue.prototype = {
+ /** @param {!TopLevelSearchTask} task */
+ addTopLevelSearchTask: function(task) {
+ this.queues_.high.push(task);
+ this.consumePending_();
+ },
+
+ /** @param {!SearchTask} task */
+ addSearchTask: function(task) {
+ this.queues_.middle.push(task);
+ this.consumePending_();
+ },
+
Dan Beam 2016/07/11 21:37:05 nit: @param here as well
dpapad 2016/07/11 23:25:32 Done.
+ addRenderTask: function(task) {
+ this.queues_.low.push(task);
+ this.consumePending_();
+ },
+
+ /**
+ * @return {?Task}
+ * @private
+ */
+ popNextTask_: function() {
+ return this.queues_.high.shift() ||
+ this.queues_.middle.shift() ||
+ this.queues_.low.shift() || null;
Dan Beam 2016/07/11 21:37:05 i don't really see the point of |null vs |undefine
dpapad 2016/07/11 23:25:32 Removed null. But since you asked explaining below
+ },
+
+ /** @private */
+ consumePending_: function() {
+ if (this.activeTask_)
+ return;
+
+ var manager = SearchManager.getInstance();
+ while (1) {
+ this.activeTask_ = this.popNextTask_();
+ if (!this.activeTask_)
+ return;
+
+ if (this.activeTask_.context.id != manager.activeContext_.id) {
Dan Beam 2016/07/11 21:37:05 why can't we drop all tasks when search() is calle
dpapad 2016/07/11 23:25:32 Done, see more detailed comment at line 423.
+ // Dropping this task without ever executing it, since a new search
+ // has been issued since this task was submitted.
Dan Beam 2016/07/11 21:37:04 nit: submitted -> queued
dpapad 2016/07/11 23:25:32 Done.
+ continue;
+ }
+
+ window.requestIdleCallback(function() {
Dan Beam 2016/07/11 21:37:05 so if this is called with nothing to do, we still
Dan Beam 2016/07/11 21:42:13 ignore this if running even 1 task synchronously c
dpapad 2016/07/11 23:25:32 Yes, a single task could potentially freeze the UI
+ this.activeTask_.exec().then(function(result) {
+ this.activeTask_ = null;
+ this.consumePending_();
+ }.bind(this));
+ }.bind(this));
+ break;
+ }
+ },
+ };
+
+ /**
+ * @constructor
+ */
+ var SearchManager = function() {
+ /** @private {!TaskQueue} */
+ this.queue_ = new TaskQueue();
+
+ /** @private {!SearchContext} */
+ this.activeContext_ = {
+ id: 0, rawQuery: null, regExp: null,
+ };
+ };
+ cr.addSingletonGetter(SearchManager);
+
+ SearchManager.prototype = {
+ /**
+ * @param {string} text The text to search for.
+ * @param {!Node} page
+ */
+ search: function(text, page) {
+ if (this.activeContext_.rawQuery != text) {
+ var newId = this.activeContext_.id + 1;
+
+ var regExp = null;
+ // Generate search text by escaping any characters that would be
+ // problematic for regular expressions.
+ var searchText = text.trim().replace(SANITIZE_REGEX, '\\$&');
+ if (searchText.length > 0)
+ regExp = new RegExp('(' + searchText + ')', 'i');
+
+ this.activeContext_ = {
+ id: newId, rawQuery: text, regExp: regExp,
+ };
Dan Beam 2016/07/11 21:37:05 why don't we clear the queue right here?
dpapad 2016/07/11 23:25:32 Done. This made me discover a small bug that I als
+ }
+
+ this.queue_.addTopLevelSearchTask(
+ new TopLevelSearchTask(this.activeContext_, page));
+ },
+ };
+
+ /**
* @param {string} text
- * @param {!Element} page Must be either <settings-basic-page> or
- * <settings-advanced-page>.
+ * @param {!Node} page
*/
function search(text, page) {
- findAndRemoveHighlights_(page);
-
- // Generate search text by escaping any characters that would be problematic
- // for regular expressions.
- var searchText = text.trim().replace(SANITIZE_REGEX, '\\$&');
- if (searchText.length == 0) {
- setSectionsVisibility_(page, true);
- return;
- }
-
- setSectionsVisibility_(page, false);
- findAndHighlightMatches_(page, new RegExp('(' + searchText + ')', 'i'));
+ SearchManager.getInstance().search(text, page);
}
return {
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698