forked from jamesallardice/Placeholders.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlaceholders.min.js
7 lines (7 loc) · 3.18 KB
/
Placeholders.min.js
1
2
3
4
5
6
7
var Placeholders=function(){function m(a){a.createTextRange?(a=a.createTextRange(),a.move("character",0),a.select()):a.selectionStart&&(a.focus(),a.setSelectionRange(0,0))}function r(){this.value===this.getAttribute("placeholder")&&(k.hideOnFocus?(this.className=this.className.replace(/\bplaceholderspolyfill\b/,""),this.value=""):m(this))}function n(){""===this.value&&(this.className+=" placeholderspolyfill",this.value=this.getAttribute("placeholder"))}function s(){var a=this.getElementsByTagName("input"),
d=this.getElementsByTagName("textarea"),e=a.length,i=e+d.length,g,f,b;for(b=0;b<i;b+=1)g=b<e?a[b]:d[b-e],f=g.getAttribute("placeholder"),g.value===f&&(g.value="")}function t(a){l=this.value;return!(l===this.getAttribute("placeholder")&&-1<u.indexOf(a.keyCode))}function v(){this.value!==l&&(this.className=this.className.replace(/\bplaceholderspolyfill\b/,""),this.value=this.value.replace(this.getAttribute("placeholder"),""));""===this.value&&(n.call(this),m(this))}function j(a,d,e){if(a.addEventListener)return a.addEventListener(d,
e.bind(a),!1);if(a.attachEvent)return a.attachEvent("on"+d,e.bind(a))}function o(a){k.hideOnFocus||(j(a,"keydown",t),j(a,"keyup",v));j(a,"focus",r);j(a,"blur",n)}function p(){var a=document.getElementsByTagName("input"),d=document.getElementsByTagName("textarea"),e=a.length,i=e+d.length,g,f,b,c;for(g=0;g<i;g+=1)if(f=g<e?a[g]:d[g-e],c=f.getAttribute("placeholder"),-1===q.indexOf(f.type)&&c&&(b=f.getAttribute("data-currentplaceholder"),c!==b)){if(f.value===b||f.value===c||!f.value)f.value=c,f.className+=
" placeholderspolyfill";b||o(f);f.setAttribute("data-currentplaceholder",c)}}var q="hidden datetime date month week time datetime-local range color checkbox radio file submit image reset button".split(" "),k={live:!1,hideOnFocus:!1},u=[37,38,39,40],l;return{init:function(a){var d,e,i;if(!document.createElement("input").placeholder){for(d in a)a.hasOwnProperty(d)&&(k[d]=a[d]);a=document.createElement("style");a.type="text/css";d=document.createTextNode(".placeholderspolyfill { color:#999 !important; }");
a.styleSheet?a.styleSheet.cssText=d.nodeValue:a.appendChild(d);document.getElementsByTagName("head")[0].appendChild(a);Array.prototype.indexOf||(Array.prototype.indexOf=function(a,b){e=b||0;for(i=this.length;e<i;e+=1)if(this[e]===a)return e;return-1});Function.prototype.bind||(Function.prototype.bind=function(a){if(typeof this!=="function")throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");var b=Array.prototype.slice.call(arguments,1),d=this,c=function(){},
e=function(){return d.apply(this instanceof c?this:a,b.concat(Array.prototype.slice.call(arguments)))};c.prototype=this.prototype;e.prototype=new c;return e});a=document.getElementsByTagName("input");d=document.getElementsByTagName("textarea");var g=a.length,f=g+d.length,b,c,h;for(b=0;b<f;b+=1)if(c=b<g?a[b]:d[b-g],h=c.getAttribute("placeholder"),-1===q.indexOf(c.type)&&h){c.setAttribute("data-currentplaceholder",h);if(""===c.value||c.value===h)c.className+=" placeholderspolyfill",c.value=h;c.form&&
(h=c.form,h.getAttribute("data-placeholdersubmit")||(j(h,"submit",s),h.setAttribute("data-placeholdersubmit","true")));o(c)}k.live&&setInterval(p,100);return!0}return!1},refresh:p}}();