1/* 2 * jquery.infieldlabel 3 * A simple jQuery plugin for adding labels that sit over a form field and fade away when the fields are populated. 4 * 5 * Copyright (c) 2009 - 2013 Doug Neiner <[email protected]> (http://code.dougneiner.com) 6 * Source: https://github.com/dcneiner/In-Field-Labels-jQuery-Plugin 7 * Dual licensed MIT or GPL 8 * MIT (http://www.opensource.org/licenses/mit-license) 9 * GPL (http://www.opensource.org/licenses/gpl-license) 10 * 11 * @version 0.1.4 12 */ 13(function ($) { 14 15 $.InFieldLabels = function (label, field, options) { 16 // To avoid scope issues, use 'base' instead of 'this' 17 // to reference this class from internal events and functions. 18 var base = this; 19 20 // Access to jQuery and DOM versions of each element 21 base.$label = $(label); 22 base.label = label; 23 24 base.$field = $(field); 25 base.field = field; 26 27 base.$label.data("InFieldLabels", base); 28 base.showing = true; 29 30 base.init = function () { 31 var initialSet; 32 33 // Merge supplied options with default options 34 base.options = $.extend({}, $.InFieldLabels.defaultOptions, options); 35 36 // Add provided class name to the label, if set 37 if ( base.options.className ) { 38 base.$label.addClass(base.options.className); 39 } 40 41 // Check if the field is already filled in 42 // add a short delay to handle autocomplete 43 setTimeout(function() { 44 if (base.$field.val() !== "") { 45 base.$label.hide(); 46 base.showing = false; 47 } else { 48 base.$label.show(); 49 base.showing = true; 50 } 51 }, 200); 52 53 base.$field.focus(function () { 54 base.fadeOnFocus(); 55 }).blur(function () { 56 base.checkForEmpty(true); 57 }).bind('keydown.infieldlabel', function (e) { 58 // Use of a namespace (.infieldlabel) allows us to 59 // unbind just this method later 60 base.hideOnChange(e); 61 }).bind('paste', function () { 62 // Since you can not paste an empty string we can assume 63 // that the field is not empty and the label can be cleared. 64 base.setOpacity(0.0); 65 }).change(function () { 66 base.checkForEmpty(); 67 }).bind('onPropertyChange', function () { 68 base.checkForEmpty(); 69 }).bind('keyup.infieldlabel', function () { 70 base.checkForEmpty(); 71 }); 72 73 if ( base.options.pollDuration > 0 ) { 74 initialSet = setInterval( function () { 75 if (base.$field.val() !== "") { 76 base.$label.hide(); 77 base.showing = false; 78 clearInterval( initialSet ); 79 } 80 }, base.options.pollDuration ); 81 82 } 83 }; 84 85 // If the label is currently showing 86 // then fade it down to the amount 87 // specified in the settings 88 base.fadeOnFocus = function () { 89 if (base.showing) { 90 base.setOpacity(base.options.fadeOpacity); 91 } 92 }; 93 94 base.setOpacity = function (opacity) { 95 base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration, function () { 96 if (opacity == 0.0) { 97 base.$label.hide(); 98 } 99 }); 100 base.showing = (opacity > 0.0); 101 }; 102 103 // Checks for empty as a fail safe 104 // set blur to true when passing from 105 // the blur event 106 base.checkForEmpty = function (blur) { 107 if (base.$field.val() === "") { 108 base.prepForShow(); 109 base.setOpacity(blur ? 1.0 : base.options.fadeOpacity); 110 } else { 111 base.setOpacity(0.0); 112 } 113 }; 114 115 base.prepForShow = function () { 116 if (!base.showing) { 117 // Prepare for a animate in... 118 base.$label.css({opacity: 0.0}).show(); 119 120 // Reattach the keydown event 121 base.$field.bind('keydown.infieldlabel', function (e) { 122 base.hideOnChange(e); 123 }); 124 } 125 }; 126 127 base.hideOnChange = function (e) { 128 if ( 129 (e.keyCode === 16) || // Skip Shift 130 (e.keyCode === 9) // Skip Tab 131 ) { 132 return; 133 } 134 135 if (base.showing) { 136 base.$label.hide(); 137 base.showing = false;
138 } 139 140 // Remove keydown event to save on CPU processing 141 base.$field.unbind('keydown.infieldlabel'); 142 }; 143 144 // Run the initialization method 145 base.init(); 146 }; 147 148 $.InFieldLabels.defaultOptions = { 149 fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be 150 fadeDuration: 300, // How long should it take to animate from 1.0 opacity to the fadeOpacity 151 pollDuration: 0, // If set to a number greater than zero, this will poll until content is detected in a field 152 enabledInputTypes: [ "text", "search", "tel", "url", "email", "password", "number", "textarea" ], 153 className: false // Class assigned to enhanced labels 154 }; 155 156 157 $.fn.inFieldLabels = function (options) { 158 var allowed_types = options && options.enabledInputTypes || $.InFieldLabels.defaultOptions.enabledInputTypes; 159 160 return this.each(function () { 161 // Find input or textarea based on for= attribute 162 // The for attribute on the label must contain the ID 163 // of the input or textarea element 164 var for_attr = $(this).attr('for'), field, restrict_type; 165 if (!for_attr) { 166 return; // Nothing to attach, since the for field wasn't used 167 } 168 169 // Find the referenced input or textarea element 170 field = document.getElementById( for_attr ); 171 if ( !field ) { 172 return; // No element found 173 } 174 175 // Restrict input type 176 restrict_type = $.inArray( field.type, allowed_types ); 177 178 if ( restrict_type === -1 && field.nodeName !== "TEXTAREA" ) { 179 return; // Again, nothing to attach 180 } 181 182 // Only create object for matched input types and textarea 183 (new $.InFieldLabels(this, field, options)); 184 }); 185 }; 186 187}(jQuery));
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.