@@ -180,6 +180,7 @@ _.extend(BaseTheme.prototype, {
180180 var font = this . font_ ;
181181 var actors = diagram . actors ;
182182 var signals = diagram . signals ;
183+ var theme = this ;
183184
184185 diagram . width = 0 ; // min width
185186 diagram . height = 0 ; // min height
@@ -230,69 +231,76 @@ _.extend(BaseTheme.prototype, {
230231 }
231232 }
232233
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 ) ;
237237
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 ;
239241
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+ }
245251
246- if ( s . type == 'Signal' ) {
252+ actorEnsureDistance ( a , b , signal . width ) ;
253+ }
247254
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+ }
250289
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+ } ;
260294
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 ) ;
291300 } else {
292301 throw new Error ( 'Unhandled signal type:' + s . type ) ;
293302 }
294303
295- actorEnsureDistance ( a , b , s . width + extraWidth ) ;
296304 this . signalsHeight_ += s . height ;
297305 } , this ) ) ;
298306
0 commit comments