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
|
|
|
|
###
|
|
|
|
(($) ->
|
2011-12-20 15:06:48 +00:00
|
|
|
$.fn.jCounter = (container, options) ->
|
2011-12-19 16:36:05 +00:00
|
|
|
options = $.extend
|
2011-12-20 15:06:48 +00:00
|
|
|
max: 0
|
2011-12-19 16:36:05 +00:00
|
|
|
counterClass: 'counter'
|
|
|
|
countUp: false
|
|
|
|
, options
|
|
|
|
|
2011-12-20 15:06:48 +00:00
|
|
|
if this.attr('maxlength')
|
|
|
|
max = this.attr('maxlength')
|
|
|
|
else max = options.max
|
2011-12-19 16:36:05 +00:00
|
|
|
|
2011-12-20 15:06:48 +00:00
|
|
|
if !container
|
|
|
|
this.wrap('<div class="counterContainer"><div></div></div>').parent().append('<div class="' + options.counterClass + ' color-1">' + max + '</div>');
|
|
|
|
jCounterContainer = $(this).parent().children('.' + options.counterClass)
|
|
|
|
else
|
|
|
|
if `typeof container == "object"`
|
|
|
|
jCounterContainer = container
|
|
|
|
else
|
|
|
|
jCounterContainer = $ container
|
|
|
|
|
|
|
|
this.on 'keypress keyup', $.proxy () ->
|
2011-12-19 16:36:05 +00:00
|
|
|
if options.countUp
|
|
|
|
length = this.val().length
|
|
|
|
else
|
2011-12-20 15:06:48 +00:00
|
|
|
length = max - this.val().length
|
2011-12-19 16:36:05 +00:00
|
|
|
|
|
|
|
if options.countUp
|
|
|
|
color = 1
|
|
|
|
else
|
2011-12-20 15:06:48 +00:00
|
|
|
if length > max / 2
|
2011-12-19 16:36:05 +00:00
|
|
|
color = 1
|
2011-12-20 15:06:48 +00:00
|
|
|
else if length <= max / 2 and length >= max / 6
|
2011-12-19 16:36:05 +00:00
|
|
|
color = 2
|
|
|
|
else
|
|
|
|
color = 3
|
|
|
|
|
|
|
|
jCounterContainer.text(length).attr('class', '').addClass(options.counterClass + ' color-'+color)
|
|
|
|
, this
|
|
|
|
)(jQuery)
|