Skip to content

React-Assignment #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
validation-*

*.html
7 changes: 0 additions & 7 deletions .travis.yml

This file was deleted.

45 changes: 0 additions & 45 deletions Gruntfile.js

This file was deleted.

18 changes: 0 additions & 18 deletions README.md

This file was deleted.

9 changes: 0 additions & 9 deletions index.html

This file was deleted.

123 changes: 123 additions & 0 deletions jquery.simplyCountable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
(function($){

$.fn.simplyCountable = function(options){

options = $.extend({
counter: '#counter',
countType: 'characters',
maxCount: 140,
strictMax: false,
countDirection: 'down',
safeClass: 'safe',
overClass: 'over',
thousandSeparator: ',',
onOverCount: function(){},
onSafeCount: function(){},
onMaxCount: function(){}
}, options);

var navKeys = [33,34,35,36,37,38,39,40];

return $(this).each(function(){

var countable = $(this);
var counter = $(options.counter);
if (!counter.length) { return false; }

var countCheck = function(){

var count;
var revCount;

var reverseCount = function(ct){
return ct - (ct*2) + options.maxCount;
}

var countInt = function(){
return (options.countDirection === 'up') ? revCount : count;
}

var numberFormat = function(ct){
var prefix = '';
if (options.thousandSeparator){
ct = ct.toString();
if (ct.match(/^-/)) {
ct = ct.substr(1);
prefix = '-';
}
for (var i = ct.length-3; i > 0; i -= 3){
ct = ct.substr(0,i) + options.thousandSeparator + ct.substr(i);
}
}
return prefix + ct;
}

var changeCountableValue = function(val){
countable.val(val).trigger('change');
}

/* Calculates count for either words or characters */
if (options.countType === 'words'){
count = options.maxCount - $.trim(countable.val()).split(/\s+/).length;
if (countable.val() === ''){ count += 1; }
}
else { count = options.maxCount - countable.val().length; }
revCount = reverseCount(count);

/* If strictMax set restrict further characters */
if (options.strictMax && count <= 0){
var content = countable.val();
if (count < 0) {
options.onMaxCount(countInt(), countable, counter);
}
if (options.countType === 'words'){
var allowedText = content.match( new RegExp('\\s?(\\S+\\s+){'+ options.maxCount +'}') );
if (allowedText) {
changeCountableValue(allowedText[0]);
}
}
else { changeCountableValue(content.substring(0, options.maxCount)); }
count = 0, revCount = options.maxCount;
}

counter.text(numberFormat(countInt()));

/* Set CSS class rules and API callbacks */
if (!counter.hasClass(options.safeClass) && !counter.hasClass(options.overClass)){
if (count < 0){ counter.addClass(options.overClass); }
else { counter.addClass(options.safeClass); }
}
else if (count < 0 && counter.hasClass(options.safeClass)){
counter.removeClass(options.safeClass).addClass(options.overClass);
options.onOverCount(countInt(), countable, counter);
}
else if (count >= 0 && counter.hasClass(options.overClass)){
counter.removeClass(options.overClass).addClass(options.safeClass);
options.onSafeCount(countInt(), countable, counter);
}

};

countCheck();

countable.on('keyup blur paste', function(e) {
switch(e.type) {
case 'keyup':
// Skip navigational key presses
if ($.inArray(e.which, navKeys) < 0) { countCheck(); }
break;
case 'paste':
// Wait a few miliseconds if a paste event
setTimeout(countCheck, (e.type === 'paste' ? 5 : 0));
break;
default:
countCheck();
break;
}
});

});

};

})(jQuery);
12 changes: 0 additions & 12 deletions package.json

This file was deleted.