-
Notifications
You must be signed in to change notification settings - Fork 11
Add truncate component #32
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
39e3636
Create truncate.js
luca-rath 23944a3
Fix ci errors
luca-rath 5361dbc
Fix ci errors
luca-rath 5f46731
Update truncate.js
luca-rath ebcfda0
Update truncate.js
luca-rath 9ba9b7f
Update truncate.js
luca-rath f2b65f6
Update CHANGELOG.md
luca-rath a8ce5d6
Update truncate.js
luca-rath 587a3a0
Update truncate.js
luca-rath 939fee0
Create debounce.js
luca-rath c434443
Update truncate.js
luca-rath 5e9648c
added missing semicolon
5e850fa
Added comments to truncate component
luca-rath b7aa809
Update truncate.js
luca-rath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,4 +187,3 @@ $media-breakpoints: $breakpoints !default; | |
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Truncate text component module | ||
|
|
||
| 'use strict'; | ||
|
|
||
| var $ = require('jquery'); | ||
|
|
||
| // Extracted from UnderscoreJS | ||
| /** | ||
| * @ignore | ||
| */ | ||
| function debounce(func, wait, immediate) { | ||
| var timeout; | ||
|
|
||
| return function() { | ||
| var context = this; | ||
| var args = arguments; | ||
|
|
||
| clearTimeout(timeout); | ||
| timeout = setTimeout(function() { | ||
| timeout = null; | ||
|
|
||
| if (!immediate) { | ||
| func.apply(context, args); | ||
| } | ||
| }, wait); | ||
|
|
||
| if (immediate && !timeout) { | ||
| func.apply(context, args); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| module.exports = function Truncate() { | ||
| var truncate = {}; | ||
|
|
||
| truncate.separator = ' ...'; | ||
| truncate.debounceDelay = 250; | ||
|
|
||
| truncate.initialize = function initialize($el, options) { | ||
| truncate.$el = $el; | ||
|
|
||
| if (options) { | ||
luca-rath marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (options.separator) { | ||
| truncate.separator = options.separator; | ||
| } | ||
|
|
||
| if (options.debounceDelay) { | ||
| truncate.debounceDelay = options.debounceDelay; | ||
| } | ||
| } | ||
|
|
||
| truncate.text = truncate.$el.text().trim(); | ||
| truncate.$inner = $('<span></span>').text(truncate.text).css('display', 'block'); | ||
| truncate.$el.html(truncate.$inner).css('display', 'block'); | ||
|
|
||
| truncate.calculateRegex(); | ||
| truncate.calculateText(); | ||
|
|
||
| $(window).on('resize', debounce(truncate.calculateText, truncate.debounceDelay)); | ||
| }; | ||
|
|
||
| truncate.calculateRegex = function calculateRegex() { | ||
| var separatorRegex = truncate.separator.split('').map(c => '\\' + c).join(''); | ||
| truncate.regex = new RegExp('\\W*\\s?(?:\\S*|\\S*' + separatorRegex + ')$'); | ||
| }; | ||
|
|
||
| truncate.calculateText = function calculateText() { | ||
| var height; | ||
|
|
||
| truncate.$inner.text(truncate.text); | ||
| height = truncate.$el.height(); | ||
|
|
||
| while (truncate.$inner.outerHeight() > height) { | ||
| truncate.$inner.text(function(index, text) { | ||
| if (text === truncate.separator) { | ||
| return ''; | ||
| } | ||
|
|
||
| return text.replace(truncate.regex, truncate.separator); | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| initialize: truncate.initialize | ||
| }; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.