123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- (function($){
-
- var ph = "PLACEHOLDER-INPUT";
- var phl = "PLACEHOLDER-LABEL";
- var boundEvents = false;
- var default_options = {
- labelClass: 'placeholder'
- };
-
-
- var input = document.createElement("input");
- if ('placeholder' in input) {
- $.fn.placeholder = $.fn.unplaceholder = function(){};
- delete input;
- return;
- };
- delete input;
-
- $(window).resize(checkResize);
- $.fn.placeholder = function(options) {
- bindEvents();
- var opts = $.extend(default_options, options)
- this.each(function(){
- var rnd=Math.random().toString(32).replace(/\./,'')
- ,input=$(this)
- ,label=$('<label style="position:absolute;display:none;top:0;left:0;"></label>');
- if (!input.attr('placeholder') || input.data(ph) === ph) return;
-
- if (!input.attr('id')) input.attr('id', 'input_' + rnd);
- label .attr('id',input.attr('id') + "_placeholder")
- .data(ph, '#' + input.attr('id'))
- .attr('for',input.attr('id'))
- .addClass(opts.labelClass)
- .addClass(opts.labelClass + '-for-' + this.tagName.toLowerCase())
- .addClass(phl)
- .text(input.attr('placeholder'));
- input
- .data(phl, '#' + label.attr('id'))
- .data(ph,ph)
- .addClass(ph)
- .after(label)
-
- itemFocus.call(this);
- itemBlur.call(this);
- });
- };
- $.fn.unplaceholder = function(){
- this.each(function(){
- var input=$(this),
- label=$(input.data(phl));
- if (input.data(ph) !== ph) return;
-
- label.remove();
- input.removeData(ph).removeData(phl).removeClass(ph).unbind('change',itemChange);
- });
- };
- function bindEvents() {
- if (boundEvents) return;
-
- $(document).on('reset', 'form', function(){$(this).find('.'+ph).each(itemBlur);});
- $(document).on('keydown mousedown mouseclick focus focusin', '.'+ph, itemFocus);
- $(document).on('blur focusout', '.'+ph, itemBlur);
- $(document).on('change', '.'+ph, itemChange);
- $(document).on('click mouseup', '.'+phl, function(){$($(this).data(ph)).focus();});
- bound = true;
- boundEvents = true;
- };
- function itemChange() {
- var input = $(this);
- if (!!input.val()) {
- $(input.data(phl)).hide();
- return;
- }
- if (input.data(ph+'FOCUSED') != 1) {
- showPHL(input);
- }
- }
- function itemFocus() {
- $($(this).data(ph+'FOCUSED',1).data(phl)).hide();
- };
- function itemBlur() {
- var that = this;
- showPHL($(this).removeData(ph+'FOCUSED'));
-
- setTimeout(function(){
- var input = $(that);
-
- if (input.data(ph+'FOCUSED') != 1) {
- showPHL(input);
- }
- }, 200);
- };
- function showPHL(input, forced) {
- var label = $(input.data(phl));
-
- if ((forced || label.css('display') == 'none') && !input.val())
- label
- .text(input.attr('placeholder'))
- .css('top', input.position().top + 'px')
- .css('left', input.position().left + 'px')
- .css('display', 'block');
-
- }
- var cr;
- function checkResize() {
- if (cr) window.clearTimeout(cr);
- cr = window.setTimeout(checkResize2, 50);
- }
- function checkResize2() {
- $('.' + ph).each(function(){
- var input = $(this);
- var focused = $(this).data(ph+'FOCUSED');
- if (!focused) showPHL(input, true);
- });
- }
- }(jQuery));
|