Skip to content

Commit

Permalink
Added changelog, updated readme, JSLint and Closure support
Browse files Browse the repository at this point in the history
Passes JSLint "The Good Parts" w/o "use strict" and with
only one warning regarding the use of a constructor with
side effects.

Added @license to the comments so the minified version can
be generated with Closure Compiler.
  • Loading branch information
dcneiner committed Apr 16, 2010
1 parent 7de0de8 commit 5c71b3c
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 164 deletions.
41 changes: 26 additions & 15 deletions README.textile
@@ -1,25 +1,13 @@
h1. In-Field Labels jQuery Plugin

*Version:* 0.1.2

*License:* "Same as jQuery (Dual GPL + MIT)":http://docs.jquery.com/License

*Author:* "Doug Neiner":http://pixelgraphics.us/company

*Size:* Under 1KB Minified and gzipped

h2. esumerfd Update

This fork of Doug's in-field solution adds support for the browser paste operation.

The in-field-label is implemented as a label presented over the top of the text field. The label tag does not support a paste operation, however, depending on your CSS definitions there may be a few pixels around the edge of the label that will allow the paste context menu option to appear and when used no event is captured by this extension to clear the label text.

This fork adds a handler for the paste event on the text field so that we can clear the label.

Note that the implementation just clears the label blindley. No check is made for text field contents. There are two reasons for this:

- when the paste event fires the data has not be entered into the text field value yet so any comparison with empty will fail anyway.

- The worst case senario would be the pasting of a single space to the text field. There is no way to copy/cut an empty string so all a user can do is paste a single space which should remove the label anyway.

h2. Overview

This is a simple plugin that turns properly formatted HTML forms into forms with in-field label support. Labels fade when the field is focussed and disappear when text entry begins. Clearing a field and leaving brings back the label.
Expand All @@ -30,7 +18,7 @@ IE6+, WebKit Browsers (Safari, Chrome), Firefox 2+

* IE6 requires a background-color be set on the label to match the background of the field.

h2. Know Issues
h2. Known Issues

Browser Auto-Complete can cause problems in hiding the labels. Less of an issue for Login Boxes, but much more of an issue with comment forms.

Expand Down Expand Up @@ -80,3 +68,26 @@ When a field is focussed, its label is animated to this opacity. Defaults to 0.5
*<code>fadeDuration:</code> Time in milliseconds*
When an animation occurs, it uses this setting to determine duration. Defaults to 300

h2. Paste Support via "esumerfd":http://github.com/esumerfd

The in-field-label is implemented as a label presented over the top of the text field (or under, depending on the CSS used). The label tag does not support a paste operation, but the input element does. Depending on your CSS definitions there may be a few pixels around the edge of the label that will allow the paste context menu option to appear, or the entire label may be below the input also allowing the context menu. When the paste event is used, this plugin captures it and clears out the label. _(Note: this only works in browsers that fire a paste event.)_

Note that the implementation just clears the label blindly. No check is made for text field contents. There are two reasons for this:

* When the paste event fires the data has not be entered into the text field value yet so any comparison with empty will fail anyway.

* The worst case senario would be the pasting of a single space to the text field. There is no way to copy/cut an empty string so all a user can do is paste a single space which should remove the label anyway.

h2. Changelog

*Version 0.1.2*

* Added support for a <tt>paste</tt> event. _Special thanks to "esumerfd":http://github.com/esumerfd for contributing this improvement._

*Version 0.1.1*

* Added support for HTML5 text fields: <tt>search</tt>, <tt>tel</tt>, <tt>url</tt>, <tt>email</tt>. _Special thanks to <a href="http://github.com/holizz">Tom Adams (holizz)</a> for contributing this improvement._

*Version 0.1*

* Initial Release
10 changes: 9 additions & 1 deletion sample/index.html
Expand Up @@ -35,7 +35,7 @@ <h1>In-Field Labels jQuery Plugin</h1>
forms with in-field label support. Labels fade when the field is focussed and disappear
when text entry begins. Clearing a field and leaving brings back the label.</p>
<p><strong>Browser Support:</strong><br />IE6+, WebKit Browsers (Safari, Chrome), Firefox 2+ <small>* IE6 requires a background-color be set on the label to match the background of the field.</small></p>
<p><strong>Know Issues:</strong><br />
<p><strong>Known Issues:</strong><br />
Browser Auto-Complete can cause problems in hiding the labels. Less of an issue for Login Boxes, but much more of an issue with comment forms.</p>
</p>
</div>
Expand Down Expand Up @@ -113,6 +113,14 @@ <h2>Options</h2>
<p><tt>fadeOpacity:</tt> <strong>Value between 0.1 and 1.0.</strong><br />When a field is focussed, its label is animated to this opacity. Defaults to 0.5</p>
<p><tt>fadeDuration:</tt> <strong>Time in milliseconds</strong><br />When an animation occurs, it uses this setting to determine duration. Defaults to 300</p>
<h2>Changelog</h2>
<p><strong>Version 0.1.2</strong></p>
<ul>
<li>Added support for a <tt>paste</tt> event. <em>Special thanks to <a href="http://github.com/esumerfd">esumerfd</a> for contributing this improvement.</em></li>
</ul>
<p><strong>Version 0.1.1</strong></p>
<ul>
<li>Added support for HTML5 text fields: <tt>search</tt>, <tt>tel</tt>, <tt>url</tt>, <tt>email</tt>. <em>Special thanks to <a href="http://github.com/holizz">Tom Adams (holizz)</a> for contributing this improvement.</em></li>
</ul>
<p><strong>Version 0.1</strong></p>
<ul>
<li>Initial Release</li>
Expand Down
279 changes: 142 additions & 137 deletions src/jquery.infieldlabel.js
@@ -1,148 +1,153 @@
/*
* In-Field Label jQuery Plugin
/**
* @license In-Field Label jQuery Plugin
* 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.
* Uses the same license as jQuery, see:
* http://docs.jquery.com/License
*
* @version 0.1
* @version 0.1.2
*/
(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);
base.field = field;

base.$label.data("InFieldLabels", base);
base.showing = true;

base.init = function(){
// Merge supplied options with default options
base.options = $.extend({},$.InFieldLabels.defaultOptions, options);

// Check if the field is already filled in
if(base.$field.val() != ""){
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);
}).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();
});
};

// If the label is currently showing
// then fade it down to the amount
// 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){
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();
(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);
base.field = field;

base.$label.data("InFieldLabels", base);
base.showing = true;

base.init = function () {
// Merge supplied options with default options
base.options = $.extend({}, $.InFieldLabels.defaultOptions, options);

// Check if the field is already filled in
if (base.$field.val() !== "") {
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);
}).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

// If the label is currently showing
// then fade it down to the amount
// specified in the settings
base.fadeOnFocus = function () {
if (base.showing) {
base.setOpacity(base.options.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');
if( !for_attr ) return; // Nothing to attach, since the for field wasn't used
// Find the referenced input or textarea element
var $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));

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);
});
}
};

})(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
$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));

0 comments on commit 5c71b3c

Please sign in to comment.