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

Commit

Permalink
Merge pull request #294 from mkfsn/Version-1.10
Browse files Browse the repository at this point in the history
Remove the overflow characters before ellipsis.
  • Loading branch information
pkempenaers authored Feb 8, 2017
2 parents d3f215e + 9465b23 commit e48d126
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "myforce-angularjs-dropdown-multiselect",
"version": "1.10.0",
"version": "1.10.2",
"authors": [
"Dotan Simha <[email protected]>",
"Pieter Kempenaers <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "[email protected]",
"name": "angularjs-dropdown-multiselect",
"version": "0.0.0",
"version": "1.10.2",
"description": "This directive gives you a Bootstrap Dropdown with the power of AngularJS directives.",
"homepage": "http://myforce.github.io/angularjs-dropdown-multiselect/#/",
"dependencies": {
Expand Down
32 changes: 31 additions & 1 deletion src/angularjs-dropdown-multiselect.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,27 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
return groupValue;
};

function textWidth(text) {
var $btn = $element.find('button');
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
ctx.font = $btn.css('font-size') + $btn.css('font-family');
// http://stackoverflow.com/questions/38823353/chrome-canvas-2d-context-measuretext-giving-me-weird-results
ctx.originalFont = $btn.css('font-size') + $btn.css('font-family');
ctx.fillStyle = '#000000';
return ctx.measureText(text).width;
}

$scope.getButtonText = function() {
if ($scope.settings.dynamicTitle && ($scope.selectedModel.length > 0 || (angular.isObject($scope.selectedModel) && Object.keys($scope.selectedModel).length > 0))) {
if ($scope.settings.smartButtonMaxItems > 0) {

var paddingWidth = 12 * 2,
borderWidth = 1 * 2,
dropdownIconWidth = 8;
var ellipsisWidth = textWidth("...");
var widthLimit = $element[0].offsetWidth - paddingWidth - borderWidth - dropdownIconWidth;

var itemsText = [];

angular.forEach($scope.options, function(optionItem) {
Expand All @@ -266,7 +284,19 @@ directiveModule.directive('ngDropdownMultiselect', ['$filter', '$document', '$co
itemsText.push('...');
}

return itemsText.join(', ');
var result = itemsText.join(', ');
var index = result.length - 4;
var countLimit = 100;
while (textWidth(result) > widthLimit) {
if (itemsText[itemsText.length - 1] !== "...") {
itemsText.push('...');
result = result + "...";
}
result = result.slice(0, index) + result.slice(index + 1);
index--;
}

return result;
} else {
var totalSelected;

Expand Down

0 comments on commit e48d126

Please sign in to comment.