forked from remybach/jQuery.superLabels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.superLabels.min.js
10 lines (10 loc) · 4.32 KB
/
jquery.superLabels.min.js
1
2
3
4
5
6
7
8
9
10
/*
* Title: jQuery Super Labels Plugin - Give your forms a helping of awesome!
* Author: Rémy Bach
* Version: 2.0
* License: http://remybach.mit-license.org
* Url: http://github.com/remybach/jQuery.superLabels
* Description:
* This plugin allows you to display your form labels on top of your form fields, saving you space on your page.
*/
(function(e){var t={autoCharLimit:false,baseZindex:0,duration:500,easingIn:e.easing&&e.easing.def?"easeInOutCubic":false,easingOut:e.easing&&e.easing.def?"easeInOutCubic":false,fadeDuration:250,labelLeft:0,labelTop:0,noAnimate:false,opacity:.5,slide:true,wrapSelector:false},n=["text","search","url","tel","email","password","number"],r=["input","textarea","select"];e.fn.superLabels=function(i){var s=[],o;if(this.length===0){return false}if(i&&i.labelLeft&&isNaN(i.labelLeft)){i.labelLeft=Number(i.labelLeft.replace(/\D+/,""))}if(i&&i.labelTop&&isNaN(i.labelTop)){i.labelTop=Number(i.labelTop.replace(/\D+/,""))}o=e.extend({},t);e(this).data("slDefaults",e.extend(o,i||{})).addClass("sl-defaults");if(this.length===1&&/form/i.test(this[0].tagName)){s=e(r.join(","),this)}else if(this.length>1){this.each(function(){if(/form/i.test(this.tagName)){e.merge(s,e(r.join(","),this))}else{s.push(this)}})}else{s=this}return e(s).each(function(){var t=e(this),i,s=t.attr("placeholder"),o;if((t[0].tagName.toLowerCase()==="input"&&e.inArray(t.attr("type"),n))===-1&&e.inArray(t[0].tagName.toLowerCase(),r)!==-1){return true}i=_getLabel(this);if(s){if(i.length===0){o='<label for="'+(t.attr("id")||t.attr("name"))+'">'+s+"</label>";o+="</label>";o=e(o);i=o;t.before(i)}else{i.attr("title",s)}t.removeAttr("placeholder")}if(i.length===0){return true}_prepLabel(t,i);if(!this.tagName.match(/select/i)){t.focus(_focus);t.blur(_blur);t.change(_blur);t.bind("input",_keyup);t.bind("propertychange",_blur);t.keyup(_keyup);i.click(function(){t.focus()})}})};_getLabel=function(t){var n=e(t).closest(".sl-defaults").data("slDefaults"),r=e(t).siblings("label");if(r.length===0){if(n.wrapSelector){r=e(t).parents(n.wrapSelector).find("label")}else{_for=t.id||t.name;r=e('[for="'+_for+'"]')}}return r};_prepLabel=function(t,n){var r,i=t.data("slCharLimit"),s=e(t).closest(".sl-defaults").data("slDefaults"),o=0,u;if(t[0].tagName.match(/select/i)){u=t.find("[selected]").length===0?" selected":"";t.prepend('<option value="" disabled'+u+' rel="label">'+n.html()+"</option>");n.css("display","none")}else{if(i==="auto"||s.autoCharLimit&&isNaN(i)){_approximateChars(t,n)}if(_noVal(t)){o=1}else if(_withinCharLimit(t)){o=s._opacity}t.css({zIndex:s.baseZindex+1}).addClass("sl_field");n.css({left:_noVal(t)?s.labelLeft:e(t).width()-n.width(),opacity:o,position:"absolute",top:s.labelTop,zIndex:s.baseZindex+2}).addClass("sl_label")}};_focus=function(){var t=e(this).closest(".sl-defaults").data("slDefaults"),n=t.duration,r,i={opacity:0};if(_noVal(this)){r=_getLabel(this);if(t.noAnimate){r.hide();return false}if(t.slide){i.left=e(this).width()-r.width();i.opacity=t.opacity}else{n=t.fadeDuration}r.stop(true,false).animate(i,n,t.easingOut)}};_blur=function(){var t=e(this).closest(".sl-defaults").data("slDefaults"),n=t.duration,r,i={opacity:1};if(_noVal(this)){r=_getLabel(this);if(t.noAnimate){r.show();return false}if(t.slide){i.left=t.labelLeft}else{n=t.fadeDuration}r.stop(true,false).animate(i,n,t.easingOut)}else{_keyup.apply(this)}};_keyup=function(){var t=e(this).closest(".sl-defaults").data("slDefaults"),n,r=0;if(t.noAnimate){return false}n=_getLabel(this);if(_noVal(this)&&n.css("opacity")>0||!_noVal(this)&&n.css("opacity")===0){return false}if(_noVal(this)&&n.css("opacity")!==0||_withinCharLimit(this)){r=t.opacity}n.stop(true,false).animate({opacity:r},t.fadeDuration,t.easingOut)};_noVal=function(t){return e(t).val()===""};_withinCharLimit=function(t){var n=e(t).data("slCharLimit");if(!n||typeof n!=="number"){return false}t=t.length?t[0]:t;return n&&t.value&&t.value.length<=n};_approximateChars=function(t,n){var r,i,s="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";_properties=["font-family","font-size","font-weight","letter-spacing","line-height","text-shadow","text-transform"],_tmp=e("<div>"+s+"</div>");e.each(_properties,function(e,n){_tmp.css(n,t.css(n))});_tmp.css({position:"absolute",visibility:"hidden"});t.parent().append(_tmp);i=Math.round(_tmp.width()/s.length);_tmp.remove();r=t.width()-n.width();t.data("slCharLimit",Math.floor(r/i))}})(jQuery)