Debian Codegraph @

htrace-htraced/go/web/app/time_cursor.js

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 var htrace = htrace || {};
21
22 // Draws a vertical bar selecting a time.
23 htrace.TimeCursor = function(params) {
24 this.positionToTime = function(x) {
25 if ((x < this.x0) || (x > this.xF)) {
26 return -1;
27 }
28 return this.begin +
29 (((x - this.x0) * (this.end - this.begin)) / (this.xF - this.x0));
30 };
31
32 this.timeToPosition = function(time) {
33 return this.x0 + (((time - this.begin) *
34 (this.xF - this.x0)) / (this.end - this.begin));
35 };
36
37 this.draw = function() {
38 if (this.selectedTime != -1) {
39 this.ctx.save();
40 this.ctx.beginPath();
41 this.ctx.rect(this.x0, this.y0,
42 this.xF - this.x0, this.yF - this.y0);
43 this.ctx.clip();
44 this.ctx.strokeStyle="#ff0000";
45 var x = this.timeToPosition(this.selectedTime);
46 this.ctx.beginPath();
47 this.ctx.moveTo(x, this.y0);
48 this.ctx.lineTo(x, this.yF);
49 this.ctx.stroke();
50 this.ctx.restore();
51 }
52 };
53
54 this.handle = function(e) {
55 switch (e.type) {
56 case "mouseMove":
57 if (htrace.inBoundingBox(e.x, e.y,
58 this.x0, this.xF, this.y0, this.yF)) {
59 this.selectedTime = this.positionToTime(e.x);
60 if (this.selectedTime < 0) {
61 $(this.el).val("");
62 } else {
63 $(this.el).val(htrace.dateToString(this.selectedTime));
64 }
65 return true;
66 }
67 return true;
68 case "draw":
69 this.draw();
70 return true;
71 }
72 };
73
74 this.selectedTime = -1;
75 for (var k in params) {
76 this[k]=params[k];
77 }
78 this.manager.register("mouseMove", this);
79 this.manager.register("draw", this);
80 return this;
81 };