Skip to content

Instantly share code, notes, and snippets.

@davidchase
Last active December 18, 2015 19:29
Show Gist options
  • Save davidchase/5833541 to your computer and use it in GitHub Desktop.
Save davidchase/5833541 to your computer and use it in GitHub Desktop.
Angularjs Directive Blurb

Usage:

<div data-blurb ng-class="'blurb'" title="Blurb Title" show="106" more="Read More" less="Read Less" ellipsis="...">
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
  quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
  consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
  cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
  proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>

TODO

Transfer to github repo

<div>
<div class="title">{{title}}</div>
<div class="brief">{{excerpt}}{{ellipsis}}</div>
<div class="content" ng-transclude></div>
<div id="details">{{lnk}}</div>
</div>
'use strict';
angular.module('app')
.directive('blurb', function () {
return {
restrict: 'EAC',
replace: true,
transclude: true,
scope: {
show: '=',
title: '@',
more: '@',
less: '@',
ellipsis: '@'
},
templateUrl:'blurb.html',
link: function (scope, element) {
var opened = true,
details = angular.element(element.children()[3]),
showChar = scope.show,
body = angular.element(element.children()[2]),
input = body.text(),
content = input.substr(showChar, input.length - showChar),
excerpt = input.substr(0, showChar);
body.text(content);
details.bind('click', function () {
scope.$apply(toggle());
});
scope.excerpt = excerpt;
function toggle() {
opened = !opened;
element.removeClass(opened ? 'closed' : 'opened');
element.addClass(opened ? 'opened' : 'closed');
scope.$watch('lnk', function () {
scope.lnk = opened ? scope.less : scope.more;
});
opened ? body.slideDown() : body.slideUp();
}
toggle();
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment