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