Skip to content

Commit

Permalink
Merge branch 'master' of github.com:massiveart/web-js
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Rath-Heel committed Nov 7, 2018
2 parents 8ce3a57 + 74c2ab7 commit 0eb9085
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ Thumbs.db
# NPM
/node_modules
yarn-error.log
yarn.lock

!**/.gitkeep
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changes

## 1.5.0

- FEATURE: Added truncate component. #32
- FEATURE: Added dot dot dot scss mixin. #31

## 1.4.1

- FEATURE: Added return of instance in startComponent. #30
Expand Down Expand Up @@ -40,4 +45,3 @@
## 1.0.0

- FEATURE Initial component and service logic.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ Update package.json version on master branch:
git checkout master
git pull origin master
npm version [ major | minor | patch ] --no-git-tag-version
# update version in changelog
git add .
git commit -m "Release <version>"
git push origin master
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "massive-web",
"version": "1.4.1",
"version": "1.5.0",
"description": "Manage your js components",
"main": "src/core.js",
"repository": {
Expand Down
5 changes: 5 additions & 0 deletions scss/tools/_dot-dot-dot.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@mixin dotDotDot() {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
1 change: 0 additions & 1 deletion scss/tools/_media.scss
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,3 @@ $media-breakpoints: $breakpoints !default;
}
}
}

71 changes: 71 additions & 0 deletions src/components/truncate.js
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
};
};
30 changes: 30 additions & 0 deletions src/services/debounce.js
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);
}
};
};

0 comments on commit 0eb9085

Please sign in to comment.