[IMP] website editor: change link editor modal layout
[odoo/odoo.git] / addons / web / static / lib / jquery.placeholder / jquery.placeholder.js
1 /*! http://mths.be/placeholder v2.0.7 by @mathias */\r
2 ;(function(window, document, $) {\r
3 \r
4     var isInputSupported = 'placeholder' in document.createElement('input'),\r
5         isTextareaSupported = 'placeholder' in document.createElement('textarea'),\r
6         prototype = $.fn,\r
7         valHooks = $.valHooks,\r
8         hooks,\r
9         placeholder;\r
10 \r
11     if (isInputSupported && isTextareaSupported) {\r
12 \r
13         placeholder = prototype.placeholder = function() {\r
14             return this;\r
15         };\r
16 \r
17         placeholder.input = placeholder.textarea = true;\r
18 \r
19     } else {\r
20 \r
21         placeholder = prototype.placeholder = function() {\r
22             var $this = this;\r
23             $this\r
24                 .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')\r
25                 .not('.placeholder')\r
26                 .bind({\r
27                     'focus.placeholder': clearPlaceholder,\r
28                     'blur.placeholder': setPlaceholder\r
29                 })\r
30                 .data('placeholder-enabled', true)\r
31                 .trigger('blur.placeholder');\r
32             return $this;\r
33         };\r
34 \r
35         placeholder.input = isInputSupported;\r
36         placeholder.textarea = isTextareaSupported;\r
37 \r
38         hooks = {\r
39             'get': function(element) {\r
40                 var $element = $(element);\r
41                 return $element.data('placeholder-enabled') && $element.hasClass('placeholder') ? '' : element.value;\r
42             },\r
43             'set': function(element, value) {\r
44                 var $element = $(element);\r
45                 if (!$element.data('placeholder-enabled')) {\r
46                     return element.value = value;\r
47                 }\r
48                 if (value == '') {\r
49                     element.value = value;\r
50                     // Issue #56: Setting the placeholder causes problems if the element continues to have focus.\r
51                     if (element != document.activeElement) {\r
52                         // We can't use `triggerHandler` here because of dummy text/password inputs :(\r
53                         setPlaceholder.call(element);\r
54                     }\r
55                 } else if ($element.hasClass('placeholder')) {\r
56                     clearPlaceholder.call(element, true, value) || (element.value = value);\r
57                 } else {\r
58                     element.value = value;\r
59                 }\r
60                 // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363\r
61                 return $element;\r
62             }\r
63         };\r
64 \r
65         isInputSupported || (valHooks.input = hooks);\r
66         isTextareaSupported || (valHooks.textarea = hooks);\r
67 \r
68         $(function() {\r
69             // Look for forms\r
70             $(document).delegate('form', 'submit.placeholder', function() {\r
71                 // Clear the placeholder values so they don't get submitted\r
72                 var $inputs = $('.placeholder', this).each(clearPlaceholder);\r
73                 setTimeout(function() {\r
74                     $inputs.each(setPlaceholder);\r
75                 }, 10);\r
76             });\r
77         });\r
78 \r
79         // Clear placeholder values upon page reload\r
80         $(window).bind('beforeunload.placeholder', function() {\r
81             $('.placeholder').each(function() {\r
82                 this.value = '';\r
83             });\r
84         });\r
85 \r
86     }\r
87 \r
88     function args(elem) {\r
89         // Return an object of element attributes\r
90         var newAttrs = {},\r
91             rinlinejQuery = /^jQuery\d+$/;\r
92         $.each(elem.attributes, function(i, attr) {\r
93             if (attr.specified && !rinlinejQuery.test(attr.name)) {\r
94                 newAttrs[attr.name] = attr.value;\r
95             }\r
96         });\r
97         return newAttrs;\r
98     }\r
99 \r
100     function clearPlaceholder(event, value) {\r
101         var input = this,\r
102             $input = $(input);\r
103         if (input.value == $input.attr('placeholder') && $input.hasClass('placeholder')) {\r
104             if ($input.data('placeholder-password')) {\r
105                 $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id'));\r
106                 // If `clearPlaceholder` was called from `$.valHooks.input.set`\r
107                 if (event === true) {\r
108                     return $input[0].value = value;\r
109                 }\r
110                 $input.focus();\r
111             } else {\r
112                 input.value = '';\r
113                 $input.removeClass('placeholder');\r
114                 input == document.activeElement && input.select();\r
115             }\r
116         }\r
117     }\r
118 \r
119     function setPlaceholder() {\r
120         var $replacement,\r
121             input = this,\r
122             $input = $(input),\r
123             $origInput = $input,\r
124             id = this.id;\r
125         if (input.value == '') {\r
126             if (input.type == 'password') {\r
127                 if (!$input.data('placeholder-textinput')) {\r
128                     try {\r
129                         $replacement = $input.clone().attr({ 'type': 'text' });\r
130                     } catch(e) {\r
131                         $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));\r
132                     }\r
133                     $replacement\r
134                         .removeAttr('name')\r
135                         .data({\r
136                             'placeholder-password': true,\r
137                             'placeholder-id': id\r
138                         })\r
139                         .bind('focus.placeholder', clearPlaceholder);\r
140                     $input\r
141                         .data({\r
142                             'placeholder-textinput': $replacement,\r
143                             'placeholder-id': id\r
144                         })\r
145                         .before($replacement);\r
146                 }\r
147                 $input = $input.removeAttr('id').hide().prev().attr('id', id).show();\r
148                 // Note: `$input[0] != input` now!\r
149             }\r
150             $input.addClass('placeholder');\r
151             $input[0].value = $input.attr('placeholder');\r
152         } else {\r
153             $input.removeClass('placeholder');\r
154         }\r
155     }\r
156 \r
157 }(this, document, jQuery));