hinderinputjs-rails 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,26 +5,33 @@
|
|
5
5
|
(function( $ ) {
|
6
6
|
|
7
7
|
/**
|
8
|
-
* Set a selection range with the given start and end indicies
|
8
|
+
* Set a selection range with the given start and end indicies.
|
9
|
+
*
|
10
|
+
* @param {number} start The position at which to start the selection.
|
11
|
+
* @param {number} end The position at which to end the selection.
|
9
12
|
**/
|
10
13
|
$.fn.setSelection = function( start, end ) {
|
11
14
|
return this.each(function( index, element ){
|
15
|
+
var range;
|
16
|
+
|
12
17
|
if ( typeof element.setSelectionRange === 'function' ) {
|
13
18
|
element.focus();
|
14
19
|
element.setSelectionRange( start, end );
|
15
20
|
}
|
16
21
|
else if( typeof element.createTextRange === 'function' ) {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
+
range = element.createTextRange();
|
23
|
+
range.collapse( true );
|
24
|
+
range.moveEnd( 'character', start );
|
25
|
+
range.moveStart( 'character', end );
|
26
|
+
range.select();
|
22
27
|
}
|
23
28
|
});
|
24
29
|
};
|
25
30
|
|
26
31
|
/**
|
27
|
-
* Set the position of the cursor to the given index
|
32
|
+
* Set the position of the cursor to the given index.
|
33
|
+
*
|
34
|
+
* @param {number} pos The position at which to set the caret.
|
28
35
|
**/
|
29
36
|
$.fn.setCaretPosition = function( pos ) {
|
30
37
|
return this.each(function( index, element ) {
|
@@ -17,28 +17,14 @@
|
|
17
17
|
return this.each(function( index, element ) {
|
18
18
|
var $target = $(this);
|
19
19
|
|
20
|
-
$target.keypress(
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
$target.setCaretAtEnd();
|
26
|
-
});
|
20
|
+
$target.keypress( $.proxy( hinderInput.addChar, hinderInput, options ) )
|
21
|
+
.keydown( $.proxy( hinderInput.addSpecial, hinderInput, options ) )
|
22
|
+
.click(function( event ) {
|
23
|
+
$target.setCaretAtEnd();
|
24
|
+
});
|
27
25
|
});
|
28
26
|
};
|
29
27
|
|
30
|
-
/**
|
31
|
-
* The default options for hinderInput.
|
32
|
-
**/
|
33
|
-
hinderInput.defaults = {
|
34
|
-
onAdd: function( value ) {
|
35
|
-
return;
|
36
|
-
},
|
37
|
-
onDelete: function() {
|
38
|
-
return;
|
39
|
-
}
|
40
|
-
};
|
41
|
-
|
42
28
|
/**
|
43
29
|
* Add a character to the input if it's valid. Execute the onAdd
|
44
30
|
* callback.
|
@@ -46,7 +32,7 @@
|
|
46
32
|
* @param {object} event The event that triggered the input.
|
47
33
|
* @param {object} options The options provided to hinderInput.
|
48
34
|
**/
|
49
|
-
hinderInput.addChar = function(
|
35
|
+
hinderInput.addChar = function( options, event ){
|
50
36
|
var $target = $(event.target),
|
51
37
|
keyCode = event.which;
|
52
38
|
|
@@ -63,22 +49,17 @@
|
|
63
49
|
* @param {object} event The event that triggered the input.
|
64
50
|
* @param {object} options The options provided to hinderInput.
|
65
51
|
**/
|
66
|
-
hinderInput.addSpecial = function(
|
52
|
+
hinderInput.addSpecial = function( options, event ) {
|
67
53
|
var $target = $( event.target ),
|
68
54
|
keyCode = event.which;
|
69
55
|
|
70
56
|
$target.setCaretAtEnd();
|
71
57
|
|
72
|
-
if ( this.isModifierPresent( event ) || this.isArrowKey( keyCode ) ) {
|
73
|
-
event.preventDefault();
|
74
|
-
return;
|
75
|
-
}
|
76
|
-
|
77
58
|
if( keyCode === 8 ) {
|
78
59
|
options.onDelete();
|
79
60
|
}
|
80
|
-
//
|
81
|
-
else if( !this.isValidChar(keyCode) ) {
|
61
|
+
//TODO more TLC
|
62
|
+
else if( !this.isValidChar(keyCode) || this.isModifierPresent( event ) ) {
|
82
63
|
event.preventDefault();
|
83
64
|
}
|
84
65
|
};
|
@@ -90,16 +71,9 @@
|
|
90
71
|
* @param {number} keyCode The char code of the character to check.
|
91
72
|
**/
|
92
73
|
hinderInput.isValidChar = function( keyCode ) {
|
93
|
-
return (keyCode
|
94
|
-
|
95
|
-
|
96
|
-
/**
|
97
|
-
* Determine if the given key code is an arrow key.
|
98
|
-
*
|
99
|
-
* @param {number} keyCode The char code of the character to check.
|
100
|
-
**/
|
101
|
-
hinderInput.isArrowKey = function( keyCode ) {
|
102
|
-
return [37, 38, 39, 40].indexOf( keyCode ) !== -1;
|
74
|
+
return (keyCode > 31 && keyCode < 37)
|
75
|
+
|| (keyCode > 40 && keyCode < 127)
|
76
|
+
|| (keyCode > 185 && keyCode < 223);
|
103
77
|
};
|
104
78
|
|
105
79
|
/**
|
@@ -112,4 +86,16 @@
|
|
112
86
|
return event.altKey || event.ctrlKey || event.metaKey;
|
113
87
|
};
|
114
88
|
|
89
|
+
/**
|
90
|
+
* The default options for hinderInput.
|
91
|
+
**/
|
92
|
+
hinderInput.defaults = {
|
93
|
+
onAdd: function( value ) {
|
94
|
+
return;
|
95
|
+
},
|
96
|
+
onDelete: function() {
|
97
|
+
return;
|
98
|
+
}
|
99
|
+
};
|
100
|
+
|
115
101
|
})( jQuery );
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hinderinputjs-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: railties
|
@@ -60,7 +60,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
60
60
|
version: '0'
|
61
61
|
segments:
|
62
62
|
- 0
|
63
|
-
hash:
|
63
|
+
hash: 3659832406573053636
|
64
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
65
|
none: false
|
66
66
|
requirements:
|
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
69
|
version: '0'
|
70
70
|
segments:
|
71
71
|
- 0
|
72
|
-
hash:
|
72
|
+
hash: 3659832406573053636
|
73
73
|
requirements: []
|
74
74
|
rubyforge_project:
|
75
75
|
rubygems_version: 1.8.24
|