Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;(function($) {
- $.fn.shorten = function(settings) {
- var config = $.extend( {
- showChars : 100,
- ellipsesText : "...",
- moreText : "more",
- lessText : "less"
- }, settings);
- $('.morelink').live('click', function() {
- var $this = $(this);
- // Toggle del nombre del link
- if ($this.hasClass('less')) { // clic en more para mostrar less
- $this.removeClass('less');
- $this.html(config.moreText);
- // muestro shorcontent y escondo allcontent
- $this.parent().prev().prev().show(); // shortcontent
- $this.parent().prev().hide(); // allcontent
- } else { // click en less para mostrar more
- $this.addClass('less');
- $this.html(config.lessText);
- $this.parent().prev().prev().hide(); // shortcontent
- $this.parent().prev().show(); // allcontent
- }
- return false;
- });
- return this.each(function() {
- var $this = $(this);
- var content = $this.html();
- if (content.length > config.showChars) {
- var c = content.substr(0, config.showChars);
- if (c.indexOf('<') >= 0) // If there's HTML don't want to cut it
- {
- var inTag = false; // I'm in a tag?
- var bag = ''; // Put the characters to be shown here
- var countChars = 0; // Current bag size
- var openTags = []; // Stack for opened tags, so I can close them later
- for (i=0; i<content.length; i++)
- {
- if (content[i] == '<' && !inTag)
- {
- inTag = true;
- // This could be "tag" or "/tag"
- tagName = content.substring(i+1, content.indexOf('>', i));
- // If its a closing tag
- if (tagName[0] == '/')
- {
- if (tagName != '/'+openTags[0]) console.log('ERROR en HTML: el tope del stack debe ser la tag que cierra');
- else
- openTags.shift(); // Pops the last tag from the open tag stack (the tag is closed in the retult HTML!)
- }
- else
- {
- // There are some nasty tags that don't have a close tag like <br/>
- if (tagName.toLowerCase() != 'br')
- openTags.unshift( tagName );// Agrega al inicio el nombre de la tag que abre
- }
- }
- if (inTag && content[i] == '>')
- {
- inTag = false;
- }
- if (inTag) bag += content[i]; // Add tag name chars to the result
- else
- {
- if (countChars < config.showChars)
- {
- bag += content[i];
- countChars ++;
- }
- else // Ya tengo los caracteres necesarios
- {
- if (openTags.length > 0) // Tengo tags sin cerrar
- {
- console.log('Quedaron tags abiertas');
- console.log(openTags);
- for (j=0; j<openTags.length; j++)
- {
- console.log('Cierro tag '+ openTags[j]);
- bag += '</'+ openTags[j] +'>'; // Cierro todas las tags que quedaron abiertas
- // You could shift the tag from the stack to check if you end with an empty stack, that means you have closed all open tags
- }
- break;
- }
- }
- }
- }
- c = bag;
- }
- var html = '<span class="shortcontent">' + c + ' ' + config.ellipsesText +
- '</span><span class="allcontent">' + content +
- '</span> <span><a href="javascript://nop/" class="morelink">' + config.moreText + '</a></span>';
- $this.html(html);
- $(".allcontent").hide(); // Esconde el contenido completo para todos los textos
- }
- });
- };
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement