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 Code for the viewport.
10 base.require('base.events');
11 base.require('base.guid');
12 base.require('base.range');
13 base.require('tracing.trace_model.instant_event');
14 base.require('tracing.trace_model');
16 base.exportTo('tracing', function() {
20 constructor: tracing.trace_model.Slice,
25 constructor: tracing.trace_model.InstantEvent,
27 pluralName: 'instantEvents'
30 constructor: tracing.trace_model.CounterSample,
31 name: 'counterSample',
32 pluralName: 'counterSamples'
35 constructor: tracing.trace_model.ObjectSnapshot,
36 name: 'objectSnapshot',
37 pluralName: 'objectSnapshots'
40 constructor: tracing.trace_model.ObjectInstance,
41 name: 'objectInstance',
42 pluralName: 'objectInstances'
45 constructor: tracing.trace_model.Sample,
52 * Represents a selection within a and its associated set of tracks.
55 function Selection(opt_events) {
56 this.bounds_dirty_ = true;
57 this.bounds_ = new base.Range();
59 this.guid_ = base.GUID.allocate();
62 for (var i = 0; i < opt_events.length; i++)
63 this.push(opt_events[i]);
66 Selection.prototype = {
67 __proto__: Object.prototype,
70 if (this.bounds_dirty_) {
72 for (var i = 0; i < this.length_; i++)
73 this[i].addBoundsToRange(this.bounds_);
74 this.bounds_dirty_ = false;
80 if (this.bounds_.isEmpty)
82 return this.bounds_.max - this.bounds_.min;
94 for (var i = 0; i < this.length_; ++i)
97 this.bounds_dirty_ = true;
100 push: function(event) {
101 this[this.length_++] = event;
102 this.bounds_dirty_ = true;
106 addSelection: function(selection) {
107 for (var i = 0; i < selection.length; i++)
108 this.push(selection[i]);
111 subSelection: function(index, count) {
114 var selection = new Selection();
115 selection.bounds_dirty_ = true;
116 if (index < 0 || index + count > this.length_)
117 throw new Error('Index out of bounds');
119 for (var i = index; i < index + count; i++)
120 selection.push(this[i]);
125 getEventsOrganizedByType: function() {
127 EVENT_TYPES.forEach(function(eventType) {
128 events[eventType.pluralName] = new Selection();
130 for (var i = 0; i < this.length_; i++) {
132 EVENT_TYPES.forEach(function(eventType) {
133 if (event instanceof eventType.constructor)
134 events[eventType.pluralName].push(event);
140 enumEventsOfType: function(type, func) {
141 for (var i = 0; i < this.length_; i++)
142 if (this[i] instanceof type)
147 for (var i = 0; i < this.length_; i++)
152 * Helper for selection previous or next.
153 * @param {boolean} forwardp If true, select one forward (next).
154 * Else, select previous.
156 * @param {TimelineViewport} viewport The viewport to use to determine what
157 * is near to the current selection.
159 * @return {boolean} true if current selection changed.
161 getShiftedSelection: function(viewport, offset) {
162 var newSelection = new Selection();
163 for (var i = 0; i < this.length_; i++) {
165 var track = viewport.trackForEvent(event);
166 track.addItemNearToProvidedEventToSelection(
167 event, offset, newSelection);
170 if (newSelection.length == 0)