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.
8 * @fileoverview FindControl and FindController.
10 tvcm.requireTemplate('tracing.find_control');
12 tvcm.require('tracing.timeline_track_view');
13 tvcm.require('tracing.filter');
14 tvcm.exportTo('tracing', function() {
20 var FindControl = tvcm.ui.define('find-control');
22 FindControl.prototype = {
23 __proto__: HTMLUnknownElement.prototype,
25 decorate: function() {
26 var shadow = this.webkitCreateShadowRoot();
27 shadow.applyAuthorStyles = true;
28 shadow.resetStyleInheritance = true;
30 shadow.appendChild(tvcm.instantiateTemplate('#find-control-template'));
32 this.hitCountEl_ = shadow.querySelector('.hit-count-label');
34 shadow.querySelector('.find-previous')
35 .addEventListener('click', this.findPrevious_.bind(this));
37 shadow.querySelector('.find-next')
38 .addEventListener('click', this.findNext_.bind(this));
40 this.filterEl_ = shadow.querySelector('#find-control-filter');
41 this.filterEl_.addEventListener('input',
42 this.filterTextChanged_.bind(this));
44 this.filterEl_.addEventListener('keydown', function(e) {
46 if (e.keyCode == 13) {
54 this.filterEl_.addEventListener('keypress', function(e) {
58 this.filterEl_.addEventListener('blur', function(e) {
59 this.updateHitCountEl_();
62 this.filterEl_.addEventListener('focus', function(e) {
63 this.controller.reset();
64 this.filterTextChanged_();
65 this.filterEl_.select();
68 // Prevent that the input text is deselected after focusing the find
69 // control with the mouse.
70 this.filterEl_.addEventListener('mouseup', function(e) {
74 this.updateHitCountEl_();
78 return this.controller_;
83 this.updateHitCountEl_();
87 this.filterEl_.focus();
90 hasFocus: function() {
91 return this === document.activeElement;
94 filterTextChanged_: function() {
95 this.controller.filterText = this.filterEl_.value;
96 this.updateHitCountEl_();
99 findNext_: function() {
101 this.controller.findNext();
102 this.updateHitCountEl_();
105 findPrevious_: function() {
107 this.controller.findPrevious();
108 this.updateHitCountEl_();
111 updateHitCountEl_: function() {
112 if (!this.controller || !this.hasFocus()) {
113 this.hitCountEl_.textContent = '';
116 var i = this.controller.currentHitIndex;
117 var n = this.controller.filterHits.length;
119 this.hitCountEl_.textContent = '0 of 0';
121 this.hitCountEl_.textContent = (i + 1) + ' of ' + n;
125 function FindController() {
126 this.timeline_ = undefined;
127 this.model_ = undefined;
128 this.filterText_ = '';
129 this.filterHits_ = new tracing.Selection();
130 this.filterHitsDirty_ = true;
131 this.currentHitIndex_ = -1;
134 FindController.prototype = {
135 __proto__: Object.prototype,
138 return this.timeline_;
143 this.filterHitsDirty_ = true;
147 return this.filterText_;
151 if (f == this.filterText_)
153 this.filterText_ = f;
154 this.filterHitsDirty_ = true;
159 this.timeline.setHighlightAndClearSelection(this.filterHits);
163 if (this.filterHitsDirty_) {
164 this.filterHitsDirty_ = false;
165 this.filterHits_ = new tracing.Selection();
166 this.currentHitIndex_ = -1;
168 if (this.timeline_ && this.filterText.length) {
169 var filter = new tracing.TitleFilter(this.filterText);
170 this.timeline.addAllObjectsMatchingFilterToSelection(
171 filter, this.filterHits_);
174 return this.filterHits_;
177 get currentHitIndex() {
178 return this.currentHitIndex_;
181 find_: function(dir) {
182 var firstHit = this.currentHitIndex_ === -1;
183 if (firstHit && dir < 0)
184 this.currentHitIndex_ = 0;
186 var N = this.filterHits.length;
187 this.currentHitIndex_ = (this.currentHitIndex_ + dir + N) % N;
192 this.timeline.selection =
193 this.filterHits.subSelection(this.currentHitIndex_, 1);
196 findNext: function() {
200 findPrevious: function() {
205 this.filterText_ = '';
206 this.filterHitsDirty_ = true;
211 FindControl: FindControl,
212 FindController: FindController