summaryrefslogtreecommitdiffstats
path: root/readmore.js
diff options
context:
space:
mode:
Diffstat (limited to 'readmore.js')
-rw-r--r--readmore.js77
1 files changed, 45 insertions, 32 deletions
diff --git a/readmore.js b/readmore.js
index 61e2f53..8ef341f 100644
--- a/readmore.js
+++ b/readmore.js
@@ -7,7 +7,7 @@
* Debounce function from http://davidwalsh.name/javascript-debounce-function
*/
-;(function($) {
+!(function($) {
var readmore = 'readmore',
defaults = {
@@ -29,42 +29,51 @@
function debounce(func, wait, immediate) {
var timeout;
+
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
- if (!immediate) func.apply(context, args);
+ if (! immediate) {
+ func.apply(context, args);
+ }
};
var callNow = immediate && !timeout;
+
clearTimeout(timeout);
timeout = setTimeout(later, wait);
- if (callNow) func.apply(context, args);
+
+ if (callNow) {
+ func.apply(context, args);
+ }
};
}
function uniqueId(prefix) {
var id = ++uniqueIdCounter;
+
return String(prefix == null ? 'readmore-js-' : prefix) + id;
}
- function Readmore( element, options ) {
+ function Readmore(element, options) {
var $this = this;
this.element = element;
- this.options = $.extend( {}, defaults, options);
+ this.options = $.extend({}, defaults, options);
- $(this.element).data('max-height', this.options.maxHeight);
- $(this.element).data('height-margin', this.options.heightMargin);
+ $(this.element).data({
+ 'max-height': this.options.maxHeight,
+ 'height-margin': this.options.heightMargin
+ });
delete(this.options.maxHeight);
- if(! cssEmbedded[this.options.selector]) {
+ if (! cssEmbedded[this.options.selector]) {
var styles = ' ';
- // Include sectionCSS if embedCSS is true
- if(this.options.embedCSS) {
- styles += this.options.selector + ' + [readmore-js-toggle], ' + this.options.selector + '[data-readmore-js-section]{' + this.options.sectionCSS + '}'
+ if (this.options.embedCSS) {
+ styles += this.options.selector + ' + [data-readmore-js-toggle], ' + this.options.selector + '[data-readmore-js-section]{' + this.options.sectionCSS + '}'
}
// Include the transition CSS even if embedCSS is false
@@ -73,15 +82,17 @@
'overflow: hidden;' +
'}';
- (function(d,u) {
- var css=d.createElement('style');
+ (function(d, u) {
+ var css = d.createElement('style');
css.type = 'text/css';
- if(css.styleSheet) {
- css.styleSheet.cssText = u;
+
+ if (css.styleSheet) {
+ css.styleSheet.cssText = u;
}
else {
css.appendChild(d.createTextNode(u));
}
+
d.getElementsByTagName('head')[0].appendChild(css);
}(document, styles));
@@ -96,8 +107,8 @@
});
}
- Readmore.prototype = {
+ Readmore.prototype = {
init: function() {
var $this = this;
@@ -106,13 +117,13 @@
maxHeight = (parseInt(current.css('max-height').replace(/[^-\d\.]/g, ''), 10) > current.data('max-height')) ? parseInt(current.css('max-height').replace(/[^-\d\.]/g, ''), 10) : current.data('max-height'),
heightMargin = current.data('height-margin');
- if(current.css('max-height') != 'none') {
+ if (current.css('max-height') != 'none') {
current.css('max-height', 'none');
}
$this.setBoxHeight(current);
- if(current.outerHeight(true) <= maxHeight + heightMargin) {
+ if (current.outerHeight(true) <= maxHeight + heightMargin) {
// The block is shorter than the limit, so there's no need to truncate it.
return true;
}
@@ -122,9 +133,9 @@
current.attr({'data-readmore-js-section': '', 'aria-expanded': false, 'id': id}).data('collapsedHeight', maxHeight);
- current.after($(useLink).on('click', function(event) { $this.toggle(this, current, event) }).attr({'data-readmore-js-toggle': '', 'aria-controls': id}));
+ current.after($(useLink).on('click', function(event) { $this.toggle(this, current, event); }).attr({'data-readmore-js-toggle': '', 'aria-controls': id}));
- if(!$this.options.startOpen) {
+ if (! $this.options.startOpen) {
current.css({height: maxHeight});
}
}
@@ -136,21 +147,20 @@
},
toggle: function(trigger, element, event) {
- if(event) {
+ if (event) {
event.preventDefault();
}
- if(! trigger) {
+ if (! trigger) {
trigger = $('[aria-controls="' + this.element.id + '"]')[0];
}
- if(! element) {
+ if (! element) {
element = this.element;
}
var $this = this,
$element = $(element),
- $trigger = $(trigger),
newHeight = newLink = '',
expanded = false,
collapsedHeight = $element.data('collapsedHeight');
@@ -160,7 +170,6 @@
newLink = 'lessLink';
expanded = true;
}
-
else {
newHeight = collapsedHeight;
newLink = 'moreLink';
@@ -178,7 +187,7 @@
$(this).attr('aria-expanded', expanded);
});
- $trigger.replaceWith($($this.options[newLink]).on('click', function(event) { $this.toggle(this, element, event) }).attr({'data-readmore-js-toggle': '', 'aria-controls': $element.attr('id')}));
+ $(trigger).replaceWith($($this.options[newLink]).on('click', function(event) { $this.toggle(this, element, event); }).attr({'data-readmore-js-toggle': '', 'aria-controls': $element.attr('id')}));
},
setBoxHeight: function(element) {
@@ -198,7 +207,7 @@
$this.setBoxHeight(current);
- if(current.height() > current.data('expandedHeight') || (current.attr('aria-expanded') && current.height() < current.data('expandedHeight')) ) {
+ if (current.height() > current.data('expandedHeight') || (current.attr('aria-expanded') && current.height() < current.data('expandedHeight')) ) {
current.css('height', current.data('expandedHeight'));
}
});
@@ -217,11 +226,12 @@
}
};
- $.fn.readmore = function( options ) {
+ $.fn.readmore = function(options) {
var args = arguments,
selector = this.selector;
+
if (options === undefined || typeof options === 'object') {
- return this.each(function () {
+ return this.each(function() {
if ($.data(this, 'plugin_' + readmore)) {
var instance = $.data(this, 'plugin_' + readmore);
instance['destroy'].apply(instance);
@@ -229,15 +239,18 @@
options['selector'] = selector;
- $.data(this, 'plugin_' + readmore, new Readmore( this, options ));
+ $.data(this, 'plugin_' + readmore, new Readmore(this, options));
});
- } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
+ }
+ else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
return this.each(function () {
var instance = $.data(this, 'plugin_' + readmore);
if (instance instanceof Readmore && typeof instance[options] === 'function') {
- instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
+ instance[options].apply(instance, Array.prototype.slice.call(args, 1));
}
});
}
};
+
})(jQuery);
+