1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/**
* jQuery TextExt Plugin
* http://textextjs.com
*
* @version 1.3.1
* @copyright Copyright (C) 2011 Alex Gorbatchev. All rights reserved.
* @license MIT License
*/
(function($)
{
/**
* Prompt plugin displays a visual user propmpt in the text input area. If user focuses
* on the input, the propt is hidden and only shown again when user focuses on another
* element and text input doesn't have a value.
*
* @author agorbatchev
* @date 2011/08/18
* @id TextExtPrompt
*/
function TextExtPrompt() {};
$.fn.textext.TextExtPrompt = TextExtPrompt;
$.fn.textext.addPlugin('prompt', TextExtPrompt);
var p = TextExtPrompt.prototype,
CSS_HIDE_PROMPT = 'text-hide-prompt',
/**
* Prompt plugin has options to change the prompt label and its HTML template. The options
* could be changed when passed to the `$().textext()` function. For example:
*
* $('textarea').textext({
* plugins: 'prompt',
* prompt: 'Your email address'
* })
*
* @author agorbatchev
* @date 2011/08/18
* @id TextExtPrompt.options
*/
/**
* Prompt message that is displayed to the user whenever there's no value in the input.
*
* @name prompt
* @default 'Awaiting input...'
* @author agorbatchev
* @date 2011/08/18
* @id TextExtPrompt.options.prompt
*/
OPT_PROMPT = 'prompt',
/**
* HTML source that is used to generate markup required for the prompt effect.
*
* @name html.prompt
* @default '<div class="text-prompt"/>'
* @author agorbatchev
* @date 2011/08/18
* @id TextExtPrompt.options.html.prompt
*/
OPT_HTML_PROMPT = 'html.prompt',
/**
* Prompt plugin dispatches or reacts to the following events.
* @id TextExtPrompt.events
*/
/**
* Prompt plugin reacts to the `focus` event and hides the markup generated from
* the `html.prompt` option.
*
* @name focus
* @author agorbatchev
* @date 2011/08/18
* @id TextExtPrompt.events.focus
*/
/**
* Prompt plugin reacts to the `blur` event and shows the prompt back if user
* hasn't entered any value.
*
* @name blur
* @author agorbatchev
* @date 2011/08/18
* @id TextExtPrompt.events.blur
*/
DEFAULT_OPTS = {
prompt : 'Awaiting input...',
html : {
prompt : '<div class="text-prompt"/>'
}
}
;
/**
* Initialization method called by the core during plugin instantiation.
*
* @signature TextExtPrompt.init(core)
*
* @param core {TextExt} Instance of the TextExt core class.
*
* @author agorbatchev
* @date 2011/08/18
* @id TextExtPrompt.init
*/
p.init = function(core)
{
var self = this,
placeholderKey = 'placeholder',
container,
prompt
;
self.baseInit(core, DEFAULT_OPTS);
container = $(self.opts(OPT_HTML_PROMPT));
$(self).data('container', container);
self.core().wrapElement().append(container);
self.setPrompt(self.opts(OPT_PROMPT));
prompt = core.input().attr(placeholderKey);
if(!prompt)
prompt = self.opts(OPT_PROMPT);
// clear placeholder attribute if set
core.input().attr(placeholderKey, '');
if(prompt)
self.setPrompt(prompt);
if($.trim(self.val()).length > 0)
self.hidePrompt();
self.on({
blur : self.onBlur,
focus : self.onFocus,
postInvalidate : self.onPostInvalidate,
postInit : self.onPostInit
});
};
//--------------------------------------------------------------------------------
// Event handlers
/**
* Reacts to the `postInit` and configures the plugin for initial display.
*
* @signature TextExtPrompt.onPostInit(e)
*
* @param e {Object} jQuery event.
*
* @author agorbatchev
* @date 2011/08/24
* @id TextExtPrompt.onPostInit
*/
p.onPostInit = function(e)
{
this.invalidateBounds();
};
/**
* Reacts to the `postInvalidate` and insures that prompt display remains correct.
*
* @signature TextExtPrompt.onPostInvalidate(e)
*
* @param e {Object} jQuery event.
*
* @author agorbatchev
* @date 2011/08/24
* @id TextExtPrompt.onPostInvalidate
*/
p.onPostInvalidate = function(e)
{
this.invalidateBounds();
};
/**
* Repositions the prompt to make sure it's always at the same place as in the text input carret.
*
* @signature TextExtPrompt.invalidateBounds()
*
* @author agorbatchev
* @date 2011/08/24
* @id TextExtPrompt.invalidateBounds
*/
p.invalidateBounds = function()
{
var self = this,
input = self.input()
;
self.containerElement().css({
paddingLeft : input.css('paddingLeft'),
paddingTop : input.css('paddingTop')
});
};
/**
* Reacts to the `blur` event and shows the prompt effect with a slight delay which
* allows quick refocusing without effect blinking in and out.
*
* The prompt is restored if the text box has no value.
*
* @signature TextExtPrompt.onBlur(e)
*
* @param e {Object} jQuery event.
*
* @author agorbatchev
* @date 2011/08/08
* @id TextExtPrompt.onBlur
*/
p.onBlur = function(e)
{
var self = this;
self.startTimer('prompt', 0.1, function()
{
self.showPrompt();
});
};
/**
* Shows prompt HTML element.
*
* @signature TextExtPrompt.showPrompt()
*
* @author agorbatchev
* @date 2011/08/22
* @id TextExtPrompt.showPrompt
*/
p.showPrompt = function()
{
var self = this,
input = self.input()
;
if($.trim(self.val()).length === 0 && !input.is(':focus'))
self.containerElement().removeClass(CSS_HIDE_PROMPT);
};
/**
* Hides prompt HTML element.
*
* @signature TextExtPrompt.hidePrompt()
*
* @author agorbatchev
* @date 2011/08/22
* @id TextExtPrompt.hidePrompt
*/
p.hidePrompt = function()
{
this.stopTimer('prompt');
this.containerElement().addClass(CSS_HIDE_PROMPT);
};
/**
* Reacts to the `focus` event and hides the prompt effect.
*
* @signature TextExtPrompt.onFocus
*
* @param e {Object} jQuery event.
* @author agorbatchev
* @date 2011/08/08
* @id TextExtPrompt.onFocus
*/
p.onFocus = function(e)
{
this.hidePrompt();
};
//--------------------------------------------------------------------------------
// Core functionality
/**
* Sets the prompt display to the specified string.
*
* @signature TextExtPrompt.setPrompt(str)
*
* @oaram str {String} String that will be displayed in the prompt.
*
* @author agorbatchev
* @date 2011/08/18
* @id TextExtPrompt.setPrompt
*/
p.setPrompt = function(str)
{
this.containerElement().text(str);
};
/**
* Returns prompt effect HTML element.
*
* @signature TextExtPrompt.containerElement()
*
* @author agorbatchev
* @date 2011/08/08
* @id TextExtPrompt.containerElement
*/
p.containerElement = function()
{
return $(this).data('container');
};
})(jQuery);
|