forked from flowhub/the-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
the-graph-nav.html
171 lines (158 loc) · 5.36 KB
/
the-graph-nav.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!--
* Wraps the-graph-thumb with a current view bounding box
* Observes changes from attached instance of the-graph-editor
-->
<link rel="import" href="../the-graph-thumb/the-graph-thumb.html">
<polymer-element name="the-graph-nav" attributes="width height editor hide" touch-action="none">
<template>
<style>
#thumb,
#outcanvas {
position: absolute;
top: 0;
left: 0;
overflow: visible;
}
#viewrect {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 150px;
border: 1px solid hsla(190, 100%, 80%, 0.4);
border-style: dotted;
}
</style>
<the-graph-thumb id="thumb"
graph="{{ editor.fbpGraph }}"
thumbrectangle="{{thumbrectangle}}"
width="{{width}}" height="{{height}}"
thumbscale="{{thumbscale}}"
hide="{{hide}}"
theme="{{ editor.theme }}">
</the-graph-thumb>
<canvas id="outcanvas" width="{{width}}" height="{{height}}" style="position:absolute;top:0;left:0;"></canvas>
<div id="viewrect"></div>
</template>
<script>
(function () {
"use strict";
Polymer('the-graph-nav', {
width: 200,
height: 150,
hide: false,
thumbscale: 1,
backgroundColor: "hsla(184, 8%, 75%, 0.9)",
outsideFill: "hsla(0, 0%, 0%, 0.4)",
ready: function () {
this.viewrectangle = [0,0,500,500];
this.scaledviewrectangle = [0,0,200,150];
this.theme = "dark";
},
attached: function () {
this.style.overflow = "hidden";
this.style.cursor = "move";
this.style.width = this.width + "px";
this.style.height = this.height + "px";
// HACK way to make PolymerGestures work for now
var noop = function(){};
PolymerGestures.addEventListener(this, "track", noop);
PolymerGestures.addEventListener(this, "trackend", noop);
PolymerGestures.addEventListener(this, "tap", noop);
// Pan graph by dragging map
this.addEventListener("track", function (event) {
if (!this.editor) { return; }
// Don't pan graph
event.stopPropagation();
var x = this.editor.pan[0];
var y = this.editor.pan[1];
var panscale = this.thumbscale / this.editor.scale;
x -= event.ddx / panscale;
y -= event.ddy / panscale;
this.editor.pan = [Math.round(x), Math.round(y)];
event.preventTap();
}.bind(this));
this.addEventListener("trackend", function (event) {
// Don't pan graph
event.stopPropagation();
}.bind(this));
// Tap to fit
this.addEventListener("tap", function () {
if (!this.editor) { return; }
this.editor.triggerFit();
});
},
observe: {
'editor.scale': 'editorScaleChanged',
'editor.width': 'editorWidthChanged',
'editor.height': 'editorHeightChanged',
'editor.pan': 'editorPanChanged',
'editor.theme': 'editorThemeChanged'
},
editorChanged: function (oldEditor, newEditor) {
},
editorPanChanged: function () {
if (!this.editor.pan) { return; }
var x = this.editor.pan[0];
var y = this.editor.pan[1];
this.viewrectangle[0] = -x;
this.viewrectangle[1] = -y;
},
editorWidthChanged: function () {
this.viewrectangle[2] = parseInt(this.editor.width, 10);
},
editorHeightChanged: function () {
this.viewrectangle[3] = parseInt(this.editor.height, 10);
},
editorScaleChanged: function () {
this.scale = this.editor.scale;
},
editorThemeChanged: function () {
var style = TheGraph.nav.calculateStyleFromTheme(this.theme);
this.backgroundColor = style.backgroundColor;
this.viewBoxBorder = style.viewBoxBorder;
this.viewBoxBorder2 = style.viewBoxBorder2;
this.outsideFill = style.outsideFill;
this.style.backgroundColor = this.backgroundColor;
// Redraw rectangle
this.viewrectangleChanged();
},
viewrectangleChanged: function () {
// Canvas to grey out outside view
var context = this.$.outcanvas.getContext('2d');
context = unwrap(context);
var properties = {
width: this.width,
height: this.height,
scale: this.scale,
thumbscale: this.thumbscale,
thumbrectangle: this.thumbrectangle,
viewrectangle: this.viewrectangle,
viewBoxBorder: this.viewBoxBorder,
viewBoxBorder2: this.viewBoxBorder2,
outsideFill: this.outsideFill,
};
var nav = TheGraph.nav.render(context, this.$.viewrect, properties);
this.hide = nav.hide;
// this.scaledviewrectangle = [x, y, w, h];
},
hideChanged: function () {
if (this.hide) {
this.style.display = "none";
} else {
this.style.display = "block";
}
},
thumbscaleChanged: function () {
this.viewrectangleChanged();
},
thumbrectangleChanged: function () {
// Binding this from the-graph-thumb to know the dimensions rendered
var w = this.thumbrectangle[2];
var h = this.thumbrectangle[3];
this.thumbscale = (w>h) ? this.width/w : this.height/h;
}
});
})();
</script>
</polymer-element>