]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/jquery.placeholder.js
d9162f0fae8dc5061546ae18ce54ca06f9f9df61
[friendica-addons.git] / jappixmini / jappix / js / jquery.placeholder.js
1 (function($) {
2         $.extend({
3                 placeholder : {
4                         settings : {
5                                 focusClass: 'placeholderFocus',
6                                 activeClass: 'placeholder',
7                                 overrideSupport: false,
8                                 preventRefreshIssues: true
9                         },
10                         debug : false,
11                         log : function(msg){
12                                 if(!$.placeholder.debug) return;
13                                 msg = "[Placeholder] " + msg;
14                                 $.placeholder.hasFirebug ?
15                                 console.log(msg) :
16                                 $.placeholder.hasConsoleLog ?
17                                         window.console.log(msg) :
18                                         alert(msg);
19                         },
20                         hasFirebug : "console" in window && "firebug" in window.console,
21                         hasConsoleLog: "console" in window && "log" in window.console
22                 }
23
24         });
25
26     // check browser support for placeholder
27     $.support.placeholder = 'placeholder' in document.createElement('input');
28
29         // Replace the val function to never return placeholders
30         $.fn.plVal = $.fn.val;
31         $.fn.val = function(value) {
32                 $.placeholder.log('in val');
33                 if(this[0]) {
34                         $.placeholder.log('have found an element');
35                         var el = $(this[0]);
36                         if(value != undefined)
37                         {
38                                 $.placeholder.log('in setter');
39                                 var currentValue = el.plVal();
40                                 var returnValue = $(this).plVal(value);
41                                 if(el.hasClass($.placeholder.settings.activeClass) && currentValue == el.attr('placeholder')){
42                                         el.removeClass($.placeholder.settings.activeClass);
43                                 }
44                                 return returnValue;
45                         }
46
47                         if(el.hasClass($.placeholder.settings.activeClass) && el.plVal() == el.attr('placeholder')) {
48                                 $.placeholder.log('returning empty because its a placeholder');
49                                 return '';
50                         } else {
51                                 $.placeholder.log('returning original val');
52                                 return el.plVal();
53                         }
54                 }
55                 $.placeholder.log('returning undefined');
56                 return undefined;
57         };
58
59         // Clear placeholder values upon page reload
60         $(window).bind('beforeunload.placeholder', function() {
61                 var els = $('input.placeholderActive' );
62                 if(els.length > 0)
63                         els.val('').attr('autocomplete','off');
64         });
65
66
67     // addon code
68         $.fn.placeholder = function(opts) {
69                 opts = $.extend({},$.placeholder.settings, opts);
70
71                 // we don't have to do anything if the browser supports placeholder
72                 if(!opts.overrideSupport && $.support.placeholder)
73                     return this;
74                         
75         return this.each(function() {
76             var $el = $(this);
77
78             // skip if we do not have the placeholder attribute
79             if(!$el.is('[placeholder]'))
80                 return;
81
82             // we cannot do password fields, but supported browsers can
83             if($el.is(':password'))
84                 return;
85                         
86                         // Prevent values from being reapplied on refresh
87                         if(opts.preventRefreshIssues)
88                                 $el.attr('autocomplete','off');
89
90             $el.bind('focus.placeholder', function(){
91                 var $el = $(this);
92                 if(this.value == $el.attr('placeholder') && $el.hasClass(opts.activeClass))
93                     $el.val('')
94                        .removeClass(opts.activeClass)
95                        .addClass(opts.focusClass);
96             });
97             $el.bind('blur.placeholder', function(){
98                 var $el = $(this);
99                                 
100                                 $el.removeClass(opts.focusClass);
101
102                 if(this.value == '')
103                   $el.val($el.attr('placeholder'))
104                      .addClass(opts.activeClass);
105             });
106
107             $el.triggerHandler('blur');
108                         
109                         // Prevent incorrect form values being posted
110                         $el.parents('form').submit(function(){
111                                 $el.triggerHandler('focus.placeholder');
112                         });
113
114         });
115     };
116 })(jQuery);