Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / trace_viewer / tracing / tracks / 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 /**
8  * @fileoverview Renders an array of slices into the provided div,
9  * using a child canvas element. Uses a FastRectRenderer to draw only
10  * the visible slices.
11  */
12
13 tvcm.requireStylesheet('tracing.tracks.track');
14
15 tvcm.require('tvcm.ui');
16 tvcm.require('tvcm.ui.container_that_decorates_its_children');
17
18 tvcm.exportTo('tracing.tracks', function() {
19   /**
20    * The base class for all tracks.
21    * @constructor
22    */
23   var Track = tvcm.ui.define('track',
24                              tvcm.ui.ContainerThatDecoratesItsChildren);
25   Track.prototype = {
26     __proto__: tvcm.ui.ContainerThatDecoratesItsChildren.prototype,
27
28     decorate: function(viewport) {
29       tvcm.ui.ContainerThatDecoratesItsChildren.prototype.decorate.call(this);
30       if (viewport === undefined)
31         throw new Error('viewport is required when creating a Track.');
32
33       this.viewport_ = viewport;
34       this.classList.add('track');
35     },
36
37     get viewport() {
38       return this.viewport_;
39     },
40
41     context: function() {
42       // This is a little weird here, but we have to be able to walk up the
43       // parent tree to get the context.
44       if (!this.parentNode)
45         return undefined;
46       if (!this.parentNode.context)
47         throw new Error('Parent container does not support context() method.');
48       return this.parentNode.context();
49     },
50
51     decorateChild_: function(childTrack) {
52     },
53
54     undecorateChild_: function(childTrack) {
55       if (childTrack.detach)
56         childTrack.detach();
57     },
58
59     updateContents_: function() {
60     },
61
62     drawTrack: function(type) {
63       var ctx = this.context();
64
65       var pixelRatio = window.devicePixelRatio || 1;
66       var bounds = this.getBoundingClientRect();
67       var canvasBounds = ctx.canvas.getBoundingClientRect();
68
69       ctx.save();
70       ctx.translate(0, pixelRatio * (bounds.top - canvasBounds.top));
71
72       var dt = this.viewport.currentDisplayTransform;
73       var viewLWorld = dt.xViewToWorld(0);
74       var viewRWorld = dt.xViewToWorld(bounds.width * pixelRatio);
75
76       this.draw(type, viewLWorld, viewRWorld);
77       ctx.restore();
78     },
79
80     draw: function(type, viewLWorld, viewRWorld) {
81     },
82
83     addEventsToTrackMap: function(eventToTrackMap) {
84     },
85
86     addIntersectingItemsInRangeToSelection: function(
87         loVX, hiVX, loVY, hiVY, selection) {
88
89       var pixelRatio = window.devicePixelRatio || 1;
90       var dt = this.viewport.currentDisplayTransform;
91       var viewPixWidthWorld = dt.xViewVectorToWorld(1);
92       var loWX = dt.xViewToWorld(loVX * pixelRatio);
93       var hiWX = dt.xViewToWorld(hiVX * pixelRatio);
94
95       var clientRect = this.getBoundingClientRect();
96       var a = Math.max(loVY, clientRect.top);
97       var b = Math.min(hiVY, clientRect.bottom);
98       if (a > b)
99         return;
100
101       this.addIntersectingItemsInRangeToSelectionInWorldSpace(
102           loWX, hiWX, viewPixWidthWorld, selection);
103     },
104
105     addIntersectingItemsInRangeToSelectionInWorldSpace: function(
106         loWX, hiWX, viewPixWidthWorld, selection) {
107     },
108
109     /**
110      * Gets implemented by supporting track types. The method adds the event
111      * closest to worldX to the selection.
112      *
113      * @param {number} worldX The position that is looked for.
114      * @param {number} worldMaxDist The maximum distance allowed from worldX to
115      *     the event.
116      * @param {number} loY Lower Y bound of the search interval in view space.
117      * @param {number} hiY Upper Y bound of the search interval in view space.
118      * @param {Selection} selection Selection to which to add hits.
119      */
120     addClosestEventToSelection: function(
121         worldX, worldMaxDist, loY, hiY, selection) {
122     },
123
124     addClosestInstantEventToSelection: function(instantEvents, worldX,
125                                                 worldMaxDist, selection) {
126       var instantEvent = tvcm.findClosestElementInSortedArray(
127           instantEvents,
128           function(x) { return x.start; },
129           worldX,
130           worldMaxDist);
131
132       if (!instantEvent)
133         return;
134
135       selection.push(instantEvent);
136     }
137   };
138
139   return {
140     Track: Track
141   };
142 });