-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces auto-truncation of long bodies and quotes which expand when you click on a "More" link. Feature flagged as 'truncate_annotations'.
- Loading branch information
1 parent
81ca666
commit 4c8f9bf
Showing
14 changed files
with
293 additions
and
18 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
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,95 @@ | ||
'use strict'; | ||
|
||
function ExcerptController() { | ||
var collapsed = true; | ||
|
||
// Enabled is a test seam: overwritten in link function. | ||
this.enabled = function () { return true; }; | ||
|
||
// Overflowing is a test seam: overwritten in link function. | ||
this.overflowing = function () { return false; }; | ||
|
||
// Is the excerpt collapsed? True if no-one has toggled the excerpt open | ||
// and the element is overflowing. | ||
this.collapsed = function () { | ||
if (!collapsed) { | ||
return false; | ||
} | ||
return this.overflowing(); | ||
}; | ||
|
||
this.uncollapsed = function () { | ||
return !collapsed; | ||
}; | ||
|
||
this.toggle = function () { | ||
collapsed = !collapsed; | ||
}; | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* @ngdoc directive | ||
* @name excerpt | ||
* @restrict E | ||
* @description This directive truncates its contents to a height specified in | ||
* CSS, and provides controls for expanding and collapsing the | ||
* resulting truncated element. For example, with the following | ||
* template HTML: | ||
* | ||
* <article class="post"> | ||
* <excerpt> | ||
* <div class="body" ng-model="post.body"></div> | ||
* </excerpt> | ||
* </article> | ||
* | ||
* You would need to define the allowable height of the excerpt in | ||
* CSS: | ||
* | ||
* article.post .excerpt { | ||
* max-height: 10em; | ||
* } | ||
* | ||
* And the excerpt directive will take care of the rest. | ||
* | ||
* You can selectively disable truncation by providing a boolean | ||
* expression to the `enabled` parameter, e.g.: | ||
* | ||
* <excerpt enabled="!post.inFull">...</excerpt> | ||
*/ | ||
function excerpt() { | ||
return { | ||
controller: ExcerptController, | ||
controllerAs: 'vm', | ||
link: function (scope, elem, attrs, ctrl) { | ||
// Test if the transcluded element is overflowing its container. We use | ||
// clientHeight rather than offsetHeight because we assume you'll be using | ||
// this with "overflow: hidden;" (i.e. no scrollbars) and it's usually | ||
// much faster to calculate than offsetHeight (which includes scrollbars). | ||
ctrl.overflowing = function overflowing() { | ||
var excerpt = elem[0].querySelector('.excerpt'); | ||
if (!excerpt) { | ||
return false; | ||
} | ||
return (excerpt.scrollHeight > excerpt.clientHeight); | ||
}; | ||
|
||
// If the `enabled` attr was provided, we override the enabled function. | ||
if (attrs.enabled) { | ||
ctrl.enabled = scope.enabled; | ||
} | ||
}, | ||
scope: { | ||
enabled: '&?', | ||
}, | ||
restrict: 'E', | ||
transclude: true, | ||
templateUrl: 'excerpt.html', | ||
}; | ||
} | ||
|
||
module.exports = { | ||
directive: excerpt, | ||
Controller: ExcerptController, | ||
}; |
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,86 @@ | ||
'use strict'; | ||
|
||
var util = require('./util'); | ||
var excerpt = require('../excerpt'); | ||
|
||
|
||
describe('excerpt.Controller', function () { | ||
var ctrl; | ||
|
||
beforeEach(function() { | ||
ctrl = new excerpt.Controller(); | ||
ctrl.overflowing = function () { return false; }; | ||
}); | ||
|
||
it('starts collapsed if the element is overflowing', function () { | ||
ctrl.overflowing = function () { return true; }; | ||
|
||
assert.isTrue(ctrl.collapsed()); | ||
}); | ||
|
||
it('does not start collapsed if the element is not overflowing', function () { | ||
assert.isFalse(ctrl.collapsed()); | ||
}); | ||
|
||
it('is not initially uncollapsed if the element is overflowing', function () { | ||
assert.isFalse(ctrl.uncollapsed()); | ||
}); | ||
|
||
it('is not initially uncollapsed if the element is not overflowing', function () { | ||
assert.isFalse(ctrl.uncollapsed()); | ||
}); | ||
|
||
describe('.toggle()', function () { | ||
beforeEach(function () { | ||
ctrl.overflowing = function () { return true; }; | ||
}); | ||
|
||
it('toggles the collapsed state', function () { | ||
var a = ctrl.collapsed(); | ||
ctrl.toggle(); | ||
var b = ctrl.collapsed(); | ||
ctrl.toggle(); | ||
var c = ctrl.collapsed(); | ||
|
||
assert.notEqual(a, b); | ||
assert.notEqual(b, c); | ||
assert.equal(a, c); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
describe('excerpt.excerpt', function () { | ||
function excerptDirective(attrs, content) { | ||
return util.createDirective(document, 'excerpt', attrs, {}, content); | ||
} | ||
|
||
before(function () { | ||
angular.module('app', []) | ||
.directive('excerpt', excerpt.directive); | ||
}); | ||
|
||
beforeEach(function () { | ||
angular.mock.module('app'); | ||
angular.mock.module('h.templates'); | ||
}); | ||
|
||
it('renders its contents in a .excerpt element by default', function () { | ||
var element = excerptDirective({}, '<span id="foo"></span>'); | ||
|
||
assert.equal(element.find('.excerpt #foo').length, 1); | ||
}); | ||
|
||
it('when enabled, renders its contents in a .excerpt element', function () { | ||
var element = excerptDirective({enabled: true}, '<span id="foo"></span>'); | ||
|
||
assert.equal(element.find('.excerpt #foo').length, 1); | ||
}); | ||
|
||
it('when disabled, renders its contents but not in a .excerpt element', function () { | ||
var element = excerptDirective({enabled: false}, '<span id="foo"></span>'); | ||
|
||
assert.equal(element.find('.excerpt #foo').length, 0); | ||
assert.equal(element.find('#foo').length, 1); | ||
}); | ||
}); |
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,31 @@ | ||
@import "compass/css3/images"; | ||
|
||
.excerpt { | ||
position: relative; | ||
overflow: hidden; | ||
} | ||
|
||
.excerpt--collapsed:after { | ||
position: absolute; | ||
bottom: 0; | ||
height: $base-line-height * 2; // This controls the apparent height of the gradient. | ||
width: 100%; | ||
content: ""; | ||
pointer-events: none; | ||
@include background(linear-gradient( | ||
to bottom, | ||
$mask-start-color, | ||
$mask-end-color | ||
)); | ||
} | ||
|
||
.excerpt--uncollapsed { | ||
max-height: 100% !important; | ||
} | ||
|
||
.excerpt-control a { | ||
display: block; | ||
text-align: right; | ||
font-weight: bold; | ||
width: 100%; | ||
} |
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
Oops, something went wrong.