diff --git a/dist/truncate.js b/dist/truncate.js index fd72ee6..512f26e 100644 --- a/dist/truncate.js +++ b/dist/truncate.js @@ -1,4 +1,12 @@ -(function (module, $, undefined) { +(function (root, factory) { + if (typeof define === 'function' && define.amd) { + define(['jQuery'], factory); + } else if (typeof module === 'object' && module.exports) { + module.exports = factory(require('jQuery')); + } else { + root.Truncate = factory(root.jQuery); + } +}(this, function ($) { var BLOCK_TAGS = ['table', 'thead', 'tbody', 'tfoot', 'tr', 'col', 'colgroup', 'object', 'embed', 'param', 'ol', 'ul', 'dl', 'blockquote', 'select', 'optgroup', 'option', 'textarea', 'script', 'style']; @@ -489,11 +497,8 @@ * Returns nothing. */ expand: function () { - var includeShowLess = true; - if(this.isExplicitlyCollapsed) { this.isExplicitlyCollapsed = false; - includeShowLess = false; } if (!this.isCollapsed) { @@ -502,7 +507,7 @@ this.isCollapsed = false; - this.element.innerHTML = this.isTruncated ? this.original + (includeShowLess ? this.options.showLess : "") : this.original; + this.element.innerHTML = this.isTruncated ? this.original + this.options.showLess : this.original; }, /* Public: Collapses the element to the truncated state. @@ -514,7 +519,7 @@ */ collapse: function (retruncate) { this.isExplicitlyCollapsed = true; - + if (this.isCollapsed) { return; } @@ -543,6 +548,6 @@ }); }; - module.Truncate = Truncate; + return Truncate; -})(this, jQuery); +})); diff --git a/dist/truncate.min.js b/dist/truncate.min.js index a0e5355..edf2091 100644 --- a/dist/truncate.min.js +++ b/dist/truncate.min.js @@ -1 +1 @@ -!function(t,e,i){function n(t){return t.replace(/\s*$/,"")}function s(t,e){if(t.innerText)t.innerText=e;else if(t.nodeValue)t.nodeValue=e;else{if(!t.textContent)return!1;t.textContent=e}}function o(t,e,i,n){var o,h=t.parent();t.remove();var r=i?i.length:0;if(h.contents().length>r)return o=h.contents().eq(-1-r),a(o,e,i,n);var l=h.prev();return o=l.contents().eq(-1),!!o.length&&(s(o[0],o.text()+n.ellipsis),h.remove(),i.length&&l.append(i),!0)}function h(t,e,i,h){for(var r,l,a=t[0],p=t.text(),d="",c=0,u=p.length;c<=u;)r=c+(u-c>>1),l=h.ellipsis+n(p.substr(r-1,p.length)),s(a,l),e.height()>h.maxHeight?c=r+1:(u=r-1,d=d.length>l.length?d:l);return d.length>0?(s(a,d),!0):o(t,e,i,h)}function r(t,e,i,h){for(var r,l,a=t[0],p=t.text(),d="",c=0,u=p.length;c<=u;)r=c+(u-c>>1),l=n(p.substr(0,r+1))+h.ellipsis,s(a,l),e.height()>h.maxHeight?u=r-1:(c=r+1,d=d.length>l.length?d:l);return d.length>0?(s(a,d),!0):o(t,e,i,h)}function l(t,e,i,h){for(var r,l,a=t[0],p=t.text(),d="",c=0,u=p.length,g=u>>1;c<=g;)r=c+(g-c>>1),l=n(p.substr(0,r))+h.ellipsis+p.substr(u-r,u-r),s(a,l),e.height()>h.maxHeight?g=r-1:(c=r+1,d=d.length>l.length?d:l);return d.length>0?(s(a,d),!0):o(t,e,i,h)}function a(t,e,i,n){return"end"===n.position?r(t,e,i,n):"start"===n.position?h(t,e,i,n):l(t,e,i,n)}function p(t,i,n,s){var o,h,r=t[0],l=t.contents(),p=l.length,d=p-1,u=!1;for(t.empty();d>=0&&!u;d--)o=l.eq(d),h=o[0],8!==h.nodeType&&(r.insertBefore(h,r.firstChild),n.length&&(e.inArray(r.tagName.toLowerCase(),g)>=0?t.after(n):t.append(n)),i.height()>s.maxHeight&&(u=3===h.nodeType?a(o,i,n,s):c(o,i,n,s)),!u&&n.length&&n.remove());return u}function d(t,i,n,s){var o,h,r=t[0],l=t.contents(),p=0,d=l.length,u=!1;for(t.empty();p=0?t.after(n):t.append(n)),i.height()>s.maxHeight&&(u=3===h.nodeType?a(o,i,n,s):c(o,i,n,s)),!u&&n.length&&n.remove());return u}function c(t,e,i,n){return"end"===n.position?d(t,e,i,n):"start"===n.position?p(t,e,i,n):d(t,e,i,n)}function u(t,i){this.element=t,this.$element=e(t),this._name="truncate",this._defaults={lines:1,ellipsis:"…",showMore:"",showLess:"",position:"end",lineHeight:"auto"},this.config(i),this.original=this.cached=t.innerHTML,this.isTruncated=!1,this.isCollapsed=!0,this.update()}var g=["table","thead","tbody","tfoot","tr","col","colgroup","object","embed","param","ol","ul","dl","blockquote","select","optgroup","option","textarea","script","style"];u.prototype={config:function(t){if(this.options=e.extend({},this._defaults,t),"auto"===this.options.lineHeight){var n=this.$element.css("line-height"),s=18;"normal"!==n&&(s=parseInt(n,10)),this.options.lineHeight=s}this.options.maxHeight===i&&(this.options.maxHeight=parseInt(this.options.lines,10)*parseInt(this.options.lineHeight,10)),"start"!==this.options.position&&"middle"!==this.options.position&&"end"!==this.options.position&&(this.options.position="end"),this.$clipNode=e(e.parseHTML(this.options.showMore),this.$element),this.original&&this.update()},update:function(t){var e=!this.isCollapsed;"undefined"!=typeof t?this.original=this.element.innerHTML=t:this.isCollapsed&&this.element.innerHTML===this.cached&&(this.element.innerHTML=this.original);var i=this.$element.wrapInner("
").children();i.css({border:"none",margin:0,padding:0,width:"auto",height:"auto","word-wrap":"break-word"}),this.isTruncated=!1,i.height()>this.options.maxHeight?(this.isTruncated=c(i,i,this.$clipNode,this.options),this.isExplicitlyCollapsed&&(this.isCollapsed=!0,e=!1)):this.isCollapsed=!1,i.replaceWith(i.contents()),this.cached=this.element.innerHTML,e&&(this.element.innerHTML=this.original)},expand:function(){var t=!0;this.isExplicitlyCollapsed&&(this.isExplicitlyCollapsed=!1,t=!1),this.isCollapsed&&(this.isCollapsed=!1,this.element.innerHTML=this.isTruncated?this.original+(t?this.options.showLess:""):this.original)},collapse:function(t){this.isExplicitlyCollapsed=!0,this.isCollapsed||(this.isCollapsed=!0,t=t||!1,t?this.update():this.element.innerHTML=this.cached)}},e.fn.truncate=function(t){var i=e.makeArray(arguments).slice(1);return this.each(function(){var n=e.data(this,"jquery-truncate");n?"function"==typeof n[t]&&n[t].apply(n,i):e.data(this,"jquery-truncate",new u(this,t))})},t.Truncate=u}(this,jQuery); \ No newline at end of file +!function(t,e){"function"==typeof define&&define.amd?define(["jQuery"],e):"object"==typeof module&&module.exports?module.exports=e(require("jQuery")):t.Truncate=e(t.jQuery)}(this,function(t){function e(t){return t.replace(/\s*$/,"")}function i(t,e){if(t.innerText)t.innerText=e;else if(t.nodeValue)t.nodeValue=e;else{if(!t.textContent)return!1;t.textContent=e}}function n(t,e,n,s){var o,h=t.parent();t.remove();var l=n?n.length:0;if(h.contents().length>l)return o=h.contents().eq(-1-l),r(o,e,n,s);var a=h.prev();return o=a.contents().eq(-1),!!o.length&&(i(o[0],o.text()+s.ellipsis),h.remove(),n.length&&a.append(n),!0)}function s(t,s,o,h){for(var r,l,a=t[0],p=t.text(),d="",u=0,c=p.length;u<=c;)r=u+(c-u>>1),l=h.ellipsis+e(p.substr(r-1,p.length)),i(a,l),s.height()>h.maxHeight?u=r+1:(c=r-1,d=d.length>l.length?d:l);return d.length>0?(i(a,d),!0):n(t,s,o,h)}function o(t,s,o,h){for(var r,l,a=t[0],p=t.text(),d="",u=0,c=p.length;u<=c;)r=u+(c-u>>1),l=e(p.substr(0,r+1))+h.ellipsis,i(a,l),s.height()>h.maxHeight?c=r-1:(u=r+1,d=d.length>l.length?d:l);return d.length>0?(i(a,d),!0):n(t,s,o,h)}function h(t,s,o,h){for(var r,l,a=t[0],p=t.text(),d="",u=0,c=p.length,g=c>>1;u<=g;)r=u+(g-u>>1),l=e(p.substr(0,r))+h.ellipsis+p.substr(c-r,c-r),i(a,l),s.height()>h.maxHeight?g=r-1:(u=r+1,d=d.length>l.length?d:l);return d.length>0?(i(a,d),!0):n(t,s,o,h)}function r(t,e,i,n){return"end"===n.position?o(t,e,i,n):"start"===n.position?s(t,e,i,n):h(t,e,i,n)}function l(e,i,n,s){var o,h,l=e[0],a=e.contents(),d=a.length,c=d-1,g=!1;for(e.empty();c>=0&&!g;c--)o=a.eq(c),h=o[0],8!==h.nodeType&&(l.insertBefore(h,l.firstChild),n.length&&(t.inArray(l.tagName.toLowerCase(),u)>=0?e.after(n):e.append(n)),i.height()>s.maxHeight&&(g=3===h.nodeType?r(o,i,n,s):p(o,i,n,s)),!g&&n.length&&n.remove());return g}function a(e,i,n,s){var o,h,l=e[0],a=e.contents(),d=0,c=a.length,g=!1;for(e.empty();d=0?e.after(n):e.append(n)),i.height()>s.maxHeight&&(g=3===h.nodeType?r(o,i,n,s):p(o,i,n,s)),!g&&n.length&&n.remove());return g}function p(t,e,i,n){return"end"===n.position?a(t,e,i,n):"start"===n.position?l(t,e,i,n):a(t,e,i,n)}function d(e,i){this.element=e,this.$element=t(e),this._name="truncate",this._defaults={lines:1,ellipsis:"…",showMore:"",showLess:"",position:"end",lineHeight:"auto"},this.config(i),this.original=this.cached=e.innerHTML,this.isTruncated=!1,this.isCollapsed=!0,this.update()}var u=["table","thead","tbody","tfoot","tr","col","colgroup","object","embed","param","ol","ul","dl","blockquote","select","optgroup","option","textarea","script","style"];return d.prototype={config:function(e){if(this.options=t.extend({},this._defaults,e),"auto"===this.options.lineHeight){var i=this.$element.css("line-height"),n=18;"normal"!==i&&(n=parseInt(i,10)),this.options.lineHeight=n}void 0===this.options.maxHeight&&(this.options.maxHeight=parseInt(this.options.lines,10)*parseInt(this.options.lineHeight,10)),"start"!==this.options.position&&"middle"!==this.options.position&&"end"!==this.options.position&&(this.options.position="end"),this.$clipNode=t(t.parseHTML(this.options.showMore),this.$element),this.original&&this.update()},update:function(t){var e=!this.isCollapsed;"undefined"!=typeof t?this.original=this.element.innerHTML=t:this.isCollapsed&&this.element.innerHTML===this.cached&&(this.element.innerHTML=this.original);var i=this.$element.wrapInner("
").children();i.css({border:"none",margin:0,padding:0,width:"auto",height:"auto","word-wrap":"break-word"}),this.isTruncated=!1,i.height()>this.options.maxHeight?(this.isTruncated=p(i,i,this.$clipNode,this.options),this.isExplicitlyCollapsed&&(this.isCollapsed=!0,e=!1)):this.isCollapsed=!1,i.replaceWith(i.contents()),this.cached=this.element.innerHTML,e&&(this.element.innerHTML=this.original)},expand:function(){this.isExplicitlyCollapsed&&(this.isExplicitlyCollapsed=!1),this.isCollapsed&&(this.isCollapsed=!1,this.element.innerHTML=this.isTruncated?this.original+this.options.showLess:this.original)},collapse:function(t){this.isExplicitlyCollapsed=!0,this.isCollapsed||(this.isCollapsed=!0,t=t||!1,t?this.update():this.element.innerHTML=this.cached)}},t.fn.truncate=function(e){var i=t.makeArray(arguments).slice(1);return this.each(function(){var n=t.data(this,"jquery-truncate");n?"function"==typeof n[e]&&n[e].apply(n,i):t.data(this,"jquery-truncate",new d(this,e))})},d}); \ No newline at end of file diff --git a/test/chrome/truncate-test.js b/test/chrome/truncate-test.js index b77d5ca..3f981cf 100644 --- a/test/chrome/truncate-test.js +++ b/test/chrome/truncate-test.js @@ -286,7 +286,7 @@ describe('truncate.js', function () { assert.equal(this.$fixture.html(), "
Members, friends, adversaries… More
"); this.$fixture.truncate('expand'); - assert.equal(this.$fixture.html(), '
Members, friends, adversaries, competitors, and colleagues
'); + assert.equal(this.$fixture.html(), "
Members, friends, adversaries, competitors, and colleagues
Less"); }); }); @@ -320,14 +320,14 @@ describe('truncate.js', function () { this.$fixture.truncate('collapse'); assert.equal(this.$fixture.html(), "
Members, friends, adversaries… More
"); }); - + it('should keep the collapsed status after multiple update', function () { this.$fixture.truncate('collapse'); assert.equal(this.$fixture.html(), "
Members, friends, adversaries… More
"); this.$fixture.truncate('update', '
Members.
'); assert.equal(this.$fixture.html(), "
Members.
"); - + this.$fixture.truncate('update', '
Members, friends, adversaries, competitors, and colleagues
'); assert.equal(this.$fixture.html(), "
Members, friends, adversaries… More
"); }); @@ -405,7 +405,7 @@ describe('truncate.js', function () { assert.equal(this.$fixture.html(), "Lorem Ipsum is simply dummy text…"); this.$fixture.truncate('config', {lines: 2}); - assert.equal(this.$fixture.html(), "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem"); + assert.equal(this.$fixture.html(), "Lorem Ipsum is simply dummy text of the printing and typesetting industry…"); }); it('should keep the collapsed after update options', function () { diff --git a/test/phantomjs/truncate-test.js b/test/phantomjs/truncate-test.js index 1d55d79..67af12c 100644 --- a/test/phantomjs/truncate-test.js +++ b/test/phantomjs/truncate-test.js @@ -286,7 +286,7 @@ describe('truncate.js', function () { assert.equal(this.$fixture.html(), "
Members, friends, adversaries,… More
"); this.$fixture.truncate('expand'); - assert.equal(this.$fixture.html(), '
Members, friends, adversaries, competitors, and colleagues
'); + assert.equal(this.$fixture.html(), "
Members, friends, adversaries, competitors, and colleagues
Less"); }); }); diff --git a/truncate.js b/truncate.js index 794db76..512f26e 100644 --- a/truncate.js +++ b/truncate.js @@ -497,11 +497,8 @@ * Returns nothing. */ expand: function () { - var includeShowLess = true; - if(this.isExplicitlyCollapsed) { this.isExplicitlyCollapsed = false; - includeShowLess = false; } if (!this.isCollapsed) { @@ -510,7 +507,7 @@ this.isCollapsed = false; - this.element.innerHTML = this.isTruncated ? this.original + (includeShowLess ? this.options.showLess : "") : this.original; + this.element.innerHTML = this.isTruncated ? this.original + this.options.showLess : this.original; }, /* Public: Collapses the element to the truncated state. @@ -522,7 +519,7 @@ */ collapse: function (retruncate) { this.isExplicitlyCollapsed = true; - + if (this.isCollapsed) { return; }