Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This handles toggling between truncated description text and the full text. Updated the CSS and added some unit tests.
- Loading branch information
Showing
7 changed files
with
185 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* Module that handles related item elements, at the moment this consists of | ||
* truncating the descriptions and allowing them to be toggled. | ||
* | ||
* truncate - The max number of characters in the description element. | ||
* truncateMore - A locale string for the "more" text. | ||
* truncateLess - A locale string for the "less" text. | ||
* truncatePrefix - A prefix for the more/less strings. | ||
* truncateSuffix - A suffix for the more/less strings. | ||
* truncateSelector - A selector for the element to truncate. | ||
* expandedClass - A class to apply to the element when expanded. | ||
*/ | ||
this.ckan.module('related-item', function (jQuery, _) { | ||
return { | ||
/* options object can be extended using data-module-* attributes */ | ||
options: { | ||
truncate: 55, | ||
truncateMore: null, | ||
truncateLess: null, | ||
truncatePrefix: ' (', | ||
truncateSuffix: ')', | ||
truncateSelector: '.prose', | ||
expandedClass: 'expanded', | ||
i18n: { | ||
more: _('show more'), | ||
less: _('show less') | ||
} | ||
}, | ||
|
||
/* Initialises the module setting up elements and event listeners. | ||
* | ||
* Returns nothing. | ||
*/ | ||
initialize: function () { | ||
jQuery.proxyAll(this, /_on/); | ||
|
||
var options = this.options; | ||
this.description = this.$(options.truncateSelector); | ||
this.truncated = this.description.truncate({ | ||
max_length: options.truncate, | ||
more: options.truncateMore || this.i18n('more'), | ||
less: options.truncateLess || this.i18n('less'), | ||
link_prefix: options.truncatePrefix, | ||
link_suffix: options.truncateSuffix | ||
}); | ||
|
||
this.collapsedHeight = this.el.height(); | ||
this.truncated.on('expand.truncate', this._onExpand); | ||
this.truncated.on('collapse.truncate', this._onCollapse); | ||
}, | ||
|
||
/* Event handler called when the truncated text expands. | ||
* | ||
* event - An event object. | ||
* | ||
* Returns nothing. | ||
*/ | ||
_onExpand: function () { | ||
var diff = this.el.height() - this.collapsedHeight; | ||
this.el.addClass(this.options.expandedClass); | ||
this.el.css('margin-bottom', diff * -1); | ||
}, | ||
|
||
/* Event handler called when the truncated text is collapsed. | ||
* | ||
* event - An event object. | ||
* | ||
* Returns nothing. | ||
*/ | ||
_onCollapse: function () { | ||
this.el.removeClass(this.options.expandedClass); | ||
this.el.css('margin-bottom', ''); | ||
} | ||
}; | ||
}); |
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,87 @@ | ||
/*globals describe before beforeEach afterEach it assert sinon ckan jQuery */ | ||
describe('ckan.module.RelatedItemModule()', function () { | ||
var RelatedItemModule = ckan.module.registry['related-item']; | ||
|
||
before(function (done) { | ||
// Load our fixture into the this.fixture element. | ||
this.loadFixture('related-item.html', function (html) { | ||
this.template = html; | ||
done(); | ||
}); | ||
}); | ||
|
||
beforeEach(function () { | ||
this.truncated = jQuery('<div/>'); | ||
jQuery.fn.truncate = sinon.stub().returns(this.truncated); | ||
|
||
// Grab the loaded fixture. | ||
this.el = this.fixture.html(this.template).children(); | ||
this.sandbox = ckan.sandbox(); | ||
this.sandbox.body = this.fixture; | ||
this.module = new RelatedItemModule(this.el, {}, this.sandbox); | ||
}); | ||
|
||
afterEach(function () { | ||
this.module.teardown(); | ||
delete jQuery.fn.truncate; | ||
}); | ||
|
||
describe('.initialize()', function () { | ||
it('should truncate the .prose element', function () { | ||
this.module.initialize(); | ||
assert.called(jQuery.fn.truncate); | ||
}); | ||
|
||
it('should pass the various options into the truncate plugin'); | ||
|
||
it('should cache the collapsed height of the plugin', function () { | ||
this.module.initialize(); | ||
assert.ok(this.module.collapsedHeight); | ||
}); | ||
|
||
it('should listen for the "truncate" events', function () { | ||
var target = sinon.stub(this.truncated, 'on'); | ||
this.module.initialize(); | ||
|
||
assert.called(target); | ||
assert.calledWith(target, 'expand.truncate', this.module._onExpand); | ||
assert.calledWith(target, 'collapse.truncate', this.module._onCollapse); | ||
}); | ||
}); | ||
|
||
describe('._onExpand(event)', function () { | ||
it('should add the "expanded" class to the element', function () { | ||
this.module._onExpand(jQuery.Event()); | ||
assert.isTrue(this.el.hasClass(this.module.options.expandedClass)); | ||
}); | ||
|
||
it('should add a bottom margin to the element', function () { | ||
this.module._onExpand(jQuery.Event()); | ||
assert.ok(this.el.css('margin-bottom')); | ||
}); | ||
|
||
it('should calcualte the difference between the current and cached height', function () { | ||
var target = sinon.stub(this.el, 'css'); | ||
sinon.stub(this.el, 'height').returns(30); | ||
this.module.collapsedHeight = 10; | ||
this.module._onExpand(jQuery.Event()); | ||
|
||
assert.called(target); | ||
assert.calledWith(target, 'margin-bottom', -20); | ||
}); | ||
}); | ||
|
||
describe('._onCollapse(event)', function () { | ||
it('should remove the "expanded" class from the element', function () { | ||
this.el.addClass(this.module.options.expandedClass); | ||
this.module._onCollapse(jQuery.Event()); | ||
assert.isFalse(this.el.hasClass(this.module.options.expandedClass)); | ||
}); | ||
|
||
it('should remove the bottom margin from the element', function () { | ||
this.el.css('margin-bottom', -90); | ||
this.module._onCollapse(jQuery.Event()); | ||
assert.equal(this.el.css('margin-bottom'), '0px'); | ||
}); | ||
}); | ||
}); |
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