Debian Codegraph @

htrace-htraced/go/web/app/triangle_button.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 // Triangle button widget.
23 htrace.TriangleButton = function(params) {
24 this.fgColor = "#6600ff";
25 this.bgColor = "#ffffff";
26 this.selected = false;
27 this.direction = "down";
28
29 this.draw = function() {
30 this.ctx.save();
31 var fg = this.selected ? this.bgColor : this.fgColor;
32 var bg = this.selected ? this.fgColor : this.bgColor;
33 this.ctx.beginPath();
34 this.ctx.rect(this.x0, this.y0,
35 this.xF - this.x0, this.yF - this.y0);
36 this.ctx.clip();
37 this.ctx.fillStyle = bg;
38 this.ctx.strokeStyle = fg;
39 this.ctx.fillRect(this.x0, this.y0,
40 this.xF - this.x0, this.yF - this.y0);
41 this.ctx.lineWidth = 3;
42 this.ctx.strokeRect(this.x0, this.y0,
43 this.xF - this.x0, this.yF - this.y0);
44 var xPad = (this.xF - this.x0) / 5;
45 var yPad = (this.yF - this.y0) / 5;
46 this.ctx.fillStyle = fg;
47 this.ctx.strokeStyle = fg;
48 this.ctx.beginPath();
49 this.ctx.strokeStyle = fg;
50 if (this.direction === "up") {
51 this.ctx.moveTo(Math.floor(this.x0 + ((this.xF - this.x0) / 2)),
52 this.y0 + yPad);
53 this.ctx.lineTo(this.xF - xPad, this.yF - yPad);
54 this.ctx.lineTo(this.x0 + xPad, this.yF - yPad);
55 } else if (this.direction === "down") {
56 this.ctx.moveTo(this.x0 + xPad, this.y0 + yPad);
57 this.ctx.lineTo(this.xF - xPad, this.y0 + yPad);
58 this.ctx.lineTo(Math.floor(this.x0 + ((this.xF - this.x0) / 2)),
59 this.yF - yPad);
60 } else {
61 console.log("TriangleButton: unknown direction " + this.direction);
62 }
63 this.ctx.closePath();
64 this.ctx.fill();
65 this.ctx.restore();
66 };
67
68 this.handle = function(e) {
69 switch (e.type) {
70 case "mouseDown":
71 if (!htrace.inBoundingBox(e.x, e.y,
72 this.x0, this.xF, this.y0, this.yF)) {
73 return true;
74 }
75 this.manager.register("mouseUp", this);
76 this.manager.register("mouseMove", this);
77 this.manager.register("mouseOut", this);
78 this.selected = true;
79 return false;
80 case "mouseUp":
81 if (this.selected) {
82 this.callback(e);
83 this.selected = false;
84 }
85 this.manager.unregister("mouseUp", this);
86 this.manager.unregister("mouseMove", this);
87 this.manager.unregister("mouseOut", this);
88 return true;
89 case "mouseMove":
90 this.selected = htrace.inBoundingBox(e.x, e.y,
91 this.x0, this.xF, this.y0, this.yF);
92 return true;
93 case "mouseOut":
94 this.selected = false;
95 return true;
96 case "draw":
97 this.draw();
98 return true;
99 }
100 };
101
102 for (var k in params) {
103 this[k]=params[k];
104 }
105 this.manager.register("mouseDown", this);
106 this.manager.register("draw", this);
107 return this;
108 };