@@ -247,9 +247,27 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
247247 return groupValue ;
248248 } ;
249249
250+ function textWidth ( text ) {
251+ var $btn = $element . find ( 'button' ) ;
252+ var canvas = document . createElement ( 'canvas' ) ;
253+ var ctx = canvas . getContext ( '2d' ) ;
254+ ctx . font = $btn . css ( 'font-size' ) + $btn . css ( 'font-family' ) ;
255+ // http://stackoverflow.com/questions/38823353/chrome-canvas-2d-context-measuretext-giving-me-weird-results
256+ ctx . originalFont = $btn . css ( 'font-size' ) + $btn . css ( 'font-family' ) ;
257+ ctx . fillStyle = '#000000' ;
258+ return ctx . measureText ( text ) . width ;
259+ }
260+
250261 $scope . getButtonText = function ( ) {
251262 if ( $scope . settings . dynamicTitle && ( $scope . selectedModel . length > 0 || ( angular . isObject ( $scope . selectedModel ) && Object . keys ( $scope . selectedModel ) . length > 0 ) ) ) {
252263 if ( $scope . settings . smartButtonMaxItems > 0 ) {
264+
265+ var paddingWidth = 12 * 2 ,
266+ borderWidth = 1 * 2 ,
267+ dropdownIconWidth = 8 ;
268+ var ellipsisWidth = textWidth ( "..." ) ;
269+ var widthLimit = $element [ 0 ] . offsetWidth - paddingWidth - borderWidth - dropdownIconWidth ;
270+
253271 var itemsText = [ ] ;
254272
255273 angular . forEach ( $scope . options , function ( optionItem ) {
@@ -266,7 +284,19 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
266284 itemsText . push ( '...' ) ;
267285 }
268286
269- return itemsText . join ( ', ' ) ;
287+ var result = itemsText . join ( ', ' ) ;
288+ var index = result . length - 4 ;
289+ var countLimit = 100 ;
290+ while ( textWidth ( result ) > widthLimit ) {
291+ if ( itemsText [ itemsText . length - 1 ] !== "..." ) {
292+ itemsText . push ( '...' ) ;
293+ result = result + "..." ;
294+ }
295+ result = result . slice ( 0 , index ) + result . slice ( index + 1 ) ;
296+ index -- ;
297+ }
298+
299+ return result ;
270300 } else {
271301 var totalSelected ;
272302
0 commit comments