From 31d6024c5c8d0e2a9e569865801190f48fc8b89f Mon Sep 17 00:00:00 2001 From: Stephan Klinger Date: Wed, 10 Aug 2016 23:46:50 +0200 Subject: [PATCH 1/4] Add .gitignore --- .gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca584fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Logs +logs +*.log +npm-debug.log* + +# Dependency directories +node_modules + From c77aaae0fa53a4d0a7b7cff798391faae175742f Mon Sep 17 00:00:00 2001 From: Stephan Klinger Date: Thu, 11 Aug 2016 00:07:02 +0200 Subject: [PATCH 2/4] Add package.json and define dependencies. Update README accordingly --- README.md | 12 ++++++++++++ package.json | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 package.json diff --git a/README.md b/README.md index ae56461..aa88ff5 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,18 @@ You can change the behaviour by changing following js variables. | `onMore` | Callback function to trigger when "more" is clicked | | `onLess` | Callback function to trigger when "less" is clicked | +Development +----------- + +Minifying of the source file with uglify-js is registered as npm script. +To start developing install dependencies: + + $ npm install + +After changes of the source file `jquery.shorten.js` the minified version +`jquery.shorten.min.js` can be generated with the following command. + + $ npm run minify Licence ------- diff --git a/package.json b/package.json new file mode 100644 index 0000000..28eb558 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "jquery.shorten", + "version": "1.0.0", + "description": "A simple jquery plugin that automatically shortens text within an element and add more link.", + "main": "index.js", + "dependencies": {}, + "devDependencies": { + "uglify-js": "latest" + }, + "scripts": { + "minify": "uglifyjs src/jquery.shorten.js > src/jquery.shorten.min.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/viralpatel/jquery.shorten.git" + }, + "keywords": [], + "author": "", + "license": "MIT", + "bugs": { + "url": "https://github.com/viralpatel/jquery.shorten/issues" + }, + "homepage": "https://github.com/viralpatel/jquery.shorten#readme" +} From 53026acdd6788d3e1db48fb7f4c13f0af8602c8a Mon Sep 17 00:00:00 2001 From: Stephan Klinger Date: Wed, 10 Aug 2016 23:27:50 +0200 Subject: [PATCH 3/4] Remove animation --- src/jquery.shorten.js | 12 ++++-------- src/jquery.shorten.min.js | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/jquery.shorten.js b/src/jquery.shorten.js index 5ed0497..b46e1db 100644 --- a/src/jquery.shorten.js +++ b/src/jquery.shorten.js @@ -26,8 +26,6 @@ ellipsesText: "...", moreText: "more", lessText: "less", - onLess: function() {}, - onMore: function() {}, errMsg: null, force: false }; @@ -50,16 +48,14 @@ if ($this.hasClass('less')) { $this.removeClass('less'); $this.html(config.moreText); - $this.parent().prev().animate({'height':'0'+'%'}, function () { $this.parent().prev().prev().show(); }).hide('fast', function() { - config.onLess(); - }); + $this.parent().prev().prev().show(); // shortcontent + $this.parent().prev().hide(); // allcontent } else { $this.addClass('less'); $this.html(config.lessText); - $this.parent().prev().animate({'height':'100'+'%'}, function () { $this.parent().prev().prev().hide(); }).show('fast', function() { - config.onMore(); - }); + $this.parent().prev().prev().hide(); // shortcontent + $this.parent().prev().show(); // allcontent } return false; } diff --git a/src/jquery.shorten.min.js b/src/jquery.shorten.min.js index 51ca902..048f2e1 100644 --- a/src/jquery.shorten.min.js +++ b/src/jquery.shorten.min.js @@ -1 +1 @@ -!function(e){e.fn.shorten=function(s){"use strict";var t={showChars:100,minHideChars:10,ellipsesText:"...",moreText:"more",lessText:"less",onLess:function(){},onMore:function(){},errMsg:null,force:!1};return s&&e.extend(t,s),e(this).data("jquery.shorten")&&!t.force?!1:(e(this).data("jquery.shorten",!0),e(document).off("click",".morelink"),e(document).on({click:function(){var s=e(this);return s.hasClass("less")?(s.removeClass("less"),s.html(t.moreText),s.parent().prev().animate({height:"0%"},function(){s.parent().prev().prev().show()}).hide("fast",function(){t.onLess()})):(s.addClass("less"),s.html(t.lessText),s.parent().prev().animate({height:"100%"},function(){s.parent().prev().prev().hide()}).show("fast",function(){t.onMore()})),!1}},".morelink"),this.each(function(){var s=e(this),n=s.html(),r=s.text().length;if(r>t.showChars+t.minHideChars){var o=n.substr(0,t.showChars);if(o.indexOf("<")>=0){for(var a=!1,i="",h=0,l=[],c=null,f=0,u=0;u<=t.showChars;f++)if("<"!=n[f]||a||(a=!0,c=n.substring(f+1,n.indexOf(">",f)),"/"==c[0]?c!="/"+l[0]?t.errMsg="ERROR en HTML: the top of the stack should be the tag that closes":l.shift():"br"!=c.toLowerCase()&&l.unshift(c)),a&&">"==n[f]&&(a=!1),a)i+=n.charAt(f);else if(u++,h<=t.showChars)i+=n.charAt(f),h++;else if(l.length>0){for(j=0;j";break}o=e("
").html(i+''+t.ellipsesText+"").html()}else o+=t.ellipsesText;var m='
'+o+'
'+n+'
'+t.moreText+"";s.html(m),s.find(".allcontent").hide(),e(".shortcontent p:last",s).css("margin-bottom",0)}}))}}(jQuery); \ No newline at end of file +(function($){$.fn.shorten=function(settings){"use strict";var config={showChars:100,minHideChars:10,ellipsesText:"...",moreText:"more",lessText:"less",errMsg:null,force:false};if(settings){$.extend(config,settings)}if($(this).data("jquery.shorten")&&!config.force){return false}$(this).data("jquery.shorten",true);$(document).off("click",".morelink");$(document).on({click:function(){var $this=$(this);if($this.hasClass("less")){$this.removeClass("less");$this.html(config.moreText);$this.parent().prev().prev().show();$this.parent().prev().hide()}else{$this.addClass("less");$this.html(config.lessText);$this.parent().prev().prev().hide();$this.parent().prev().show()}return false}},".morelink");return this.each(function(){var $this=$(this);var content=$this.html();var contentlen=$this.text().length;if(contentlen>config.showChars+config.minHideChars){var c=content.substr(0,config.showChars);if(c.indexOf("<")>=0){var inTag=false;var bag="";var countChars=0;var openTags=[];var tagName=null;for(var i=0,r=0;r<=config.showChars;i++){if(content[i]=="<"&&!inTag){inTag=true;tagName=content.substring(i+1,content.indexOf(">",i));if(tagName[0]=="/"){if(tagName!="/"+openTags[0]){config.errMsg="ERROR en HTML: the top of the stack should be the tag that closes"}else{openTags.shift()}}else{if(tagName.toLowerCase()!="br"){openTags.unshift(tagName)}}}if(inTag&&content[i]==">"){inTag=false}if(inTag){bag+=content.charAt(i)}else{r++;if(countChars<=config.showChars){bag+=content.charAt(i);countChars++}else{if(openTags.length>0){for(j=0;j"}break}}}}c=$("
").html(bag+''+config.ellipsesText+"").html()}else{c+=config.ellipsesText}var html='
'+c+'
'+content+'
'+config.moreText+"";$this.html(html);$this.find(".allcontent").hide();$(".shortcontent p:last",$this).css("margin-bottom",0)}})}})(jQuery); From b680099e29eede4584c454e61d767aa6b835528f Mon Sep 17 00:00:00 2001 From: Stephan Klinger Date: Fri, 12 Aug 2016 12:09:35 +0200 Subject: [PATCH 4/4] Update package.json with correct "main" and jquery dependency --- package.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 28eb558..c8398b4 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,10 @@ "name": "jquery.shorten", "version": "1.0.0", "description": "A simple jquery plugin that automatically shortens text within an element and add more link.", - "main": "index.js", - "dependencies": {}, + "main": "src/jquery.shorten.js", + "dependencies": { + "jquery": "^3.1.0" + }, "devDependencies": { "uglify-js": "latest" },