-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:massiveart/web-js
- Loading branch information
Showing
8 changed files
with
114 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -14,5 +14,6 @@ Thumbs.db | |
# NPM | ||
/node_modules | ||
yarn-error.log | ||
yarn.lock | ||
|
||
!**/.gitkeep |
This file contains 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 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 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 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,5 @@ | ||
@mixin dotDotDot() { | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
white-space: nowrap; | ||
} |
This file contains 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 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,71 @@ | ||
// Truncate text component module | ||
|
||
'use strict'; | ||
|
||
var $ = require('jquery'); | ||
var debounce = require('../services/debounce'); | ||
|
||
module.exports = function Truncate() { | ||
var truncate = {}; | ||
|
||
/** | ||
* @example | ||
* <div id="truncate" style="overflow: hidden; line-height: 20px; max-height: 60px;"> | ||
* Lorem ipsum ... | ||
* </div> | ||
* | ||
* import truncate from 'massive-web/src/components/truncate'; | ||
* truncate.initialize($('#truncate'), {}); | ||
* | ||
* @param {jQuery} $el | ||
* @param {object} options | ||
*/ | ||
truncate.initialize = function initialize($el, options) { | ||
truncate.$el = $el; | ||
|
||
// Set default options if no custom options are defined | ||
truncate.separator = options.separator || ' ...'; | ||
truncate.debounceDelay = options.debounceDelay || 250; | ||
|
||
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)); | ||
}; | ||
|
||
/** | ||
* Calculate regex based on the separator. | ||
*/ | ||
truncate.calculateRegex = function calculateRegex() { | ||
var separatorRegex = truncate.separator.split('').map(c => '\\' + c).join(''); | ||
truncate.regex = new RegExp('\\W*\\s?(?:\\S*|\\S*' + separatorRegex + ')$'); | ||
}; | ||
|
||
/** | ||
* Calculate output text based on the element's height. | ||
*/ | ||
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 | ||
}; | ||
}; |
This file contains 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,30 @@ | ||
// Debounce function | ||
|
||
'use strict'; | ||
|
||
/** | ||
* Extracted from UnderscoreJS | ||
* | ||
* @ignore | ||
*/ | ||
module.exports = 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); | ||
} | ||
}; | ||
}; |