forked from timmywil/password123
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.password123.js
490 lines (423 loc) · 13.6 KB
/
jquery.password123.js
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/**
* @preserve jQuery password123: iPhone Style Passwords Plugin - v1.5 - 2/1/2011
* http://timmywillison.com/samples/password123/
* Copyright (c) 2011 timmy willison
* Dual licensed under the MIT and GPL licences.
* http://timmywillison.com/licence/
*/
// *Version: 1.5, Last updated: 2/1/2011*
//
// Demo - http://timmywillison.com/samples/password123/
// Testing - http://timmywillison.com/samples/password123/qunit/test/
// GitHub - http://github.com/timmywil/password123
// Source - http://github.com/timmywil/password123/raw/master/jquery.password123.js (13.8kb)
// (Minified) - http://github.com/timmywil/password123/raw/master/jquery.password123.min.js (5.2kb)
//
// License
//
// Copyright (c) 2011 timmy willison,
// Dual licensed under the MIT and GPL licenses.
// http://timmywillison.com/licence/
//
// Support and Testing
//
// Versions of jQuery and browsers this was tested on.
//
// jQuery Versions - 1.3.2-1.5
// Browsers Tested - Internet Explorer 6-8, Firefox 2-3.7, Safari 3-5,
// Chrome 4-6, Opera 9.6-10.5.
//
// Release History
//
// 1.5 - (2/1/2011) Added widget-style option method, only instantiate when not calling a method
// 1.4 - (2/1/2011) Restructured plugin, added destroy method, added tests
// 1.3 - (11/23/2010) Added Google Closure Compiler comments, common password field attribute
// support when replacing fields, and no longer sends placeholder value when submitting the form.
// 1.2 - (9/28/2010) Placeholders changed to only work with HTML5 placeholder attribute,
// 'value' attribute now reserved for actual values
// 1.1 - (7/5/2010) Add Placeholder functionality
// 1.0 - (7/4/2010) Initial release
//
// See README for usage and placeholder explanation
;(function ($, window, document, undefined) {
// Extend jQuery
$.fn.password123 = function ( options ) {
var instance = $.data( this[0], "password123" ),
args = Array.prototype.slice.call( arguments, 1 );
// Catch method calls
if ( !!instance && typeof options === "string" && options.charAt( 0 ) !== "_" && instance[ options ] ) {
args.unshift( this );
return instance[ options ].apply( instance, args );
}
return this.map(function( i, elem ) {
// Returns the new password fields for chaining
// Old password fields are removed
return new password123._init( elem, options );
});
};
var clear_timeout = window.clearTimeout,
counter = 0,
password123 = {
_init: function( elem, options ) {
// Catch fields that aren't password fields
if ( !elem.type === "password" ) {
return elem;
}
var self = this;
// Continue regularly
self.options = $.extend({
// You can use any html character code or
// plain text character
character: "•",
// This is the delay(ms) for when the last
// character will change
delay: 2000,
// Use any prefix you like for the new
// field ids, but they will always be zero-indexed
prefix: "iField",
// Enable the override of the placeholder attribute
placeholder: true,
// With this classname, you can set placeholder
// specific styles in your own css
placeholderClass: 'place',
// When true, this will mask the placeholder or initial value
maskInitial: false
}, options);
// HTML encode the character
self.encodedChar = $('<div>'+ self.options.character +'</div>').text();
// Replace the fields with what we need
// and store in var fields
self.$field = self._replaceField( elem )
// Bind textchange to the fields with
// the letterChange function
.bind('textchange', function() {
self._letterChange.call( self );
});
// Add placeholder stuff
if ( self.options.placeholder ) {
self._bindPlaceholder();
}
// Mask the placeholder or initial value if needed
if ( self.options.maskInitial ) {
self.$field.keyup();
}
// Return the new password field
return self.$field[0];
},
/**
* Replaces the password field with a hidden field
* and adds the new visible text field.
* @param {object} $field One field to replace
* @return {object} The new text field.
* @private
*/
_replaceField: function( field ) {
var self = this,
$field = $(field),
place = $field.attr('placeholder') || undefined,
field_id = self.options.prefix + counter++,
value = $field.val() || ( self.options.placeholder ? place || '' : '' ),
classes = self.options.placeholder && place !== undefined && (value === place || value === '') ?
field.className + ' ' + self.options.placeholderClass :
field.className,
attrs = { 'class': classes, 'id': field_id, 'value': value, 'placeholder': self.options.placeholder ? undefined : place },
standards = [ 'size', 'tabindex', 'readonly', 'disabled', 'maxlength' ];
// Combine attrs with standard attrs
for ( var i = 0; i < standards.length; i++ ) {
var attr = $field.attr( standards[i] );
attrs[ standards[i] ] = attr && attr > -1 ? attr : undefined;
}
// The hidden field that will get sent with the form
self.$hidden = $('<input type="hidden"/>')
.attr({
'name': $field.attr('name'),
'id': field.id,
'class': field.className,
'disabled': $field.attr('disabled')
})
.replaceAll( $field )
.val( value !== place ? value : '' );
self.$oldField = $field;
// The main field
return $('<input type="text"/>')
.attr( attrs )
.insertAfter( self.$hidden )
.data({
'value': $field.val() || '', // If value was set, fill it in correctly
'placeholder': place, // Attach placeholder for comparison to value
'newVal': value, // This to avoid sending the placeholder value when submitting the form
'password123': self // Attach the instance of this plugin
});
},
/**
* Calls for the necessary adjustments when
* a field is changed
* @private
*/
_letterChange: function( elem ) {
var fv = this.$field.val();
if ( fv.length > this.$field.data('value').length ) {
// Apply fieldChange as normal
this.$field.data( 'value', this._fieldChange() );
} else if ( fv.length < this.$field.data('value').length ) {
// Clear the timeout for the last character
clear_timeout( this.last );
var old = this.$hidden.val(),
newVal;
if ( fv.length < old.length - 1 ) {
newVal = old.substr( 0, fv.length );
} else {
var cp = this._getCursorPosition();
// Create the new value with the correct
// character deleted
newVal = old.length > cp + 1 && cp > -1
? old.slice(0, cp).concat(old.slice(cp + 1))
: old.slice(0, cp);
}
// Update data and the hidden input
this.$field.data({ 'value': fv, 'newVal': newVal });
if ( newVal !== this.$field.data('placeholder') ) {
this.$hidden.val( newVal );
}
}
return this;
},
/**
* Updates the field with the dots as the user types,
* and sets the timeout for the last char.
* @return {string} The new value to set to field.data
* @private
*/
_fieldChange: function() {
var self = this;
// Clear the timeout for the last character
clear_timeout( self.last );
var fv = self.$field.val(), len = fv.length,
old = self.$hidden.val(),
cp = self._getCursorPosition(),
newVal;
// Update the hidden value with the correct value
// depending on cursor position
newVal = old.length > cp + 1 && cp > -1
? old.substr(0, cp-1) + fv.charAt(cp-1) + old.substr(cp-1)
: old + fv.charAt(len - 1);
self.$field.data('newVal', newVal);
if ( newVal !== self.$field.data('placeholder') ) {
self.$hidden.val( newVal );
}
if ( len > 1 ) {
// Loop through and change all characters
for ( var i = 0; i < len - 1; i++ ) {
fv = fv.replace( fv.charAt(i), self.encodedChar );
}
// Update the field
self.$field.val( fv );
}
if ( len > 0 ) {
// Set the timeout for the last character
self.last = setTimeout(function () {
cp = self._getCursorPosition();
fv = self.$field.val();
fv = fv.replace( fv.charAt(len - 1), self.encodedChar );
self.$field.val( fv ).data('value', fv);
// Reset cursor position to what it was if not at end
if ( cp != len ) {
self._setCursorPosition( cp );
}
}, self.options.delay);
}
// Reset cursor position to what it was if not at end
if ( cp != len ) {
self._setCursorPosition( cp );
}
return fv;
},
/**
* Placeholder functionality
* @private
*/
_bindPlaceholder: function() {
var self = this,
place = self.$field.data('placeholder');
if ( place !== undefined ) {
self.$field.bind({
"focus.password123": function() {
// Compare the hidden value with the placeholder value
if ( self.$field.data('newVal') === place ) {
self.$field.val('').removeClass( self.options.placeholderClass ).data( 'newVal', '' );
self.$hidden.val('');
}
},
"blur.password123": function() {
// If it's empty, put the placeholder in as the value
if ( place !== undefined && self.$field.val() === '' ) {
self.$field.val( place ).addClass( self.options.placeholderClass ).data( 'newVal', place );
// Mask the placeholder if needed
if ( self.options.maskInitial ) {
self.$field.keyup();
}
}
}
});
} else {
self.$field.keyup();
}
return self;
},
/**
* Remove placeholder functionality
* @private
*/
_changePlaceholder: function( isPlace ) {
if ( isPlace && !this.options.placeholder ) {
return this._bindPlaceholder();
} else if ( !isPlace && this.options.placeholder ) {
this.$field.focus().unbind('.password123').blur();
}
return this;
},
/**
* Change the id prefix on this field
* @private
*/
_changePrefix: function( value ) {
var cur = this.$field.attr("id"),
prev = this.options.prefix;
this.$field.attr( 'id', cur.replace( prev, value ) );
return this;
},
/**
* Gets the current cursor position in a textfield
* to determine where to delete/update a character.
* @return {Number} The index for the position of the cursor.
* @private
*/
_getCursorPosition: function() {
var elem = this.$field[0];
if ( elem != null ) {
// IE
if ( document.selection ) {
elem.focus();
var sel = document.selection.createRange();
sel.moveStart( 'character', -elem.value.length );
return sel.text.length;
}
// Others
else if ( elem.selectionStart !== undefined ) {
return elem.selectionStart;
}
}
return -1;
},
/**
* Sets the cursor position to a previous state
* if the field was updated somewhere in the middle.
* @param {Element} field The raw field.
* @param {Number} pos The position to set to.
* @private
*/
_setCursorPosition: function( pos ) {
var elem = this.$field[0];
if ( elem != null ) {
// IE
if ( elem.createTextRange ) {
var range = elem.createTextRange();
range.move('character', pos);
range.select();
}
// Others
else if ( elem.setSelectionRange ) {
elem.focus();
elem.setSelectionRange( pos, pos );
}
else {
elem.focus();
}
}
},
/**
* Internally sets options
* @return password123 instance
* @private
*/
_setOptions: function( options ) {
var self = this;
$.each( options, function( key, value ) {
switch ( key ) {
case "placeholder":
self._changePlaceholder( value === "false" ? false : value );
break;
case "prefix":
self._changePrefix( value );
}
self.options[ key ] = value;
});
return this;
},
/**
* Destroys everything password123 has done and
* sets the original password field back in place
* @return The original password field
*/
destroy: function( elem ) {
var val = this.$hidden.remove().val();
return this.$oldField.val( val ).replaceAll( elem );
},
/**
* Get/set option on an existing password123 instance
* @return The password123 field
*/
option: function( elems, key, value ) {
if ( !key ) {
return $.extend( {}, this.options ); // Avoids returning direct reference
}
var options = key;
if ( typeof key === "string" ) {
if ( value === undefined ) {
return this.options[ key ];
}
options = {};
options[ key ] = value;
}
this._setOptions( options );
return elems;
}
};
password123._init.prototype = password123;
// Textchange event with a little extra
$.event.special.textchange = {
setup: function( data, namespaces ) {
$(this).bind('keyup.textchange', $.event.special.textchange.handler);
$(this).bind('cut.textchange paste.textchange input.textchange', $.event.special.textchange.delayedHandler);
},
teardown: function( namespaces ) {
$(this).unbind('.textchange');
},
handler: function( event ) {
$.event.special.textchange.triggerIfChanged( $(this) );
},
delayedHandler: function( event ) {
var $element = $(this);
setTimeout(function() {
$.event.special.textchange.triggerIfChanged( $element );
}, 25);
},
triggerIfChanged: function( $element ) {
var v = $element.val();
if ( $element.val() !== $element.data('lastValue') ) {
// Check if something larger than one letter was pasted into the field
var p123 = $.data( $element[0], "password123" );
if ( v.length > 1 && v.indexOf( p123.encodedChar ) === -1 ) {
// If so, we need to save it before it disappears,
// but continue with triggering the field change
// by taking the last letter off
v = v.substr(0, v.length-1);
$element.data('value', v);
p123.$hidden.val( v );
}
$element.trigger( 'textchange', $element.data('lastValue') );
$element.data('lastValue', $element.val());
}
}
};
})(jQuery, this, this.document);