@@ -180,6 +180,7 @@ _.extend(BaseTheme.prototype, {
180
180
var font = this . font_ ;
181
181
var actors = diagram . actors ;
182
182
var signals = diagram . signals ;
183
+ var theme = this ;
183
184
184
185
diagram . width = 0 ; // min width
185
186
diagram . height = 0 ; // min height
@@ -230,69 +231,76 @@ _.extend(BaseTheme.prototype, {
230
231
}
231
232
}
232
233
233
- _ . each ( signals , _ . bind ( function ( s ) {
234
- // Indexes of the left and right actors involved
235
- var a ;
236
- var b ;
234
+ function buildSignalLayout ( signal ) {
235
+ var a , b ;
236
+ var bb = theme . textBBox ( signal . message , font ) ;
237
237
238
- var bb = this . textBBox ( s . message , font ) ;
238
+ signal . textBB = bb ;
239
+ signal . width = bb . width + ( SIGNAL_MARGIN + SIGNAL_PADDING ) * 2 ;
240
+ signal . height = bb . height + ( SIGNAL_MARGIN + SIGNAL_PADDING ) * 2 ;
239
241
240
- s . textBB = bb ;
241
- s . width = bb . width ;
242
- s . height = bb . height ;
243
-
244
- var extraWidth = 0 ;
242
+ if ( signal . isSelf ( ) ) {
243
+ // TODO Self signals need a min height
244
+ a = signal . actorA . index ;
245
+ b = a + 1 ;
246
+ signal . width += SELF_SIGNAL_WIDTH ;
247
+ } else {
248
+ a = Math . min ( signal . actorA . index , signal . actorB . index ) ;
249
+ b = Math . max ( signal . actorA . index , signal . actorB . index ) ;
250
+ }
245
251
246
- if ( s . type == 'Signal' ) {
252
+ actorEnsureDistance ( a , b , signal . width ) ;
253
+ }
247
254
248
- s . width += ( SIGNAL_MARGIN + SIGNAL_PADDING ) * 2 ;
249
- s . height += ( SIGNAL_MARGIN + SIGNAL_PADDING ) * 2 ;
255
+ function buildNoteLayout ( signal ) {
256
+ var a , b ;
257
+ var bb = theme . textBBox ( signal . message , font ) ;
258
+
259
+ signal . textBB = bb ;
260
+ signal . width = bb . width + ( NOTE_MARGIN + NOTE_PADDING ) * 2 ;
261
+ signal . height = bb . height + ( NOTE_MARGIN + NOTE_PADDING ) * 2 ;
262
+
263
+ // HACK lets include the actor'signal padding
264
+ var extraWidth = 2 * ACTOR_MARGIN ;
265
+
266
+ if ( signal . placement == PLACEMENT . LEFTOF ) {
267
+ b = signal . actor . index ;
268
+ a = b - 1 ;
269
+ actorEnsureDistance ( a , b , signal . width + extraWidth ) ;
270
+ } else if ( signal . placement == PLACEMENT . RIGHTOF ) {
271
+ a = signal . actor . index ;
272
+ b = a + 1 ;
273
+ actorEnsureDistance ( a , b , signal . width + extraWidth ) ;
274
+ } else if ( signal . placement == PLACEMENT . OVER && signal . hasManyActors ( ) ) {
275
+ // Over multiple actors
276
+ a = Math . min ( signal . actor [ 0 ] . index , signal . actor [ 1 ] . index ) ;
277
+ b = Math . max ( signal . actor [ 0 ] . index , signal . actor [ 1 ] . index ) ;
278
+
279
+ // We don't need our padding, and we want to overlap
280
+ extraWidth = - ( NOTE_PADDING * 2 + NOTE_OVERLAP * 2 ) ;
281
+ actorEnsureDistance ( a , b , signal . width + extraWidth ) ;
282
+ } else if ( signal . placement == PLACEMENT . OVER ) {
283
+ // Over single actor
284
+ a = signal . actor . index ;
285
+ actorEnsureDistance ( a - 1 , a , signal . width / 2 ) ;
286
+ actorEnsureDistance ( a , a + 1 , signal . width / 2 ) ;
287
+ }
288
+ }
250
289
251
- if ( s . isSelf ( ) ) {
252
- // TODO Self signals need a min height
253
- a = s . actorA . index ;
254
- b = a + 1 ;
255
- s . width += SELF_SIGNAL_WIDTH ;
256
- } else {
257
- a = Math . min ( s . actorA . index , s . actorB . index ) ;
258
- b = Math . max ( s . actorA . index , s . actorB . index ) ;
259
- }
290
+ var layoutBuilders = {
291
+ Signal : buildSignalLayout ,
292
+ Note : buildNoteLayout
293
+ } ;
260
294
261
- } else if ( s . type == 'Note' ) {
262
- s . width += ( NOTE_MARGIN + NOTE_PADDING ) * 2 ;
263
- s . height += ( NOTE_MARGIN + NOTE_PADDING ) * 2 ;
264
-
265
- // HACK lets include the actor's padding
266
- extraWidth = 2 * ACTOR_MARGIN ;
267
-
268
- if ( s . placement == PLACEMENT . LEFTOF ) {
269
- b = s . actor . index ;
270
- a = b - 1 ;
271
- } else if ( s . placement == PLACEMENT . RIGHTOF ) {
272
- a = s . actor . index ;
273
- b = a + 1 ;
274
- } else if ( s . placement == PLACEMENT . OVER && s . hasManyActors ( ) ) {
275
- // Over multiple actors
276
- a = Math . min ( s . actor [ 0 ] . index , s . actor [ 1 ] . index ) ;
277
- b = Math . max ( s . actor [ 0 ] . index , s . actor [ 1 ] . index ) ;
278
-
279
- // We don't need our padding, and we want to overlap
280
- extraWidth = - ( NOTE_PADDING * 2 + NOTE_OVERLAP * 2 ) ;
281
-
282
- } else if ( s . placement == PLACEMENT . OVER ) {
283
- // Over single actor
284
- a = s . actor . index ;
285
- actorEnsureDistance ( a - 1 , a , s . width / 2 ) ;
286
- actorEnsureDistance ( a , a + 1 , s . width / 2 ) ;
287
- this . signalsHeight_ += s . height ;
288
-
289
- return ; // Bail out early
290
- }
295
+ _ . each ( signals , _ . bind ( function ( s ) {
296
+ // Indexes of the left and right actors involved
297
+ var buildLayout = layoutBuilders [ s . type ] ;
298
+ if ( buildLayout ) {
299
+ buildLayout ( s ) ;
291
300
} else {
292
301
throw new Error ( 'Unhandled signal type:' + s . type ) ;
293
302
}
294
303
295
- actorEnsureDistance ( a , b , s . width + extraWidth ) ;
296
304
this . signalsHeight_ += s . height ;
297
305
} , this ) ) ;
298
306
0 commit comments