1/* 2* TypeWatch 3 3* 4* Examples/Docs: github.com/dennyferra/TypeWatch 5* 6* Dual licensed under the MIT and GPL licenses: 7* http://www.opensource.org/licenses/mit-license.php 8* http://www.gnu.org/licenses/gpl.html 9*/ 10 11!function(root, factory) { 12 if (typeof define === 'function' && define.amd) { 13 define(['jquery'], factory); 14 } else if (typeof exports === 'object') { 15 factory(require('jquery')); 16 } else { 17 factory(root.jQuery); 18 } 19}(this, function($) { 20 'use strict'; 21 $.fn.typeWatch = function(o) { 22 // The default input types that are supported 23 var _supportedInputTypes = 24 ['TEXT', 'TEXTAREA', 'PASSWORD', 'TEL', 'SEARCH', 'URL', 'EMAIL', 'DATETIME', 'DATE', 'MONTH', 'WEEK', 'TIME', 'DATETIME-LOCAL', 'NUMBER', 'RANGE', 'DIV']; 25 26 // Options 27 var options = $.extend({ 28 wait: 750, 29 callback: function() { }, 30 highlight: true, 31 captureLength: 2, 32 allowSubmit: false, 33 inputTypes: _supportedInputTypes 34 }, o); 35 36 function checkElement(timer, override) { 37 var value = timer.type === 'DIV' 38 ? jQuery(timer.el).html() 39 : jQuery(timer.el).val(); 40 41 // If has capture length and has changed value 42 // Or override and has capture length or allowSubmit option is true 43 // Or capture length is zero and changed value 44 if ((value.length >= options.captureLength && value != timer.text) 45 || (override && (value.length >= options.captureLength || options.allowSubmit)) 46 || (value.length == 0 && timer.text)) 47 { 48 timer.text = value; 49 timer.cb.call(timer.el, value); 50 } 51 }; 52 53 function watchElement(elem) { 54 var elementType = (elem.type || elem.nodeName).toUpperCase(); 55 if (jQuery.inArray(elementType, options.inputTypes) >= 0) { 56 57 // Allocate timer element 58 var timer = { 59 timer: null, 60 text: (elementType === 'DIV') ? jQuery(elem).html() : jQuery(elem).val(), 61 cb: options.callback, 62 el: elem, 63 type: elementType, 64 wait: options.wait 65 }; 66 67 // Set focus action (highlight) 68 if (options.highlight && elementType !== 'DIV') 69 jQuery(elem).focus(function() { this.select(); }); 70 71 // Key watcher / clear and reset the timer 72 var startWatch = function(evt) { 73 var timerWait = timer.wait; 74 var overrideBool = false; 75 var evtElementType = elementType; 76 77 // If enter key is pressed and not a TEXTAREA or DIV 78 if (typeof evt.keyCode != 'undefined' && evt.keyCode == 13 79 && evtElementType !== 'TEXTAREA' && elementType !== 'DIV') { 80 timerWait = 1; 81 overrideBool = true; 82 } 83 84 var timerCallbackFx = function() { 85 checkElement(timer, overrideBool) 86 } 87 88 // Clear timer 89 clearTimeout(timer.timer); 90 timer.timer = setTimeout(timerCallbackFx, timerWait); 91 }; 92 93 jQuery(elem).on('keydown paste cut input', startWatch); 94 } 95 }; 96 97 // Watch each element 98 return this.each(function() { 99 watchElement(this); 100 }); 101 }; 102});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.