File size: 11,216 Bytes
52d9fb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*
 *  jquery-input-lettering - v0.0.1
 *  Light-weight jquery input lettering plugin
 *  https://github.com/sergmaestro/jquery-input-lettering
 *
 *  Made by Sergei Kuznetsov
 *  Under MIT License
 */
;( function( $, window, document, undefined ) {

    "use strict";

    $.fn.letteringInput = function( options ) {
        var settings,
            defaults = {
                inputClass: "letter",
                hiddenInputName: "",
                hiddenInputWrapperID: "",
                forbiddenKeyCodes: [ 9, 16, 17, 18, 20, 27, 32, 33, 34, 38, 40, 45, 144 ],
                onFocusLetter: function() {},
                onBlurLetter: function() {},
                onLetterKeyup: function() {},
                onSet: function() {}
            };

        if ( typeof options === "object" ) {
            settings = $.extend( {}, defaults, options );
        } else {
            settings = defaults;
        }

        var methods = {
            destroy: function( $el ) {
                $( "input." + settings.inputClass, $el ).unbind();
            }
        };

        $.fn.getCursorPosition = function() {
            var input = this.get( 0 );
            if ( !input ) {
                return; // No (input) element found
            }
            if ( "selectionStart" in input ) {
                return input.selectionStart;
            } else if ( document.selection ) {

                // IE
                input.focus();
                var sel = document.selection.createRange();
                var selLen = document.selection.createRange().text.length;
                sel.moveStart( "character", -input.value.length );
                return sel.text.length - selLen;
            }
        };

        $.fn.setCaretPosition = function( caretPos ) {
            var input = this.get( 0 );

            if ( input != null ) {
                if ( input.createTextRange ) {
                    var range = input.createTextRange();
                    range.move( "character", caretPos );
                    range.select();
                } else {
                    if ( input.selectionStart ) {
                        input.focus();
                        input.setSelectionRange( caretPos, caretPos );
                    } else {
                        input.focus();
                    }
                }
            }
        };

        function init( $letter ) {
            var $allLetters = $( "input." + settings.inputClass, $letter );

            $allLetters.on( "keydown", function( event ) {
                var $el = $( this );
                var val = $.trim( $el.val() );
                var caretPosition = $el.getCursorPosition();

                $el.val( val ); // trim and update

                if ( event.keyCode === 37 ) { // Back arrow
                    if ( !caretPosition ) {
                        focusOnPrevious( $el, $allLetters );
                    }
                } else if ( event.keyCode === 39 ) { // Next arrow
                    if ( caretPosition ) {
                        focusOnNext( $el, $allLetters );
                    }
                } else if ( event.keyCode === 8 ) { // Backspace
                    $el = caretPosition ? $el : previousElement( $el, $allLetters );
                    if ( $el ) {
                        focusOnElement( $el );
                    }

                    setValue( $allLetters );
                    if ( caretPosition && $el.val() !== "" ) {
                        setTimeout( function() {
                            focusOnPrevious( $el, $allLetters );
                        }, 20 );
                    }
                } else if ( event.keyCode === 46 ) { // del
                    var next = nextElement( $el, $allLetters );
                    if ( caretPosition && next ) {
                        $( next ).val( "" );
                    }

                    setValue( $allLetters );

                    var updatedNext = nextElement( $el, $allLetters );
                    if ( updatedNext && $( updatedNext ).val() !== "" ) {
                        setTimeout( function() {
                            $el.setCaretPosition( 0 );  // Set caret position
                        }, 20 );
                    }
                } else if ( $.inArray( event.keyCode, settings.forbiddenKeyCodes ) === -1 ) {
                    var keyValue = event.key;
                    var currentCode = event.which || event.code;
                    if ( keyValue === "Unidentified" ) {
                        keyValue = String.fromCharCode( currentCode );
                    }
                    insertNewDigit( $el, caretPosition, keyValue, $allLetters );
                }

                if ( typeof defaults.onSet === "function" ) {
                    setTimeout( function() {
                        settings.onSet.call( null, $( this ), event );
                    }, 40 );
                }
            } ).on( "keyup", function( event ) {
                /**
                 * On keyup user defined function
                 */
                if ( typeof defaults.onLetterKeyup === "function" ) {
                    setTimeout( function() {
                        settings.onLetterKeyup.call( null, $( this ), event );
                    }, 1 );
                }
            } ).on( "focus", function( event ) {
                var $el = $( this );
                var currentIndex = $allLetters.index( $el );

                if ( $el.val() ) {

                    // If current input has value => do nothing
                    return false;
                } else {

                    // Get previous letters with empty value
                    var $previousEmptyLetters = $allLetters.filter( function( index ) {
                        return !this.value && index < currentIndex;
                    } );

                    if ( typeof $previousEmptyLetters[ 0 ] !== "undefined" ) {
                        focusOnElement( $previousEmptyLetters[ 0 ] );
                    }
                }

                /**
                 * On focus user defined function
                 */
                if ( typeof defaults.onFocusLetter === "function" ) {
                    settings.onFocusLetter.call( null, $( this ), event );
                }
            } ).on( "blur", function( event ) {
                /**
                 * On blur user defined function
                 */
                setTimeout( function() {
                    var value = getValue( $allLetters );
                    if ( typeof defaults.onBlurLetter === "function" ) {
                        settings.onBlurLetter.call( null, $( this ), event, value );
                    }
                }, 40 );
            } );
        }

        function setValue( $allLetters, value ) {
            setTimeout( function() {
                var newValue = typeof value !== "undefined" ? value : getValue( $allLetters );

                if ( settings.hiddenInputName && settings.hiddenInputWrapperID ) {

                    // update hidden input
                    $(
                        "input[name=\"" + settings.hiddenInputName + "\"]",
                        "#" + settings.hiddenInputWrapperID )
                        .val( newValue );
                }

                $allLetters.map( function() {
                    this.value = typeof newValue[ 0 ] !== "undefined" ? newValue[ 0 ] : "";
                    newValue = newValue.substr( 1 );
                } );
            }, 30 );
        }

        function focusOnPrevious( $el, $allLetters ) {
            var $prevEl = previousElement( $el, $allLetters );

            if ( !$prevEl ) {
                return false;
            }

            focusOnElement( $prevEl );
        }

        function focusOnNext( $el, $allLetters ) {
            var $nextEl = nextElement( $el, $allLetters );

            if ( !$nextEl ) {
                return false;
            }

            focusOnElement( $nextEl );
        }

        function nextElement( $el, $allLetters ) {
            var currentIndex = $allLetters.index( $el );
            var $nextElem = null;

            if ( currentIndex !== -1 && $allLetters.length - 1 > currentIndex ) {
                var next = $allLetters[ currentIndex + 1 ];

                // If the next input is hidden (e.g. style="display:none") return null
                $nextElem = $( next ).is( ":hidden" ) ? null : next;
            }

            return $nextElem;
        }

        function previousElement( $el, $allLetters ) {
            var currentIndex = $allLetters.index( $el );
            return currentIndex > 0 ? $( $allLetters[ currentIndex - 1 ] ) : null;
        }

        function focusOnElement( $el ) {
            $el.click();
            setTimeout( function() {
                $el.focus();
            }, 1 ); // Safari focus bug fix
        }

        function getValue( $allLetters ) {
            return $allLetters.map( function() {
                return this.value;
            } ).get().join( "" );
        }

        function insertNewDigit( $el, caretPosition, newValue, $inputs ) {
            var index = $inputs.index( $el );
            var value = getValue( $inputs );

            var lettersArray = $inputs.filter( function() {
                return !$( this ).is( ":hidden" ) && !this.value;
            } );

            // No more empty letters
            if ( !lettersArray.length ) {
                setValue( $inputs );
                return false;
            }

            // if new value exists and > than 1 letter/digit => do nothing
            if ( typeof newValue !== "undefined" && newValue.length > 1 ) {
                return false;
            }

            if ( index !== -1 ) {
                index = !caretPosition ? index : index + 1;
            }

            if ( value.length < $inputs.length ) {
                if ( $el.val() !== "" ) { // current input has previous value
                    value = [ value.slice( 0, index ), newValue, value.slice( index ) ].join( "" );
                    setValue( $inputs, value );

                    var next = nextElement( $el, $inputs );
                    if ( next ) {
                        setTimeout( function() {
                            focusOnNext( $el, $inputs );
                        }, 1 );
                    }
                } else {
                    setTimeout( function() {
                        focusOnNext( $el, $inputs );
                    }, 1 );
                    setValue( $inputs );
                }
            }
        }

        /**
         * letteringInput methods processing
         */
        if ( typeof options === "string" ) {
            if ( options && methods[ options ] ) {
                return methods[ options ].call( null, $( this ) );
            }
            $.error( "Method " + options + " does not exist in jQuery.letteringInput" );
            return this;
        }

        /**
         * Initialize letteringInput events
         */
        return this.each( function() {
            init( $( this ) );
        } );
    };

} )( jQuery, window, document );