Skip to content

Instantly share code, notes, and snippets.

@casualjim
Created August 11, 2012 23:34
Show Gist options
  • Save casualjim/3327826 to your computer and use it in GitHub Desktop.
Save casualjim/3327826 to your computer and use it in GitHub Desktop.
CoffeeScript version of jQuery text area with offset calculation for pasted urls
$ = jQuery
$.fn.extend
tweetArea: (options) ->
settings =
offset: 21
charDisplay: '#char-count'
warnAt: 120
postBox: '#tweet-area'
max: 140
warnClass: 'label-important'
noWarnClass: 'label-info'
settings = $.extend settings, options
checkPostForURL = (post) ->
matches = []
# regex taken from https://gist.github.com/1033143
urlexp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi
if post and urlexp.test(post)
offset = 0
matches = post.match urlexp
$.each matches, () ->
len = this.length
matchoffset = len - settings.offset
offset = offset + matchoffset
offset
pbId = settings.postBox.replace /^#/,''
cntId = settings.charDisplay.replace /^#/, ''
elemHtml = '<textarea id="'+pbId+'" name="'+pbId+'"></textarea><br />'
elemHtml += 'There are <span id="'+cntId+'" class="label">140</span> characters remaining.'
return @each () ->
$this = $(this)
$this.html elemHtml
$textBox = $(settings.postBox)
$disp = $(settings.charDisplay)
handler = (e) ->
a = $textBox.val().length
post = $textBox.val()
offset = checkPostForURL post
a = a - offset if offset
$disp.text(settings.max-a)
if a > settings.warnAt
if settings.warnClass and not $disp.hasClass(settings.warnClass)
$disp.removeClass settings.noWarnClass
$disp.addClass settings.warnClass
else
if settings.noWarnClass and not $disp.hasClass(settings.noWarnClass)
$disp.removeClass settings.warnClass
$disp.addClass settings.noWarnClass
$textBox.keyup handler
$this
$ ->
$('#tweetArea').tweetArea()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment