Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added binary chop algorithm to speed up finding a string that fits. #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions src/angular-ellipsis.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,15 @@ angular.module('dibari.angular-ellipsis', [])
//Set data-overflow on element for targeting
element.attr('data-overflowed', 'true');

// Set complete text and remove one word at a time, until there is no overflow
for (; i < bindArrayStartingLength; i++) {
var current = bindArray.pop();

//if the last string still overflowed, then truncate the last string
if (bindArray.length === 0) {
bindArray[0] = current.substring(0, Math.min(current.length, 5));
}

if (isHtml) {
element.html(bindArray.join(ellipsisSeparator) + appendString);
} else {
element.text(bindArray.join(ellipsisSeparator)).html(element.html() + appendString);
}

if ((scope.useParent ? element.parent()[0] : element[0]).scrollHeight < initialMaxHeight || isOverflowed(element, scope.useParent) === false) {
attributes.isTruncated = true;
break;
}
}
const leastUpperBound = calculateLeastUpperBound(element, bindArray, ellipsisSeparator, appendString, isHtml, scope, initialMaxHeight);

if (isHtml) {
element.html(bindArray.slice(0, leastUpperBound).join(ellipsisSeparator) + appendString);
} else {
element.text(bindArray.slice(0, leastUpperBound).join(ellipsisSeparator)).html(element.html() + appendString);
}

attributes.isTruncated = true;

// If append string was passed and append click function included
if (ellipsisSymbol != appendString && typeof(scope.ellipsisAppendClick) !== 'undefined' && scope.ellipsisAppendClick !== '') {
Expand Down Expand Up @@ -186,6 +175,40 @@ angular.module('dibari.angular-ellipsis', [])
return thisElement[0].scrollHeight > thisElement[0].clientHeight;
}

function doesItFit(guess, element, bindArray, ellipsisSeparator, appendString, isHtml, scope, initialMaxHeight){
if (isHtml) {
element.html(bindArray.slice(0, guess).join(ellipsisSeparator) + appendString);
} else {
element.text(bindArray.slice(0, guess).join(ellipsisSeparator)).html(element.html() + appendString);
}

return ((scope.useParent ? element.parent()[0] : element[0]).scrollHeight < initialMaxHeight || isOverflowed(element, scope.useParent) === false);
}

function calculateLeastUpperBound(element, bindArray, ellipsisSeparator, appendString, isHtml, scope, initialMaxHeight){
var upperBound = bindArray.length;
var lowerBound = 0;

if(doesItFit(upperBound, element, bindArray, ellipsisSeparator, appendString, isHtml, scope, initialMaxHeight)){
lowerBound = upperBound;
} else {
while(true){
currentGuess = Math.floor((upperBound + lowerBound) / 2);
if(doesItFit(currentGuess, element, bindArray, ellipsisSeparator, appendString, isHtml, scope, initialMaxHeight)){
lowerBound = currentGuess;
} else {
upperBound = currentGuess;
}

if((upperBound - lowerBound) <= 1){
break;
}
}
}

return lowerBound;
}

/**
* Watchers
*/
Expand Down
2 changes: 1 addition & 1 deletion src/angular-ellipsis.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.