@@ -235,9 +235,27 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
235
235
return groupValue ;
236
236
} ;
237
237
238
+ function textWidth ( text ) {
239
+ var $btn = $element . find ( 'button' ) ;
240
+ var canvas = document . createElement ( 'canvas' ) ;
241
+ var ctx = canvas . getContext ( '2d' ) ;
242
+ ctx . font = $btn . css ( 'font-size' ) + $btn . css ( 'font-family' ) ;
243
+ // http://stackoverflow.com/questions/38823353/chrome-canvas-2d-context-measuretext-giving-me-weird-results
244
+ ctx . originalFont = $btn . css ( 'font-size' ) + $btn . css ( 'font-family' ) ;
245
+ ctx . fillStyle = '#000000' ;
246
+ return ctx . measureText ( text ) . width ;
247
+ }
248
+
238
249
$scope . getButtonText = function ( ) {
239
250
if ( $scope . settings . dynamicTitle && ( $scope . selectedModel . length > 0 || ( angular . isObject ( $scope . selectedModel ) && Object . keys ( $scope . selectedModel ) . length > 0 ) ) ) {
240
251
if ( $scope . settings . smartButtonMaxItems > 0 ) {
252
+
253
+ var paddingWidth = 12 * 2 ,
254
+ borderWidth = 1 * 2 ,
255
+ dropdownIconWidth = 8 ;
256
+ var ellipsisWidth = textWidth ( "..." ) ;
257
+ var widthLimit = $element [ 0 ] . offsetWidth - paddingWidth - borderWidth - dropdownIconWidth ;
258
+
241
259
var itemsText = [ ] ;
242
260
243
261
angular . forEach ( $scope . options , function ( optionItem ) {
@@ -254,7 +272,19 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
254
272
itemsText . push ( '...' ) ;
255
273
}
256
274
257
- return itemsText . join ( ', ' ) ;
275
+ var result = itemsText . join ( ', ' ) ;
276
+ var index = result . length - 4 ;
277
+ var countLimit = 100 ;
278
+ while ( textWidth ( result ) > widthLimit ) {
279
+ if ( itemsText [ itemsText . length - 1 ] !== "..." ) {
280
+ itemsText . push ( '...' ) ;
281
+ result = result + "..." ;
282
+ }
283
+ result = result . slice ( 0 , index ) + result . slice ( index + 1 ) ;
284
+ index -- ;
285
+ }
286
+
287
+ return result ;
258
288
} else {
259
289
var totalSelected ;
260
290
0 commit comments