1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 cr
.define('options', function() {
6 /** @const */ var Page
= cr
.ui
.pageManager
.Page
;
7 /** @const */ var PageManager
= cr
.ui
.pageManager
.PageManager
;
10 * Encapsulated handling of a search bubble.
12 * @extends {HTMLDivElement}
14 function SearchBubble(text
) {
15 var el
= cr
.doc
.createElement('div');
16 SearchBubble
.decorate(el
);
22 * Prohibit search for guests on desktop.
24 function ShouldEnableSearch() {
25 return !loadTimeData
.getBoolean('profileIsGuest') || cr
.isChromeOS
;
28 SearchBubble
.decorate = function(el
) {
29 el
.__proto__
= SearchBubble
.prototype;
33 SearchBubble
.prototype = {
34 __proto__
: HTMLDivElement
.prototype,
36 decorate: function() {
37 this.className
= 'search-bubble';
39 this.innards_
= cr
.doc
.createElement('div');
40 this.innards_
.className
= 'search-bubble-innards';
41 this.appendChild(this.innards_
);
43 // We create a timer to periodically update the position of the bubbles.
44 // While this isn't all that desirable, it's the only sure-fire way of
45 // making sure the bubbles stay in the correct location as sections
46 // may dynamically change size at any time.
47 this.intervalId
= setInterval(this.updatePosition
.bind(this), 250);
51 * Sets the text message in the bubble.
52 * @param {string} text The text the bubble will show.
55 this.innards_
.textContent
= text
;
59 * Attach the bubble to the element.
61 attachTo: function(element
) {
62 var parent
= element
.parentElement
;
65 if (parent
.tagName
== 'TD') {
66 // To make absolute positioning work inside a table cell we need
67 // to wrap the bubble div into another div with position:relative.
68 // This only works properly if the element is the first child of the
69 // table cell which is true for all options pages.
70 this.wrapper
= cr
.doc
.createElement('div');
71 this.wrapper
.className
= 'search-bubble-wrapper';
72 this.wrapper
.appendChild(this);
73 parent
.insertBefore(this.wrapper
, element
);
75 parent
.insertBefore(this, element
);
80 * Clear the interval timer and remove the element from the page.
83 clearInterval(this.intervalId
);
85 var child
= this.wrapper
|| this;
86 var parent
= child
.parentNode
;
88 parent
.removeChild(child
);
92 * Update the position of the bubble. Called at creation time and then
93 * periodically while the bubble remains visible.
95 updatePosition: function() {
96 // This bubble is 'owned' by the next sibling.
97 var owner
= (this.wrapper
|| this).nextSibling
;
99 // If there isn't an offset parent, we have nothing to do.
100 if (!owner
.offsetParent
)
103 // Position the bubble below the location of the owner.
104 var left
= owner
.offsetLeft
+ owner
.offsetWidth
/ 2 -
105 this.offsetWidth
/ 2;
106 var top
= owner
.offsetTop
+ owner
.offsetHeight
;
108 // Update the position in the CSS. Cache the last values for
110 if (left
!= this.lastLeft
) {
111 this.style
.left
= left
+ 'px';
112 this.lastLeft
= left
;
114 if (top
!= this.lastTop
) {
115 this.style
.top
= top
+ 'px';
122 * Encapsulated handling of the search page.
124 * @extends {cr.ui.pageManager.Page}
126 function SearchPage() {
127 Page
.call(this, 'search',
128 loadTimeData
.getString('searchPageTabTitle'),
132 cr
.addSingletonGetter(SearchPage
);
134 SearchPage
.prototype = {
135 // Inherit SearchPage from Page.
136 __proto__
: Page
.prototype,
139 * A boolean to prevent recursion. Used by setSearchText_().
143 insideSetSearchText_
: false,
146 initializePage: function() {
147 Page
.prototype.initializePage
.call(this);
149 this.searchField
= $('search-field');
151 // Handle search events. (No need to throttle, WebKit's search field
152 // will do that automatically.)
153 this.searchField
.onsearch = function(e
) {
154 this.setSearchText_(e
.currentTarget
.value
);
157 // Install handler for key presses.
158 document
.addEventListener('keydown',
159 this.keyDownEventHandler_
.bind(this));
168 didShowPage: function() {
169 // This method is called by the PageManager after all pages have had their
170 // visibility attribute set. At this point we can perform the
171 // search-specific DOM manipulation.
172 this.setSearchActive_(true);
176 didChangeHash: function() {
177 this.setSearchActive_(true);
181 willHidePage: function() {
182 // This method is called by the PageManager before all pages have their
183 // visibility attribute set. Before that happens, we need to undo the
184 // search-specific DOM manipulation that was performed in didShowPage.
185 this.setSearchActive_(false);
189 * Update the UI to reflect whether we are in a search state.
190 * @param {boolean} active True if we are on the search page.
193 setSearchActive_: function(active
) {
194 // It's fine to exit if search wasn't active and we're not going to
196 if (!this.searchActive_
&& !active
)
199 if (!ShouldEnableSearch())
202 this.searchActive_
= active
;
205 var hash
= this.hash
;
207 this.searchField
.value
=
208 decodeURIComponent(hash
.slice(1).replace(/\+/g, ' '));
209 } else if (!this.searchField
.value
) {
210 // This should only happen if the user goes directly to
211 // chrome://settings-frame/search
212 PageManager
.showDefaultPage();
216 // Move 'advanced' sections into the main settings page to allow
218 if (!this.advancedSections_
) {
219 this.advancedSections_
=
220 $('advanced-settings-container').querySelectorAll('section');
221 for (var i
= 0, section
; section
= this.advancedSections_
[i
]; i
++)
222 $('settings').appendChild(section
);
225 this.searchField
.value
= '';
228 var pagesToSearch
= this.getSearchablePages_();
229 for (var key
in pagesToSearch
) {
230 var page
= pagesToSearch
[key
];
233 page
.visible
= false;
235 // Update the visible state of all top-level elements that are not
236 // sections (ie titles, button strips). We do this before changing
237 // the page visibility to avoid excessive re-draw.
238 for (var i
= 0, childDiv
; childDiv
= page
.pageDiv
.children
[i
]; i
++) {
240 if (childDiv
.tagName
!= 'SECTION')
241 childDiv
.classList
.add('search-hidden');
243 childDiv
.classList
.remove('search-hidden');
248 // When search is active, remove the 'hidden' tag. This tag may have
249 // been added by the PageManager.
250 page
.pageDiv
.hidden
= false;
255 this.setSearchText_(this.searchField
.value
);
256 this.searchField
.focus();
258 // After hiding all page content, remove any search results.
259 this.unhighlightMatches_();
260 this.removeSearchBubbles_();
262 // Move 'advanced' sections back into their original container.
263 if (this.advancedSections_
) {
264 for (var i
= 0, section
; section
= this.advancedSections_
[i
]; i
++)
265 $('advanced-settings-container').appendChild(section
);
266 this.advancedSections_
= null;
272 * Set the current search criteria.
273 * @param {string} text Search text.
276 setSearchText_: function(text
) {
277 if (!ShouldEnableSearch())
280 // Prevent recursive execution of this method.
281 if (this.insideSetSearchText_
) return;
282 this.insideSetSearchText_
= true;
284 // Cleanup the search query string.
285 text
= SearchPage
.canonicalizeQuery(text
);
287 // If the search string becomes empty, flip back to the default page.
289 if (this.searchActive_
)
290 PageManager
.showDefaultPage();
291 this.insideSetSearchText_
= false;
295 // Toggle the search page if necessary. Otherwise, update the hash.
296 var hash
= '#' + encodeURIComponent(text
);
297 if (this.searchActive_
) {
298 if (this.hash
!= hash
)
301 PageManager
.showPageByName(this.name
, true, {hash
: hash
});
304 var foundMatches
= false;
306 // Remove any prior search results.
307 this.unhighlightMatches_();
308 this.removeSearchBubbles_();
310 var pagesToSearch
= this.getSearchablePages_();
311 for (var key
in pagesToSearch
) {
312 var page
= pagesToSearch
[key
];
313 var elements
= page
.pageDiv
.querySelectorAll('section');
314 for (var i
= 0, node
; node
= elements
[i
]; i
++) {
315 node
.classList
.add('search-hidden');
319 var bubbleControls
= [];
321 // Generate search text by applying lowercase and escaping any characters
322 // that would be problematic for regular expressions.
324 text
.toLowerCase().replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
325 // Generate a regular expression for hilighting search terms.
326 var regExp
= new RegExp('(' + searchText
+ ')', 'ig');
328 if (searchText
.length
) {
329 // Search all top-level sections for anchored string matches.
330 for (var key
in pagesToSearch
) {
331 var page
= pagesToSearch
[key
];
333 page
.pageDiv
.querySelectorAll('section');
334 for (var i
= 0, node
; node
= elements
[i
]; i
++) {
335 if (this.highlightMatches_(regExp
, node
)) {
336 node
.classList
.remove('search-hidden');
343 // Search all sub-pages, generating an array of top-level sections that
344 // we need to make visible.
345 var subPagesToSearch
= this.getSearchableSubPages_();
347 for (var key
in subPagesToSearch
) {
348 var page
= subPagesToSearch
[key
];
349 if (this.highlightMatches_(regExp
, page
.pageDiv
)) {
350 this.revealAssociatedSections_(page
);
353 bubbleControls
.concat(this.getAssociatedControls_(page
));
360 // Configure elements on the search results page based on search results.
361 $('searchPageNoMatches').hidden
= foundMatches
;
363 // Create search balloons for sub-page results.
364 var length
= bubbleControls
.length
;
365 for (var i
= 0; i
< length
; i
++)
366 this.createSearchBubble_(bubbleControls
[i
], text
);
368 // Cleanup the recursion-prevention variable.
369 this.insideSetSearchText_
= false;
373 * Reveal the associated section for |subpage|, as well as the one for its
374 * |parentPage|, and its |parentPage|'s |parentPage|, etc.
377 revealAssociatedSections_: function(subpage
) {
378 for (var page
= subpage
; page
; page
= page
.parentPage
) {
379 var section
= page
.associatedSection
;
381 section
.classList
.remove('search-hidden');
386 * @return {!Array<HTMLElement>} all the associated controls for |subpage|,
387 * including |subpage.associatedControls| as well as any controls on parent
388 * pages that are indirectly necessary to get to the subpage.
391 getAssociatedControls_: function(subpage
) {
393 for (var page
= subpage
; page
; page
= page
.parentPage
) {
394 if (page
.associatedControls
)
395 controls
= controls
.concat(page
.associatedControls
);
401 * Wraps matches in spans.
402 * @param {RegExp} regExp The search query (in regexp form).
403 * @param {Element} element An HTML container element to recursively search
405 * @return {boolean} true if the element was changed.
408 highlightMatches_: function(regExp
, element
) {
412 // Walk the tree, searching each TEXT node.
413 var walker
= document
.createTreeWalker(element
,
414 NodeFilter
.SHOW_TEXT
,
417 var node
= walker
.nextNode();
419 var textContent
= node
.nodeValue
;
420 // Perform a search and replace on the text node value.
421 var split
= textContent
.split(regExp
);
422 if (split
.length
> 1) {
424 var nextNode
= walker
.nextNode();
425 var parentNode
= node
.parentNode
;
426 // Use existing node as placeholder to determine where to insert the
427 // replacement content.
428 for (var i
= 0; i
< split
.length
; ++i
) {
430 parentNode
.insertBefore(document
.createTextNode(split
[i
]), node
);
432 var span
= document
.createElement('span');
433 span
.className
= 'search-highlighted';
434 span
.textContent
= split
[i
];
435 parentNode
.insertBefore(span
, node
);
439 parentNode
.removeChild(node
);
442 node
= walker
.nextNode();
450 * Removes all search highlight tags from the document.
453 unhighlightMatches_: function() {
454 // Find all search highlight elements.
455 var elements
= document
.querySelectorAll('.search-highlighted');
457 // For each element, remove the highlighting.
459 for (var i
= 0, node
; node
= elements
[i
]; i
++) {
460 parent
= node
.parentNode
;
462 // Replace the highlight element with the first child (the text node).
463 parent
.replaceChild(node
.firstChild
, node
);
465 // Normalize the parent so that multiple text nodes will be combined.
471 * Creates a search result bubble attached to an element.
472 * @param {Element} element An HTML element, usually a button.
473 * @param {string} text A string to show in the bubble.
476 createSearchBubble_: function(element
, text
) {
477 // avoid appending multiple bubbles to a button.
478 var sibling
= element
.previousElementSibling
;
479 if (sibling
&& (sibling
.classList
.contains('search-bubble') ||
480 sibling
.classList
.contains('search-bubble-wrapper')))
483 var parent
= element
.parentElement
;
485 var bubble
= new SearchBubble(text
);
486 bubble
.attachTo(element
);
487 bubble
.updatePosition();
492 * Removes all search match bubbles.
495 removeSearchBubbles_: function() {
496 var elements
= document
.querySelectorAll('.search-bubble');
497 var length
= elements
.length
;
498 for (var i
= 0; i
< length
; i
++)
499 elements
[i
].dispose();
503 * Builds a list of top-level pages to search. Omits the search page and
505 * @return {Array} An array of pages to search.
508 getSearchablePages_: function() {
509 var name
, page
, pages
= [];
510 for (name
in PageManager
.registeredPages
) {
511 if (name
!= this.name
) {
512 page
= PageManager
.registeredPages
[name
];
513 if (!page
.parentPage
)
521 * Builds a list of sub-pages (and overlay pages) to search. Ignore pages
522 * that have no associated controls, or whose controls are hidden.
523 * @return {Array} An array of pages to search.
526 getSearchableSubPages_: function() {
527 var name
, pageInfo
, page
, pages
= [];
528 for (name
in PageManager
.registeredPages
) {
529 page
= PageManager
.registeredPages
[name
];
530 if (page
.parentPage
&&
531 page
.associatedSection
&&
532 !page
.associatedSection
.hidden
) {
536 for (name
in PageManager
.registeredOverlayPages
) {
537 page
= PageManager
.registeredOverlayPages
[name
];
538 if (page
.associatedSection
&&
539 !page
.associatedSection
.hidden
&&
540 page
.pageDiv
!= undefined) {
548 * A function to handle key press events.
549 * @param {Event} event A keydown event.
552 keyDownEventHandler_: function(event
) {
553 /** @const */ var ESCAPE_KEY_CODE
= 27;
554 /** @const */ var FORWARD_SLASH_KEY_CODE
= 191;
556 switch (event
.keyCode
) {
557 case ESCAPE_KEY_CODE
:
558 if (event
.target
== this.searchField
) {
559 this.setSearchText_('');
560 this.searchField
.blur();
561 event
.stopPropagation();
562 event
.preventDefault();
565 case FORWARD_SLASH_KEY_CODE
:
566 if (!/INPUT|SELECT|BUTTON|TEXTAREA/.test(event
.target
.tagName
) &&
567 !event
.ctrlKey
&& !event
.altKey
) {
568 this.searchField
.focus();
569 event
.stopPropagation();
570 event
.preventDefault();
578 * Standardizes a user-entered text query by removing extra whitespace.
579 * @param {string} text The user-entered text.
580 * @return {string} The trimmed query.
582 SearchPage
.canonicalizeQuery = function(text
) {
583 // Trim beginning and ending whitespace.
584 return text
.replace(/^\s+|\s+$/g, '');
589 SearchPage
: SearchPage