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

Commit e48d126

Browse files
authored
Merge pull request #294 from mkfsn/Version-1.10
Remove the overflow characters before ellipsis.
2 parents d3f215e + 9465b23 commit e48d126

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "myforce-angularjs-dropdown-multiselect",
3-
"version": "1.10.0",
3+
"version": "1.10.2",
44
"authors": [
55
"Dotan Simha <[email protected]>",
66
"Pieter Kempenaers <[email protected]>"

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"author": "[email protected]",
33
"name": "angularjs-dropdown-multiselect",
4-
"version": "0.0.0",
4+
"version": "1.10.2",
55
"description": "This directive gives you a Bootstrap Dropdown with the power of AngularJS directives.",
66
"homepage": "http://myforce.github.io/angularjs-dropdown-multiselect/#/",
77
"dependencies": {

src/angularjs-dropdown-multiselect.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)