Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update jquery.shorten.js #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions src/jquery.shorten.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
moreText: "more",
lessText: "less",
errMsg: null,
force: false
force: false,
showlines: 4 // show line numbers
};

if (settings) {
Expand Down Expand Up @@ -62,9 +63,50 @@

return this.each(function() {
var $this = $(this);

// 20150331 I also used follow two lines in my project, but I'm not sure It's suitable for the public, so just comment here
//var content = $this.html().trim();
//var contentlen = $this.text().trim().length;
//
var content = $this.html();
var contentlen = $this.text().length;

// 20150330 added by edgar, added function for collapsed by lines
var allrows = $this.html().split('<br>');
var ilinecount = allrows.length;
var collLines = config.showlines;

if (allrows.length < 1) {
var re = /\r\n|\n\r|\n|\r/g;
allrows = $this.text().split(re);
ilinecount = allrows.length;
}


if (ilinecount > collLines && contentlen < config.showChars) {
var bag = '';
var collContent = '';
var collrowcounter = 0;
$.each(allrows, function (index, item) {
if (collrowcounter >= collLines)
return;
collContent += item;
collContent += ' <br/> ';
collrowcounter++;

});

c = $('<div/>').html(bag + '<span class="ellip">' + collContent.trim() + '</span>').html();
var html = '<div class="shortcontent">' + c +
'</div><div class="allcontent">' + content +
'</div><span><a href="javascript://nop/" class="morelink">' + config.moreText + '</a></span>';

$this.html(html);
$this.find(".allcontent").hide(); // Hide all text
$('.shortcontent p:last', $this).css('margin-bottom', 0); //Remove bottom margin on last paragraph as it's likely shortened
return;
}
//

if (contentlen > config.showChars) {
var c = content.substr(0, config.showChars);
if (c.indexOf('<') >= 0) // If there's HTML don't want to cut it
Expand Down