blob: 6ba1bcdd1e6c2568098900275e78b69432700491 [file] [log] [blame]
Chris Craik93216d02015-03-05 13:58:42 -08001<!DOCTYPE html>
2<!--
3Copyright (c) 2013 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7<link rel="stylesheet" href="/core/tracks/track.css">
8<link rel="import" href="/base/ui.html">
Chris Craik93216d02015-03-05 13:58:42 -08009<link rel="import" href="/base/ui/container_that_decorates_its_children.html">
10
11<script>
12'use strict';
13
14/**
15 * @fileoverview Renders an array of slices into the provided div,
16 * using a child canvas element. Uses a FastRectRenderer to draw only
17 * the visible slices.
18 */
19tv.exportTo('tv.c.tracks', function() {
20 /**
21 * The base class for all tracks.
22 * @constructor
23 */
24 var Track = tv.b.ui.define('track',
25 tv.b.ui.ContainerThatDecoratesItsChildren);
26 Track.prototype = {
27 __proto__: tv.b.ui.ContainerThatDecoratesItsChildren.prototype,
28
29 decorate: function(viewport) {
30 tv.b.ui.ContainerThatDecoratesItsChildren.prototype.decorate.call(this);
31 if (viewport === undefined)
32 throw new Error('viewport is required when creating a Track.');
33
34 this.viewport_ = viewport;
35 this.classList.add('track');
36 },
37
38 get viewport() {
39 return this.viewport_;
40 },
41
42 get drawingContainer() {
43 var cur = this;
44 while (cur) {
45 if (cur instanceof tv.c.tracks.DrawingContainer)
46 return cur;
47 cur = cur.parentElement;
48 }
49 return undefined;
50 },
51
52 get eventContainer() {
53 },
54
55 invalidateDrawingContainer: function() {
56 var dc = this.drawingContainer;
57 if (dc)
58 dc.invalidate();
59 },
60
61 context: function() {
62 // This is a little weird here, but we have to be able to walk up the
63 // parent tree to get the context.
64 if (!this.parentNode)
65 return undefined;
66 if (!this.parentNode.context)
67 throw new Error('Parent container does not support context() method.');
68 return this.parentNode.context();
69 },
70
71 decorateChild_: function(childTrack) {
72 },
73
74 undecorateChild_: function(childTrack) {
75 if (childTrack.detach)
76 childTrack.detach();
77 },
78
79 updateContents_: function() {
80 },
81
82 drawTrack: function(type) {
83 var ctx = this.context();
84
85 var pixelRatio = window.devicePixelRatio || 1;
86 var bounds = this.getBoundingClientRect();
87 var canvasBounds = ctx.canvas.getBoundingClientRect();
88
89 ctx.save();
90 ctx.translate(0, pixelRatio * (bounds.top - canvasBounds.top));
91
92 var dt = this.viewport.currentDisplayTransform;
93 var viewLWorld = dt.xViewToWorld(0);
94 var viewRWorld = dt.xViewToWorld(bounds.width * pixelRatio);
95
96 this.draw(type, viewLWorld, viewRWorld);
97 ctx.restore();
98 },
99
100 draw: function(type, viewLWorld, viewRWorld) {
101 },
102
103 addEventsToTrackMap: function(eventToTrackMap) {
104 },
105
106 addContainersToTrackMap: function(containerToTrackMap) {
107 },
108
Chris Craik44c28202015-05-12 17:25:16 -0700109 addIntersectingEventsInRangeToSelection: function(
Chris Craik93216d02015-03-05 13:58:42 -0800110 loVX, hiVX, loVY, hiVY, selection) {
111
112 var pixelRatio = window.devicePixelRatio || 1;
113 var dt = this.viewport.currentDisplayTransform;
114 var viewPixWidthWorld = dt.xViewVectorToWorld(1);
115 var loWX = dt.xViewToWorld(loVX * pixelRatio);
116 var hiWX = dt.xViewToWorld(hiVX * pixelRatio);
117
118 var clientRect = this.getBoundingClientRect();
119 var a = Math.max(loVY, clientRect.top);
120 var b = Math.min(hiVY, clientRect.bottom);
121 if (a > b)
122 return;
123
Chris Craik44c28202015-05-12 17:25:16 -0700124 this.addIntersectingEventsInRangeToSelectionInWorldSpace(
Chris Craik93216d02015-03-05 13:58:42 -0800125 loWX, hiWX, viewPixWidthWorld, selection);
126 },
127
Chris Craik44c28202015-05-12 17:25:16 -0700128 addIntersectingEventsInRangeToSelectionInWorldSpace: function(
Chris Craik93216d02015-03-05 13:58:42 -0800129 loWX, hiWX, viewPixWidthWorld, selection) {
130 },
131
132 /**
133 * Gets implemented by supporting track types. The method adds the event
134 * closest to worldX to the selection.
135 *
136 * @param {number} worldX The position that is looked for.
137 * @param {number} worldMaxDist The maximum distance allowed from worldX to
138 * the event.
139 * @param {number} loY Lower Y bound of the search interval in view space.
140 * @param {number} hiY Upper Y bound of the search interval in view space.
141 * @param {Selection} selection Selection to which to add hits.
142 */
143 addClosestEventToSelection: function(
144 worldX, worldMaxDist, loY, hiY, selection) {
145 },
146
147 addClosestInstantEventToSelection: function(instantEvents, worldX,
148 worldMaxDist, selection) {
149 var instantEvent = tv.b.findClosestElementInSortedArray(
150 instantEvents,
151 function(x) { return x.start; },
152 worldX,
153 worldMaxDist);
154
155 if (!instantEvent)
156 return;
157
158 selection.push(instantEvent);
159 }
160 };
161
162 return {
163 Track: Track
164 };
165});
166</script>