Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / src / tracing / tracks / slice_track.js
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.
4
5 'use strict';
6
7 tvcm.requireStylesheet('tracing.tracks.slice_track');
8
9 tvcm.require('tvcm.sorted_array_utils');
10 tvcm.require('tracing.tracks.heading_track');
11 tvcm.require('tracing.fast_rect_renderer');
12 tvcm.require('tracing.draw_helpers');
13 tvcm.require('tvcm.ui');
14
15 tvcm.exportTo('tracing.tracks', function() {
16
17   /**
18    * A track that displays an array of Slice objects.
19    * @constructor
20    * @extends {HeadingTrack}
21    */
22   var SliceTrack = tvcm.ui.define(
23       'slice-track', tracing.tracks.HeadingTrack);
24
25   SliceTrack.prototype = {
26
27     __proto__: tracing.tracks.HeadingTrack.prototype,
28
29     decorate: function(viewport) {
30       tracing.tracks.HeadingTrack.prototype.decorate.call(this, viewport);
31       this.classList.add('slice-track');
32       this.asyncStyle_ = false;
33       this.slices_ = null;
34     },
35
36     get asyncStyle() {
37       return this.asyncStyle_;
38     },
39
40     set asyncStyle(v) {
41       this.asyncStyle_ = !!v;
42     },
43
44     get slices() {
45       return this.slices_;
46     },
47
48     set slices(slices) {
49       this.slices_ = slices || [];
50     },
51
52     get height() {
53       return window.getComputedStyle(this).height;
54     },
55
56     set height(height) {
57       this.style.height = height;
58     },
59
60     get hasVisibleContent() {
61       return this.slices.length > 0;
62     },
63
64     draw: function(type, viewLWorld, viewRWorld) {
65       switch (type) {
66         case tracing.tracks.DrawType.SLICE:
67           this.drawSlices_(viewLWorld, viewRWorld);
68           break;
69       }
70     },
71
72     drawSlices_: function(viewLWorld, viewRWorld) {
73       var ctx = this.context();
74
75       ctx.save();
76       var bounds = this.getBoundingClientRect();
77       tracing.drawSlices(
78           ctx,
79           this.viewport.currentDisplayTransform,
80           viewLWorld,
81           viewRWorld,
82           bounds.height,
83           this.slices_,
84           this.asyncStyle_);
85       ctx.restore();
86
87       if (bounds.height <= 8)
88         return;
89
90       tracing.drawLabels(
91           ctx,
92           this.viewport.currentDisplayTransform,
93           viewLWorld,
94           viewRWorld,
95           this.slices_,
96           this.asyncStyle_);
97     },
98
99     addEventsToTrackMap: function(eventToTrackMap) {
100       if (this.slices_ === undefined || this.slices_ === null)
101         return;
102
103       this.slices_.forEach(function(slice) {
104         eventToTrackMap.addEvent(slice, this);
105       }, this);
106     },
107
108     addIntersectingItemsInRangeToSelectionInWorldSpace: function(
109         loWX, hiWX, viewPixWidthWorld, selection) {
110       function onSlice(slice) {
111         selection.push(slice);
112       }
113       tvcm.iterateOverIntersectingIntervals(this.slices_,
114           function(x) { return x.start; },
115           function(x) { return x.duration; },
116           loWX, hiWX,
117           onSlice);
118     },
119
120     /**
121      * Find the index for the given slice.
122      * @return {index} Index of the given slice, or undefined.
123      * @private
124      */
125     indexOfSlice_: function(slice) {
126       var index = tvcm.findLowIndexInSortedArray(this.slices_,
127           function(x) { return x.start; },
128           slice.start);
129       while (index < this.slices_.length &&
130           slice.start == this.slices_[index].start &&
131           slice.colorId != this.slices_[index].colorId) {
132         index++;
133       }
134       return index < this.slices_.length ? index : undefined;
135     },
136
137     /**
138      * Add the item to the left or right of the provided event, if any, to the
139      * selection.
140      * @param {slice} The current slice.
141      * @param {Number} offset Number of slices away from the event to look.
142      * @param {Selection} selection The selection to add an event to,
143      * if found.
144      * @return {boolean} Whether an event was found.
145      * @private
146      */
147     addItemNearToProvidedEventToSelection: function(event, offset, selection) {
148       var index = this.indexOfSlice_(event);
149       if (index === undefined)
150         return false;
151
152       var newIndex = index + offset;
153       if (newIndex < 0 || newIndex >= this.slices_.length)
154         return false;
155
156       selection.push(this.slices_[newIndex]);
157       return true;
158     },
159
160     addAllObjectsMatchingFilterToSelection: function(filter, selection) {
161       for (var i = 0; i < this.slices_.length; ++i) {
162         if (filter.matchSlice(this.slices_[i]))
163           selection.push(this.slices_[i]);
164       }
165     },
166
167     addClosestEventToSelection: function(worldX, worldMaxDist, loY, hiY,
168                                          selection) {
169       var slice = tvcm.findClosestIntervalInSortedIntervals(
170           this.slices_,
171           function(x) { return x.start; },
172           function(x) { return x.end; },
173           worldX,
174           worldMaxDist);
175
176       if (slice)
177         selection.push(slice);
178     }
179   };
180
181   return {
182     SliceTrack: SliceTrack
183   };
184 });