diff --git a/js/angular-readmore.js b/js/angular-readmore.js index eb60a95..e8cb026 100644 --- a/js/angular-readmore.js +++ b/js/angular-readmore.js @@ -5,86 +5,90 @@ * */ -var readMore = angular.module('readMore', []); +(function () { + 'use strict'; -readMore.directive('readMore', function() { - return { - restrict: 'A', - transclude: true, - replace: true, - template: '
', - scope: { - moreText: '@', - lessText: '@', - words: '@', - ellipsis: '@', - char: '@', - limit: '@', - content: '@' - }, - link: function(scope, elem, attr, ctrl, transclude) { - var moreText = angular.isUndefined(scope.moreText) ? ' Read More...' : ' ' + scope.moreText + '', - lessText = angular.isUndefined(scope.lessText) ? ' Less ^' : ' ' + scope.lessText + '', - 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: '', + scope: { + moreText: '@', + lessText: '@', + words: '@', + ellipsis: '@', + char: '@', + limit: '@', + content: '@' + }, + link: function (scope, elem, attr, ctrl, transclude) { + var moreText = angular.isUndefined(scope.moreText) ? ' Read More...' : ' ' + scope.moreText + '', + lessText = angular.isUndefined(scope.lessText) ? ' Less ^' : ' ' + scope.lessText + '', + 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 + '' + more + lessText + ''; - } + 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 + '' + more + lessText + ''; + } - if (count > limit) { - text = orig.slice(0, limit) + ellipsis; - more = orig.slice(limit, count); - markup = text + moreText + '' + more + lessText + ''; - } + } else { // Count characters - } + if (count > limit) { + text = orig.slice(0, limit) + ellipsis; + more = orig.slice(limit, count); + markup = text + moreText + '' + more + lessText + ''; + } - 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'); + }); + + } + } + }; } - }; -}); +})();