diff --git a/app/views/layouts/_javascript.html.erb b/app/views/layouts/_javascript.html.erb index 33c05d4..e737392 100644 --- a/app/views/layouts/_javascript.html.erb +++ b/app/views/layouts/_javascript.html.erb @@ -2,7 +2,7 @@ 'jquery-2.1.1.min', 'jquery-migrate-1.2.1', 'jquery.noconflict.js', 'jquery.cycle.all.min.js', 'thickbox.js', 'lightbox', 'colorbox', 'jquery-ui-1.10.4/js/jquery-ui-1.10.4.min', 'jquery.scrollTo', 'jquery.form.js', 'jquery-validation/jquery.validate', -'jquery.cookie', 'jquery.ba-bbq.min.js', 'reflection', 'jquery.tokeninput', +'jquery.cookie', 'jquery.ba-bbq.min.js', 'reflection', 'jquery.tokeninput', 'jquery.typewatch', 'add-and-join', 'report-abuse', 'catalog', 'manage-products', 'autogrow', 'jquery-timepicker-addon/dist/jquery-ui-timepicker-addon', 'application.js', 'rails.js', :cache => 'cache/application' %> diff --git a/public/javascripts/jquery.typewatch.js b/public/javascripts/jquery.typewatch.js new file mode 100644 index 0000000..a49c070 --- /dev/null +++ b/public/javascripts/jquery.typewatch.js @@ -0,0 +1,103 @@ +/* +* TypeWatch 2.2.1 +* +* Examples/Docs: github.com/dennyferra/TypeWatch +* +* Copyright(c) 2014 +* Denny Ferrassoli - dennyferra.com +* Charles Christolini +* +* Dual licensed under the MIT and GPL licenses: +* http://www.opensource.org/licenses/mit-license.php +* http://www.gnu.org/licenses/gpl.html +*/ + +!function(root, factory) { + if (typeof define === 'function' && define.amd) { + define(['jquery'], factory); + } else if (typeof exports === 'object') { + factory(require('jquery')); + } else { + factory(root.jQuery); + } +}(this, function($) { + 'use strict'; + $.fn.typeWatch = function(o) { + // The default input types that are supported + var _supportedInputTypes = + ['TEXT', 'TEXTAREA', 'PASSWORD', 'TEL', 'SEARCH', 'URL', 'EMAIL', 'DATETIME', 'DATE', 'MONTH', 'WEEK', 'TIME', 'DATETIME-LOCAL', 'NUMBER', 'RANGE']; + + // Options + var options = $.extend({ + wait: 750, + callback: function() { }, + highlight: true, + captureLength: 2, + inputTypes: _supportedInputTypes + }, o); + + function checkElement(timer, override) { + var value = $(timer.el).val(); + + // Fire if text >= options.captureLength AND text != saved text OR if override AND text >= options.captureLength + if ((value.length >= options.captureLength && value.toUpperCase() != timer.text) + || (override && value.length >= options.captureLength)) + { + timer.text = value.toUpperCase(); + timer.cb.call(timer.el, value); + } + }; + + function watchElement(elem) { + var elementType = elem.type.toUpperCase(); + if ($.inArray(elementType, options.inputTypes) >= 0) { + + // Allocate timer element + var timer = { + timer: null, + text: $(elem).val().toUpperCase(), + cb: options.callback, + el: elem, + wait: options.wait + }; + + // Set focus action (highlight) + if (options.highlight) { + $(elem).focus( + function() { + this.select(); + }); + } + + // Key watcher / clear and reset the timer + var startWatch = function(evt) { + var timerWait = timer.wait; + var overrideBool = false; + var evtElementType = this.type.toUpperCase(); + + // If enter key is pressed and not a TEXTAREA and matched inputTypes + if (typeof evt.keyCode != 'undefined' && evt.keyCode == 13 && evtElementType != 'TEXTAREA' && $.inArray(evtElementType, options.inputTypes) >= 0) { + timerWait = 1; + overrideBool = true; + } + + var timerCallbackFx = function() { + checkElement(timer, overrideBool) + } + + // Clear timer + clearTimeout(timer.timer); + timer.timer = setTimeout(timerCallbackFx, timerWait); + }; + + $(elem).on('keydown paste cut input', startWatch); + } + }; + + // Watch Each Element + return this.each(function() { + watchElement(this); + }); + + }; +}); -- libgit2 0.21.2