From 057d9c205e15d6d9fdf6f98e4875ab727fe13455 Mon Sep 17 00:00:00 2001 From: Dekatron Date: Mon, 15 Jan 2018 11:44:32 +0000 Subject: [PATCH 1/5] Fix dissapearing less button closes #42 The show less button dissapears after click show more for the second time. It looks like it was originally planned that there would be a seperate behaviour for explicit and implicit expand and collapse but I can't see that an implicit expand and collapse was ever implemented. As such all open and closes are explicit and the show less button shouldn't be removed unless we want to remove it (in which case it can be done manually in the appication logic) --- truncate.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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; } From 0c441b519eda9de3554d510d05c8c99d5106cdd1 Mon Sep 17 00:00:00 2001 From: Dekatron Date: Mon, 15 Jan 2018 11:57:46 +0000 Subject: [PATCH 2/5] White space only --- test/chrome/truncate-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/chrome/truncate-test.js b/test/chrome/truncate-test.js index b77d5ca..73ca244 100644 --- a/test/chrome/truncate-test.js +++ b/test/chrome/truncate-test.js @@ -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
"); }); From ec27d3b1070194221fe55ee67d6eeeb3736cfbed Mon Sep 17 00:00:00 2001 From: Dekatron Date: Mon, 15 Jan 2018 12:00:33 +0000 Subject: [PATCH 3/5] Correct multiple updates tests If we have specified showLess and we have expanded the text we would expect a show less button. --- test/chrome/truncate-test.js | 2 +- test/phantomjs/truncate-test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/chrome/truncate-test.js b/test/chrome/truncate-test.js index 73ca244..cbcd084 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"); }); }); 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"); }); }); From 3ac5d90b8fa9a35636dad3423ebae2b1f63b134a Mon Sep 17 00:00:00 2001 From: Dekatron Date: Mon, 15 Jan 2018 12:03:17 +0000 Subject: [PATCH 4/5] Add compiled files --- dist/truncate.js | 21 +++++++++++++-------- dist/truncate.min.js | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) 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 From 616767e9ef9f88cd26fc95c268c2524473393dce Mon Sep 17 00:00:00 2001 From: Dekatron Date: Mon, 15 Jan 2018 12:08:10 +0000 Subject: [PATCH 5/5] Fix Chrome should update options test --- test/chrome/truncate-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/chrome/truncate-test.js b/test/chrome/truncate-test.js index cbcd084..3f981cf 100644 --- a/test/chrome/truncate-test.js +++ b/test/chrome/truncate-test.js @@ -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 () {