@@ -23,23 +23,24 @@ joint.shapes.uml.Class = joint.shapes.basic.Generic.extend({
2323 '<rect class="uml-class-methods-rect"/>' ,
2424 '<text class="uml-class-methods-label">Methods</text>' ,
2525 '</g>' ,
26- //'<image xlink:href="img/icons/yellowCube.png" x="3" y="3" width="12" height="12"/>',
27- //'<text fill="black" font-size="12" x="16" y="13">System</text>',
2826 '<text class="uml-class-name-text"/>' ,
2927 '<text class="uml-class-params-text"/>' ,
3028 '<text class="uml-class-attrs-text"/>' ,
3129 '<text class="uml-class-methods-text"/>' ,
3230 '</g>'
3331 ] . join ( '' ) ,
3432
33+ HEAD_EMPTY_LINES : 0 , // controls number of empty lines in header
34+
3535 defaults : joint . util . deepSupplement ( {
3636
3737 type : 'uml.Class' ,
3838
39- size : { width : 300 , height : 300 } ,
39+ MIN_WIDTH : 100 ,
40+ size : { width : 0 , height : 300 } ,
4041
4142 attrs : {
42- rect : { 'width' : 200 } ,
43+ rect : { 'width' : 0 } ,
4344
4445 '.uml-class-name-rect' : { 'stroke' : 'black' , 'stroke-width' : 1 , 'fill' : '#3498db' } ,
4546 '.uml-class-params-rect' : { 'stroke' : 'black' , 'stroke-width' : 1 , 'fill' : 'white' } ,
@@ -77,27 +78,72 @@ joint.shapes.uml.Class = joint.shapes.basic.Generic.extend({
7778 name : [ ] ,
7879 params : [ ] ,
7980 attributes : [ ] ,
80- methods : [ ]
81+ methods : [ ] ,
82+ classSigns : [ ]
8183
8284 } , joint . shapes . basic . Generic . prototype . defaults ) ,
8385
84- initialize : function ( ) {
86+ initialize : function ( ) {
87+
88+ var rects = [
89+ { type : 'name' , text : this . getClassName ( ) } ,
90+ { type : 'params' , text : this . get ( 'params' ) } ,
91+ { type : 'attrs' , text : this . get ( 'attributes' ) } ,
92+ { type : 'methods' , text : this . get ( 'methods' ) }
93+ ] ,
94+ self = this ,
95+ classSigns = this . get ( 'classSigns' ) ,
96+ SYMBOL_12_WIDTH = this . get ( 'SYMBOL_12_WIDTH' ) || 6.6 ,
97+ i , blockWidth , left = 3 , top = 3 , w ;
98+
99+ // preserve space for sub-labels
100+ w = 0 ; for ( i in classSigns ) {
101+ w += classSigns [ i ] . text . length * SYMBOL_12_WIDTH + ( classSigns [ i ] . icon ? 13 : 0 ) + ( i ? 3 : 0 ) ;
102+ i = 1 ;
103+ }
104+
105+ this . defaults . size . width = Math . max ( this . defaults . MIN_WIDTH , Math . min ( w , 250 ) ) ;
106+ _ . each ( rects , function ( rect ) {
107+ ( rect . text instanceof Array ? rect . text : [ rect . text ] ) . forEach ( function ( s ) {
108+ var t = s . split ( "\x1b" ) [ 0 ] . length * SYMBOL_12_WIDTH + 8 ;
109+ if ( t > self . defaults . size . width ) {
110+ self . defaults . size . width = t ;
111+ }
112+ } ) ;
113+ } ) ;
114+
115+ blockWidth = this . defaults . size . width ;
85116
86- this . on ( 'change:name change:attributes change:methods' , function ( ) {
117+ if ( classSigns . length ) this . HEAD_EMPTY_LINES = 1 ;
118+
119+ for ( i in classSigns ) {
120+ w = classSigns [ i ] . text . length * SYMBOL_12_WIDTH + ( classSigns [ i ] . icon ? 13 : 0 ) ;
121+ if ( left + w - 3 > blockWidth ) { top += 12 ; left = 3 ; this . HEAD_EMPTY_LINES ++ ; }
122+ this . markup += '<g transform="translate(' + left + ', ' + top + ')">' +
123+ ( classSigns [ i ] . icon ? '<image xlink:href="' + classSigns [ i ] . icon +
124+ '" width="13" height="13"/>' : '' ) + '<text fill="black" font-size="11" ' +
125+ ( classSigns [ i ] . textStyle ? 'style="' + classSigns [ i ] . textStyle + '"' : '' ) +
126+ ' x="' + ( classSigns [ i ] . icon ? 13 : 0 ) + '" y="10">' + classSigns [ i ] . text +
127+ '</text></g>' ;
128+ left += w + 3 ;
129+ }
130+
131+ this . on ( 'change:name change:attributes change:methods' , function ( ) {
87132 this . updateRectangles ( ) ;
88133 this . trigger ( 'uml-update' ) ;
89134 } , this ) ;
90135
91136 this . updateRectangles ( ) ;
92137
93138 joint . shapes . basic . Generic . prototype . initialize . apply ( this , arguments ) ;
139+
94140 } ,
95141
96- getClassName : function ( ) {
142+ getClassName : function ( ) {
97143 return this . get ( 'name' ) ;
98144 } ,
99145
100- updateRectangles : function ( ) {
146+ updateRectangles : function ( ) {
101147
102148 var attrs = this . get ( 'attrs' ) ,
103149 self = this ,
@@ -110,25 +156,19 @@ joint.shapes.uml.Class = joint.shapes.basic.Generic.extend({
110156 { type : 'methods' , text : this . get ( 'methods' ) }
111157 ] ;
112158
113- var offsetY = 0 ,
114- maxWidth = 100 ;
159+ var offsetY = 0 ;
115160
116161 var dp = self . get ( "directProps" ) || { } ,
117162 nameClickHandler = dp . nameClickHandler ;
118163
119- _ . each ( rects , function ( rect ) {
120- ( rect . text instanceof Array ? rect . text : [ rect . text ] ) . forEach ( function ( s ) { var t = s . split ( "\x1b" ) [ 0 ] . length * SYMBOL_12_WIDTH + 8 ; if ( t > maxWidth ) {
121- maxWidth = t ;
122- } } ) ;
123- } ) ;
124-
125- this . attributes . size . width = maxWidth ; // max width assign
126-
127164 _ . each ( rects , function ( rect ) {
128165
129166 var lines = _ . isArray ( rect . text ) ? rect . text : [ rect . text ] ;
130167
131- //if (rect.type === "name") lines.unshift("");
168+ if ( rect . type === "name" ) {
169+ if ( self . HEAD_EMPTY_LINES ) lines . unshift ( "" ) ;
170+ for ( var i = 0 ; i < self . HEAD_EMPTY_LINES ; i ++ ) lines . unshift ( "" ) ;
171+ }
132172
133173 var rectHeight = lines . length * 12 + ( lines . length ? 10 : 0 ) ,
134174 rectText = attrs [ '.uml-class-' + rect . type + '-text' ] ,
@@ -156,7 +196,9 @@ joint.shapes.uml.Class = joint.shapes.basic.Generic.extend({
156196 } ) ;
157197
158198 this . attributes . size . height = offsetY ;
159- this . attributes . attrs . rect . width = maxWidth ;
199+ this . attributes . size . width = this . defaults . size . width ; // max width assign
200+ this . attributes . attrs . rect . width = this . defaults . size . width ;
201+
160202 }
161203
162204} ) ;
0 commit comments