SHARE
TWEET

CoffeeScript-jQuery shorten

patrickmcelwee Oct 15th, 2012 276 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $ = jQuery
  2.  
  3. # Add a jQuery plugin to shorten/expand a long section
  4. $.fn.extend
  5.   shorten: (settings) ->
  6.  
  7.     # Defaults
  8.     config =
  9.       showChars: 200
  10.       ellipsesText: "..."
  11.       moreText: "more"
  12.       lessText: "less"
  13.  
  14.     # Merge settings into defaults
  15.     config = $.extend config, settings
  16.  
  17.     # Handle clicks on more and less buttons
  18.     $('.more_link').live 'click', (e) ->
  19.       e.preventDefault()
  20.       $this = $(this)
  21.  
  22.       if $this.hasClass 'less'
  23.         $this.removeClass 'less'
  24.         $this.html(config.moreText)
  25.       else
  26.         $this.addClass 'less'
  27.         $this.html(config.lessText)
  28.  
  29.       $this.parent().prev().prev().toggle()
  30.       $this.parent().prev().toggle()
  31.  
  32.     # Split content based on max characters
  33.     this.each () ->
  34.       $this = $(this)
  35.  
  36.       content = $this.html()
  37.       if content.length > config.showChars
  38.         visible = content.substr(0, config.showChars)
  39.  
  40.         # Need to be sure that this does not split HTML tags
  41.         if visible.indexOf('<') >= 0 # Test if HTML exists
  42.           inTag = false # Am I in a tag?
  43.           bag = '' # Characters to be shown
  44.           countChars = 0
  45.           openTags = [] # Stack for opened tags, so can be closed
  46.  
  47.           # Iterate over HTML content
  48.           for i in [0...content.length]
  49.             # Tag start
  50.             if (content[i] == '<') and not inTag
  51.               inTag = true
  52.  
  53.               # Test to see if opening "tag" or closing "/tag"
  54.               tagName = content.substring(i+1, content.indexOf('>', i))
  55.  
  56.               # If closing tag
  57.               if tagName[0] == '/'
  58.                 if tagName != "/#{openTags[0]}"
  59.                   console.log("HTML error: the top of the openTags stack should be the closing tag")
  60.                 else
  61.                   openTags.shift(); # Pops the now closed tag from the stack
  62.  
  63.               #Case that it is an opening tag
  64.               else
  65.                 #First check to see if it closes itself like <br/>
  66.                 openTags.unshift(tagName) unless tagName[tagName.length-2] == '/'
  67.  
  68.             # Tag close
  69.             if inTag and content[i] == '>'
  70.               inTag = false
  71.  
  72.             if inTag
  73.               bag += content[i] # Add in-tag characters to visible bag
  74.             else
  75.               if countChars < config.showChars
  76.                 bag += content[i]
  77.                 countChars++
  78.               else # Already have reach max characters to show
  79.                 if openTags.length > 0 # if there are still open tags
  80.                   console.log("There were open tags")
  81.                   console.log(openTags)
  82.                   for j in [0...openTags.length]
  83.                     console.log("Closing tag #{openTags[j]}")
  84.                     bag += "</#{openTags[j]}>"
  85.                 break # Better to empty stack than this?
  86.  
  87.           visible = bag # Setting visible to bag in case there was HTML
  88.  
  89.         html = """
  90.               <span class="short_content">#{visible}
  91.               <span class="more_ellipses">#{config.ellipsesText} </span></span>
  92.                <span class='all_content'>#{content}</span>  
  93.                <span><a href='#' class='more_link'>#{config.moreText}</a></span>
  94.               """
  95.         $this.html(html)
  96.         $('.all_content').hide()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top