Marked the problem area in infieldlabels
This commit is contained in:
parent
63b3d0186a
commit
a7659ae902
1 changed files with 141 additions and 125 deletions
|
|
@ -1,140 +1,156 @@
|
||||||
/*
|
/**
|
||||||
* 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){
|
|
||||||
// To avoid scope issues, use 'base' instead of 'this'
|
|
||||||
// to reference this class from internal events and functions.
|
|
||||||
var base = this;
|
|
||||||
|
|
||||||
// Access to jQuery and DOM versions of each element
|
|
||||||
base.$label = $(label);
|
|
||||||
base.label = label;
|
|
||||||
|
|
||||||
base.$field = $(field);
|
$.InFieldLabels = function (label, field, options) {
|
||||||
base.field = field;
|
// To avoid scope issues, use 'base' instead of 'this'
|
||||||
|
// to reference this class from internal events and functions.
|
||||||
base.$label.data("InFieldLabels", base);
|
var base = this;
|
||||||
base.showing = true;
|
|
||||||
|
// Access to jQuery and DOM versions of each element
|
||||||
base.init = function(){
|
base.$label = $(label);
|
||||||
// Merge supplied options with default options
|
base.label = label;
|
||||||
base.options = $.extend({},$.InFieldLabels.defaultOptions, options);
|
|
||||||
|
|
||||||
// Check if the field is already filled in
|
base.$field = $(field);
|
||||||
if(base.$field.val() != ""){
|
base.field = field;
|
||||||
base.$label.hide();
|
|
||||||
base.showing = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
base.$field.focus(function(){
|
|
||||||
base.fadeOnFocus();
|
|
||||||
}).blur(function(){
|
|
||||||
base.checkForEmpty(true);
|
|
||||||
}).bind('keydown.infieldlabel',function(e){
|
|
||||||
// Use of a namespace (.infieldlabel) allows us to
|
|
||||||
// unbind just this method later
|
|
||||||
base.hideOnChange(e);
|
|
||||||
}).change(function(e){
|
|
||||||
base.checkForEmpty();
|
|
||||||
}).bind('onPropertyChange', function(){
|
|
||||||
base.checkForEmpty();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
// If the label is currently showing
|
base.$label.data("InFieldLabels", base);
|
||||||
// then fade it down to the amount
|
base.showing = true;
|
||||||
// specified in the settings
|
|
||||||
base.fadeOnFocus = function(){
|
|
||||||
if(base.showing){
|
|
||||||
base.setOpacity(base.options.fadeOpacity);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
base.setOpacity = function(opacity){
|
|
||||||
base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration);
|
|
||||||
base.showing = (opacity > 0.0);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Checks for empty as a fail safe
|
|
||||||
// set blur to true when passing from
|
|
||||||
// the blur event
|
|
||||||
base.checkForEmpty = function(blur){
|
|
||||||
if(base.$field.val() == ""){
|
|
||||||
base.prepForShow();
|
|
||||||
base.setOpacity( blur ? 1.0 : base.options.fadeOpacity );
|
|
||||||
} else {
|
|
||||||
base.setOpacity(0.0);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
base.prepForShow = function(e){
|
|
||||||
if(!base.showing) {
|
|
||||||
// Prepare for a animate in...
|
|
||||||
base.$label.css({opacity: 0.0}).show();
|
|
||||||
|
|
||||||
// Reattach the keydown event
|
|
||||||
base.$field.bind('keydown.infieldlabel',function(e){
|
|
||||||
base.hideOnChange(e);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
base.hideOnChange = function(e){
|
base.init = function () {
|
||||||
if(
|
// Merge supplied options with default options
|
||||||
(e.keyCode == 16) || // Skip Shift
|
base.options = $.extend({}, $.InFieldLabels.defaultOptions, options);
|
||||||
(e.keyCode == 9) // Skip Tab
|
|
||||||
) return;
|
// Check if the field is already filled in
|
||||||
|
if (base.$field.val() !== "") {
|
||||||
if(base.showing){
|
base.$label.hide();
|
||||||
base.$label.hide();
|
base.showing = false;
|
||||||
base.showing = false;
|
}
|
||||||
};
|
|
||||||
|
base.$field.focus(function () {
|
||||||
// Remove keydown event to save on CPU processing
|
base.fadeOnFocus();
|
||||||
base.$field.unbind('keydown.infieldlabel');
|
}).blur(function () {
|
||||||
};
|
base.checkForEmpty(true);
|
||||||
|
}).bind('keydown.infieldlabel', function (e) {
|
||||||
// Run the initialization method
|
// Use of a namespace (.infieldlabel) allows us to
|
||||||
base.init();
|
// unbind just this method later
|
||||||
|
base.hideOnChange(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();
|
||||||
|
}).bind('onPropertyChange', function () {
|
||||||
|
base.checkForEmpty();
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
$.InFieldLabels.defaultOptions = {
|
|
||||||
fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be
|
|
||||||
fadeDuration: 300 // How long should it take to animate from 1.0 opacity to the fadeOpacity
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
$.fn.inFieldLabels = function(options){
|
// If the label is currently showing
|
||||||
return this.each(function(){
|
// then fade it down to the amount
|
||||||
// Find input or textarea based on for= attribute
|
// specified in the settings
|
||||||
// The for attribute on the label must contain the ID
|
base.fadeOnFocus = function () {
|
||||||
// of the input or textarea element
|
if (base.showing) {
|
||||||
var for_attr = $(this).attr('for');
|
base.setOpacity(base.options.fadeOpacity);
|
||||||
if( !for_attr ) return; // Nothing to attach, since the for field wasn't used
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Find the referenced input or textarea element
|
base.setOpacity = function (opacity) {
|
||||||
var $field = $(
|
base.$label.stop().animate({ opacity: opacity }, base.options.fadeDuration);
|
||||||
"input#" + for_attr + "[type='text']," +
|
base.showing = (opacity > 0.0);
|
||||||
"input#" + for_attr + "[type='password']," +
|
};
|
||||||
"textarea#" + for_attr
|
|
||||||
);
|
// Checks for empty as a fail safe
|
||||||
|
// set blur to true when passing from
|
||||||
if( $field.length == 0) return; // Again, nothing to attach
|
// the blur event
|
||||||
|
base.checkForEmpty = function (blur) {
|
||||||
// Only create object for input[text], input[password], or textarea
|
if (base.$field.val() === "") {
|
||||||
(new $.InFieldLabels(this, $field[0], options));
|
base.prepForShow();
|
||||||
|
base.setOpacity(blur ? 1.0 : base.options.fadeOpacity);
|
||||||
|
} else {
|
||||||
|
base.setOpacity(0.0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
base.prepForShow = function (e) {
|
||||||
|
if (!base.showing) {
|
||||||
|
// Prepare for a animate in...
|
||||||
|
base.$label.css({opacity: 0.0}).show();
|
||||||
|
|
||||||
|
// Reattach the keydown event
|
||||||
|
base.$field.bind('keydown.infieldlabel', function (e) {
|
||||||
|
base.hideOnChange(e);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
})(jQuery);
|
base.hideOnChange = function (e) {
|
||||||
|
if (
|
||||||
|
(e.keyCode === 16) || // Skip Shift
|
||||||
|
(e.keyCode === 9) // Skip Tab
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (base.showing) {
|
||||||
|
base.$label.hide();
|
||||||
|
base.showing = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove keydown event to save on CPU processing
|
||||||
|
base.$field.unbind('keydown.infieldlabel');
|
||||||
|
};
|
||||||
|
|
||||||
|
// Run the initialization method
|
||||||
|
base.init();
|
||||||
|
};
|
||||||
|
|
||||||
|
$.InFieldLabels.defaultOptions = {
|
||||||
|
fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be
|
||||||
|
fadeDuration: 300 // How long should it take to animate from 1.0 opacity to the fadeOpacity
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
$.fn.inFieldLabels = function (options) {
|
||||||
|
return this.each(function () {
|
||||||
|
// Find input or textarea based on for= attribute
|
||||||
|
// The for attribute on the label must contain the ID
|
||||||
|
// of the input or textarea element
|
||||||
|
var for_attr = $(this).attr('for'), $field;
|
||||||
|
if (!for_attr) {
|
||||||
|
return; // Nothing to attach, since the for field wasn't used
|
||||||
|
}
|
||||||
|
|
||||||
|
// Find the referenced input or textarea element
|
||||||
|
// 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='search']," +
|
||||||
|
"input#" + for_attr + "[type='tel']," +
|
||||||
|
"input#" + for_attr + "[type='url']," +
|
||||||
|
"input#" + for_attr + "[type='email']," +
|
||||||
|
"input#" + for_attr + "[type='password']," +
|
||||||
|
"textarea#" + for_attr
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($field.length === 0) {
|
||||||
|
return; // Again, nothing to attach
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only create object for input[text], input[password], or textarea
|
||||||
|
(new $.InFieldLabels(this, $field[0], options));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
}(jQuery));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue