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