Skip to content
This repository was archived by the owner on Dec 16, 2019. It is now read-only.

Commit 9465b23

Browse files
committed
Remove the overflow characters before ellipsis.
1 parent b7e7bf1 commit 9465b23

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/angularjs-dropdown-multiselect.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,27 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
235235
return groupValue;
236236
};
237237

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+
238249
$scope.getButtonText = function() {
239250
if ($scope.settings.dynamicTitle && ($scope.selectedModel.length > 0 || (angular.isObject($scope.selectedModel) && Object.keys($scope.selectedModel).length > 0))) {
240251
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+
241259
var itemsText = [];
242260

243261
angular.forEach($scope.options, function(optionItem) {
@@ -254,7 +272,19 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
254272
itemsText.push('...');
255273
}
256274

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;
258288
} else {
259289
var totalSelected;
260290

0 commit comments

Comments
 (0)