Skip to content

Commit ef45be5

Browse files
luca-rathalexander-schranz
authored andcommitted
Add truncate component (#32)
* Add truncate component
1 parent 2724fc9 commit ef45be5

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ Thumbs.db
1414
# NPM
1515
/node_modules
1616
yarn-error.log
17+
yarn.lock
1718

1819
!**/.gitkeep

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## unreleased
4+
5+
- FEATURE: Added truncate component. #32
6+
37
## 1.4.1
48

59
- FEATURE: Added return of instance in startComponent. #30

scss/tools/_media.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,3 @@ $media-breakpoints: $breakpoints !default;
187187
}
188188
}
189189
}
190-

src/components/truncate.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Truncate text component module
2+
3+
'use strict';
4+
5+
var $ = require('jquery');
6+
var debounce = require('../services/debounce');
7+
8+
module.exports = function Truncate() {
9+
var truncate = {};
10+
11+
/**
12+
* @example
13+
* <div id="truncate" style="overflow: hidden; line-height: 20px; max-height: 60px;">
14+
* Lorem ipsum ...
15+
* </div>
16+
*
17+
* import truncate from 'massive-web/src/components/truncate';
18+
* truncate.initialize($('#truncate'), {});
19+
*
20+
* @param {jQuery} $el
21+
* @param {object} options
22+
*/
23+
truncate.initialize = function initialize($el, options) {
24+
truncate.$el = $el;
25+
26+
// Set default options if no custom options are defined
27+
truncate.separator = options.separator || ' ...';
28+
truncate.debounceDelay = options.debounceDelay || 250;
29+
30+
truncate.text = truncate.$el.text().trim();
31+
truncate.$inner = $('<span></span>').text(truncate.text).css('display', 'block');
32+
truncate.$el.html(truncate.$inner).css('display', 'block');
33+
34+
truncate.calculateRegex();
35+
truncate.calculateText();
36+
37+
$(window).on('resize', debounce(truncate.calculateText, truncate.debounceDelay));
38+
};
39+
40+
/**
41+
* Calculate regex based on the separator.
42+
*/
43+
truncate.calculateRegex = function calculateRegex() {
44+
var separatorRegex = truncate.separator.split('').map(c => '\\' + c).join('');
45+
truncate.regex = new RegExp('\\W*\\s?(?:\\S*|\\S*' + separatorRegex + ')$');
46+
};
47+
48+
/**
49+
* Calculate output text based on the element's height.
50+
*/
51+
truncate.calculateText = function calculateText() {
52+
var height;
53+
54+
truncate.$inner.text(truncate.text);
55+
height = truncate.$el.height();
56+
57+
while (truncate.$inner.outerHeight() > height) {
58+
truncate.$inner.text(function(index, text) {
59+
if (text === truncate.separator) {
60+
return '';
61+
}
62+
63+
return text.replace(truncate.regex, truncate.separator);
64+
});
65+
}
66+
};
67+
68+
return {
69+
initialize: truncate.initialize
70+
};
71+
};

src/services/debounce.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Debounce function
2+
3+
'use strict';
4+
5+
/**
6+
* Extracted from UnderscoreJS
7+
*
8+
* @ignore
9+
*/
10+
module.exports = function debounce(func, wait, immediate) {
11+
var timeout;
12+
13+
return function() {
14+
var context = this;
15+
var args = arguments;
16+
17+
clearTimeout(timeout);
18+
timeout = setTimeout(function() {
19+
timeout = null;
20+
21+
if (!immediate) {
22+
func.apply(context, args);
23+
}
24+
}, wait);
25+
26+
if (immediate && !timeout) {
27+
func.apply(context, args);
28+
}
29+
};
30+
};

0 commit comments

Comments
 (0)