-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConn.vue
222 lines (199 loc) · 6.29 KB
/
Conn.vue
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<template>
<g
:class="['conn', {
't' : !isList && !isRef,
'l' : isList,
'r' : isRef,
'uc': isUC,
}]"
>
<line
v-if="!isRef && drawBack"
:x1="backX1 - lwh"
:y1="backY + h"
:x2="backX2 + lwh + h + h"
:y2="backY + h"
:style="backStyle"
class="back"
/>
<line
v-if="isList && drawBack"
:x1="backX1 - lwh"
:y1="backY + h + sizes.connListBackSep"
:x2="backX2 + lwh + h + h"
:y2="backY + h + sizes.connListBackSep"
:style="backStyle"
class="back two"
/>
<line
v-if="isRef && drawBack"
:x1="backX1 - lwh"
:y1="backY + h"
:x2="backXM"
:y2="backY + h"
:style="backStyle"
:stroke-dasharray="sizes.connRefDashes"
class="back left"
/>
<line
v-if="isRef && drawBack"
:x1="backX2 + lwh + h + h"
:y1="backY + h"
:x2="backXM"
:y2="backY + h"
:style="backStyle"
:stroke-dasharray="sizes.connRefDashes"
class="back right"
/>
<conn-leg
v-for="(leg, i) in legs"
:key="i"
:leg="leg"
:sizes="sizes"
:l="l"
/>
<conn-leg-stub
v-if="legStub"
:stub="legStub"
:sizes="sizes"
:l="l"
/>
</g>
</template>
<script>
import ConnLeg from './ConnLeg.vue';
import ConnLegStub from './ConnLegStub.vue';
const mid = (a, b) => ~~((a + b - 1) / 2);
export default {
name: 'Conn',
components: {
'conn-leg': ConnLeg,
'conn-leg-stub': ConnLegStub
},
props: {
conn: {
type: Object,
required: true
},
sizes: {
type: Object,
required: true
},
levelTop: {
type: Function,
required: true
},
termX1s: {
type: Array,
required: true
},
termX2s: {
type: Array,
required: true
}
},
computed: {
/**
* These three functions help with precise positioning of lines relative
* to each other.
* + They help with making lines being drawn sharply. Note e.g. that for a
* horizontal line at height `y`, with strokeWidth 1, the browser draws it
* from half a pixel above to half a pixel below `y`. So it draws the line
* anti-aliased over two rows of pixels, which makes it look blurry. But:
* a stroke at <int>`y` + 0.5 draws on 1 row of pixels only, i.e. sharply.
* + But, adding 0.5 must not be done for an even strokeWidth. E.g. for
* strokeWidth 2, it fills 1 pixel-row above and 1 below, perfectly.
* + The horizontal backbone and vertical legs at its outer left/right side
* must visually join perfectly. Therefore, the backbone starts and ends
* half a strokeWidth more left/right, and legs start half a strokeWidth
* lower. This again is adjusted for even/odd strokeWidth, for sharpness.
* + We must use <line>s and not <rect>s (which would be simpler wrt. coos),
* because the ref-conn must be drawn with dashes, which only <line>s can.
*/
h () { return this.sizes.connLineWidth % 2 / 2 },
lw () { return this.sizes.connLineWidth },
lwh () { return ~~(this.lw / 2) },
// Combines the three above, so we can easily pass them using one prop.
l() { return { h: this.h, lw: this.lw, lwh: this.lwh } },
isRef () { return this.conn.type == 'R' },
isList() { return this.conn.type == 'L' },
isUC () { return this.legs.filter(o => o.isUC).length > 0 },
realPos() { return this.conn.pos.filter(p => p >= 0) },
posA() { return Math.min(...this.realPos) }, // Outer left Term position.
posZ() { return Math.max(...this.realPos) }, // Outer right Term position.
// Where the backbone starts & ends.
backX1() {
return mid(this.termX1s[this.conn.posA], this.termX2s[this.conn.posA]) ||0;
},
backX2() {
return mid(this.termX1s[this.conn.posZ], this.termX2s[this.conn.posZ]) ||0;
},
// `backM` helps split the ref-conn in two parts, and drawing both from side
// to middle. This makes its dashes (never gaps) always connect to the sides.
backXM() { return (this.backX1 + this.backX2 - 1) / 2 + 0.5 },
backY() {
return this.levelTop(this.conn.backLevel) +
this.sizes.connBackDepth;
},
// `drawBack` tells if the backbone should be drawn. It should not be drawn
// for an under-construction connector with only one leg so far.
drawBack() {
return this.conn.pos.length > 1;
},
backStyle () {
return `stroke: ${this.sizes.connBackColor}; stroke-width: ${this.lw};`;
},
legs() {
return this.conn.pos.reduce((a, p, i) => {
if (p >= 0) {
var type =
this.conn.type =='T' ? ( !i ? 'S' : i==1 ? 'R' : i==2 ? 'O' : ''):
this.conn.type =='L' ? ( !i ? 'L' : 'E' ) :
this.conn.type=='R' ? ( !i ? 'C' : i==1 ? 'P' : '' ) : '';
if (type) {
var footX1 = this.termX1s[p] || 0;
var footX2 = this.termX2s[p] || 0;
var footLevel = this.conn.footLevels[i];
var isUC = this.conn.isUC && i == this.conn.pos.length - 1;
a.push({
x: mid(footX1, footX2),
y1: this.backY,
y2: this.levelTop(footLevel) + this.sizes.connFootDepth,
footX1,
footX2,
type,
isUC,
// Tells if a list-leg is at the same position of a previous one.
doublesUp: this.conn.pos.slice(0, i).includes(p)
});
}
}
return a;
}, []);
///console.log(JSON.stringify(q));
///return q;
},
legStub() {
const isRev = (a, b) => this.conn.pos[a] > this.conn.pos[b];
if (this.conn.type == 'T') {
var y1 = this.backY + this.h;
if ( this.conn.pos[0] < 0) {
var rev = isRev(1, 2);
return { type: 'S',
side: rev ? 1 : -1, x: rev ? this.backX2 : this.backX1, y1 };
}
else if (this.conn.pos[1] < 0) {
return { type: 'R',
side: 0, x: this.backXM, y1 };
}
else if (this.conn.pos[2] < 0) {
rev = isRev(0, 1);
return { type: 'O',
side: rev ? -1 : 1, x: rev ? this.backX1 : this.backX2, y1 };
}
}
return false;
}
}
};
</script>