@@ -247,9 +247,27 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
247
247
return groupValue ;
248
248
} ;
249
249
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
+
250
261
$scope . getButtonText = function ( ) {
251
262
if ( $scope . settings . dynamicTitle && ( $scope . selectedModel . length > 0 || ( angular . isObject ( $scope . selectedModel ) && Object . keys ( $scope . selectedModel ) . length > 0 ) ) ) {
252
263
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
+
253
271
var itemsText = [ ] ;
254
272
255
273
angular . forEach ( $scope . options , function ( optionItem ) {
@@ -266,7 +284,19 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
266
284
itemsText . push ( '...' ) ;
267
285
}
268
286
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 ;
270
300
} else {
271
301
var totalSelected ;
272
302
0 commit comments