Merge "Begin exposing SiteConfiguration via site contexts"
[lhc/web/wiklou.git] / resources / jquery / jquery.placeholder.js
1 /**
2 * HTML5 placeholder emulation for jQuery plugin
3 *
4 * This will automatically use the HTML5 placeholder attribute if supported, or emulate this behavior if not.
5 *
6 * This is a fork from Mathias Bynens' jquery.placeholder as of this commit
7 * https://github.com/mathiasbynens/jquery-placeholder/blob/47f05d400e2dd16b59d144141a2cf54a9a77c502/jquery.placeholder.js
8 *
9 * @author Mathias Bynens <http://mathiasbynens.be/>
10 * @author Trevor Parscal <tparscal@wikimedia.org>, 2012
11 * @author Krinkle <krinklemail@gmail.com>, 2012
12 * @author Alex Ivanov <alexivanov97@gmail.com>, 2013
13 * @version 2.1.0
14 * @license MIT
15 */
16 (function($) {
17
18 var isInputSupported = 'placeholder' in document.createElement('input'),
19 isTextareaSupported = 'placeholder' in document.createElement('textarea'),
20 prototype = $.fn,
21 valHooks = $.valHooks,
22 propHooks = $.propHooks,
23 hooks,
24 placeholder;
25
26 if (isInputSupported && isTextareaSupported) {
27
28 placeholder = prototype.placeholder = function(text) {
29 var hasArgs = arguments.length;
30
31 if( hasArgs ) {
32 changePlaceholder.call(this, text);
33 }
34
35 return this;
36 };
37
38 placeholder.input = placeholder.textarea = true;
39
40 } else {
41
42 placeholder = prototype.placeholder = function(text) {
43 var $this = this,
44 hasArgs = arguments.length;
45
46 if(hasArgs) {
47 changePlaceholder.call(this, text);
48 }
49
50
51 $this
52 .filter((isInputSupported ? 'textarea' : ':input') + '[placeholder]')
53 .filter(function() {
54 return !$(this).data('placeholder-enabled');
55 })
56 .bind({
57 'focus.placeholder drop.placeholder': clearPlaceholder,
58 'blur.placeholder': setPlaceholder
59 })
60 .data('placeholder-enabled', true)
61 .trigger('blur.placeholder');
62 return $this;
63 };
64
65 placeholder.input = isInputSupported;
66 placeholder.textarea = isTextareaSupported;
67
68 hooks = {
69 'get': function(element) {
70 var $element = $(element),
71 $passwordInput = $element.data('placeholder-password');
72 if ($passwordInput) {
73 return $passwordInput[0].value;
74 }
75
76 return $element.data('placeholder-enabled') && $element.hasClass('placeholder') ? '' : element.value;
77 },
78 'set': function(element, value) {
79 var $element = $(element),
80 $passwordInput = $element.data('placeholder-password');
81 if ($passwordInput) {
82 $passwordInput[0].value = value;
83 return value;
84 }
85
86 if (!$element.data('placeholder-enabled')) {
87 element.value = value;
88 return value;
89 }
90 if (!value) {
91 element.value = value;
92 // Issue #56: Setting the placeholder causes problems if the element continues to have focus.
93 if (element !== safeActiveElement()) {
94 // We can't use `triggerHandler` here because of dummy text/password inputs :(
95 setPlaceholder.call(element);
96 }
97 } else if ($element.hasClass('placeholder')) {
98 if(!clearPlaceholder.call(element, true, value)) {
99 element.value = value;
100 }
101 } else {
102 element.value = value;
103 }
104 // `set` can not return `undefined`; see http://jsapi.info/jquery/1.7.1/val#L2363
105 return $element;
106 }
107 };
108
109 if (!isInputSupported) {
110 valHooks.input = hooks;
111 propHooks.value = hooks;
112 }
113 if (!isTextareaSupported) {
114 valHooks.textarea = hooks;
115 propHooks.value = hooks;
116 }
117
118 $(function() {
119 // Look for forms
120 $(document).delegate('form', 'submit.placeholder', function() {
121 // Clear the placeholder values so they don't get submitted
122 var $inputs = $('.placeholder', this).each(clearPlaceholder);
123 setTimeout(function() {
124 $inputs.each(setPlaceholder);
125 }, 10);
126 });
127 });
128
129 // Clear placeholder values upon page reload
130 $(window).bind('beforeunload.placeholder', function() {
131 $('.placeholder').each(function() {
132 this.value = '';
133 });
134 });
135
136 }
137
138 function args(elem) {
139 // Return an object of element attributes
140 var newAttrs = {},
141 rinlinejQuery = /^jQuery\d+$/;
142 $.each(elem.attributes, function(i, attr) {
143 if (attr.specified && !rinlinejQuery.test(attr.name)) {
144 newAttrs[attr.name] = attr.value;
145 }
146 });
147 return newAttrs;
148 }
149
150 function clearPlaceholder(event, value) {
151 var input = this,
152 $input = $(input);
153 if (input.value === $input.attr('placeholder') && $input.hasClass('placeholder')) {
154 if ($input.data('placeholder-password')) {
155 $input = $input.hide().next().show().attr('id', $input.removeAttr('id').data('placeholder-id'));
156 // If `clearPlaceholder` was called from `$.valHooks.input.set`
157 if (event === true) {
158 $input[0].value = value;
159 return value;
160 }
161 $input.focus();
162 } else {
163 input.value = '';
164 $input.removeClass('placeholder');
165 if(input === safeActiveElement()) {
166 input.select();
167 }
168 }
169 }
170 }
171
172 function setPlaceholder() {
173 var $replacement,
174 input = this,
175 $input = $(input),
176 id = this.id;
177 if (!input.value) {
178 if (input.type === 'password') {
179 if (!$input.data('placeholder-textinput')) {
180 try {
181 $replacement = $input.clone().attr({ 'type': 'text' });
182 } catch(e) {
183 $replacement = $('<input>').attr($.extend(args(this), { 'type': 'text' }));
184 }
185 $replacement
186 .removeAttr('name')
187 .data({
188 'placeholder-password': $input,
189 'placeholder-id': id
190 })
191 .bind('focus.placeholder drop.placeholder', clearPlaceholder);
192 $input
193 .data({
194 'placeholder-textinput': $replacement,
195 'placeholder-id': id
196 })
197 .before($replacement);
198 }
199 $input = $input.removeAttr('id').hide().prev().attr('id', id).show();
200 // Note: `$input[0] != input` now!
201 }
202 $input.addClass('placeholder');
203 $input[0].value = $input.attr('placeholder');
204 } else {
205 $input.removeClass('placeholder');
206 }
207 }
208
209 function safeActiveElement() {
210 // Avoid IE9 `document.activeElement` of death
211 // https://github.com/mathiasbynens/jquery-placeholder/pull/99
212 try {
213 return document.activeElement;
214 } catch (err) {}
215 }
216
217 function changePlaceholder(text) {
218 var hasArgs = arguments.length,
219 $input = this;
220 if(hasArgs) {
221 if($input.attr('placeholder') !== text) {
222 $input.prop('placeholder', text);
223 if($input.hasClass('placeholder')) {
224 $input[0].value = text;
225 }
226 }
227 }
228 }
229
230 }(jQuery));