-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.expander.min.js
23 lines (23 loc) · 3.98 KB
/
jquery.expander.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*!
* jQuery Expander Plugin v1.3.1
*
* Date: Tue Dec 06 00:02:41 2011 EST
* Requires: jQuery v1.3+
*
* Copyright 2011, Karl Swedberg
* Dual licensed under the MIT and GPL licenses (just like jQuery):
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*
*
*
*/
(function(d){d.expander={version:"1.3.1",defaults:{slicePoint:100,preserveWords:true,widow:4,expandText:"read more",expandPrefix:"… ",expandAfterSummary:false,summaryClass:"summary",detailClass:"details",moreClass:"read-more",lessClass:"read-less",collapseTimer:0,expandEffect:"fadeIn",expandSpeed:250,collapseEffect:"fadeOut",collapseSpeed:200,userCollapse:true,userCollapseText:"read less",userCollapsePrefix:" ",onSlice:null,beforeExpand:null,afterExpand:null,onCollapse:null}};d.fn.expander=
function(F){function G(a,c){var h="span",g=a.summary;if(c){h="div";if(t.test(g)&&!a.expandAfterSummary)g=g.replace(t,a.moreLabel+"$1");else g+=a.moreLabel;g='<div class="'+a.summaryClass+'">'+g+"</div>"}else g+=a.moreLabel;return[g,"<",h+' class="'+a.detailClass+'"',">",a.details,"</"+h+">"].join("")}function H(a){var c='<span class="'+a.moreClass+'">'+a.expandPrefix;c+='<a href="#">'+a.expandText+"</a></span>";return c}function u(a,c){if(a.lastIndexOf("<")>a.lastIndexOf(">"))a=a.slice(0,a.lastIndexOf("<"));
if(c)a=a.replace(I,"");return a}function v(a,c){c.stop(true,true)[a.collapseEffect](a.collapseSpeed,function(){c.prev("span."+a.moreClass).show().length||c.parent().children("div."+a.summaryClass).show().find("span."+a.moreClass).show()})}function J(a,c,h){if(a.collapseTimer)w=setTimeout(function(){v(a,c);d.isFunction(a.onCollapse)&&a.onCollapse.call(h,false)},a.collapseTimer)}var x=d.extend({},d.expander.defaults,F),K=/^<(?:area|br|col|embed|hr|img|input|link|meta|param).*>$/i,I=/(&(?:[^;]+;)?|\w+)$/,
L=/<\/?(\w+)[^>]*>/g,y=/<(\w+)[^>]*>/g,z=/<\/(\w+)>/g,t=/(<\/[^>]+>)\s*$/,M=/^<[^>]+>.?/,w;this.each(function(){var a,c,h,g,k,m,s,A=[],q=[],n={},o=this,f=d(this),B=d([]),b=d.meta?d.extend({},x,f.data()):x,N=!!f.find("."+b.detailClass).length,p=!!f.find("*").filter(function(){return/^block|table|list/.test(d(this).css("display"))}).length,r=(p?"div":"span")+"."+b.detailClass,C="span."+b.moreClass,O=b.expandSpeed||0,l=d.trim(f.html());d.trim(f.text());var e=l.slice(0,b.slicePoint);if(!d.data(this,"expander")){d.data(this,
"expander",true);d.each(["onSlice","beforeExpand","afterExpand","onCollapse"],function(i,j){n[j]=d.isFunction(b[j])});e=u(e);for(summTagless=e.replace(L,"").length;summTagless<b.slicePoint;){newChar=l.charAt(e.length);if(newChar=="<")newChar=l.slice(e.length).match(M)[0];e+=newChar;summTagless++}e=u(e,b.preserveWords);g=e.match(y)||[];k=e.match(z)||[];h=[];d.each(g,function(i,j){K.test(j)||h.push(j)});g=h;c=k.length;for(a=0;a<c;a++)k[a]=k[a].replace(z,"$1");d.each(g,function(i,j){var D=j.replace(y,
"$1"),E=d.inArray(D,k);if(E===-1){A.push(j);q.push("</"+D+">")}else k.splice(E,1)});q.reverse();if(N){c=f.find(r).remove().html();e=f.html();l=e+c;a=""}else{c=l.slice(e.length);if(c===""||c.split(/\s+/).length<b.widow)return;a=q.pop()||"";e+=q.join("");c=A.join("")+c}b.moreLabel=f.find(C).length?"":H(b);if(p)c=l;e+=a;b.summary=e;b.details=c;b.lastCloseTag=a;if(n.onSlice)b=(h=b.onSlice.call(o,b))&&h.details?h:b;p=G(b,p);f.html(p);m=f.find(r);s=f.find(C);m.hide();s.find("a").unbind("click.expander").bind("click.expander",
function(i){i.preventDefault();s.hide();B.hide();n.beforeExpand&&b.beforeExpand.call(o);m.stop(false,true)[b.expandEffect](O,function(){m.css({zoom:""});n.afterExpand&&b.afterExpand.call(o);J(b,m,o)})});B=f.find("div."+b.summaryClass);b.userCollapse&&!f.find("span."+b.lessClass).length&&f.find(r).append('<span class="'+b.lessClass+'">'+b.userCollapsePrefix+'<a href="#">'+b.userCollapseText+"</a></span>");f.find("span."+b.lessClass+" a").unbind("click.expander").bind("click.expander",function(i){i.preventDefault();
clearTimeout(w);i=d(this).closest(r);v(b,i);n.onCollapse&&b.onCollapse.call(o,true)})}});return this};d.fn.expander.defaults=d.expander.defaults})(jQuery);