Marked the problem area in infieldlabels

This commit is contained in:
Raphael 2010-06-30 21:53:05 -07:00
parent 63b3d0186a
commit a7659ae902

View file

@ -1,17 +1,17 @@
/* /**
* In-Field Label jQuery Plugin * @license In-Field Label jQuery Plugin
* http://fuelyourcoding.com/scripts/infield.html * http://fuelyourcoding.com/scripts/infield.html
* *
* Copyright (c) 2009 Doug Neiner * Copyright (c) 2009-2010 Doug Neiner
* Dual licensed under the MIT and GPL licenses. * Dual licensed under the MIT and GPL licenses.
* Uses the same license as jQuery, see: * Uses the same license as jQuery, see:
* http://docs.jquery.com/License * http://docs.jquery.com/License
* *
* @version 0.1 * @version 0.1.2
*/ */
(function($){ (function ($) {
$.InFieldLabels = function(label,field, options){ $.InFieldLabels = function (label, field, options) {
// To avoid scope issues, use 'base' instead of 'this' // To avoid scope issues, use 'base' instead of 'this'
// to reference this class from internal events and functions. // to reference this class from internal events and functions.
var base = this; var base = this;
@ -26,27 +26,31 @@
base.$label.data("InFieldLabels", base); base.$label.data("InFieldLabels", base);
base.showing = true; base.showing = true;
base.init = function(){ base.init = function () {
// Merge supplied options with default options // Merge supplied options with default options
base.options = $.extend({},$.InFieldLabels.defaultOptions, options); base.options = $.extend({}, $.InFieldLabels.defaultOptions, options);
// Check if the field is already filled in // Check if the field is already filled in
if(base.$field.val() != ""){ if (base.$field.val() !== "") {
base.$label.hide(); base.$label.hide();
base.showing = false; base.showing = false;
}; }
base.$field.focus(function(){ base.$field.focus(function () {
base.fadeOnFocus(); base.fadeOnFocus();
}).blur(function(){ }).blur(function () {
base.checkForEmpty(true); base.checkForEmpty(true);
}).bind('keydown.infieldlabel',function(e){ }).bind('keydown.infieldlabel', function (e) {
// Use of a namespace (.infieldlabel) allows us to // Use of a namespace (.infieldlabel) allows us to
// unbind just this method later // unbind just this method later
base.hideOnChange(e); base.hideOnChange(e);
}).change(function(e){ }).bind('paste', function (e) {
// Since you can not paste an empty string we can assume
// that the fieldis not empty and the label can be cleared.
base.setOpacity(0.0);
}).change(function (e) {
base.checkForEmpty(); base.checkForEmpty();
}).bind('onPropertyChange', function(){ }).bind('onPropertyChange', function () {
base.checkForEmpty(); base.checkForEmpty();
}); });
}; };
@ -54,13 +58,13 @@
// If the label is currently showing // If the label is currently showing
// then fade it down to the amount // then fade it down to the amount
// specified in the settings // specified in the settings
base.fadeOnFocus = function(){ base.fadeOnFocus = function () {
if(base.showing){ if (base.showing) {
base.setOpacity(base.options.fadeOpacity); base.setOpacity(base.options.fadeOpacity);
}; }
}; };
base.setOpacity = function(opacity){ base.setOpacity = function (opacity) {
base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration); base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration);
base.showing = (opacity > 0.0); base.showing = (opacity > 0.0);
}; };
@ -68,37 +72,39 @@
// Checks for empty as a fail safe // Checks for empty as a fail safe
// set blur to true when passing from // set blur to true when passing from
// the blur event // the blur event
base.checkForEmpty = function(blur){ base.checkForEmpty = function (blur) {
if(base.$field.val() == ""){ if (base.$field.val() === "") {
base.prepForShow(); base.prepForShow();
base.setOpacity( blur ? 1.0 : base.options.fadeOpacity ); base.setOpacity(blur ? 1.0 : base.options.fadeOpacity);
} else { } else {
base.setOpacity(0.0); base.setOpacity(0.0);
}; }
}; };
base.prepForShow = function(e){ base.prepForShow = function (e) {
if(!base.showing) { if (!base.showing) {
// Prepare for a animate in... // Prepare for a animate in...
base.$label.css({opacity: 0.0}).show(); base.$label.css({opacity: 0.0}).show();
// Reattach the keydown event // Reattach the keydown event
base.$field.bind('keydown.infieldlabel',function(e){ base.$field.bind('keydown.infieldlabel', function (e) {
base.hideOnChange(e); base.hideOnChange(e);
}); });
}; }
}; };
base.hideOnChange = function(e){ base.hideOnChange = function (e) {
if( if (
(e.keyCode == 16) || // Skip Shift (e.keyCode === 16) || // Skip Shift
(e.keyCode == 9) // Skip Tab (e.keyCode === 9) // Skip Tab
) return; ) {
return;
}
if(base.showing){ if (base.showing) {
base.$label.hide(); base.$label.hide();
base.showing = false; base.showing = false;
}; }
// Remove keydown event to save on CPU processing // Remove keydown event to save on CPU processing
base.$field.unbind('keydown.infieldlabel'); base.$field.unbind('keydown.infieldlabel');
@ -114,27 +120,37 @@
}; };
$.fn.inFieldLabels = function(options){ $.fn.inFieldLabels = function (options) {
return this.each(function(){ return this.each(function () {
// Find input or textarea based on for= attribute // Find input or textarea based on for= attribute
// The for attribute on the label must contain the ID // The for attribute on the label must contain the ID
// of the input or textarea element // of the input or textarea element
var for_attr = $(this).attr('for'); var for_attr = $(this).attr('for'), $field;
if( !for_attr ) return; // Nothing to attach, since the for field wasn't used if (!for_attr) {
return; // Nothing to attach, since the for field wasn't used
}
// Find the referenced input or textarea element // Find the referenced input or textarea element
var $field = $( // This fucking for_attr thing is some seriously dumb shit.
//
// It must be dealt with
$field = $(
"input#" + for_attr + "[type='text']," + "input#" + for_attr + "[type='text']," +
"input#" + for_attr + "[type='search']," +
"input#" + for_attr + "[type='tel']," +
"input#" + for_attr + "[type='url']," +
"input#" + for_attr + "[type='email']," +
"input#" + for_attr + "[type='password']," + "input#" + for_attr + "[type='password']," +
"textarea#" + for_attr "textarea#" + for_attr
); );
if( $field.length == 0) return; // Again, nothing to attach if ($field.length === 0) {
return; // Again, nothing to attach
}
// Only create object for input[text], input[password], or textarea // Only create object for input[text], input[password], or textarea
(new $.InFieldLabels(this, $field[0], options)); (new $.InFieldLabels(this, $field[0], options));
}); });
}; };
})(jQuery); }(jQuery));