1
0
mirror of https://github.com/wbbaddons/Tims-Chat.git synced 2024-10-31 14:10:08 +00:00
Tims-Chat/file/js/jCounter.jQuery.coffee

50 lines
1.4 KiB
CoffeeScript
Raw Normal View History

2011-12-19 16:36:05 +00:00
###
# jCounter - a simple character counter
#
# @author Maximilian Mader
# @copyright 2011 Maximilian Mader
# @license Creative Commons Attribution-NonCommercial-ShareAlike <http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode>
# @package jQuery.jCounter
###
(($) ->
$.fn.jCounter = (container, options) ->
2011-12-19 16:36:05 +00:00
options = $.extend
max: 0
2012-03-11 14:07:28 +00:00
counterClass: 'jCounter'
2011-12-19 16:36:05 +00:00
countUp: false
2012-03-11 14:07:28 +00:00
width: '100%'
2011-12-19 16:36:05 +00:00
, options
2013-01-29 21:15:20 +00:00
max = if @.attr('maxlength')? then @.attr 'maxlength' else options.max
2012-03-11 14:07:28 +00:00
2013-01-29 21:15:20 +00:00
if not container?
@.addClass 'jCounterInput'
@.wrap("""<div class="jCounterContainer" style="width: #{options.width}"><div></div></div>""").parent().append """<div class="#{options.counterClass} color-1">#{max}</div>"""
jCounterContainer = $(@).parent().children ".#{options.counterClass}"
else
2013-01-29 21:15:20 +00:00
jCounterContainer = if typeof container is 'object' then container else $ container
@.on 'keypress keyup', $.proxy () ->
2013-01-29 21:15:20 +00:00
length = if options.countUp then @.val().length else max - @.val().length
2011-12-19 16:36:05 +00:00
2012-03-11 14:07:28 +00:00
if options.countUp && max > 0
if length < max / 2
color = 1
2013-01-29 21:15:20 +00:00
else if max / 2 < length <= max / 1.2
2012-03-11 14:07:28 +00:00
color = 2
else
color = 3
else if options.countUp
2011-12-19 16:36:05 +00:00
color = 1
else
2013-01-29 21:15:20 +00:00
if max / 2 < length
2011-12-19 16:36:05 +00:00
color = 1
2013-01-29 21:15:20 +00:00
else if max / 6 <= length <= max / 2
2011-12-19 16:36:05 +00:00
color = 2
else
color = 3
2013-01-29 21:15:20 +00:00
jCounterContainer.text(length).removeClass().addClass "#{options.counterClass} color-#{color}"
, @
2011-12-19 16:36:05 +00:00
)(jQuery)