-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy paththeme-snap.js
292 lines (243 loc) · 8.7 KB
/
theme-snap.js
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/** js sequence diagrams
* https://bramp.github.io/js-sequence-diagrams/
* (c) 2012-2017 Andrew Brampton (bramp.net)
* Simplified BSD license.
*/
/*global Diagram, Snap, WebFont _ */
// TODO Move defintion of font onto the <svg>, so it can easily be override at each level
if (typeof Snap != 'undefined') {
var xmlns = 'http://www.w3.org/2000/svg';
var LINE = {
'stroke': '#000000',
'stroke-width': 2, // BUG TODO This gets set as a style, not as a attribute. Look at eve.on("snap.util.attr"...
'fill': 'none'
};
var RECT = {
'stroke': '#000000',
'stroke-width': 2,
'fill': '#fff'
};
var LOADED_FONTS = {};
/******************
* SnapTheme
******************/
var SnapTheme = function(diagram, options, resume) {
_.defaults(options, {
'css-class': 'simple',
'font-size': 16,
'font-family': 'Andale Mono, monospace'
});
this.init(diagram, options, resume);
};
_.extend(SnapTheme.prototype, BaseTheme.prototype, {
init: function(diagram, options, resume) {
BaseTheme.prototype.init.call(this, diagram);
this.paper_ = undefined;
this.cssClass_ = options['css-class'] || undefined;
this.font_ = {
'font-size': options['font-size'],
'font-family': options['font-family']
};
var a = this.arrowTypes_ = {};
a[ARROWTYPE.FILLED] = 'Block';
a[ARROWTYPE.OPEN] = 'Open';
var l = this.lineTypes_ = {};
l[LINETYPE.SOLID] = '';
l[LINETYPE.DOTTED] = '6,2';
var that = this;
this.waitForFont(function() {
resume(that);
});
},
// Wait for loading of the font
waitForFont: function(callback) {
var fontFamily = this.font_['font-family'];
if (typeof WebFont == 'undefined') {
throw new Error('WebFont is required (https://github.com/typekit/webfontloader).');
}
if (LOADED_FONTS[fontFamily]) {
// If already loaded, just return instantly.
callback();
return;
}
WebFont.load({
custom: {
families: [fontFamily] // TODO replace this with something that reads the css
},
classes: false, // No need to place classes on the DOM, just use JS Events
active: function() {
LOADED_FONTS[fontFamily] = true;
callback();
},
inactive: function() {
// If we fail to fetch the font, still continue.
LOADED_FONTS[fontFamily] = true;
callback();
}
});
},
addDescription: function(svg, description) {
var desc = document.createElementNS(xmlns, 'desc');
desc.appendChild(document.createTextNode(description));
svg.appendChild(desc);
},
setupPaper: function(container) {
// Container must be a SVG element. We assume it's a div, so lets create a SVG and insert
var svg = document.createElementNS(xmlns, 'svg');
container.appendChild(svg);
this.addDescription(svg, this.diagram.title || '');
this.paper_ = Snap(svg);
this.paper_.addClass('sequence');
if (this.cssClass_) {
this.paper_.addClass(this.cssClass_);
}
this.beginGroup();
// TODO Perhaps only include the markers if we actually use them.
var a = this.arrowMarkers_ = {};
var arrow = this.paper_.path('M 0 0 L 5 2.5 L 0 5 z');
a[ARROWTYPE.FILLED] = arrow.marker(0, 0, 5, 5, 5, 2.5)
.attr({id: 'markerArrowBlock'});
arrow = this.paper_.path('M 9.6,8 1.92,16 0,13.7 5.76,8 0,2.286 1.92,0 9.6,8 z');
a[ARROWTYPE.OPEN] = arrow.marker(0, 0, 9.6, 16, 9.6, 8)
.attr({markerWidth: '4', id: 'markerArrowOpen'});
},
layout: function() {
BaseTheme.prototype.layout.call(this);
this.paper_.attr({
width: this.diagram.width + 'px',
height: this.diagram.height + 'px'
});
},
textBBox: function(text, font) {
// TODO getBBox will return the bounds with any whitespace/kerning. This makes some of our aligments screwed up
var t = this.createText(text, font);
var bb = t.getBBox();
t.remove();
return bb;
},
// For each drawn element, push onto the stack, so it can be wrapped in a single outer element
pushToStack: function(element) {
this._stack.push(element);
return element;
},
// Begin a group of elements
beginGroup: function() {
this._stack = [];
},
// Finishes the group, and returns the <group> element
finishGroup: function(groupName, lineno) {
var g = this.paper_.group.apply(this.paper_, this._stack);
this.beginGroup();
if (groupName) {
g.addClass(groupName);
}
if (lineno !== undefined) {
g.attr({'data-lineno': lineno + 1}); // +1 to correct jison line numbering
}
return g;
},
createText: function(text, font) {
text = _.invoke(text.split('\n'), 'trim');
var t = this.paper_.text(0, 0, text);
t.attr(font || {});
if (text.length > 1) {
// Every row after the first, set tspan to be 1.2em below the previous line
t.selectAll('tspan:nth-child(n+2)').attr({
dy: '1.2em',
x: 0
});
}
return t;
},
drawLine: function(x1, y1, x2, y2, linetype, arrowhead) {
var line = this.paper_.line(x1, y1, x2, y2).attr(LINE);
if (linetype !== undefined) {
line.attr('strokeDasharray', this.lineTypes_[linetype]);
}
if (arrowhead !== undefined) {
line.attr('markerEnd', this.arrowMarkers_[arrowhead]);
}
return this.pushToStack(line);
},
drawRect: function(x, y, w, h) {
var rect = this.paper_.rect(x, y, w, h).attr(RECT);
return this.pushToStack(rect);
},
/**
* Draws text with a optional white background
* x,y (int) x,y top left point of the text, or the center of the text (depending on align param)
* text (string) text to print
* font (Object)
* align (string) ALIGN_LEFT or ALIGN_CENTER
*/
drawText: function(x, y, text, font, align) {
var t = this.createText(text, font);
var bb = t.getBBox();
if (align == ALIGN_CENTER) {
x = x - bb.width / 2;
y = y - bb.height / 2;
}
// Now move the text into place
// `y - bb.y` because text(..) is positioned from the baseline, so this moves it down.
t.attr({x: x - bb.x, y: y - bb.y});
t.selectAll('tspan').attr({x: x});
this.pushToStack(t);
return t;
},
drawTitle: function() {
this.beginGroup();
BaseTheme.prototype.drawTitle.call(this);
return this.finishGroup('title', this.title_ ? this.title_.lineno : undefined);
},
drawActor: function(actor, offsetY, height) {
this.beginGroup();
BaseTheme.prototype.drawActor.call(this, actor, offsetY, height);
return this.finishGroup('actor', actor.lineno);
},
drawSignal: function(signal, offsetY) {
this.beginGroup();
BaseTheme.prototype.drawSignal.call(this, signal, offsetY);
return this.finishGroup('signal', signal.lineno);
},
drawSelfSignal: function(signal, offsetY) {
this.beginGroup();
BaseTheme.prototype.drawSelfSignal.call(this, signal, offsetY);
return this.finishGroup('signal', signal.lineno);
},
drawNote: function(note, offsetY) {
this.beginGroup();
BaseTheme.prototype.drawNote.call(this, note, offsetY);
return this.finishGroup('note', note.lineno);
},
});
/******************
* SnapHandTheme
******************/
var SnapHandTheme = function(diagram, options, resume) {
_.defaults(options, {
'css-class': 'hand',
'font-size': 16,
'font-family': 'danielbd'
});
this.init(diagram, options, resume);
};
// Take the standard SnapTheme and make all the lines wobbly
_.extend(SnapHandTheme.prototype, SnapTheme.prototype, {
drawLine: function(x1, y1, x2, y2, linetype, arrowhead) {
var line = this.paper_.path(handLine(x1, y1, x2, y2)).attr(LINE);
if (linetype !== undefined) {
line.attr('strokeDasharray', this.lineTypes_[linetype]);
}
if (arrowhead !== undefined) {
line.attr('markerEnd', this.arrowMarkers_[arrowhead]);
}
return this.pushToStack(line);
},
drawRect: function(x, y, w, h) {
var rect = this.paper_.path(handRect(x, y, w, h)).attr(RECT);
return this.pushToStack(rect);
}
});
registerTheme('snapSimple', SnapTheme);
registerTheme('snapHand', SnapHandTheme);
}