hinderinputjs-rails 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
6
+ *DS_Store
7
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Morgan Brown
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,13 @@
1
+ # Hinder Input
2
+ This is a jQuery plugin that attempts to disallow text selection, cursor placement, and characters outside of the "normal" ASCII print character range.
3
+
4
+ ## Usage
5
+
6
+ $(element).hinderInput({
7
+ onAdd: function ( value ) {
8
+ // do something when a valid character is added
9
+ },
10
+ onDelete: function() {
11
+ // do something when a character is deleted
12
+ }
13
+ });
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hinderinputjs-rails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hinderinputjs-rails"
7
+ s.version = Hinderinputjs::Rails::VERSION
8
+ s.authors = ["Morgan Brown"]
9
+ s.email = ["brown.mhg@gmail.com"]
10
+ s.homepage = "https://github.com/discom4rt/hinderinputjs-rails"
11
+ s.summary = "A method for hindering input capabilities within a textarea or text input."
12
+ s.description = "A method for hindering input capabilities within a textarea or text input. Attempts to disallow text selection, cursor placement, and characters outside of the \"normal\" ASCII print character range."
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+
16
+ s.add_dependency "railties", "~> 3.1"
17
+ end
18
+
@@ -0,0 +1,5 @@
1
+ module Hinderinputjs
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require "hinderinputjs-rails/version"
2
+
3
+ module Hinderinputjs
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ end
7
+ end
8
+ end
9
+
@@ -0,0 +1,45 @@
1
+ /**
2
+ * A set of methods to manipulate caret position. Does the world need another set of
3
+ * these methods? Definitely. :P
4
+ **/
5
+ (function( $ ) {
6
+
7
+ /**
8
+ * Set a selection range with the given start and end indicies
9
+ **/
10
+ $.fn.setSelection = function( start, end ) {
11
+ return this.each(function( index, element ){
12
+ if ( typeof element.setSelectionRange === 'function' ) {
13
+ element.focus();
14
+ element.setSelectionRange( start, end );
15
+ }
16
+ else if( typeof element.createTextRange === 'function' ) {
17
+ var range = element.createTextRange();
18
+ range.collapse( true );
19
+ range.moveEnd( 'character', start );
20
+ range.moveStart( 'character', end );
21
+ range.select();
22
+ }
23
+ });
24
+ };
25
+
26
+ /**
27
+ * Set the position of the cursor to the given index
28
+ **/
29
+ $.fn.setCaretPosition = function( pos ) {
30
+ return this.each(function( index, element ) {
31
+ $(element).setSelection( pos, pos );
32
+ });
33
+ };
34
+
35
+ /**
36
+ * Set the position of the cursor to the end
37
+ **/
38
+ $.fn.setCaretAtEnd = function() {
39
+ return this.each(function( index, element ) {
40
+ var length = element.value.length;
41
+ $(element).setCaretPosition( length, length );
42
+ });
43
+ };
44
+
45
+ })( jQuery );
@@ -0,0 +1,115 @@
1
+ /**
2
+ * A method for hindering input capabilities within a textarea or text input. Attempts to disallow text selection,
3
+ * cursor placement, and characters outside of the "normal" ASCII print character range.
4
+ **/
5
+ (function( $ ) {
6
+
7
+ /**
8
+ * Hinder the input capabilites of the selected elements.
9
+ *
10
+ * @param {object} options A set of configuration options.
11
+ * onAdd: A function called when a character is added.
12
+ * onDelete: A function called when a character is deleted.
13
+ **/
14
+ var hinderInput = $.fn.hinderInput = function( options ) {
15
+ var options = $.extend( {}, hinderInput.defaults, options );
16
+
17
+ return this.each(function( index, element ) {
18
+ var $target = $(this);
19
+
20
+ $target.keypress(function( event ) {
21
+ hinderInput.addChar.call( hinderInput, event, options );
22
+ }).keydown(function( event ) {
23
+ hinderInput.addSpecial.call( hinderInput, event, options );
24
+ }).click(function( event ) {
25
+ $target.setCaretAtEnd();
26
+ });
27
+ });
28
+ };
29
+
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
+ /**
43
+ * Add a character to the input if it's valid. Execute the onAdd
44
+ * callback.
45
+ *
46
+ * @param {object} event The event that triggered the input.
47
+ * @param {object} options The options provided to hinderInput.
48
+ **/
49
+ hinderInput.addChar = function( event, options ){
50
+ var $target = $(event.target),
51
+ keyCode = event.which;
52
+
53
+ if ( this.isValidChar( keyCode ) ) {
54
+ $target.setCaretAtEnd();
55
+ options.onAdd( String.fromCharCode( keyCode ) );
56
+ }
57
+ };
58
+
59
+ /**
60
+ * Apply a special character to the input if it's allowed. Currently,
61
+ * only delete is allowed. Execute the onDelete callback.
62
+ *
63
+ * @param {object} event The event that triggered the input.
64
+ * @param {object} options The options provided to hinderInput.
65
+ **/
66
+ hinderInput.addSpecial = function( event, options ) {
67
+ var $target = $( event.target ),
68
+ keyCode = event.which;
69
+
70
+ $target.setCaretAtEnd();
71
+
72
+ if ( this.isModifierPresent( event ) || this.isArrowKey( keyCode ) ) {
73
+ event.preventDefault();
74
+ return;
75
+ }
76
+
77
+ if( keyCode === 8 ) {
78
+ options.onDelete();
79
+ }
80
+ // TODO refactor
81
+ else if( !this.isValidChar(keyCode) ) {
82
+ event.preventDefault();
83
+ }
84
+ };
85
+
86
+ /**
87
+ * Determine if the given key code is within the acceptable
88
+ * range.
89
+ *
90
+ * @param {number} keyCode The char code of the character to check.
91
+ **/
92
+ hinderInput.isValidChar = function( keyCode ) {
93
+ return (keyCode >= 32) && (keyCode <= 126) || (keyCode >= 186) && (keyCode <= 222);
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;
103
+ };
104
+
105
+ /**
106
+ * Determine if any modifier keys were present during the given
107
+ * event.
108
+ *
109
+ * @param {object} event The event to check.
110
+ **/
111
+ hinderInput.isModifierPresent = function( event ) {
112
+ return event.altKey || event.ctrlKey || event.metaKey;
113
+ };
114
+
115
+ })( jQuery );
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hinderinputjs-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Morgan Brown
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.1'
30
+ description: A method for hindering input capabilities within a textarea or text input.
31
+ Attempts to disallow text selection, cursor placement, and characters outside of
32
+ the "normal" ASCII print character range.
33
+ email:
34
+ - brown.mhg@gmail.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - .gitignore
40
+ - Gemfile
41
+ - LICENSE
42
+ - README.md
43
+ - Rakefile
44
+ - hinderinputjs-rails.gemspec
45
+ - lib/hinderinputjs-rails.rb
46
+ - lib/hinderinputjs-rails/version.rb
47
+ - vendor/assets/javascripts/jquery.caret.js
48
+ - vendor/assets/javascripts/jquery.hinderinput.js
49
+ homepage: https://github.com/discom4rt/hinderinputjs-rails
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
62
+ - 0
63
+ hash: -806024439033560381
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ segments:
71
+ - 0
72
+ hash: -806024439033560381
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.24
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: A method for hindering input capabilities within a textarea or text input.
79
+ test_files: []