Skip to content

UPDATED: wrapped module in IIFE #3

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 72 additions & 68 deletions js/angular-readmore.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,86 +5,90 @@
*
*/

var readMore = angular.module('readMore', []);
(function () {
'use strict';

readMore.directive('readMore', function() {
return {
restrict: 'A',
transclude: true,
replace: true,
template: '<p></p>',
scope: {
moreText: '@',
lessText: '@',
words: '@',
ellipsis: '@',
char: '@',
limit: '@',
content: '@'
},
link: function(scope, elem, attr, ctrl, transclude) {
var moreText = angular.isUndefined(scope.moreText) ? ' <a class="read-more">Read More...</a>' : ' <a class="read-more">' + scope.moreText + '</a>',
lessText = angular.isUndefined(scope.lessText) ? ' <a class="read-less">Less ^</a>' : ' <a class="read-less">' + scope.lessText + '</a>',
ellipsis = angular.isUndefined(scope.ellipsis) ? '' : scope.ellipsis,
limit = angular.isUndefined(scope.limit) ? 150 : scope.limit;
angular.module('readMore', [])
.directive('readMore', readMore);

attr.$observe('content', function(str) {
readmore(str);
});

transclude(scope.$parent, function(clone, scope) {
readmore(clone.text().trim());
});
function readMore() {
return {
restrict: 'A',
transclude: true,
replace: true,
template: '<div></div>',
scope: {
moreText: '@',
lessText: '@',
words: '@',
ellipsis: '@',
char: '@',
limit: '@',
content: '@'
},
link: function (scope, elem, attr, ctrl, transclude) {
var moreText = angular.isUndefined(scope.moreText) ? ' <a class="read-more">Read More...</a>' : ' <a class="read-more">' + scope.moreText + '</a>',
lessText = angular.isUndefined(scope.lessText) ? ' <a class="read-less">Less ^</a>' : ' <a class="read-less">' + scope.lessText + '</a>',
ellipsis = angular.isUndefined(scope.ellipsis) ? '' : scope.ellipsis,
limit = angular.isUndefined(scope.limit) ? 150 : scope.limit;

function readmore(text) {
attr.$observe('content', function (str) {
readmore(str);
});

var text = text,
orig = text,
regex = /\s+/gi,
charCount = text.length,
wordCount = text.trim().replace(regex, ' ').split(' ').length,
countBy = 'char',
count = charCount,
foundWords = [],
markup = text,
more = '';
transclude(scope.$parent, function (clone, scope) {
readmore(clone.text().trim());
});

if (!angular.isUndefined(attr.words)) {
countBy = 'words';
count = wordCount;
}
function readmore(text) {
var orig = text,
regex = /\s+/gi,
charCount = text.length,
wordCount = text.trim().replace(regex, ' ').split(' ').length,
countBy = 'char',
count = charCount,
foundWords = [],
markup = text,
more = '';

if (countBy === 'words') { // Count words
if (!angular.isUndefined(attr.words)) {
countBy = 'words';
count = wordCount;
}

foundWords = text.split(/\s+/);
if (countBy === 'words') { // Count words

if (foundWords.length > limit) {
text = foundWords.slice(0, limit).join(' ') + ellipsis;
more = foundWords.slice(limit, count).join(' ');
markup = text + moreText + '<span class="more-text">' + more + lessText + '</span>';
}
foundWords = text.split(/\s+/);

} else { // Count characters
if (foundWords.length > limit) {
text = foundWords.slice(0, limit).join(' ') + ellipsis;
more = foundWords.slice(limit, count).join(' ');
markup = text + moreText + '<span class="more-text">' + more + lessText + '</span>';
}

if (count > limit) {
text = orig.slice(0, limit) + ellipsis;
more = orig.slice(limit, count);
markup = text + moreText + '<span class="more-text">' + more + lessText + '</span>';
}
} else { // Count characters

}
if (count > limit) {
text = orig.slice(0, limit) + ellipsis;
more = orig.slice(limit, count);
markup = text + moreText + '<span class="more-text">' + more + lessText + '</span>';
}

elem.append(markup);
elem.find('.read-more').on('click', function() {
$(this).hide();
elem.find('.more-text').addClass('show').slideDown();
});
elem.find('.read-less').on('click', function() {
elem.find('.read-more').show();
elem.find('.more-text').hide().removeClass('show');
});
}

}
elem.append(markup);
elem.find('.read-more').on('click', function () {
$(this).hide();
elem.find('.more-text').addClass('show').slideDown();
});
elem.find('.read-less').on('click', function () {
elem.find('.read-more').show();
elem.find('.more-text').hide().removeClass('show');
});

}
}
};
}
};
});
})();