﻿/*
   jQuery (character and word) counter
   Copyright (C) 2009  Wilkins Fernandez

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/(function($) { $.fn.extend({ counter: function(options) { var defaults = { type: 'char', count: 'down', goal: 140 }; var options = $.extend({}, defaults, options); var flag = false; return this.each(function() { var msg; var $obj = $(this); function get_msg_equation(objLength) { if (typeof options.type !== 'string') { } else { switch (options.type) { case 'char': if (options.count === 'down') { msg = " character(s) left"; return (options.goal - objLength) } else if (options.count === 'up') { msg = " characters (" + options.goal + " max)"; return objLength } break; case 'word': if (options.count === 'down') { msg = " mot(s) restant(s)"; return (options.goal - objLength) } else if (options.count === 'up') { msg = " words (" + options.goal + " max)"; return objLength } break; default: } } } $('<div id=\"' + this.id + '_counter\"><span>' + get_msg_equation(0) + '</span>' + msg + '</div>').insertAfter($obj); var $currentCount = $("#" + this.id + "_counter" + " span"); $obj.bind('keyup click blur focus change paste', function(new_length) { switch (options.type) { case 'char': new_length = $($obj).val().length; break; case 'word': if ($obj.val() === '') { new_length = 0 } else { new_length = $.trim($obj.val()).replace(/\s+/g, " ").split(' ').length } break; default: } switch (options.count) { case 'up': if (get_msg_equation(new_length) >= options.goal && options.type === 'char') { $(this).val($(this).val().substring(0, options.goal)); flag = true; break } if (get_msg_equation(new_length) === options.goal && options.type === 'word') { flag = true; break } else if (get_msg_equation(new_length) > options.goal && options.type === 'word') { $(this).val(""); $currentCount.text("0"); flag = true; break } break; case 'down': if (get_msg_equation(new_length) <= 0 && options.type === 'char') { $(this).val($(this).val().substring(0, options.goal)); flag = true; break } if (get_msg_equation(new_length) === 0 && options.type === 'word') { flag = true } else if (get_msg_equation(new_length) < 0 && options.type === 'word') { $(this).val(""); flag = true; break } break; default: } $obj.keydown(function(event) { if (flag) { this.focus(); if ((event.keyCode !== 46 && event.keyCode !== 8)) { if ($(this).val().length > options.goal && options.type === 'char') { $(this).val($(this).val().substring(0, options.goal)); return false } else if (event.keyCode !== 32 && event.keyCode !== 8 && options.type === 'word') { return true } else { return false } } else { flag = false; return true } } }); $currentCount.text(get_msg_equation(new_length)) }) }) } }) })(jQuery);