Skip to content

Commit

Permalink
Merge pull request #15 from priyanjitdey94/develop
Browse files Browse the repository at this point in the history
Updated test cases, added caveats
  • Loading branch information
siawo authored Jul 11, 2019
2 parents f35012c + 5b85fb9 commit 2920da7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ To get the size of a given text
* }
* If detailedCalculationFlag is set to false the returned object wont have the detailObj prop.
*/
size = sl.getOriSize(text, detailedCalculationFlag);
size = sl.getSize(text, detailedCalculationFlag);
```

To dispose the components
Expand Down
4 changes: 2 additions & 2 deletions dist/fusioncharts-smartlabel.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/fusioncharts-smartlabel.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion es/SmartlabelManager.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion es/lib.js

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

46 changes: 23 additions & 23 deletions src/SmartlabelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ SmartLabelManager.prototype.getSmartText = function (text, maxWidth, maxHeight,
tmpText = text = text.replace(slLib.ltgtRegex, function (match) {
return match === '&lt;' ? '<' : '>';
});
getOriSizeImproveObj = this.getOriSize(tmpText, true, {
getOriSizeImproveObj = this.getSize(tmpText, true, {
hasHTMLTag: hasHTMLTag,
hasOnlyBrTag: hasOnlyBrTag,
cleanText: true
Expand Down Expand Up @@ -913,7 +913,7 @@ SmartLabelManager.prototype.getSmartText = function (text, maxWidth, maxHeight,
* }
* If detailedCalculationFlag is set to false the returned object wont have the detailObj prop.
*/
SmartLabelManager.prototype.getOriSize = function (text = '', detailedCalculationFlag = true, config = {}) {
SmartLabelManager.prototype.getSize = function (text = '', detailedCalculationFlag = true, config = {}) {
if (!this._init) {
return false;
}
Expand Down Expand Up @@ -951,35 +951,39 @@ SmartLabelManager.prototype.getOriSize = function (text = '', detailedCalculatio
}
this._updateStyle();
container = this._container;
// If text has br tag, return the width and height with proper calculations
if (hasOnlyBrTag) {
return slLib._getDimentionOfMultiLineText(text, this);
}

// When text is normal text
if (!detailedCalculationFlag) {
return this._calCharDimWithCache(text);
} else {
// Calculate the width of every letter with an approximation
textArr = text.split('');
for (i = 0, l = textArr.length; i < l; i++) {
letter = textArr[i];
lSize = this._calCharDimWithCache(letter, false, textArr.length);
height = max(height, lSize.height);
cumulativeSize += lSize.width;
indiSizeStore[letter] = lSize.width;
}
}
// If text has br tag, return the width and height with proper calculations
if (hasOnlyBrTag) {
return {
...slLib._getDimentionOfMultiLineText(text, this),
detailObj: indiSizeStore
};
}

// text contains html tags other than br
if (hasHTMLTag) {
container.innerHTML = text;
return {
width: container.offsetWidth,
height: container.offsetHeight
height: container.offsetHeight,
detailObj: indiSizeStore
};
}

// Calculate the width of every letter with an approximation
textArr = text.split('');
for (i = 0, l = textArr.length; i < l; i++) {
letter = textArr[i];
lSize = this._calCharDimWithCache(letter, false, textArr.length);
height = max(height, lSize.height);
cumulativeSize += lSize.width;
indiSizeStore[letter] = lSize.width;
}

return {
width: round(cumulativeSize),
height: height,
Expand All @@ -991,12 +995,8 @@ SmartLabelManager.prototype.getOriSize = function (text = '', detailedCalculatio
* getOriSize API will eventually be deprecated and will be renamed to getSize API. For the next two versions,
* both getOriSize and getSize API will be supported.
*/
SmartLabelManager.prototype.getSize = function (text = '', detailedCalculationFlag = true, config = {}) {
if (!this._init) {
return false;
}

return this.getOriSize(text, detailedCalculationFlag, config);
SmartLabelManager.prototype.getOriSize = function (text = '', detailedCalculationFlag = true, config = {}) {
return this.getSize(text, detailedCalculationFlag, config);
};
/*
* Dispose the container and object allocated by the smartlabel
Expand Down
18 changes: 15 additions & 3 deletions test/smartlabel-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('SmartLabel', function () {
'useEllipsesOnOverflow',
'getSmartText',
'getOriSize',
'getSize',
'dispose'
],
dirty = false;
Expand Down Expand Up @@ -85,10 +86,10 @@ describe('SmartLabel', function () {

it('sets the style properly',
function () {
var sizeWOStyle = sl.getOriSize('A quick brown fox'),
var sizeWOStyle = sl.getSize('A quick brown fox'),
sizeWithStyle = sl
.setStyle({fontSize: '30px'})
.getOriSize('A quick brown fox');
.getSize('A quick brown fox');


expect(sizeWOStyle).to.not.deep.equal(sizeWithStyle);
Expand All @@ -102,7 +103,7 @@ describe('SmartLabel', function () {
fontSize: '20px',
fontFamily: 'Verdana'
})
.getOriSize('a quick brown fox');
.getSize('a quick brown fox');

expect(size.height).to.be.equal(24);
// Error value: +-2px
Expand Down Expand Up @@ -222,6 +223,17 @@ describe('SmartLabel', function () {
expect(smarttext.lines.length).to.equal(6);
});

it('getSize API should always return detailObj', function () {
var smarttext,
present;

smarttext = sl.getSize('AAAA');
expect(smarttext.hasOwnProperty('detailObj')).to.equal(true);
smarttext = sl.getSize('AAA <br> AAA');
expect(smarttext.hasOwnProperty('detailObj')).to.equal(true);
smarttext = sl.getSize('AAA <div></div> AAA');
expect(smarttext.hasOwnProperty('detailObj')).to.equal(true);
})

it('removes the div when disposed',
function () {
Expand Down

0 comments on commit 2920da7

Please sign in to comment.