-
Notifications
You must be signed in to change notification settings - Fork 168
/
jquery.expander.min.js
7 lines (7 loc) · 5.83 KB
/
jquery.expander.min.js
1
2
3
4
5
6
7
/*!
* jQuery Expander Plugin - v2.0.2 - 2022-01-29
* https://kswedberg.github.io/jquery-expander/
* Copyright (c) 2022 Karl Swedberg
* Licensed MIT (http://kswedberg.mit-license.org/)
*/
!function(e){"function"==typeof define&&define.amd?define(["jquery"],e):"object"==typeof module&&"object"==typeof module.exports?module.exports=e:e(jQuery)}(function(U){U.expander={version:"2.0.2",defaults:{slicePoint:100,sliceOn:null,preserveWords:!0,normalizeWhitespace:!0,showWordCount:!1,detailPrefix:" ",wordCountText:" ({{count}} words)",widow:4,expandText:"read more",expandPrefix:"… ",expandAfterSummary:!1,wordEnd:/(&(?:[^;]+;)?|[0-9a-zA-Z\u00C0-\u0100]+|[^\u0000-\u007F]+)$/,summaryClass:"summary",detailClass:"details",moreClass:"read-more",lessClass:"read-less",moreLinkClass:"more-link",lessLinkClass:"less-link",collapseTimer:0,expandEffect:"slideDown",expandSpeed:250,collapseEffect:"slideUp",collapseSpeed:200,userCollapse:!0,userCollapseText:"read less",userCollapsePrefix:" ",onSlice:null,beforeExpand:null,afterExpand:null,onCollapse:null,afterCollapse:null}},U.fn.expander=function(e){var a="init";"string"==typeof e&&(a=e,e={});var P,O=U.extend({},U.expander.defaults,e),j=/^<(?:area|br|col|embed|hr|img|input|link|meta|param).*>$/i,l=O.wordEnd,L=/<\/?(\w+)[^>]*>/g,$=/<(\w+)[^>]*>/g,D=/<\/(\w+)>/g,I=/(<\/([^>]+)>)\s*$/,A=/^(<[^>]+>)+.?/,W=/(?:^\s+|\s+$)/g,z=/\s\s+/g,e={init:function(){this.each(function(){var e,a,l,s,n,t,r,i,o,d,c,p,f=[],u=[],m={},x=this,h=U(this),C=U([]),v=U.extend({},O,h.data("expander")||{}),g=!!h.find("."+v.detailClass).length,y=!!h.find("*").filter(function(){var e=U(this).css("display");return/^block|table|list/.test(e)}).length,w=(y?"div":"span")+"."+v.detailClass,E=v.moreClass+"",T=v.lessClass+"",S=v.expandSpeed||0,b=(p=h.html(),O.normalizeWhitespace?(p||"").replace(W,"").replace(z," "):p),k=b.slice(0,v.slicePoint);if(v.moreSelector="span."+E.split(" ").join("."),v.lessSelector="span."+T.split(" ").join("."),!U.data(this,"expanderInit")){for(U.data(this,"expanderInit",!0),U.data(this,"expander",v),U.each(["onSlice","beforeExpand","afterExpand","onCollapse","afterCollapse"],function(e,a){m[a]=U.isFunction(v[a])}),n=(k=F(k)).replace(L,"").length;n<v.slicePoint;)k+=s="<"===(s=b.charAt(k.length))?b.slice(k.length).match(A)[0]:s,n++;for(T=(k=F(k=v.sliceOn?function(e){var a="ExpandMoreHere374216623",l=e.summaryText.replace(e.sliceOn,a),l=(l=U("<div>"+l+"</div>").text()).indexOf(a),a=e.summaryText.indexOf(e.sliceOn);-1!==l&&l<e.slicePoint&&(e.summaryText=e.allHtml.slice(0,a));return e}({sliceOn:v.sliceOn,slicePoint:v.slicePoint,allHtml:b,summaryText:k}).summaryText:k,v.preserveWords&&b.slice(k.length).length)).match($)||[],t=k.match(D)||[],l=[],U.each(T,function(e,a){j.test(a)||l.push(a)}),T=l,a=t.length,e=0;e<a;e++)t[e]=t[e].replace(D,"$1");if(U.each(T,function(e,a){var l=a.replace($,"$1"),s=U.inArray(l,t);-1===s?(f.push(a),u.push("</"+l+">")):t.splice(s,1)}),u.reverse(),g)i=h.find(w).remove().html(),b=(k=h.html())+i,r="";else{if(""===(g=(i=b.slice(k.length)).replace(L,"").replace(W,""))||g.split(/\s+/).length<v.widow)return;r=u.pop()||"",k+=u.join(""),i=f.join("")+i}v.moreLabel=h.find(v.moreSelector).length?"":function(e,a){var l='<span class="'+e.moreClass+'">'+e.expandPrefix;e.showWordCount?e.wordCountText=e.wordCountText.replace(/\{\{count\}\}/,a.replace(L,"").replace(/&(?:amp|nbsp);/g,"").replace(/(?:^\s+|\s+$)/,"").match(/\w+/g).length):e.wordCountText="";return l+='<a href="#" class="'+e.moreLinkClass+'">'+e.expandText+e.wordCountText+"</a></span>"}(v,i),y?i=b:"&"===k.charAt(k.length-1)&&(c=/^[#\w\d\\]+;/.exec(i))&&(i=i.slice(c[0].length),k+=c[0]),v.summary=k+=r,v.details=i,v.lastCloseTag=r,y=function(e,a){var l="span",s=e.summary,n=I.exec(s),t=n?n[2].toLowerCase():"";a?(l="div",n&&"a"!==t&&!e.expandAfterSummary?s=s.replace(I,e.moreLabel+"$1"):s+=e.moreLabel,s='<div class="'+e.summaryClass+'">'+s+"</div>"):s+=e.moreLabel;return[s,e.detailPrefix||"","<",l+' class="'+e.detailClass+'"',">",e.details,"</"+l+">"].join("")}(v=m.onSlice?(l=v.onSlice.call(x,v))&&l.details?l:v:v,y),h.empty().append(y),o=h.find(w),d=h.find(v.moreSelector),"slideUp"===v.collapseEffect&&"slideDown"!==v.expandEffect||h.is(":hidden")?o.css({display:"none"}):o[v.collapseEffect](0),C=h.find("div."+v.summaryClass),y=function(e){e.preventDefault();e=e.startExpanded?0:S;d.hide(),C.hide(),m.beforeExpand&&v.beforeExpand.call(x),o.stop(!1,!0)[v.expandEffect](e,function(){var e,a,l;o.css({zoom:""}),m.afterExpand&&v.afterExpand.call(x),a=o,l=x,(e=v).collapseTimer&&(P=setTimeout(function(){H(e,a),U.isFunction(e.onCollapse)&&e.onCollapse.call(l,!1)},e.collapseTimer))})},d.find("a").off("click.expander").on("click.expander",y),v.userCollapse&&!h.find(v.lessSelector).length&&h.find(w).append('<span class="'+v.lessClass+'">'+v.userCollapsePrefix+'<a href="#" class="'+v.lessLinkClass+'">'+v.userCollapseText+"</a></span>"),h.find(v.lessSelector+" a").off("click.expander").on("click.expander",function(e){e.preventDefault(),clearTimeout(P);e=U(this).closest(w);H(v,e),m.onCollapse&&v.onCollapse.call(x,!0)}),v.startExpanded&&y({preventDefault:function(){},startExpanded:!0})}})},destroy:function(){this.each(function(){var e,a,l=U(this);l.data("expanderInit")&&(e=U.extend({},l.data("expander")||{},O),a=l.find("."+e.detailClass).contents(),l.removeData("expanderInit"),l.removeData("expander"),l.find(e.moreSelector).remove(),l.find("."+e.summaryClass).remove(),l.find("."+e.detailClass).after(a).remove(),l.find(e.lessSelector).remove())})}};function F(e,a){return e.lastIndexOf("<")>e.lastIndexOf(">")&&(e=e.slice(0,e.lastIndexOf("<"))),(e=a?e.replace(l,""):e).replace(W,"")}function H(e,a){a.stop(!0,!0)[e.collapseEffect](e.collapseSpeed,function(){a.prev("span."+e.moreClass).show().length||a.parent().children("div."+e.summaryClass).show().find("span."+e.moreClass).show(),e.afterCollapse&&e.afterCollapse.call(a)})}return e[a]&&e[a].call(this),this},U.fn.expander.defaults=U.expander.defaults});