Files
karma/assets/static/jquery.typing-0.2.0.js
Łukasz Mierzwa f8420d4632 Use go-bindata-assetfs for static files
This allows unsee to be compiled into a single self-contained binary, without any need to external static files or templates
2017-03-26 17:41:17 -07:00

83 lines
2.1 KiB
JavaScript

// jQuery-typing
//
// Version: 0.2.0
// Website: http://narf.pl/jquery-typing/
// License: public domain <http://unlicense.org/>
// Author: Maciej Konieczny <hello@narf.pl>
(function ($) {
//--------------------
// jQuery extension
//--------------------
$.fn.typing = function (options) {
return this.each(function (i, elem) {
listenToTyping(elem, options);
});
};
//-------------------
// actual function
//-------------------
function listenToTyping(elem, options) {
// override default settings
var settings = $.extend({
start: null,
stop: null,
delay: 400
}, options);
// create other function-scope variables
var $elem = $(elem),
typing = false,
delayedCallback;
// start typing
function startTyping(event) {
if (!typing) {
// set flag and run callback
typing = true;
if (settings.start) {
settings.start(event, $elem);
}
}
}
// stop typing
function stopTyping(event, delay) {
if (typing) {
// discard previous delayed callback and create new one
clearTimeout(delayedCallback);
delayedCallback = setTimeout(function () {
// set flag and run callback
typing = false;
if (settings.stop) {
settings.stop(event, $elem);
}
}, delay >= 0 ? delay : settings.delay);
}
}
// listen to regular keypresses
$elem.keypress(startTyping);
// listen to backspace and delete presses
$elem.keydown(function (event) {
if (event.keyCode === 8 || event.keyCode === 46) {
startTyping(event);
}
});
// listen to keyups
$elem.keyup(stopTyping);
// listen to blurs
$elem.blur(function (event) {
stopTyping(event, 0);
});
}
})(jQuery);