/* * InputHint jQuery Plugin * Taken from InFieldLabel: https://github.com/mattvague/In-Field-Labels-jQuery-Plugin * * Copyright (c) 2012 Caleb Clark * Dual licensed under the MIT and GPL licenses. * Uses the same license as jQuery, see: * http://docs.jquery.com/License * * @version 0.1 */ (function($){ $.InputHint = 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("InputHint", base); base.showing = true; base.init = function(){ // Merge supplied options with default options base.options = $.extend({},$.InputHint.defaultOptions, options); base.$label.css('position','absolute'); var fieldPosition = base.$field.position(); base.$label.css({ // 'left' : fieldPosition.left, // 'top' : fieldPosition.top }).addClass(base.options.labelClass); // Check if the field is already filled in if(base.$field.val() != ""){ base.$label.hide(); base.showing = false; }; base.$label.click(function(){ base.$field.focus(); }) 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(); }).bind('keyup.infieldlabel', 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); if(!base.showing) base.$label.hide(); }; // 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.currentTarget).val() == "") return; if(base.showing){ base.$label.hide(); base.showing = false; }; // Remove keydown and keyup events to save on CPU processing base.$field.unbind('keydown.infieldlabel'); }; // Run the initialization method base.init(); }; $.InputHint.defaultOptions = { fadeOpacity: 0.5, // Once a field has focus, how transparent should the label be fadeDuration: 200, // How long should it take to animate from 1.0 opacity to the fadeOpacity labelClass: 'inputhint' // Class to be applied to label when positioned over form field }; $.fn.inputHint = function(options){ return this.each(function(){ tagName = this.tagName.toLowerCase(); if($(this).attr('type')) inputType = $(this).attr('type').toLowerCase(); else inputType = 'text'; // Only create object for input[text], input[password], or textarea if((tagName != 'input' && tagName != 'textarea') || (inputType != 'text' && inputType != 'password')) return // make sure it's only setup once if($(this).data('usesInputHint') != true) { text = $(this).attr('placeholder'); if(!text) return // add label $label = $('').insertBefore(this) $(this).removeAttr('placeholder'); $(this).data('usesInputHint', true); }else{ return } (new $.InputHint($label, this, options)); }); }; })(jQuery);