silverstripe-userforms/thirdparty/Placeholders.js/Placeholders.min.js
2013-03-18 19:06:47 -04:00

11 lines
5.5 KiB
JavaScript

var Placeholders=function(){var validTypes=["text","search","url","tel","email","password","number","textarea"],settings={live:false,hideOnFocus:false,className:"placeholderspolyfill",textColor:"#999",styleImportant:true},badKeys=[37,38,39,40],interval,valueKeyDown,classNameRegExp=new RegExp("\\b"+settings.className+"\\b");function cursorToStart(elem){var range;if(elem.createTextRange){range=elem.createTextRange();range.move("character",0);range.select()}else if(elem.selectionStart){elem.focus();
elem.setSelectionRange(0,0)}}function focusHandler(){var type;if(this.value===this.getAttribute("placeholder"))if(!settings.hideOnFocus)cursorToStart(this);else{this.className=this.className.replace(classNameRegExp,"");this.value="";type=this.getAttribute("data-placeholdertype");if(type)this.type=type}}function blurHandler(){var type;if(this.value===""){this.className=this.className+" "+settings.className;this.value=this.getAttribute("placeholder");type=this.getAttribute("data-placeholdertype");if(type)this.type=
"text"}}function submitHandler(){var inputs=this.getElementsByTagName("input"),textareas=this.getElementsByTagName("textarea"),numInputs=inputs.length,num=numInputs+textareas.length,element,placeholder,i;for(i=0;i<num;i+=1){element=i<numInputs?inputs[i]:textareas[i-numInputs];placeholder=element.getAttribute("placeholder");if(element.value===placeholder)element.value=""}}function keydownHandler(event){valueKeyDown=this.value;return!(valueKeyDown===this.getAttribute("placeholder")&&badKeys.indexOf(event.keyCode)>
-1)}function keyupHandler(){var type;if(this.value!==valueKeyDown){this.className=this.className.replace(classNameRegExp,"");this.value=this.value.replace(this.getAttribute("placeholder"),"");type=this.getAttribute("data-placeholdertype");if(type)this.type=type}if(this.value===""){blurHandler.call(this);cursorToStart(this)}}function addEventListener(element,event,fn){if(element.addEventListener)return element.addEventListener(event,fn.bind(element),false);if(element.attachEvent)return element.attachEvent("on"+
event,fn.bind(element))}function addEventListeners(element){if(!settings.hideOnFocus){addEventListener(element,"keydown",keydownHandler);addEventListener(element,"keyup",keyupHandler)}addEventListener(element,"focus",focusHandler);addEventListener(element,"blur",blurHandler)}function updatePlaceholders(){var inputs=document.getElementsByTagName("input"),textareas=document.getElementsByTagName("textarea"),numInputs=inputs.length,num=numInputs+textareas.length,i,form,element,oldPlaceholder,newPlaceholder;
for(i=0;i<num;i+=1){element=i<numInputs?inputs[i]:textareas[i-numInputs];newPlaceholder=element.getAttribute("placeholder");if(validTypes.indexOf(element.type)>-1)if(newPlaceholder){oldPlaceholder=element.getAttribute("data-currentplaceholder");if(newPlaceholder!==oldPlaceholder){if(element.value===oldPlaceholder||element.value===newPlaceholder||!element.value){element.value=newPlaceholder;element.className=element.className+" "+settings.className}if(!oldPlaceholder){if(element.form){form=element.form;
if(!form.getAttribute("data-placeholdersubmit")){addEventListener(form,"submit",submitHandler);form.setAttribute("data-placeholdersubmit","true")}}addEventListeners(element)}element.setAttribute("data-currentplaceholder",newPlaceholder)}}}}function createPlaceholders(){var inputs=document.getElementsByTagName("input"),textareas=document.getElementsByTagName("textarea"),numInputs=inputs.length,num=numInputs+textareas.length,i,element,form,placeholder;for(i=0;i<num;i+=1){element=i<numInputs?inputs[i]:
textareas[i-numInputs];placeholder=element.getAttribute("placeholder");if(validTypes.indexOf(element.type)>-1)if(placeholder){if(element.type==="password")try{element.type="text";element.setAttribute("data-placeholdertype","password")}catch(e){}element.setAttribute("data-currentplaceholder",placeholder);if(element.value===""||element.value===placeholder){element.className=element.className+" "+settings.className;element.value=placeholder}if(element.form){form=element.form;if(!form.getAttribute("data-placeholdersubmit")){addEventListener(form,
"submit",submitHandler);form.setAttribute("data-placeholdersubmit","true")}}addEventListeners(element)}}}function init(opts){var test=document.createElement("input"),opt,styleElem,styleRules,i,j;if(typeof test.placeholder==="undefined"){for(opt in opts)if(opts.hasOwnProperty(opt))settings[opt]=opts[opt];styleElem=document.createElement("style");styleElem.type="text/css";var importantValue=settings.styleImportant?"!important":"";styleRules=document.createTextNode("."+settings.className+" { color:"+
settings.textColor+importantValue+"; }");if(styleElem.styleSheet)styleElem.styleSheet.cssText=styleRules.nodeValue;else styleElem.appendChild(styleRules);document.getElementsByTagName("head")[0].appendChild(styleElem);if(!Array.prototype.indexOf)Array.prototype.indexOf=function(obj,start){for(i=start||0,j=this.length;i<j;i+=1)if(this[i]===obj)return i;return-1};if(!Function.prototype.bind)Function.prototype.bind=function(oThis){if(typeof this!=="function")throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
var aArgs=Array.prototype.slice.call(arguments,1),fToBind=this,FNop=function(){},fBound=function(){return fToBind.apply(this instanceof FNop?this:oThis,aArgs.concat(Array.prototype.slice.call(arguments)))};FNop.prototype=this.prototype;fBound.prototype=new FNop;return fBound};createPlaceholders();if(settings.live)interval=setInterval(updatePlaceholders,100);return true}return false}return{init:init,refresh:updatePlaceholders}}();